double-ratchet-rs/tests/mod.rs

180 lines
7 KiB
Rust
Raw Permalink Normal View History

use double_ratchet_rs::{Ratchet, RatchetEncHeader};
2023-08-02 14:11:26 +00:00
#[cfg(not(feature = "std"))]
2021-06-18 13:15:55 +00:00
extern crate alloc;
2021-05-13 11:24:52 +00:00
2023-08-02 14:11:26 +00:00
#[cfg(feature = "std")]
extern crate std as alloc;
2021-05-13 11:24:52 +00:00
#[test]
fn init() {
2021-05-13 11:24:52 +00:00
let sk = [1; 32];
let (_bob_ratchet, public_key) = Ratchet::init_bob(sk);
let _alice_ratchet = Ratchet::init_alice(sk, public_key);
}
#[test]
fn enc_single() {
2021-05-13 11:24:52 +00:00
let sk = [1; 32];
let (mut bob_ratchet, public_key) = Ratchet::init_bob(sk);
let mut alice_ratchet = Ratchet::init_alice(sk, public_key);
let data = include_bytes!("../src/header.rs").to_vec();
let (header, encrypted, nonce) = alice_ratchet.encrypt(&data, b"");
2024-10-09 09:22:08 +00:00
let decrypted = bob_ratchet.decrypt(&header, &encrypted, &nonce, b"").unwrap();
2021-05-13 11:24:52 +00:00
assert_eq!(data, decrypted)
}
#[test]
fn enc_skip() {
2021-05-13 11:24:52 +00:00
let sk = [1; 32];
let (mut bob_ratchet, public_key) = Ratchet::init_bob(sk);
let mut alice_ratchet = Ratchet::init_alice(sk, public_key);
let data = include_bytes!("../src/header.rs").to_vec();
let (header1, encrypted1, nonce1) = alice_ratchet.encrypt(&data, b"");
let (header2, encrypted2, nonce2) = alice_ratchet.encrypt(&data, b"");
let (header3, encrypted3, nonce3) = alice_ratchet.encrypt(&data, b"");
2024-10-09 09:22:08 +00:00
let decrypted3 = bob_ratchet.decrypt(&header3, &encrypted3, &nonce3, b"").unwrap();
let decrypted2 = bob_ratchet.decrypt(&header2, &encrypted2, &nonce2, b"").unwrap();
let decrypted1 = bob_ratchet.decrypt(&header1, &encrypted1, &nonce1, b"").unwrap();
let comp_res = decrypted1 == data && decrypted2 == data && decrypted3 == data;
2021-05-13 11:24:52 +00:00
assert!(comp_res)
}
#[test]
#[should_panic]
fn panic_bob() {
2021-05-13 11:24:52 +00:00
let sk = [1; 32];
2021-05-13 14:41:09 +00:00
let (mut bob_ratchet, _) = Ratchet::init_bob(sk);
2021-05-13 11:24:52 +00:00
let data = include_bytes!("../src/header.rs").to_vec();
let (_, _, _) = bob_ratchet.encrypt(&data, b"");
2021-05-13 11:24:52 +00:00
}
#[test]
fn encryt_decrypt_four() {
2021-05-13 11:24:52 +00:00
let sk = [1; 32];
let data = include_bytes!("../src/dh.rs").to_vec();
let (mut bob_ratchet, public_key) = Ratchet::init_bob(sk);
let mut alice_ratchet = Ratchet::init_alice(sk, public_key);
let (header1, encrypted1, nonce1) = alice_ratchet.encrypt(&data, b"");
2024-10-09 09:22:08 +00:00
let decrypted1 = bob_ratchet.decrypt(&header1, &encrypted1, &nonce1, b"").unwrap();
let (header2, encrypted2, nonce2) = bob_ratchet.encrypt(&data, b"");
2024-10-09 09:22:08 +00:00
let decrypted2 = alice_ratchet.decrypt(&header2, &encrypted2, &nonce2, b"").unwrap();
2021-05-14 15:38:39 +00:00
let comp_res = decrypted1 == data && decrypted2 == data;
assert!(comp_res)
}
#[test]
fn ench_init() {
2021-05-14 15:38:39 +00:00
let sk = [1; 32];
let shared_hka = [2; 32];
let shared_nhkb = [3; 32];
let (_bob_ratchet, public_key) = RatchetEncHeader::init_bob(sk, shared_hka, shared_nhkb);
let _alice_ratchet = RatchetEncHeader::init_alice(sk, public_key, shared_hka, shared_nhkb);
2021-05-14 15:38:39 +00:00
}
#[test]
fn ench_enc_single() {
2021-05-14 15:38:39 +00:00
let sk = [1; 32];
let shared_hka = [2; 32];
let shared_nhkb = [3; 32];
let (mut bob_ratchet, public_key) = RatchetEncHeader::init_bob(sk, shared_hka, shared_nhkb);
let mut alice_ratchet = RatchetEncHeader::init_alice(sk, public_key, shared_hka, shared_nhkb);
2021-05-14 15:38:39 +00:00
let data = include_bytes!("../src/header.rs").to_vec();
let (header, encrypted, nonce) = alice_ratchet.encrypt(&data, b"");
2024-10-09 09:22:08 +00:00
let decrypted = bob_ratchet.decrypt(&header, &encrypted, &nonce, b"").unwrap();
2021-05-14 15:38:39 +00:00
assert_eq!(data, decrypted)
}
#[test]
fn ench_enc_skip() {
2021-05-14 15:38:39 +00:00
let sk = [1; 32];
let shared_hka = [2; 32];
let shared_nhkb = [3; 32];
let (mut bob_ratchet, public_key) = RatchetEncHeader::init_bob(sk, shared_hka, shared_nhkb);
let mut alice_ratchet = RatchetEncHeader::init_alice(sk, public_key, shared_hka, shared_nhkb);
2021-05-14 15:38:39 +00:00
let data = include_bytes!("../src/header.rs").to_vec();
let (header1, encrypted1, nonce1) = alice_ratchet.encrypt(&data, b"");
let (header2, encrypted2, nonce2) = alice_ratchet.encrypt(&data, b"");
let (header3, encrypted3, nonce3) = alice_ratchet.encrypt(&data, b"");
2024-10-09 09:22:08 +00:00
let decrypted3 = bob_ratchet.decrypt(&header3, &encrypted3, &nonce3, b"").unwrap();
let decrypted2 = bob_ratchet.decrypt(&header2, &encrypted2, &nonce2, b"").unwrap();
let decrypted1 = bob_ratchet.decrypt(&header1, &encrypted1, &nonce1, b"").unwrap();
let comp_res = decrypted1 == data && decrypted2 == data && decrypted3 == data;
2021-05-14 15:38:39 +00:00
assert!(comp_res)
}
#[test]
#[should_panic]
fn ench_panic_bob() {
2021-05-14 15:38:39 +00:00
let sk = [1; 32];
let shared_hka = [2; 32];
let shared_nhkb = [3; 32];
let (mut bob_ratchet, _) = RatchetEncHeader::init_bob(sk, shared_hka, shared_nhkb);
2021-05-14 15:38:39 +00:00
let data = include_bytes!("../src/header.rs").to_vec();
let (_, _, _) = bob_ratchet.encrypt(&data, b"");
2021-05-14 15:38:39 +00:00
}
#[test]
fn ench_decrypt_four() {
2021-05-14 15:38:39 +00:00
let sk = [1; 32];
let shared_hka = [2; 32];
let shared_nhkb = [3; 32];
let (mut bob_ratchet, public_key) = RatchetEncHeader::init_bob(sk, shared_hka, shared_nhkb);
2021-05-14 15:38:39 +00:00
let mut alice_ratchet = RatchetEncHeader::init_alice(sk, public_key, shared_hka, shared_nhkb);
let data = include_bytes!("../src/dh.rs").to_vec();
let (header1, encrypted1, nonce1) = alice_ratchet.encrypt(&data, b"");
2024-10-09 09:22:08 +00:00
let decrypted1 = bob_ratchet.decrypt(&header1, &encrypted1, &nonce1, b"").unwrap();
let (header2, encrypted2, nonce2) = bob_ratchet.encrypt(&data, b"");
2024-10-09 09:22:08 +00:00
let decrypted2 = alice_ratchet.decrypt(&header2, &encrypted2, &nonce2, b"").unwrap();
2021-05-13 11:24:52 +00:00
let comp_res = decrypted1 == data && decrypted2 == data;
assert!(comp_res)
2021-06-18 13:15:55 +00:00
}
#[test]
#[should_panic]
fn ench_enc_skip_panic() {
2021-06-18 13:15:55 +00:00
let sk = [1; 32];
let shared_hka = [2; 32];
let shared_nhkb = [3; 32];
let (mut bob_ratchet, public_key) = RatchetEncHeader::init_bob(sk, shared_hka, shared_nhkb);
let mut alice_ratchet = RatchetEncHeader::init_alice(sk, public_key, shared_hka, shared_nhkb);
2021-06-18 13:15:55 +00:00
let data = include_bytes!("../src/header.rs").to_vec();
let mut headers = alloc::vec![];
let mut encrypteds = alloc::vec![];
let mut nonces = alloc::vec![];
let mut decrypteds = alloc::vec![];
for _ in 0..200 {
let (header, encrypted, nonce) = alice_ratchet.encrypt(&data, b"");
2021-06-18 13:15:55 +00:00
headers.push(header);
encrypteds.push(encrypted);
nonces.push(nonce);
}
headers.reverse();
encrypteds.reverse();
nonces.reverse();
for idx in 0..200 {
let header = headers.get(idx).unwrap();
let encrypted = encrypteds.get(idx).unwrap();
let nonce = nonces.get(idx).unwrap();
2024-10-09 09:22:08 +00:00
let decrypted = bob_ratchet.decrypt(header, encrypted, nonce, b"").unwrap();
2021-06-18 13:15:55 +00:00
decrypteds.push(decrypted);
}
}
2021-08-23 12:05:05 +00:00
#[test]
fn import_export() {
let sk = [1; 32];
let shared_hka = [2; 32];
let shared_nhkb = [3; 32];
let (bob_ratchet, public_key) = RatchetEncHeader::init_bob(sk, shared_hka, shared_nhkb);
2021-08-23 12:05:05 +00:00
let alice_ratchet = RatchetEncHeader::init_alice(sk, public_key, shared_hka, shared_nhkb);
let ex_bob_ratchet = bob_ratchet.export();
let in_bob_ratchet = RatchetEncHeader::import(&ex_bob_ratchet).unwrap();
2021-08-23 12:05:05 +00:00
assert_eq!(in_bob_ratchet, bob_ratchet);
let ex_alice_ratchet = alice_ratchet.export();
let in_alice_ratchet = RatchetEncHeader::import(&ex_alice_ratchet).unwrap();
2021-08-23 12:05:05 +00:00
assert_eq!(in_alice_ratchet, alice_ratchet);
}