test: add more qmc_v1 test

This commit is contained in:
鲁树人 2024-09-04 22:24:34 +01:00
parent 2c53e4d950
commit 0f1233b45a
2 changed files with 41 additions and 0 deletions

View File

@ -9,3 +9,17 @@ pub enum QmcCryptoError {
#[error("QMC V2/Map Cipher: Key is empty")]
QMCV2MapKeyEmpty,
}
#[cfg(test)]
mod test {
pub fn generate_key(len: usize) -> Vec<u8> {
(1..=len).map(|i| i as u8).collect()
}
#[cfg(test)]
pub fn generate_key_128() -> [u8; 128] {
generate_key(128)
.try_into()
.expect("failed to make test key")
}
}

View File

@ -11,3 +11,30 @@ pub fn qmc1_transform(key: &[u8; V1_KEY_SIZE], value: u8, offset: usize) -> u8 {
value ^ key[offset % V1_KEY_SIZE]
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test::generate_key_128;
#[test]
fn test_decode_start() {
let test_key = generate_key_128();
let mut data = *b"igohj&pg{fo";
for (i, datum) in data.iter_mut().enumerate() {
*datum = qmc1_transform(&test_key, *datum, i);
}
assert_eq!(data, *b"hello world");
}
#[test]
fn test_decode_boundary() {
let test_key = generate_key_128();
let mut data = [
0x13, 0x19, 0x11, 0x12, 0x10, 0xa0, 0x75, 0x6c, 0x76, 0x69, 0x62,
];
for (i, datum) in data.iter_mut().enumerate() {
*datum = qmc1_transform(&test_key, *datum, 0x7FFA + i);
}
assert_eq!(data, *b"hello world");
}
}