Skip to content

Commit 42b9683

Browse files
committed
test(crypto): Factor out test helper for encrypting to-device content
I'm going to need to suppress `sender_device_keys` for more tests, so pull out a test helper to help with this.
1 parent 3caa741 commit 42b9683

File tree

2 files changed

+72
-30
lines changed

2 files changed

+72
-30
lines changed

crates/matrix-sdk-crypto/src/machine/test_helpers.rs

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,23 @@ use ruma::{
3333
to_device::DeviceIdOrAllDevices,
3434
user_id, DeviceId, OwnedOneTimeKeyId, TransactionId, UserId,
3535
};
36+
use serde::Serialize;
3637
use serde_json::{json, Value};
3738
use tokio::sync::Mutex;
3839

3940
use crate::{
4041
machine::tests,
4142
olm::PrivateCrossSigningIdentity,
42-
session_manager::CollectStrategy,
4343
store::{types::Changes, CryptoStoreWrapper, MemoryStore},
4444
types::{
45-
events::ToDeviceEvent,
45+
events::{room::encrypted::ToDeviceEncryptedEventContent, ToDeviceEvent},
4646
requests::{AnyOutgoingRequest, ToDeviceRequest},
4747
DeviceKeys,
4848
},
4949
utilities::json_convert,
5050
verification::VerificationMachine,
51-
Account, CrossSigningBootstrapRequests, DecryptionSettings, Device, DeviceData,
52-
EncryptionSyncChanges, OlmMachine, OtherUserIdentityData, TrustRequirement,
51+
Account, CollectStrategy, CrossSigningBootstrapRequests, DecryptionSettings, Device,
52+
DeviceData, EncryptionSyncChanges, OlmMachine, OtherUserIdentityData, TrustRequirement,
5353
};
5454

5555
/// These keys need to be periodically uploaded to the server.
@@ -227,6 +227,58 @@ pub async fn send_and_receive_encrypted_to_device_test_helper(
227227
decrypted[0].clone()
228228
}
229229

230+
/// Encrypt the given event content into the content of an
231+
/// olm-encrypted to-device event, suppressing the `sender_device_keys` field in
232+
/// the encrypted content.
233+
///
234+
/// This is much the same as calling [`Device::encrypt`] on the recipient
235+
/// device, other than the suppression of `sender_device_keys`.
236+
///
237+
/// # Arguments
238+
///
239+
/// * `sender` - The OlmMachine to use to encrypt the event.
240+
/// * `recipient` - The recipient of the encrypted event.
241+
/// * `event_type` - The type of the event to encrypt.
242+
/// * `content` - The content of the event to encrypt.
243+
pub async fn build_encrypted_to_device_content_without_sender_data(
244+
sender: &OlmMachine,
245+
recipient_device: &DeviceKeys,
246+
event_type: &str,
247+
content: &impl Serialize,
248+
) -> ToDeviceEncryptedEventContent {
249+
let sender_store = &sender.inner.store;
250+
251+
let sender_key = recipient_device.curve25519_key().unwrap();
252+
let sessions = sender_store
253+
.get_sessions(&sender_key.to_base64())
254+
.await
255+
.expect("Could not get most recent session")
256+
.expect("No olm session found");
257+
let mut olm_session = sessions.lock().await.first().unwrap().clone();
258+
259+
let plaintext = serde_json::to_string(&json!({
260+
"sender": sender.user_id(),
261+
"sender_device": sender.device_id(),
262+
"keys": { "ed25519": sender.identity_keys().ed25519.to_base64() },
263+
"recipient": recipient_device.user_id,
264+
"recipient_keys": { "ed25519": recipient_device.ed25519_key().unwrap().to_base64() },
265+
"type": event_type,
266+
"content": content,
267+
}))
268+
.unwrap();
269+
270+
let ciphertext = olm_session.encrypt_helper(&plaintext).await;
271+
let content =
272+
olm_session.build_encrypted_event(ciphertext, None).await.expect("could not encrypt");
273+
274+
sender_store
275+
.save_changes(Changes { sessions: vec![olm_session], ..Default::default() })
276+
.await
277+
.expect("Could not save session");
278+
279+
content
280+
}
281+
230282
/// Create a session for the two supplied Olm machines to communicate.
231283
pub async fn build_session_for_pair(
232284
alice: OlmMachine,

crates/matrix-sdk-crypto/src/machine/tests/send_encrypted_to_device.rs

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ use serde_json::{json, value::to_raw_value, Value};
2828
use crate::{
2929
machine::{
3030
test_helpers::{
31-
build_session_for_pair, get_machine_pair, get_machine_pair_with_session,
32-
get_prepared_machine_test_helper, send_and_receive_encrypted_to_device_test_helper,
31+
build_encrypted_to_device_content_without_sender_data, build_session_for_pair,
32+
get_machine_pair, get_machine_pair_with_session, get_prepared_machine_test_helper,
33+
send_and_receive_encrypted_to_device_test_helper,
3334
},
3435
tests::{self, decryption_verification_state::mark_alice_identity_as_verified_test_helper},
3536
},
@@ -45,7 +46,7 @@ use crate::{
4546
utilities::json_convert,
4647
verification::tests::bob_id,
4748
CrossSigningBootstrapRequests, DecryptionSettings, DeviceData, EncryptionSettings,
48-
EncryptionSyncChanges, LocalTrust, OlmError, OlmMachine, Session, TrustRequirement,
49+
EncryptionSyncChanges, LocalTrust, OlmError, OlmMachine, TrustRequirement,
4950
};
5051

5152
#[async_test]
@@ -635,33 +636,22 @@ async fn create_and_share_session_without_sender_data(
635636
// the behaviour of the real implementation. See
636637
// `GroupSessionManager::share_room_key` for inspiration on how to do that.
637638

638-
let olm_sessions = alice
639-
.store()
640-
.get_sessions(&bob.identity_keys().curve25519.to_base64())
639+
let bob_device = alice
640+
.get_device(bob.user_id(), bob.device_id(), None)
641641
.await
642642
.unwrap()
643-
.unwrap();
644-
let mut olm_session: Session = olm_sessions.lock().await[0].clone();
645-
643+
.expect("Attempt to send message to unknown device");
646644
let room_key_content = outbound_session.as_content().await;
647-
let plaintext = serde_json::to_string(&json!({
648-
"sender": alice.user_id(),
649-
"sender_device": alice.device_id(),
650-
"keys": { "ed25519": alice.identity_keys().ed25519.to_base64() },
651-
// We deliberately do *not* include:
652-
// "org.matrix.msc4147.device_keys": alice_device_keys,
653-
"recipient": bob.user_id(),
654-
"recipient_keys": { "ed25519": bob.identity_keys().ed25519.to_base64() },
655-
"type": room_key_content.event_type(),
656-
"content": room_key_content,
657-
}))
658-
.unwrap();
659-
660-
let ciphertext = olm_session.encrypt_helper(&plaintext).await;
661-
ToDeviceEvent::new(
662-
alice.user_id().to_owned(),
663-
olm_session.build_encrypted_event(ciphertext, None).await.unwrap(),
645+
646+
let content = build_encrypted_to_device_content_without_sender_data(
647+
alice,
648+
&bob_device.device_keys,
649+
room_key_content.event_type(),
650+
&room_key_content,
664651
)
652+
.await;
653+
654+
ToDeviceEvent::new(alice.user_id().to_owned(), content)
665655
}
666656

667657
/// Simulate uploading keys for alice that mean bob thinks alice's device

0 commit comments

Comments
 (0)