Skip to content

Commit

Permalink
Use encrypted paths for repo file browser
Browse files Browse the repository at this point in the history
  • Loading branch information
bancek committed Dec 1, 2023
1 parent 593aac6 commit 19eeaa6
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 124 deletions.
29 changes: 27 additions & 2 deletions vault-core-tests/src/fixtures/repo_fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use vault_core::{
},
repos::state::RepoUnlockMode,
types::{
DecryptedName, DecryptedPath, EncryptedName, EncryptedPath, MountId, RemotePath, RepoId,
DecryptedName, DecryptedPath, EncryptedName, EncryptedPath, MountId, RemotePath,
RepoFileId, RepoId,
},
utils::repo_encrypted_path_utils,
Vault,
Expand Down Expand Up @@ -111,11 +112,25 @@ impl RepoFixture {
self.cipher.encrypt_path(&DecryptedPath(path.into()))
}

pub fn get_file_id(&self, path: &str) -> RepoFileId {
repo_files::selectors::get_file_id(&self.repo_id, &self.encrypt_path(path.into()))
}

pub async fn create_dir(&self, path: &str) -> RepoFile {
let cipher = self.vault.repos_service.get_cipher(&self.repo_id).unwrap();
let path = cipher.encrypt_path(&DecryptedPath(path.to_owned()));
let (parent_path, name) = repo_encrypted_path_utils::split_parent_name(&path).unwrap();

self.create_dir_encrypted(&parent_path, name).await
}

