From 0f1233b45a900061a1c2b28bd4a22a9ef9b1865f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B2=81=E6=A0=91=E4=BA=BA?= Date: Wed, 4 Sep 2024 22:24:34 +0100 Subject: [PATCH] test: add more qmc_v1 test --- um_crypto/qmc/src/lib.rs | 14 ++++++++++++++ um_crypto/qmc/src/v1/cipher.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/um_crypto/qmc/src/lib.rs b/um_crypto/qmc/src/lib.rs index 6110d33..494fa00 100644 --- a/um_crypto/qmc/src/lib.rs +++ b/um_crypto/qmc/src/lib.rs @@ -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 { + (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") + } +} diff --git a/um_crypto/qmc/src/v1/cipher.rs b/um_crypto/qmc/src/v1/cipher.rs index 7d35e2f..f473dc3 100644 --- a/um_crypto/qmc/src/v1/cipher.rs +++ b/um_crypto/qmc/src/v1/cipher.rs @@ -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"); + } +}