792f2f174b
The 32-byte test key "0123456789abcdef0123456789abcdef" was hardcoded in three places: src/crypto.rs unit tests, tests/roundtrip.rs, and tests/library_api.rs - three copies to keep in sync if the fixture ever changes. Add tests/common/mod.rs exposing the KEY bytes and a test_key() SecretBytes32 constructor; roundtrip.rs and library_api.rs now pull from it. The unit tests in src/crypto.rs cannot reach an integration-test module and keep their own copy. The module carries #![allow(dead_code)] because each test crate compiles its own copy and none uses every fixture. Test-only change. Test plan: cargo test passes all suites (43 CLI roundtrip tests, 2 library_api tests, 13 unit tests).
80 lines
2.1 KiB
Rust
80 lines
2.1 KiB
Rust
use std::fs;
|
|
|
|
use fcry::{
|
|
DecryptOptions,
|
|
DecryptRangeOptions,
|
|
EncryptOptions,
|
|
KdfParams,
|
|
OutputOptions,
|
|
decrypt,
|
|
decrypt_range,
|
|
default_argon_decrypt_cap_mib,
|
|
encrypt,
|
|
};
|
|
use tempfile::TempDir;
|
|
|
|
mod common;
|
|
use common::test_key;
|
|
|
|
#[test]
|
|
fn library_file_roundtrip_raw_key() {
|
|
let dir = TempDir::new().unwrap();
|
|
let plain = dir.path().join("plain.bin");
|
|
let ct = dir.path().join("cipher.fcry");
|
|
let out = dir.path().join("out.bin");
|
|
let data: Vec<u8> = (0..=255).cycle().take(100_000).collect();
|
|
fs::write(&plain, &data).unwrap();
|
|
|
|
let key = test_key();
|
|
let encrypt_options = EncryptOptions {
|
|
input_file: Some(plain),
|
|
output_file: Some(ct.clone()),
|
|
chunk_size: 4096,
|
|
threads: 1,
|
|
output: OutputOptions::default(),
|
|
};
|
|
encrypt(&encrypt_options, &key, KdfParams::Raw).unwrap();
|
|
|
|
let decrypt_options = DecryptOptions {
|
|
input_file: Some(ct),
|
|
output_file: Some(out.clone()),
|
|
threads: 1,
|
|
..DecryptOptions::default()
|
|
};
|
|
decrypt(&decrypt_options, Some(&key), None).unwrap();
|
|
|
|
assert_eq!(fs::read(out).unwrap(), data);
|
|
}
|
|
|
|
#[test]
|
|
fn library_range_decrypt_raw_key() {
|
|
let dir = TempDir::new().unwrap();
|
|
let plain = dir.path().join("plain.bin");
|
|
let ct = dir.path().join("cipher.fcry");
|
|
let out = dir.path().join("slice.bin");
|
|
let data: Vec<u8> = (0..=255).cycle().take(50_000).collect();
|
|
fs::write(&plain, &data).unwrap();
|
|
|
|
let key = test_key();
|
|
let encrypt_options = EncryptOptions {
|
|
input_file: Some(plain),
|
|
output_file: Some(ct.clone()),
|
|
chunk_size: 1024,
|
|
threads: 2,
|
|
output: OutputOptions::default(),
|
|
};
|
|
encrypt(&encrypt_options, &key, KdfParams::Raw).unwrap();
|
|
|
|
let range_options = DecryptRangeOptions {
|
|
input_file: ct,
|
|
output_file: Some(out.clone()),
|
|
offset: 1234,
|
|
length: 20_000,
|
|
max_argon_memory_mib: default_argon_decrypt_cap_mib(),
|
|
output: OutputOptions::default(),
|
|
};
|
|
decrypt_range(&range_options, Some(&key), None).unwrap();
|
|
|
|
assert_eq!(fs::read(out).unwrap(), data[1234..21_234]);
|
|
}
|