Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(olm): Add remaining mutation tests #139

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .cargo/mutants.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ exclude_re = [
"src/olm/messages/message\\.rs.*replace \\+ with \\* in <impl TryFrom for Message>::try_from",
# Drop implementations perform zeroisation which cannot be tested in Rust
"impl Drop",
# These cause olm/account tests to hang
"RemoteChainKey::chain_index",
"RemoteChainKey::advance",
# The constant value can't really be tested
"src/olm/account/one_time_keys\\.rs.*replace \\* with \\+$",
]
12 changes: 11 additions & 1 deletion src/olm/messages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,17 @@ mod tests {
eUppqmWqug4QASIgRhZ2cgZcIWQbIa23R7U4y1Mo1R/t\
LCaMU+xjzRV5smGsCrJ6AHwktg";

const PRE_KEY_MESSAGE_CIPHERTEXT: [u8; 32] = [
70, 22, 118, 114, 6, 92, 33, 100, 27, 33, 173, 183, 71, 181, 56, 203, 83, 40, 213, 31, 237,
44, 38, 140, 83, 236, 99, 205, 21, 121, 178, 97,
];

const MESSAGE: &str = "AwogI7JhE/UsMZqXKb3xV6kUZWoJc6jTm2+AIgWYmaETIR0QASIQ\
+X2zb7kEX/3JvoLspcNBcLWOFXYpV0nS";

const MESSAGE_CIPHERTEXT: [u8; 16] =
[249, 125, 179, 111, 185, 4, 95, 253, 201, 190, 130, 236, 165, 195, 65, 112];

#[test]
fn message_type_from_usize() {
assert_eq!(
Expand Down Expand Up @@ -246,15 +254,17 @@ mod tests {
MessageType::PreKey,
"Expected message to be recognized as a pre-key Olm message."
);

assert_eq!(message.message(), PRE_KEY_MESSAGE_CIPHERTEXT);
assert_eq!(message.to_parts(), (0, PRE_KEY_MESSAGE.to_string()), "Roundtrip not identity.");

let message = OlmMessage::from_parts(1, MESSAGE)?;
assert_matches!(message, OlmMessage::Normal(_));
assert_eq!(
message.message_type(),
MessageType::Normal,
"Expected message to be recognized as a normal Olm message."
);
assert_eq!(message.message(), MESSAGE_CIPHERTEXT);
assert_eq!(message.to_parts(), (1, MESSAGE.to_string()), "Roundtrip not identity.");

OlmMessage::from_parts(3, PRE_KEY_MESSAGE)
Expand Down
22 changes: 22 additions & 0 deletions src/olm/session/chain_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,25 @@ impl ChainKey {
message_key
}
}

#[cfg(test)]
mod tests {
use super::ChainKey;
use crate::olm::session::chain_key::RemoteChainKey;

#[test]
fn advancing_chain_key_increments_index() {
let mut key = ChainKey::new(Box::new(*b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
assert_eq!(key.index(), 0);
key.advance();
assert_eq!(key.index(), 1);
}

#[test]
fn advancing_remote_chain_key_increments_index() {
let mut key = RemoteChainKey::new(Box::new(*b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
assert_eq!(key.chain_index(), 0);
key.advance();
assert_eq!(key.chain_index(), 1);
}
}
54 changes: 54 additions & 0 deletions src/olm/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,26 @@ mod test {
}
}

#[test]
fn session_config() {
let (_, _, alice_session, _) = session_and_libolm_pair().unwrap();
assert_eq!(alice_session.session_config(), SessionConfig::version_1());
}

#[test]
fn has_received_message() {
let (_, _, mut alice_session, bob_session) = session_and_libolm_pair().unwrap();
assert!(!alice_session.has_received_message());
assert!(!bob_session.has_received_message());
let message = bob_session.encrypt("Message").into();
assert_eq!(
"Message".as_bytes(),
alice_session.decrypt(&message).expect("Should be able to decrypt message")
);
assert!(alice_session.has_received_message());
assert!(!bob_session.has_received_message());
}

#[test]
fn out_of_order_decryption() {
let (_, _, mut alice_session, bob_session) = session_and_libolm_pair().unwrap();
Expand Down Expand Up @@ -680,6 +700,40 @@ mod test {
);
}

#[test]
fn pickle_default_config() {
let json = r#"
{
"receiving_chains": {
"inner": []
},
"sending_ratchet": {
"active_ratchet": {
"ratchet_key": [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
"root_key": [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
},
"parent_ratchet_key": null,
"ratchet_count": {
"Known": 1
},
"symmetric_key_ratchet": {
"index": 1,
"key": [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
},
"type": "active"
},
"session_keys": {
"base_key": [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
"identity_key": [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
"one_time_key": [6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6]
}
}
"#;
let pickle: SessionPickle =
serde_json::from_str(json).expect("Should be able to deserialize JSON");
assert_eq!(pickle.config, SessionConfig::version_1());
}

#[test]
#[cfg(feature = "libolm-compat")]
fn libolm_unpickling() {
Expand Down
20 changes: 20 additions & 0 deletions src/olm/session/ratchet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,23 @@ impl Ratchet {
&self.ratchet_key
}
}

#[cfg(test)]
mod test {
use super::RatchetKey;
use crate::{olm::RatchetPublicKey, Curve25519SecretKey};

#[test]
fn ratchet_key_from_curve_25519_secret_key() {
let bytes = b"aaaaaaaaaaaaaaawaaaaaaaaaaaaaaaa";
let key = RatchetKey::from(Curve25519SecretKey::from_slice(bytes));
assert_eq!(key.0.to_bytes().as_ref(), bytes);
}

#[test]
fn ratchet_public_key_from_bytes() {
let bytes = b"aaaaaaaaaaaaaaawaaaaaaaaaaaaaaaa";
let key = RatchetPublicKey::from(*bytes);
assert_eq!(key.0.to_bytes().as_ref(), bytes);
}
}
12 changes: 12 additions & 0 deletions src/olm/session_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,15 @@ impl Default for SessionConfig {
Self::version_2()
}
}

#[cfg(test)]
mod test {
use super::SessionConfig;
use crate::olm::session_config::Version;

#[test]
fn version() {
assert_eq!(SessionConfig::version_1().version(), Version::V1 as u8);
assert_eq!(SessionConfig::version_2().version(), Version::V2 as u8);
}
}
Loading