@@ -26,6 +26,11 @@ use matrix_sdk_common::{
26
26
executor:: spawn,
27
27
} ;
28
28
use matrix_sdk_test:: { async_test, message_like_event_content, ruma_response_from_json, test_json} ;
29
+ #[ cfg( feature = "experimental-encrypted-state-events" ) ]
30
+ use ruma:: events:: {
31
+ room:: topic:: { OriginalRoomTopicEvent , RoomTopicEventContent } ,
32
+ StateEvent ,
33
+ } ;
29
34
use ruma:: {
30
35
api:: client:: {
31
36
keys:: { get_keys, upload_keys} ,
@@ -727,6 +732,84 @@ async fn test_megolm_encryption() {
727
732
}
728
733
}
729
734
735
+ #[ cfg( feature = "experimental-encrypted-state-events" ) ]
736
+ #[ async_test]
737
+ async fn test_megolm_state_encryption ( ) {
738
+ use ruma:: events:: { AnyStateEvent , EmptyStateKey } ;
739
+
740
+ let ( alice, bob) =
741
+ get_machine_pair_with_setup_sessions_test_helper ( alice_id ( ) , user_id ( ) , false ) . await ;
742
+ let room_id = room_id ! ( "!test:example.org" ) ;
743
+
744
+ let to_device_requests = alice
745
+ . share_room_key ( room_id, iter:: once ( bob. user_id ( ) ) , EncryptionSettings :: default ( ) )
746
+ . await
747
+ . unwrap ( ) ;
748
+
749
+ let event = ToDeviceEvent :: new (
750
+ alice. user_id ( ) . to_owned ( ) ,
751
+ to_device_requests_to_content ( to_device_requests) ,
752
+ ) ;
753
+
754
+ let decryption_settings =
755
+ DecryptionSettings { sender_device_trust_requirement : TrustRequirement :: Untrusted } ;
756
+
757
+ let group_session = bob
758
+ . store ( )
759
+ . with_transaction ( |mut tr| async {
760
+ let res = bob
761
+ . decrypt_to_device_event (
762
+ & mut tr,
763
+ & event,
764
+ & mut Changes :: default ( ) ,
765
+ & decryption_settings,
766
+ )
767
+ . await ?;
768
+ Ok ( ( tr, res) )
769
+ } )
770
+ . await
771
+ . unwrap ( )
772
+ . inbound_group_session
773
+ . unwrap ( ) ;
774
+ let sessions = std:: slice:: from_ref ( & group_session) ;
775
+ bob. store ( ) . save_inbound_group_sessions ( sessions) . await . unwrap ( ) ;
776
+
777
+ let plaintext = "It is a secret to everybody" ;
778
+
779
+ let content = RoomTopicEventContent :: new ( plaintext. to_owned ( ) ) ;
780
+
781
+ let encrypted_content =
782
+ alice. encrypt_state_event ( room_id, content, EmptyStateKey ) . await . unwrap ( ) ;
783
+
784
+ let event = json ! ( {
785
+ "event_id" : "$xxxxx:example.org" ,
786
+ "origin_server_ts" : MilliSecondsSinceUnixEpoch :: now( ) ,
787
+ "sender" : alice. user_id( ) ,
788
+ "type" : "m.room.encrypted" ,
789
+ "content" : encrypted_content,
790
+ } ) ;
791
+
792
+ let event = json_convert ( & event) . unwrap ( ) ;
793
+
794
+ let decryption_settings =
795
+ DecryptionSettings { sender_device_trust_requirement : TrustRequirement :: Untrusted } ;
796
+
797
+ let decryption_result =
798
+ bob. try_decrypt_room_event ( & event, room_id, & decryption_settings) . await . unwrap ( ) ;
799
+ assert_let ! ( RoomEventDecryptionResult :: Decrypted ( decrypted_event) = decryption_result) ;
800
+ let decrypted_event = decrypted_event. event . deserialize ( ) . unwrap ( ) ;
801
+
802
+ if let AnyTimelineEvent :: State ( AnyStateEvent :: RoomTopic ( StateEvent :: Original (
803
+ OriginalRoomTopicEvent { sender, content, .. } ,
804
+ ) ) ) = decrypted_event
805
+ {
806
+ assert_eq ! ( & sender, alice. user_id( ) ) ;
807
+ assert_eq ! ( & content. topic, plaintext) ;
808
+ } else {
809
+ panic ! ( "Decrypted room event has the wrong type" ) ;
810
+ }
811
+ }
812
+
730
813
#[ async_test]
731
814
async fn test_withheld_unverified ( ) {
732
815
let ( alice, bob) =
0 commit comments