Skip to content

Commit

Permalink
test: add encryption algorithm consistency test
Browse files Browse the repository at this point in the history
Added a test to verify that the encryption algorithm produces consistent
results when encrypting the same input data multiple times. The test:

- Creates identical input files with deterministic content
- Encrypts them using separate storage instances
- Verifies that:
  - Data maps have identical structure
  - Generated chunks have identical content
  - Chunk hashes match between runs

This helps ensure that changes to the streaming implementation don't
affect the underlying encryption algorithm's deterministic behavior.
  • Loading branch information
dirvine committed Nov 29, 2024
1 parent 90f2b05 commit 5794755
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,4 +937,69 @@ mod tests {

Ok(())
}

#[test]
fn test_encryption_algorithm_consistency() -> Result<()> {
// Create deterministic test data
let test_data = vec![42u8; MIN_ENCRYPTABLE_BYTES * 2]; // Repeating value for predictability

// First encryption
let storage1 = Arc::new(Mutex::new(HashMap::new()));
let storage1_clone = storage1.clone();

let store1 = move |hash: XorName, content: Bytes| -> Result<()> {
let _ = storage1_clone.lock().unwrap().insert(hash, content.to_vec());
Ok(())
};

// Second encryption
let storage2 = Arc::new(Mutex::new(HashMap::new()));
let storage2_clone = storage2.clone();

let store2 = move |hash: XorName, content: Bytes| -> Result<()> {
let _ = storage2_clone.lock().unwrap().insert(hash, content.to_vec());
Ok(())
};

// Create temporary files with same content
let temp_dir = tempfile::TempDir::new()?;
let file_path1 = temp_dir.path().join("test1.bin");
let file_path2 = temp_dir.path().join("test2.bin");

std::fs::write(&file_path1, &test_data)?;
std::fs::write(&file_path2, &test_data)?;

// Encrypt same data twice
let data_map1 = streaming_encrypt_from_file(&file_path1, store1)?;
let data_map2 = streaming_encrypt_from_file(&file_path2, store2)?;

// Compare data maps
assert_eq!(
data_map1.chunk_identifiers.len(),
data_map2.chunk_identifiers.len(),
"Data maps should have same number of chunks"
);

// Compare stored chunks
let stored1 = storage1.lock().unwrap();
let stored2 = storage2.lock().unwrap();

assert_eq!(
stored1.len(),
stored2.len(),
"Should have same number of stored chunks"
);

// Compare each chunk's content
for (hash, content1) in stored1.iter() {
let content2 = stored2.get(hash).expect("Chunk should exist in both storages");
assert_eq!(
content1,
content2,
"Encrypted chunks should be identical for same input"
);
}

Ok(())
}
}
1 change: 1 addition & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,4 @@ fn test_data_map_len_and_is_child() {
assert_eq!(empty_data_map.len(), 0);
assert!(!empty_data_map.is_child());
}

0 comments on commit 5794755

Please sign in to comment.