Skip to content

Commit

Permalink
add a test and improve a test
Browse files Browse the repository at this point in the history
  • Loading branch information
uhoreg committed Jan 15, 2025
1 parent 26f79ad commit 800719a
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions src/olm/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ impl Account {
pickle.try_into()
}

#[cfg(fuzzing)]
#[cfg(any(fuzzing, test))]
#[doc(hidden)]
pub fn from_decrypted_dehydrated_device(
pickle: &[u8],
Expand Down Expand Up @@ -1459,6 +1459,7 @@ mod test {
fn decrypt_with_dehydrated_device() -> Result<()> {
let mut alice = Account::new();
let bob = Account::new();
let carol = Account::new();

alice.generate_one_time_keys(alice.max_number_of_one_time_keys());
alice.generate_fallback_key();
Expand All @@ -1477,8 +1478,20 @@ mod test {
.1,
);

let mut carol_session = carol.create_outbound_session(
SessionConfig::version_1(),
alice.curve25519_key(),
*alice
.fallback_key()
.iter()
.next()
.context("Failed getting alice's fallback key, which should never happen here.")?
.1,
);

let message = "It's a secret to everybody";
let olm_message = bob_session.encrypt(message);
let bob_olm_message = bob_session.encrypt(message);
let carol_olm_message = carol_session.encrypt(message);

let mut alice_rehydrated = Account::from_dehydrated_device(
&alice_dehydrated_result.ciphertext,
Expand All @@ -1487,7 +1500,7 @@ mod test {
)
.expect("Should be able to rehydrate device");

if let OlmMessage::PreKey(m) = olm_message {
if let OlmMessage::PreKey(m) = bob_olm_message {
let InboundCreationResult { session: alice_session, plaintext } =
alice_rehydrated.create_inbound_session(bob.curve25519_key(), &m)?;

Expand All @@ -1497,6 +1510,16 @@ mod test {
panic!("Expected a pre-key message");
}

if let OlmMessage::PreKey(m) = carol_olm_message {
let InboundCreationResult { session: alice_session, plaintext } =
alice_rehydrated.create_inbound_session(carol.curve25519_key(), &m)?;

assert_eq!(alice_session.session_id(), carol_session.session_id());
assert_eq!(message.as_bytes(), plaintext);
} else {
panic!("Expected a pre-key message");
}

Ok(())
}

Expand Down Expand Up @@ -1559,4 +1582,21 @@ mod test {

Ok(())
}

#[test]
fn decrypted_dehydration_cycle() {
use dehydrated_device::Pickle;

let alice = Account::new();

let mut encoded = Vec::<u8>::new();
let pickle = Pickle::from(&alice);
let size = pickle.encode(&mut encoded).expect("Should dehydrate");
assert_eq!(size, encoded.len());

let account =
Account::from_decrypted_dehydrated_device(&encoded).expect("Should rehydrate account");

assert_eq!(alice.identity_keys(), account.identity_keys());
}
}

0 comments on commit 800719a

Please sign in to comment.