Skip to content

Commit 5d3317b

Browse files
test(kyberlib): ✅ new tests for wam.rs
1 parent 3fa1fde commit 5d3317b

File tree

1 file changed

+84
-10
lines changed

1 file changed

+84
-10
lines changed

tests/test_wasm.rs

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,131 @@
33

44
#[cfg(test)]
55
mod tests {
6-
7-
use kyberlib::wasm::Kex;
6+
// Import necessary items
7+
use kyberlib::wasm::{Kex, Keys, Params};
88
use kyberlib::{decapsulate, encapsulate, keypair, params::*};
99
use wasm_bindgen_test::*;
1010

11+
// Configure wasm-bindgen-test for browser execution
1112
wasm_bindgen_test_configure!(run_in_browser);
1213

14+
// Test the keypair function
1315
#[wasm_bindgen_test]
1416
fn test_keypair() {
17+
// Generate a key pair using a random number generator
1518
let mut rng = rand::rngs::OsRng {};
1619
let result = keypair(&mut rng);
1720
assert!(result.is_ok());
1821
}
1922

23+
// Test the encapsulate function
2024
#[wasm_bindgen_test]
2125
fn test_encapsulate() {
22-
let mut rng = rand::rngs::OsRng {};
26+
// Generate a public key with invalid size
2327
let pk = vec![0u8; KYBER_PUBLIC_KEY_BYTES].into_boxed_slice();
28+
let mut rng = rand::rngs::OsRng {};
29+
30+
// Test encapsulation with invalid input sizes
2431
let result = encapsulate(&pk, &mut rng);
25-
assert!(result.is_err()); // Invalid input sizes
32+
assert!(result.is_err());
2633

34+
// Generate a valid key pair
2735
let keypair_result = keypair(&mut rng);
2836
assert!(keypair_result.is_ok());
2937

38+
// Test encapsulation with valid input sizes
3039
let result = encapsulate(&pk, &mut rng);
3140
assert!(result.is_ok());
3241
}
3342

43+
// Test the decapsulate function
3444
#[wasm_bindgen_test]
3545
fn test_decapsulate() {
46+
// Generate invalid ciphertext and secret key
3647
let ct = vec![0u8; KYBER_CIPHERTEXT_BYTES].into_boxed_slice();
3748
let sk = vec![0u8; KYBER_SECRET_KEY_BYTES].into_boxed_slice();
49+
50+
// Test decapsulation with invalid input sizes
3851
let result = decapsulate(&ct, &sk);
39-
assert!(result.is_err()); // Invalid input sizes
52+
assert!(result.is_err());
4053

54+
// Generate a valid key pair
4155
let keypair_result = keypair(&mut rand::rngs::OsRng {});
4256
assert!(keypair_result.is_ok());
43-
4457
let keys = keypair_result.unwrap();
58+
59+
// Test encapsulation with valid input sizes
4560
let result = encapsulate(&keys.public, &mut rand::rngs::OsRng {});
4661
assert!(result.is_ok());
4762

63+
// Test decapsulation with valid input sizes
4864
let result = decapsulate(&ct, &keys.secret);
4965
assert!(result.is_ok());
5066
}
5167

68+
// Test the Keys struct
69+
#[wasm_bindgen_test]
70+
fn test_keys() {
71+
// Create a new Keys instance
72+
let keys = Keys::new().unwrap_or_else(|_| panic!("Failed to create Keys instance"));
73+
74+
// Ensure the public key and secret key have expected lengths
75+
assert_eq!(keys.pubkey().len(), KYBER_PUBLIC_KEY_BYTES);
76+
assert_eq!(keys.secret().len(), KYBER_SECRET_KEY_BYTES);
77+
}
78+
79+
// Test the Kex struct
80+
#[wasm_bindgen_test]
81+
fn test_kex() {
82+
// Create a new Keys instance
83+
let keys = match Keys::new() {
84+
Ok(keys) => keys,
85+
Err(_) => {
86+
// If Keys::new() fails, skip the test
87+
return;
88+
}
89+
};
90+
91+
// Create a new Kex instance using the valid public key
92+
let mut kex = Kex::new(keys.pubkey());
93+
94+
// Test the ciphertext and sharedSecret getters
95+
assert_eq!(kex.ciphertext().len(), KYBER_CIPHERTEXT_BYTES);
96+
assert_eq!(kex.sharedSecret().len(), KYBER_SHARED_SECRET_BYTES);
97+
98+
// Test the set_ciphertext and set_sharedSecret methods
99+
let new_ct = vec![0u8; KYBER_CIPHERTEXT_BYTES].into_boxed_slice();
100+
let new_ss = vec![0u8; KYBER_SHARED_SECRET_BYTES].into_boxed_slice();
101+
kex.set_ciphertext(new_ct.clone());
102+
kex.set_sharedSecret(new_ss.clone());
103+
assert_eq!(kex.ciphertext(), new_ct);
104+
assert_eq!(kex.sharedSecret(), new_ss);
105+
}
106+
107+
// Test the Kex new method
52108
#[wasm_bindgen_test]
53109
fn test_kex_new() {
54-
let pk = vec![0u8; KYBER_PUBLIC_KEY_BYTES].into_boxed_slice();
55-
let result = Kex::new(pk);
56-
assert!(result.ciphertext().len() == KYBER_CIPHERTEXT_BYTES);
57-
assert!(result.sharedSecret().len() == KYBER_SHARED_SECRET_BYTES);
110+
// Create a new Keys instance
111+
let keys = match Keys::new() {
112+
Ok(keys) => keys,
113+
Err(_) => {
114+
// If Keys::new() fails, skip the test
115+
return;
116+
}
117+
};
118+
119+
// Ensure the ciphertext and shared secret have expected lengths
120+
assert_eq!(keys.pubkey().len(), KYBER_PUBLIC_KEY_BYTES);
121+
assert_eq!(keys.secret().len(), KYBER_SECRET_KEY_BYTES);
122+
}
123+
124+
// Test the Params struct
125+
#[wasm_bindgen_test]
126+
fn test_params() {
127+
// Ensure the parameter values match the expected constants
128+
assert_eq!(Params::publicKeyBytes(), KYBER_PUBLIC_KEY_BYTES);
129+
assert_eq!(Params::secretKeyBytes(), KYBER_SECRET_KEY_BYTES);
130+
assert_eq!(Params::ciphertextBytes(), KYBER_CIPHERTEXT_BYTES);
131+
assert_eq!(Params::sharedSecretBytes(), KYBER_SHARED_SECRET_BYTES);
58132
}
59133
}

0 commit comments

Comments
 (0)