Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
richvdh committed Aug 29, 2024
1 parent 2f1ff55 commit 92950d7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion crates/matrix-sdk-crypto/src/machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ impl OlmMachine {
session.sender_data = sender_data;

if self.store().compare_group_session(&session).await? == SessionOrdering::Better {
info!("Received a new megolm room key");
info!(?session, "Received a new megolm room key");

Ok(Some(session))
} else {
Expand Down
57 changes: 56 additions & 1 deletion crates/matrix-sdk-crypto/src/machine/tests/megolm_sender_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::{
olm::{InboundGroupSession, SenderData},
store::RoomKeyInfo,
types::events::{room::encrypted::ToDeviceEncryptedEventContent, EventType, ToDeviceEvent},
EncryptionSettings, EncryptionSyncChanges, OlmMachine, Session,
DeviceData, EncryptionSettings, EncryptionSyncChanges, OlmMachine, Session,
};

/// Test the behaviour when a megolm session is received from an unknown device,
Expand Down Expand Up @@ -105,6 +105,61 @@ async fn test_receive_megolm_session_from_known_device() {
);
}

/// If we have a megolm session from an unknown device, test what happens when
/// we get a /keys/query response that includes that device.
#[async_test]
async fn test_update_unknown_device_senderdata_on_keys_query() {
// GIVEN we have a megolm session from an unknown device

let (alice, bob) = get_machine_pair().await;
let mut bob_room_keys_received_stream = Box::pin(bob.store().room_keys_received_stream());

// `get_machine_pair_with_setup_sessions_test_helper` tells Bob about Alice's
// device keys, so to run this test, we need to make him forget them.
forget_devices_for_user(&bob, alice.user_id()).await;

// Alice starts a megolm session and shares the key with Bob, *without* sending
// the sender data.
let room_id = room_id!("!test:example.org");
let event = create_and_share_session_without_sender_data(&alice, &bob, room_id).await;

// Bob receives the to-device message
receive_to_device_event(&bob, &event).await;

// and now Bob should know about the session.
let room_key_info = get_room_key_received_update(&mut bob_room_keys_received_stream);
let session = get_inbound_group_session_or_assert(&bob, &room_key_info).await;

// Double-check that it is, in fact, an unknown device session.
assert_matches!(session.sender_data, SenderData::UnknownDevice { .. });

// WHEN Bob gets a /keys/query response for Alice, that includes the
// sending device...

let alice_device = DeviceData::from_machine_test_helper(&alice).await.unwrap();
let kq_response = json!({
"device_keys": { alice.user_id() : { alice.device_id(): alice_device.as_device_keys()}}
});
bob.receive_keys_query_response(
&TransactionId::new(),
&matrix_sdk_test::ruma_response_from_json(&kq_response),
)
.await
.unwrap();

// THEN Bob should have received an update about the session, and it should now
// be `SenderData::DeviceInfo`
let room_key_info = get_room_key_received_update(&mut bob_room_keys_received_stream);
let session = get_inbound_group_session_or_assert(&bob, &room_key_info).await;

assert_matches!(
session.sender_data,
SenderData::DeviceInfo {legacy_session, ..} => {
assert_eq!(legacy_session, true); // TODO: change when https://github.com/matrix-org/matrix-rust-sdk/pull/3785 lands
}
);
}

/// Convenience wrapper for [`get_machine_pair_with_setup_sessions_test_helper`]
/// using standard user ids.
async fn get_machine_pair() -> (OlmMachine, OlmMachine) {
Expand Down

0 comments on commit 92950d7

Please sign in to comment.