use super::*;
fn key(byte: u8) -> SealKey {
SealKey::from_bytes(KeyGuard::new([byte; 32])).unwrap()
}
fn keys(byte: u8) -> SealKeys {
SealKeys::single(key(byte))
}
fn sample(len: usize) -> Vec<u8> {
(0..len).map(|i| (i * 7 + i / 251) as u8).collect()
}
const SIZES: [usize; 7] = [
0,
1,
CHUNK_LEN - 1,
CHUNK_LEN,
CHUNK_LEN + 1,
2 * CHUNK_LEN,
3 * CHUNK_LEN + 17,
];
#[test]
fn sealed_bytes_open_to_what_was_sealed_at_every_chunk_boundary() {
let k = keys(1);
for len in SIZES {
let plain = sample(len);
let sealed = seal(&k, "vectors.usearch", &plain).unwrap();
assert!(looks_sealed(&sealed));
assert_eq!(
open(&k, "vectors.usearch", &sealed).unwrap(),
plain,
"{len}"
);
}
}
#[test]
fn sealing_the_same_bytes_twice_never_repeats_a_ciphertext() {
let k = keys(2);
let plain = sample(100);
assert_ne!(
seal(&k, "a", &plain).unwrap(),
seal(&k, "a", &plain).unwrap()
);
}
#[test]
fn the_plaintext_does_not_appear_in_the_sealed_bytes() {
let k = keys(3);
let plain = b"the ferry timetable moved to the harbour office".repeat(50);
let sealed = seal(&k, "a", &plain).unwrap();
assert!(
!sealed
.windows(16)
.any(|w| plain.windows(16).any(|p| p == w))
);
}
#[test]
fn another_key_does_not_open_a_sealed_file() {
let sealed = seal(&keys(4), "a", b"secret").unwrap();
let err = open(&keys(5), "a", &sealed).unwrap_err();
assert_eq!(
seal_error(&err),
Some(&SealError::UnknownKey(key(4).id())),
"{err:#}"
);
}
#[test]
fn a_retired_key_still_opens_what_it_sealed() {
let sealed = seal(&keys(6), "a", b"secret").unwrap();
let rotated = SealKeys::new(key(7), vec![key(6)]);
assert_eq!(open(&rotated, "a", &sealed).unwrap(), b"secret");
assert_eq!(rotated.current_id(), key(7).id());
}
#[test]
fn a_file_cut_short_or_opened_under_another_name_fails() {
let k = keys(8);
let plain = sample(2 * CHUNK_LEN + 5);
let sealed = seal(&k, "a", &plain).unwrap();
let at_boundary = HEADER_LEN + 2 * SEALED_CHUNK_LEN;
for cut in [
at_boundary,
HEADER_LEN + SEALED_CHUNK_LEN,
sealed.len() - 1,
HEADER_LEN,
] {
let err = open(&k, "a", &sealed[..cut]).unwrap_err();
assert_eq!(
seal_error(&err),
Some(&SealError::Damaged),
"cut at {cut}: {err:#}"
);
}
let err = open(&k, "b", &sealed).unwrap_err();
assert_eq!(seal_error(&err), Some(&SealError::Damaged));
let mut flipped = sealed.clone();
flipped[HEADER_LEN + 3] ^= 1;
assert!(open(&k, "a", &flipped).is_err());
}
#[test]
fn plaintext_is_reported_as_not_sealed() {
let err = open(&keys(9), "a", b"{\"segments\":[]}").unwrap_err();
assert_eq!(seal_error(&err), Some(&SealError::NotSealed));
}
#[test]
fn files_round_trip_and_a_sealed_file_needs_its_key() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("vectors.usearch");
let k = keys(10);
let plain = sample(CHUNK_LEN * 2 + 3);
write_file(&path, Some(&k), "vectors.usearch", &plain).unwrap();
assert_eq!(sealed_key_id(&path).unwrap(), Some(k.current_id()));
assert_eq!(
read_file(&path, Some(&k), "vectors.usearch").unwrap(),
plain
);
let err = read_file(&path, None, "vectors.usearch").unwrap_err();
assert_eq!(seal_error(&err), Some(&SealError::KeyRequired));
let only_entries: Vec<_> = fs::read_dir(dir.path()).unwrap().collect();
assert_eq!(only_entries.len(), 1, "no temporary file is left behind");
}
#[test]
fn a_plaintext_file_reads_as_is_with_or_without_keys() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("meta.json");
write_file(&path, None, "meta.json", b"{}").unwrap();
assert_eq!(sealed_key_id(&path).unwrap(), None);
assert_eq!(read_file(&path, None, "meta.json").unwrap(), b"{}");
assert_eq!(
read_file(&path, Some(&keys(11)), "meta.json").unwrap(),
b"{}"
);
}
#[test]
fn resealing_moves_plaintext_and_retired_files_under_the_current_key() {
let dir = tempfile::tempdir().unwrap();
let plain_path = dir.path().join("plain");
let old_path = dir.path().join("old");
let plain = sample(CHUNK_LEN + 9);
fs::write(&plain_path, &plain).unwrap();
write_file(&old_path, Some(&keys(12)), "old", &plain).unwrap();
let rotated = SealKeys::new(key(13), vec![key(12)]);
assert!(reseal_file(&plain_path, &rotated, "plain").unwrap());
assert!(reseal_file(&old_path, &rotated, "old").unwrap());
assert!(!reseal_file(&old_path, &rotated, "old").unwrap());
for (path, name) in [(&plain_path, "plain"), (&old_path, "old")] {
assert_eq!(sealed_key_id(path).unwrap(), Some(key(13).id()));
assert_eq!(read_file(path, Some(&keys(13)), name).unwrap(), plain);
}
}
#[test]
fn a_reader_opens_any_chunk_alone() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("seg");
let k = keys(14);
let plain = sample(3 * CHUNK_LEN + 100);
write_file(&path, Some(&k), "seg", &plain).unwrap();
let reader = SealedReader::open(&path, &k, "seg").unwrap();
assert_eq!(reader.len(), plain.len() as u64);
assert_eq!(reader.chunks(), 4);
assert_eq!(reader.read_chunk(3).unwrap(), &plain[3 * CHUNK_LEN..]);
assert_eq!(
reader.read_chunk(1).unwrap(),
&plain[CHUNK_LEN..2 * CHUNK_LEN]
);
assert!(reader.read_chunk(4).is_err());
}
#[test]
fn key_ids_follow_the_key_and_reveal_nothing_of_it() {
assert_eq!(key(15).id(), key(15).id());
assert_ne!(key(15).id(), key(16).id());
assert_ne!(&key(15).id().0[..], &[15u8; 8][..]);
}
#[test]
fn only_a_short_read_counts_as_damage() {
let short = short_read_as(
io::Error::from(ErrorKind::UnexpectedEof),
SealError::Damaged,
);
assert_eq!(seal_error(&short), Some(&SealError::Damaged));
let failing_disk = short_read_as(io::Error::other("input/output error"), SealError::Damaged);
assert_eq!(seal_error(&failing_disk), None);
assert!(failing_disk.downcast_ref::<io::Error>().is_some());
}