@@ -29,17 +29,25 @@ use ruma::{
2929 encryption:: OneTimeKey ,
3030 events:: dummy:: ToDeviceDummyEventContent ,
3131 serde:: Raw ,
32+ to_device:: DeviceIdOrAllDevices ,
3233 user_id, DeviceId , OwnedOneTimeKeyId , TransactionId , UserId ,
3334} ;
34- use serde_json:: json;
35+ use serde_json:: { json, Value } ;
3536use tokio:: sync:: Mutex ;
3637
3738use crate :: {
39+ machine:: tests,
3840 olm:: PrivateCrossSigningIdentity ,
3941 store:: { Changes , CryptoStoreWrapper , MemoryStore } ,
40- types:: { events:: ToDeviceEvent , requests:: AnyOutgoingRequest , DeviceKeys } ,
42+ types:: {
43+ events:: ToDeviceEvent ,
44+ requests:: { AnyOutgoingRequest , ToDeviceRequest } ,
45+ DeviceKeys , ProcessedToDeviceEvent ,
46+ } ,
47+ utilities:: json_convert,
4148 verification:: VerificationMachine ,
42- Account , CrossSigningBootstrapRequests , Device , DeviceData , OlmMachine , OtherUserIdentityData ,
49+ Account , CrossSigningBootstrapRequests , Device , DeviceData , EncryptionSyncChanges , OlmMachine ,
50+ OtherUserIdentityData ,
4351} ;
4452
4553/// These keys need to be periodically uploaded to the server.
@@ -174,6 +182,46 @@ pub async fn get_machine_pair_with_session(
174182 build_session_for_pair ( alice, bob, one_time_keys) . await
175183}
176184
185+ pub async fn send_and_receive_encrypted_to_device_test_helper (
186+ sender : & OlmMachine ,
187+ recipient : & OlmMachine ,
188+ event_type : & str ,
189+ content : Value ,
190+ ) -> ProcessedToDeviceEvent {
191+ let device =
192+ sender. get_device ( recipient. user_id ( ) , recipient. device_id ( ) , None ) . await . unwrap ( ) . unwrap ( ) ;
193+
194+ let raw_encrypted = device
195+ . encrypt_event_raw ( event_type, & content)
196+ . await
197+ . expect ( "Should have encrypted the content" ) ;
198+
199+ let request = ToDeviceRequest :: new (
200+ recipient. user_id ( ) ,
201+ DeviceIdOrAllDevices :: DeviceId ( recipient. device_id ( ) . to_owned ( ) ) ,
202+ "m.room.encrypted" ,
203+ raw_encrypted. cast ( ) ,
204+ ) ;
205+ let event = ToDeviceEvent :: new (
206+ sender. user_id ( ) . to_owned ( ) ,
207+ tests:: to_device_requests_to_content ( vec ! [ request. clone( ) . into( ) ] ) ,
208+ ) ;
209+
210+ let event = json_convert ( & event) . unwrap ( ) ;
211+
212+ let sync_changes = EncryptionSyncChanges {
213+ to_device_events : vec ! [ event] ,
214+ changed_devices : & Default :: default ( ) ,
215+ one_time_keys_counts : & Default :: default ( ) ,
216+ unused_fallback_keys : None ,
217+ next_batch_token : None ,
218+ } ;
219+
220+ let ( decrypted, _) = recipient. receive_sync_changes ( sync_changes) . await . unwrap ( ) ;
221+ assert_eq ! ( 1 , decrypted. len( ) ) ;
222+ decrypted[ 0 ] . clone ( )
223+ }
224+
177225/// Create a session for the two supplied Olm machines to communicate.
178226pub async fn build_session_for_pair (
179227 alice : OlmMachine ,
0 commit comments