Skip to content

Commit

Permalink
Fix duplicate file ids on decryption error
Browse files Browse the repository at this point in the history
  • Loading branch information
bancek committed Oct 13, 2023
1 parent 7cde387 commit 7855c12
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 6 deletions.
150 changes: 149 additions & 1 deletion vault-core-tests/tests/integration/repo_files_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use futures::FutureExt;
use similar_asserts::assert_eq;
use vault_core::repo_files::state::RepoFilesState;
use vault_core::{
files::file_category::FileCategory,
repo_files::state::{
RepoFile, RepoFileName, RepoFilePath, RepoFileSize, RepoFileType, RepoFilesState,
},
};
use vault_core_tests::helpers::with_repo;

#[test]
Expand Down Expand Up @@ -57,3 +62,146 @@ fn test_repo_lock_unlock_remove() {
.boxed()
});
}

#[test]
fn test_name_decryption_error() {
with_repo(|fixture| {
async move {
fixture
.user_fixture
.upload_remote_file("/My safe box/Plain.txt", "test")
.await;

fixture
.vault
.repo_files_service
.load_files(&fixture.repo_id, "/")
.await
.unwrap();

let file = fixture.vault.with_state(|state| {
vault_core::repo_files::selectors::select_files(state, &fixture.repo_id, "/")
.next()
.cloned()
.unwrap()
});

assert_eq!(
file,
RepoFile {
id: format!("err:{}:/Plain.txt", fixture.repo_id),
mount_id: fixture.mount_id.clone(),
remote_path: "/My safe box/Plain.txt".into(),
repo_id: fixture.repo_id.clone(),
path: RepoFilePath::DecryptError {
parent_path: "/".into(),
encrypted_name: "Plain.txt".into(),
error: file.path.decrypted_path().clone().unwrap_err()
},
name: RepoFileName::DecryptError {
encrypted_name: "Plain.txt".into(),
encrypted_name_lower: "plain.txt".into(),
error: file.name.decrypted_name().clone().unwrap_err()
},
ext: None,
content_type: None,
typ: RepoFileType::File,
size: Some(file.size.clone().unwrap()),
modified: Some(file.modified.unwrap()),
unique_name: file.unique_name.clone(),
remote_hash: Some(file.remote_hash.clone().unwrap()),
category: FileCategory::Generic,
}
);
}
.boxed()
});
}

#[test]
fn test_encrypted_decrypted_same_name() {
with_repo(|fixture| {
async move {
fixture.upload_file("/Plain.txt", "test").await;
fixture
.user_fixture
.upload_remote_file("/My safe box/Plain.txt", "test")
.await;

fixture
.vault
.repo_files_service
.load_files(&fixture.repo_id, "/")
.await
.unwrap();

let files = fixture.vault.with_state(|state| {
vault_core::repo_files::selectors::select_files(state, &fixture.repo_id, "/")
.cloned()
.collect::<Vec<_>>()
});

let err_file = files.get(0).cloned().unwrap();
let ok_file = files.get(1).cloned().unwrap();

assert_eq!(
files,
vec![
RepoFile {
id: format!("err:{}:/Plain.txt", fixture.repo_id),
mount_id: fixture.mount_id.clone(),
remote_path: "/My safe box/Plain.txt".into(),
repo_id: fixture.repo_id.clone(),
path: RepoFilePath::DecryptError {
parent_path: "/".into(),
encrypted_name: "Plain.txt".into(),
error: err_file.path.decrypted_path().clone().unwrap_err()
},
name: RepoFileName::DecryptError {
encrypted_name: "Plain.txt".into(),
encrypted_name_lower: "plain.txt".into(),
error: err_file.name.decrypted_name().clone().unwrap_err()
},
ext: None,
content_type: None,
typ: RepoFileType::File,
size: Some(err_file.size.clone().unwrap()),
modified: Some(err_file.modified.unwrap()),
unique_name: err_file.unique_name.clone(),
remote_hash: Some(err_file.remote_hash.clone().unwrap()),
category: FileCategory::Generic,
},
RepoFile {
id: format!("{}:/Plain.txt", fixture.repo_id),
mount_id: fixture.mount_id.clone(),
remote_path: format!(
"/My safe box/{}",
fixture
.vault
.repo_files_service
.encrypt_filename(&fixture.repo_id, "Plain.txt")
.unwrap()
),
repo_id: fixture.repo_id.clone(),
path: RepoFilePath::Decrypted {
path: "/Plain.txt".into()
},
name: RepoFileName::Decrypted {
name: "Plain.txt".into(),
name_lower: "plain.txt".into()
},
ext: Some("txt".into()),
content_type: Some("text/plain".into()),
typ: RepoFileType::File,
size: Some(RepoFileSize::Decrypted { size: 4 }),
modified: Some(ok_file.modified.unwrap()),
unique_name: ok_file.unique_name.clone(),
remote_hash: Some(ok_file.remote_hash.clone().unwrap()),
category: FileCategory::Text,
},
]
);
}
.boxed()
});
}
6 changes: 3 additions & 3 deletions vault-core/src/repo_files/mutations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ pub fn decrypt_file(
error,
..
} => {
let id = selectors::get_file_id(
let id = selectors::get_error_file_id(
repo_id,
&path_utils::join_path_name(parent_path, &encrypted_name),
);
Expand Down Expand Up @@ -635,7 +635,7 @@ mod tests {
assert_eq!(
decrypt_file("r1", "/", &remote_file, &cipher),
RepoFile {
id: String::from("r1:/D1"),
id: String::from("err:r1:/D1"),
mount_id: remote_file.mount_id.clone(),
remote_path: remote_file.path.clone(),
repo_id: String::from("r1",),
Expand Down Expand Up @@ -708,7 +708,7 @@ mod tests {
assert_eq!(
decrypt_file("r1", "/", &remote_file, &cipher),
RepoFile {
id: String::from("r1:/F1"),
id: String::from("err:r1:/F1"),
mount_id: remote_file.mount_id.clone(),
remote_path: remote_file.path.clone(),
repo_id: String::from("r1",),
Expand Down
4 changes: 4 additions & 0 deletions vault-core/src/repo_files/selectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ pub fn get_file_id(repo_id: &str, path: &str) -> String {
format!("{}:{}", repo_id, path)
}

pub fn get_error_file_id(repo_id: &str, path: &str) -> String {
format!("err:{}:{}", repo_id, path)
}

pub fn get_file_unique_name(remote_file_unique_id: &str, ext: Option<&str>) -> String {
match ext {
Some(ext) => format!("{}.{}", remote_file_unique_id, ext),
Expand Down
4 changes: 2 additions & 2 deletions vault-core/src/repo_files_list/mutations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ mod tests {
"non-zero trailing bits at 1"
))),
file: RepoFile {
id: String::from("r1:/D1"),
id: String::from("err:r1:/D1"),
mount_id: String::from("m1"),
remote_path: String::from("/Vault/D1"),
repo_id: String::from("r1"),
Expand Down Expand Up @@ -305,7 +305,7 @@ mod tests {
"non-zero trailing bits at 1"
))),
file: RepoFile {
id: String::from("r1:/F1"),
id: String::from("err:r1:/F1"),
mount_id: String::from("m1"),
remote_path: String::from("/Vault/F1"),
repo_id: String::from("r1"),
Expand Down

0 comments on commit 7855c12

Please sign in to comment.