pub async fn create_dir_encrypted(
&self,
parent_path: &EncryptedPath,
name: EncryptedName,
) -> RepoFile {
let path = repo_encrypted_path_utils::join_path_name(parent_path, &name);

self.vault
.repo_files_service
.clone()
Expand All @@ -142,6 +157,16 @@ impl RepoFixture {
let path = cipher.encrypt_path(&DecryptedPath(path.to_owned()));
let (parent_path, name) = repo_encrypted_path_utils::split_parent_name(&path).unwrap();

self.upload_file_encrypted(&parent_path, name, content)
.await
}

pub async fn upload_file_encrypted(
&self,
parent_path: &EncryptedPath,
name: EncryptedName,
content: &str,
) -> (RepoFilesUploadResult, RepoFile) {
let bytes = content.as_bytes().to_vec();
let size = bytes.len();
let reader = Box::pin(Cursor::new(bytes));
Expand All @@ -152,7 +177,7 @@ impl RepoFixture {
.clone()
.upload_file_reader(
&self.repo_id,
&parent_path,
parent_path,
name,
reader,
Some(size as i64),
Expand Down
152 changes: 112 additions & 40 deletions vault-core-tests/tests/integration/repo_files_browsers_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use futures::{join, FutureExt};
use similar_asserts::assert_eq;
use vault_core::{
common::state::Status,
common::{self, state::Status},
dialogs,
repo_files::{
errors::LoadFilesError,
Expand All @@ -18,7 +18,7 @@ use vault_core::{
selection::state::SelectionSummary,
sort::state::SortDirection,
store,
types::{DecryptedName, DecryptedPath, EncryptedPath},
types::{EncryptedName, EncryptedPath, RepoFileId},
};
use vault_core_tests::{
fixtures::repo_fixture::RepoFixture,
Expand All @@ -32,22 +32,17 @@ fn test_repo_lock_unlock_remove() {
async move {
let (browser_id, load_future) = fixture.vault.repo_files_browsers_create(
fixture.repo_id.clone(),
&DecryptedPath("/".into()),
&EncryptedPath("/".into()),
RepoFilesBrowserOptions { select_name: None },
);
load_future.await.unwrap();

let get_state = || fixture.vault.with_state(|state| state.clone());
let select_info = |state| {
vault_core::repo_files_browsers::selectors::select_info(state, browser_id).unwrap()
};
let select_info =
|state| repo_files_browsers::selectors::select_info(state, browser_id).unwrap();
let select_items =
|state| vault_core::repo_files_browsers::selectors::select_items(state, browser_id);
|state| repo_files_browsers::selectors::select_items(state, browser_id);

let root_id = vault_core::repo_files::selectors::get_file_id(
&fixture.repo_id,
&EncryptedPath("/".into()),
);
let (_, file) = fixture.upload_file("/file.txt", "test").await;
let dir = fixture.create_dir("/dir").await;

Expand All @@ -56,14 +51,14 @@ fn test_repo_lock_unlock_remove() {
select_info(&state_before_lock),
RepoFilesBrowserInfo {
repo_id: Some(&fixture.repo_id),
path: Some(&DecryptedPath("/".into())),
path: Some(&EncryptedPath("/".into())),
selection_summary: SelectionSummary::None,
sort: RepoFilesSort {
field: RepoFilesSortField::Name,
direction: SortDirection::Asc
},
status: vault_core::common::state::Status::Loaded,
title: Some(DecryptedName("My safe box".to_owned())),
status: common::state::Status::Loaded,
title: Some("My safe box".into()),
total_count: 2,
total_size: 4,
selected_count: 0,
Expand All @@ -83,13 +78,13 @@ fn test_repo_lock_unlock_remove() {
is_selected: false,
}
],
breadcrumbs: vec![RepoFilesBreadcrumb {
id: root_id.clone(),
breadcrumbs: Some(&[RepoFilesBreadcrumb {
id: fixture.get_file_id("/"),
repo_id: fixture.repo_id.clone(),
path: DecryptedPath("/".into()),
name: DecryptedName("My safe box".into()),
path: EncryptedPath("/".into()),
name: "My safe box".into(),
last: true
}],
}]),
}
);

Expand All @@ -100,17 +95,17 @@ fn test_repo_lock_unlock_remove() {
select_info(&state_after_lock),
RepoFilesBrowserInfo {
repo_id: Some(&fixture.repo_id),
path: Some(&DecryptedPath("/".into())),
path: Some(&EncryptedPath("/".into())),
selection_summary: SelectionSummary::None,
sort: RepoFilesSort {
field: RepoFilesSortField::Name,
direction: SortDirection::Asc
},
status: vault_core::common::state::Status::Error {
status: common::state::Status::Error {
error: LoadFilesError::RepoLocked(RepoLockedError),
loaded: false
},
title: Some(DecryptedName("My safe box".to_owned())),
title: None,
total_count: 0,
total_size: 0,
selected_count: 0,
Expand All @@ -121,13 +116,7 @@ fn test_repo_lock_unlock_remove() {
can_move_selected: false,
can_delete_selected: false,
items: vec![],
breadcrumbs: vec![RepoFilesBreadcrumb {
id: root_id.clone(),
repo_id: fixture.repo_id.clone(),
path: DecryptedPath("/".into()),
name: DecryptedName("My safe box".into()),
last: true
}],
breadcrumbs: None,
}
);

Expand All @@ -150,13 +139,13 @@ fn test_repo_lock_unlock_remove() {
select_info(&state_after_remove),
RepoFilesBrowserInfo {
repo_id: Some(&fixture.repo_id),
path: Some(&DecryptedPath("/".into())),
path: Some(&EncryptedPath("/".into())),
selection_summary: SelectionSummary::None,
sort: RepoFilesSort {
field: RepoFilesSortField::Name,
direction: SortDirection::Asc
},
status: vault_core::common::state::Status::Error {
status: common::state::Status::Error {
error: LoadFilesError::RepoNotFound(RepoNotFoundError),
loaded: false
},
Expand All @@ -171,7 +160,84 @@ fn test_repo_lock_unlock_remove() {
can_move_selected: false,
can_delete_selected: false,
items: vec![],
breadcrumbs: vec![]
breadcrumbs: None
}
);

fixture.vault.repo_files_browsers_destroy(browser_id);
}
.boxed()
});
}

#[test]
fn test_repo_decrypt_path_error() {
with_repo(|fixture| {
async move {
fixture
.create_dir_encrypted(&EncryptedPath("/".into()), EncryptedName("dir".into()))
.await;
let (_, file) = fixture
.upload_file_encrypted(
&EncryptedPath("/dir".into()),
fixture.encrypt_filename("file.txt".into()),
"test",
)
.await;

let (browser_id, load_future) = fixture.vault.repo_files_browsers_create(
fixture.repo_id.clone(),
&EncryptedPath("/dir".into()),
RepoFilesBrowserOptions { select_name: None },
);
load_future.await.unwrap();

let get_state = || fixture.vault.with_state(|state| state.clone());
let select_info =
|state| repo_files_browsers::selectors::select_info(state, browser_id).unwrap();

let state = get_state();
assert_eq!(
select_info(&state),
RepoFilesBrowserInfo {
repo_id: Some(&fixture.repo_id),
path: Some(&EncryptedPath("/dir".into())),
selection_summary: SelectionSummary::None,
sort: RepoFilesSort {
field: RepoFilesSortField::Name,
direction: SortDirection::Asc
},
status: common::state::Status::Loaded,
title: Some("dir".into()),
total_count: 1,
total_size: 4,
selected_count: 0,
selected_size: 0,
selected_file: None,
can_download_selected: false,
can_copy_selected: false,
can_move_selected: false,
can_delete_selected: false,
items: vec![RepoFilesBrowserItem {
file: &file,
is_selected: false,
}],
breadcrumbs: Some(&[
RepoFilesBreadcrumb {
id: fixture.get_file_id("/"),
repo_id: fixture.repo_id.clone(),
path: EncryptedPath("/".into()),
name: "My safe box".into(),
last: false
},
RepoFilesBreadcrumb {
id: RepoFileId(format!("{}:/dir", fixture.repo_id.0)),
repo_id: fixture.repo_id.clone(),
path: EncryptedPath("/dir".into()),
name: "dir".into(),
last: true
}
]),
}
);

Expand All @@ -195,7 +261,7 @@ fn test_create() {

let (browser_id, load_future) = fixture.vault.repo_files_browsers_create(
fixture.repo_id.clone(),
&DecryptedPath("/".into()),
&EncryptedPath("/".into()),
RepoFilesBrowserOptions { select_name: None },
);
load_future.await.unwrap();
Expand Down Expand Up @@ -262,7 +328,7 @@ fn test_create_already_loaded() {

let (browser_id, load_future) = fixture.vault.repo_files_browsers_create(
fixture.repo_id.clone(),
&DecryptedPath("/".into()),
&EncryptedPath("/".into()),
RepoFilesBrowserOptions { select_name: None },
);
load_future.await.unwrap();
Expand Down Expand Up @@ -316,7 +382,7 @@ fn test_reload() {

let (browser_id, load_future) = fixture.vault.repo_files_browsers_create(
fixture.repo_id.clone(),
&DecryptedPath("/".into()),
&EncryptedPath("/".into()),
RepoFilesBrowserOptions { select_name: None },
);
load_future.await.unwrap();
Expand Down Expand Up @@ -392,8 +458,7 @@ fn expected_browsers_state(
options: RepoFilesBrowserOptions { select_name: None },
location: Some(RepoFilesBrowserLocation {
repo_id: fixture.repo_id.clone(),
path: DecryptedPath("/".into()),
encrypted_path: EncryptedPath("/".into()),
path: EncryptedPath("/".into()),
eventstream_mount_subscription: state
.browsers
.get(&1)
Expand All @@ -405,6 +470,13 @@ fn expected_browsers_state(
.clone(),
}),
status: Status::Initial,
breadcrumbs: Some(vec![RepoFilesBreadcrumb {
id: fixture.get_file_id("/"),
repo_id: fixture.repo_id.clone(),
path: EncryptedPath("/".into()),
name: "My safe box".into(),
last: true,
}]),
file_ids: vec![],
selection: Default::default(),
sort: Default::default(),
Expand All @@ -425,7 +497,7 @@ fn test_create_dir() {
async move {
let (browser_id, load_future) = fixture.vault.repo_files_browsers_create(
fixture.repo_id.clone(),
&DecryptedPath("/".into()),
&EncryptedPath("/".into()),
RepoFilesBrowserOptions { select_name: None },
);
load_future.await.unwrap();
Expand Down Expand Up @@ -469,7 +541,7 @@ fn test_create_dir_validation() {
async move {
let (browser_id, load_future) = fixture.vault.repo_files_browsers_create(
fixture.repo_id.clone(),
&DecryptedPath("/".into()),
&EncryptedPath("/".into()),
RepoFilesBrowserOptions { select_name: None },
);
load_future.await.unwrap();
Expand Down Expand Up @@ -529,7 +601,7 @@ fn test_eventstream() {

let (browser_id, load_future) = fixture.vault.repo_files_browsers_create(
fixture.repo_id.clone(),
&DecryptedPath("/".into()),
&EncryptedPath("/".into()),
RepoFilesBrowserOptions { select_name: None },
);
load_future.await.unwrap();
Expand Down
Loading

0 comments on commit 19eeaa6

Please sign in to comment.