test(03-01): add 19 unit tests for crypto, compression, and format modules
- crypto: encrypt/decrypt roundtrip, empty data, size formula, HMAC compute/verify, SHA-256 known values - compression: compress/decompress roundtrip, empty data, large data, should_compress heuristic - format: header write/read roundtrip, TOC entry roundtrip (ASCII + Cyrillic + empty name), bad magic/version rejection, entry size calculation matching FORMAT.md worked example - Update hex-literal to v1.1
This commit is contained in:
@@ -76,3 +76,66 @@ pub fn sha256_hash(data: &[u8]) -> [u8; 32] {
|
||||
use sha2::Digest;
|
||||
sha2::Sha256::digest(data).into()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::key::KEY;
|
||||
use hex_literal::hex;
|
||||
|
||||
#[test]
|
||||
fn test_encrypt_decrypt_roundtrip() {
|
||||
let plaintext = b"Hello, World!";
|
||||
let iv = [0u8; 16];
|
||||
let ciphertext = encrypt_data(plaintext, &KEY, &iv);
|
||||
let decrypted = decrypt_data(&ciphertext, &KEY, &iv).unwrap();
|
||||
assert_eq!(decrypted, plaintext);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_encrypt_decrypt_empty() {
|
||||
let plaintext = b"";
|
||||
let iv = [0u8; 16];
|
||||
let ciphertext = encrypt_data(plaintext, &KEY, &iv);
|
||||
let decrypted = decrypt_data(&ciphertext, &KEY, &iv).unwrap();
|
||||
assert_eq!(decrypted, plaintext.as_slice());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_encrypted_size_formula() {
|
||||
let iv = [0u8; 16];
|
||||
// 5 bytes -> ((5/16)+1)*16 = 16
|
||||
assert_eq!(encrypt_data(b"Hello", &KEY, &iv).len(), 16);
|
||||
// 16 bytes -> ((16/16)+1)*16 = 32 (full padding block)
|
||||
assert_eq!(encrypt_data(&[0u8; 16], &KEY, &iv).len(), 32);
|
||||
// 0 bytes -> ((0/16)+1)*16 = 16
|
||||
assert_eq!(encrypt_data(b"", &KEY, &iv).len(), 16);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hmac_compute_verify() {
|
||||
let iv = [0xAA; 16];
|
||||
let ciphertext = b"some ciphertext data here!!12345";
|
||||
let hmac_tag = compute_hmac(&KEY, &iv, ciphertext);
|
||||
// Verify with correct tag
|
||||
assert!(verify_hmac(&KEY, &iv, ciphertext, &hmac_tag));
|
||||
// Verify with wrong tag
|
||||
let wrong_tag = [0u8; 32];
|
||||
assert!(!verify_hmac(&KEY, &iv, ciphertext, &wrong_tag));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sha256_known_value() {
|
||||
// SHA-256("Hello") from FORMAT.md Section 12.3
|
||||
let expected = hex!("185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969");
|
||||
let result = sha256_hash(b"Hello");
|
||||
assert_eq!(result, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sha256_empty() {
|
||||
let expected = hex!("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
|
||||
let result = sha256_hash(b"");
|
||||
assert_eq!(result, expected);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user