use zarlib::operations::{
AddInputOptions, CommitDetails, CommitDetailsUpdate, CommitField, CreateFilterOptions,
CreateInputOptions, EncodingOptions, FilterSet, PasswordSet, add_archive_with_commit_details,
create_archive_with_commit_details, info_archive_with_passwords,
join_archive_with_encoding_progress_and_commit_details, update_commit_details,
};
use zarlib::{ContentAead, EncryptionScope};
#[test]
fn create_update_and_remove_commit_details_preserve_encrypted_history() {
for (scope, algorithm) in [
(EncryptionScope::Header, ContentAead::Aes256Gcm),
(EncryptionScope::Data, ContentAead::Aes256Gcm),
(
EncryptionScope::HeaderAndData,
ContentAead::ChaCha20Poly1305,
),
] {
let dir = tempfile::tempdir().unwrap();
let archive = dir.path().join("archive.zar");
let file = dir.path().join("input");
let retained = dir.path().join("retained");
std::fs::write(&file, b"before").unwrap();
std::fs::write(&retained, b"retained contents").unwrap();
let path = archive.to_str().unwrap();
let filters = FilterSet::default();
let encryption = Some((b"secret".as_slice(), algorithm, scope));
let passwords = PasswordSet::new(Some(b"secret".to_vec()));
create_archive_with_commit_details(
path,
&[file.clone(), retained],
false,
EncodingOptions::default(),
encryption,
None,
CreateFilterOptions {
input: CreateInputOptions::default(),
filters: &filters,
},
CommitDetails {
name: Some("initial"),
description: Some("first"),
},
)
.unwrap();
std::fs::write(&file, b"after").unwrap();
add_archive_with_commit_details(
path,
&[file],
EncodingOptions::default(),
None,
AddInputOptions::default(),
&filters,
encryption,
&passwords,
CommitDetails {
name: Some("next"),
description: None,
},
)
.unwrap();
let before = std::fs::read(path).unwrap();
let update = CommitDetailsUpdate {
name: CommitField::Set("changed_1"),
description: CommitField::Set("new description"),
};
assert!(update_commit_details(path, "00000001", update, &PasswordSet::default()).is_err());
assert!(
update_commit_details(
path,
"00000001",
update,
&PasswordSet::new(Some(b"wrong".to_vec()))
)
.is_err()
);
assert_eq!(std::fs::read(path).unwrap(), before);
update_commit_details(path, "00000001", update, &passwords).unwrap();
let info = info_archive_with_passwords(path, false, &passwords).unwrap();
assert_eq!(info.len(), 2);
assert_eq!(info[0].name.as_deref(), Some("changed_1"));
assert_eq!(info[0].description.as_deref(), Some("new description"));
assert_eq!(info[1].name.as_deref(), Some("next"));
let diff = zarlib::operations::diff_archive_with_passwords(
path, "00000001", "00000002", &passwords,
)
.unwrap();
assert_eq!(diff.changed.len(), 1);
let prepared =
zarlib::operations::PreparedExtraction::new_complete(path, None, &passwords).unwrap();
assert_eq!(prepared.read_file_range("input", 0, 100).unwrap(), b"after");
assert_eq!(
prepared.read_file_range("retained", 0, 100).unwrap(),
b"retained contents"
);
update_commit_details(
path,
"00000001",
CommitDetailsUpdate {
name: CommitField::Remove,
description: CommitField::Remove,
},
&passwords,
)
.unwrap();
let info = info_archive_with_passwords(path, false, &passwords).unwrap();
assert_eq!(info[0].name, None);
assert_eq!(info[0].description, None);
}
}
#[test]
fn invalid_commit_details_do_not_change_archive() {
let dir = tempfile::tempdir().unwrap();
let archive = dir.path().join("archive.zar");
let file = dir.path().join("input");
std::fs::write(&file, b"data").unwrap();
let path = archive.to_str().unwrap();
zarlib::operations::create_archive(path, &[file]).unwrap();
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600)).unwrap();
}
let before = std::fs::read(path).unwrap();
for name in ["", "invalid-name", "x".repeat(65).as_str()] {
assert!(
update_commit_details(
path,
"00000001",
CommitDetailsUpdate {
name: CommitField::Set(name),
..Default::default()
},
&PasswordSet::default()
)
.is_err()
);
}
for description in [
"x".repeat(129),
(0..9).map(|_| "line").collect::<Vec<_>>().join("\n"),
] {
assert!(
update_commit_details(
path,
"00000001",
CommitDetailsUpdate {
description: CommitField::Set(&description),
..Default::default()
},
&PasswordSet::default()
)
.is_err()
);
}
assert!(
update_commit_details(
path,
"ffffffff",
CommitDetailsUpdate {
name: CommitField::Set("valid"),
..Default::default()
},
&PasswordSet::default()
)
.is_err()
);
assert_eq!(std::fs::read(path).unwrap(), before);
let name = "a".repeat(64);
let description = (0..8)
.map(|_| "é".repeat(128))
.collect::<Vec<_>>()
.join("\n");
update_commit_details(
path,
"00000001",
CommitDetailsUpdate {
name: CommitField::Set(&name),
description: CommitField::Set(&description),
},
&PasswordSet::default(),
)
.unwrap();
let info = info_archive_with_passwords(path, false, &PasswordSet::default()).unwrap();
assert_eq!(info[0].name.as_deref(), Some(name.as_str()));
assert_eq!(info[0].description.as_deref(), Some(description.as_str()));
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
assert_eq!(
std::fs::metadata(path).unwrap().permissions().mode() & 0o777,
0o600
);
}
}
#[test]
fn joining_commits_sets_the_requested_commit_details() {
let directory = tempfile::tempdir().unwrap();
let archive = directory.path().join("archive.zar");
let first = directory.path().join("first.txt");
let second = directory.path().join("second.txt");
std::fs::write(&first, b"first").unwrap();
create_archive_with_commit_details(
archive.to_str().unwrap(),
std::slice::from_ref(&first),
false,
EncodingOptions::default(),
None,
None,
CreateFilterOptions {
input: CreateInputOptions::default(),
filters: &FilterSet::default(),
},
CommitDetails {
name: Some("first"),
description: Some("first description"),
},
)
.unwrap();
std::fs::write(&second, b"second").unwrap();
add_archive_with_commit_details(
archive.to_str().unwrap(),
&[second],
EncodingOptions::default(),
None,
AddInputOptions::default(),
&FilterSet::default(),
None,
&PasswordSet::default(),
CommitDetails {
name: Some("second"),
description: Some("second description"),
},
)
.unwrap();
join_archive_with_encoding_progress_and_commit_details(
archive.to_str().unwrap(),
"00000001",
"00000002",
EncodingOptions::default(),
None,
&PasswordSet::default(),
CommitDetailsUpdate {
name: CommitField::Set("combined"),
description: CommitField::Set("combined description"),
},
)
.unwrap();
let commits =
info_archive_with_passwords(archive.to_str().unwrap(), false, &PasswordSet::default())
.unwrap();
assert_eq!(commits.len(), 1);
assert_eq!(commits[0].name.as_deref(), Some("combined"));
assert_eq!(
commits[0].description.as_deref(),
Some("combined description")
);
}
#[test]
fn selected_listing_uses_details_from_selected_revision() {
use zarlib::operations::{
CommitSelection, list_archive_with_details_and_passwords,
list_archive_with_details_and_selection,
};
let dir = tempfile::tempdir().unwrap();
let archive = dir.path().join("archive.zar");
let input = dir.path().join("file");
let path = archive.to_str().unwrap();
std::fs::write(&input, vec![b'a'; 1024]).unwrap();
zarlib::operations::create_archive(path, std::slice::from_ref(&input)).unwrap();
std::fs::write(&input, vec![b'b'; 2048]).unwrap();
zarlib::operations::add_archive(path, std::slice::from_ref(&input)).unwrap();
std::fs::write(&input, vec![b'c'; 4096]).unwrap();
zarlib::operations::add_archive(path, &[input]).unwrap();
let passwords = PasswordSet::default();
let (toc, details) = list_archive_with_details_and_selection(
path,
CommitSelection {
from: Some("00000002"),
to: Some("00000002"),
names: &[],
},
&passwords,
)
.unwrap();
let (_, expected) =
list_archive_with_details_and_passwords(path, Some("00000002"), &passwords).unwrap();
assert_eq!(details, expected);
assert!(
matches!(toc.iter().next().unwrap().1, zarlib::TocEntry::File(file) if file.size == 2048)
);
}
#[test]
fn commit_selection_applies_only_selected_changes_and_keeps_referenced_blocks() {
use zarlib::operations::{
CollisionPolicy, CommitSelection, ExistingPolicy, ExtractionFrameValidation,
ExtractionOptions, cross_commit_references, extract_archive_to_memory_with_selection,
extract_archive_with_selection, list_archive_with_details_and_selection,
list_archive_with_selection,
};
let dir = tempfile::tempdir().unwrap();
let archive = dir.path().join("archive.zar");
let first = dir.path().join("first.txt");
let copy = dir.path().join("copy.txt");
let last = dir.path().join("last.txt");
std::fs::write(&first, b"shared content").unwrap();
let path = archive.to_str().unwrap();
let filters = FilterSet::default();
let passwords = PasswordSet::default();
create_archive_with_commit_details(
path,
&[first],
false,
EncodingOptions::default(),
None,
None,
CreateFilterOptions {
input: CreateInputOptions::default(),
filters: &filters,
},
CommitDetails {
name: Some("initial"),
description: None,
},
)
.unwrap();
std::fs::write(©, b"shared content").unwrap();
add_archive_with_commit_details(
path,
&[copy],
EncodingOptions::default(),
None,
AddInputOptions::default(),
&filters,
None,
&passwords,
CommitDetails {
name: Some("backup_1"),
description: None,
},
)
.unwrap();
std::fs::write(&last, b"last").unwrap();
add_archive_with_commit_details(
path,
&[last],
EncodingOptions::default(),
None,
AddInputOptions::default(),
&filters,
None,
&passwords,
CommitDetails::default(),
)
.unwrap();
assert_eq!(cross_commit_references(path).unwrap().len(), 1);
let names = vec!["backup_*".to_owned()];
let selections = [
CommitSelection {
from: Some("00000002"),
to: Some("00000002"),
names: &[],
},
CommitSelection {
from: None,
to: None,
names: &names,
},
];
for selection in selections {
let toc = list_archive_with_selection(path, selection, &passwords).unwrap();
let (detailed_toc, details) =
list_archive_with_details_and_selection(path, selection, &passwords).unwrap();
assert_eq!(detailed_toc, toc);
assert_eq!(details.len(), 1);
assert_eq!(
toc.iter().map(|(p, _)| p.as_str()).collect::<Vec<_>>(),
["copy.txt"]
);
let contents = extract_archive_to_memory_with_selection(
path, selection, &passwords, None, 0, &filters,
)
.unwrap();
assert_eq!(contents.len(), 1);
assert_eq!(contents["copy.txt"], b"shared content");
let destination = dir.path().join(if selection.from.is_some() {
"range"
} else {
"names"
});
extract_archive_with_selection(
path,
&destination,
ExtractionOptions {
dry_run: false,
to: selection.to,
collision_policy: CollisionPolicy::Rename,
existing_policy: ExistingPolicy::Error,
restore_metadata: false,
restore_symlinks: false,
restore_devices: false,
strip_top_level: false,
},
selection,
&passwords,
None,
&filters,
ExtractionFrameValidation::default(),
)
.unwrap();
assert_eq!(
std::fs::read(destination.join("copy.txt")).unwrap(),
b"shared content"
);
assert!(!destination.join("first.txt").exists());
assert!(!destination.join("last.txt").exists());
}
let all_names = vec!["*".to_owned()];
let selected = list_archive_with_selection(
path,
CommitSelection {
names: &all_names,
..Default::default()
},
&passwords,
)
.unwrap();
assert_eq!(selected.len(), 2);
let tail = list_archive_with_selection(
path,
CommitSelection {
from: Some("00000002"),
..Default::default()
},
&passwords,
)
.unwrap();
assert_eq!(
tail.iter().map(|(p, _)| p.as_str()).collect::<Vec<_>>(),
["copy.txt", "last.txt"]
);
let invalid_pattern = vec!["[".to_owned()];
for selection in [
CommitSelection {
from: Some("00000003"),
to: Some("00000002"),
names: &[],
},
CommitSelection {
from: Some("00000009"),
..Default::default()
},
CommitSelection {
to: Some("00000009"),
..Default::default()
},
CommitSelection {
from: Some("00000001"),
names: &names,
..Default::default()
},
CommitSelection {
names: &invalid_pattern,
..Default::default()
},
] {
assert!(list_archive_with_selection(path, selection, &passwords).is_err());
}
}
#[test]
fn selected_extraction_skips_unrelated_encrypted_data_but_not_required_data() {
use zarlib::operations::{
CommitSelection, extract_archive_to_memory_with_selection, list_archive_with_selection,
};
for (duplicate, needs_password) in [(false, false), (true, true)] {
let dir = tempfile::tempdir().unwrap();
let archive = dir.path().join("archive.zar");
let first = dir.path().join("first");
let second = dir.path().join("second");
std::fs::write(&first, b"secret data").unwrap();
std::fs::write(
&second,
if duplicate {
b"secret data"
} else {
b"other data!"
},
)
.unwrap();
let path = archive.to_str().unwrap();
let filters = FilterSet::default();
let passwords = PasswordSet::new(Some(b"password".to_vec()));
create_archive_with_commit_details(
path,
&[first],
false,
EncodingOptions::default(),
Some((b"password", ContentAead::Aes256Gcm, EncryptionScope::Data)),
None,
CreateFilterOptions {
input: CreateInputOptions::default(),
filters: &filters,
},
CommitDetails {
name: Some("first"),
description: None,
},
)
.unwrap();
add_archive_with_commit_details(
path,
&[second],
EncodingOptions::default(),
None,
AddInputOptions::default(),
&filters,
Some((b"password", ContentAead::Aes256Gcm, EncryptionScope::Data)),
&passwords,
CommitDetails {
name: Some("second"),
description: None,
},
)
.unwrap();
let references =
zarlib::operations::cross_commit_references_with_passwords(path, &passwords).unwrap();
assert_eq!(references.len(), usize::from(duplicate));
let names = vec!["second".to_owned()];
let selection = CommitSelection {
names: &names,
..Default::default()
};
assert_eq!(
list_archive_with_selection(path, selection, &PasswordSet::default())
.unwrap()
.len(),
1
);
let mut second_password = PasswordSet::default();
second_password.insert_for_commit(2_u32.to_le_bytes(), b"password".to_vec());
let result = extract_archive_to_memory_with_selection(
path,
selection,
&second_password,
None,
0,
&filters,
);
assert_eq!(result.is_err(), needs_password);
let contents = extract_archive_to_memory_with_selection(
path, selection, &passwords, None, 0, &filters,
)
.unwrap();
assert_eq!(
contents["second"],
if duplicate {
b"secret data"
} else {
b"other data!"
}
);
}
let dir = tempfile::tempdir().unwrap();
let file = dir.path().join("file");
let archive = dir.path().join("archive.zar");
std::fs::write(&file, b"secret").unwrap();
let filters = FilterSet::default();
create_archive_with_commit_details(
archive.to_str().unwrap(),
&[file],
false,
EncodingOptions::default(),
Some((b"password", ContentAead::Aes256Gcm, EncryptionScope::Header)),
None,
CreateFilterOptions {
input: CreateInputOptions::default(),
filters: &filters,
},
CommitDetails {
name: Some("hidden"),
description: None,
},
)
.unwrap();
let names = vec!["*".to_owned()];
let selection = CommitSelection {
names: &names,
..Default::default()
};
assert!(
list_archive_with_selection(
archive.to_str().unwrap(),
selection,
&PasswordSet::default()
)
.is_err()
);
assert_eq!(
list_archive_with_selection(
archive.to_str().unwrap(),
selection,
&PasswordSet::new(Some(b"password".to_vec()))
)
.unwrap()
.len(),
1
);
}