@@ -733,15 +733,9 @@ async fn test_megolm_encryption() {
733
733
}
734
734
735
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
- use crate :: types:: events:: room:: { encrypted:: RoomEncryptedEventContent , Event } ;
741
-
736
+ async fn megolm_encryption_setup_helper ( room_id : & RoomId ) -> ( OlmMachine , OlmMachine ) {
742
737
let ( alice, bob) =
743
738
get_machine_pair_with_setup_sessions_test_helper ( alice_id ( ) , user_id ( ) , false ) . await ;
744
- let room_id = room_id ! ( "!test:example.org" ) ;
745
739
746
740
let to_device_requests = alice
747
741
. share_room_key ( room_id, iter:: once ( bob. user_id ( ) ) , EncryptionSettings :: default ( ) )
@@ -776,10 +770,19 @@ async fn test_megolm_state_encryption() {
776
770
let sessions = std:: slice:: from_ref ( & group_session) ;
777
771
bob. store ( ) . save_inbound_group_sessions ( sessions) . await . unwrap ( ) ;
778
772
779
- let plaintext = "It is a secret to everybody" ;
773
+ ( alice, bob)
774
+ }
780
775
781
- let content = RoomTopicEventContent :: new ( plaintext. to_owned ( ) ) ;
776
+ #[ cfg( feature = "experimental-encrypted-state-events" ) ]
777
+ #[ async_test]
778
+ async fn test_megolm_state_encryption ( ) {
779
+ use ruma:: events:: { AnyStateEvent , EmptyStateKey } ;
782
780
781
+ let room_id = room_id ! ( "!test:example.org" ) ;
782
+ let ( alice, bob) = megolm_encryption_setup_helper ( room_id) . await ;
783
+
784
+ let plaintext = "It is a secret to everybody" ;
785
+ let content = RoomTopicEventContent :: new ( plaintext. to_owned ( ) ) ;
783
786
let encrypted_content =
784
787
alice. encrypt_state_event ( room_id, content, EmptyStateKey ) . await . unwrap ( ) ;
785
788
@@ -792,6 +795,42 @@ async fn test_megolm_state_encryption() {
792
795
"content" : encrypted_content,
793
796
} ) ;
794
797
798
+ let event = json_convert ( & event) . unwrap ( ) ;
799
+
800
+ let decryption_settings =
801
+ DecryptionSettings { sender_device_trust_requirement : TrustRequirement :: Untrusted } ;
802
+
803
+ let decryption_result =
804
+ bob. try_decrypt_room_event ( & event, room_id, & decryption_settings) . await . unwrap ( ) ;
805
+
806
+ assert_let ! ( RoomEventDecryptionResult :: Decrypted ( decrypted_event) = decryption_result) ;
807
+
808
+ let decrypted_event = decrypted_event. event . deserialize ( ) . unwrap ( ) ;
809
+
810
+ if let AnyTimelineEvent :: State ( AnyStateEvent :: RoomTopic ( StateEvent :: Original (
811
+ OriginalRoomTopicEvent { sender, content, .. } ,
812
+ ) ) ) = decrypted_event
813
+ {
814
+ assert_eq ! ( & sender, alice. user_id( ) ) ;
815
+ assert_eq ! ( & content. topic, plaintext) ;
816
+ } else {
817
+ panic ! ( "Decrypted room event has the wrong type" ) ;
818
+ }
819
+ }
820
+
821
+ #[ cfg( feature = "experimental-encrypted-state-events" ) ]
822
+ #[ async_test]
823
+ async fn test_megolm_state_encryption_bad_type ( ) {
824
+ use ruma:: events:: EmptyStateKey ;
825
+
826
+ let room_id = room_id ! ( "!test:example.org" ) ;
827
+ let ( alice, bob) = megolm_encryption_setup_helper ( room_id) . await ;
828
+
829
+ let plaintext = "It is a secret to everybody" ;
830
+ let content = RoomTopicEventContent :: new ( plaintext. to_owned ( ) ) ;
831
+ let encrypted_content =
832
+ alice. encrypt_state_event ( room_id, content, EmptyStateKey ) . await . unwrap ( ) ;
833
+
795
834
// Malformed events
796
835
let bad_type_event = json ! ( {
797
836
"event_id" : "$xxxxx:example.org" ,
@@ -801,6 +840,37 @@ async fn test_megolm_state_encryption() {
801
840
"state_key" : "m.room.malformed:" ,
802
841
"content" : encrypted_content,
803
842
} ) ;
843
+
844
+ let bad_type_event = json_convert ( & bad_type_event) . unwrap ( ) ;
845
+
846
+ let decryption_settings =
847
+ DecryptionSettings { sender_device_trust_requirement : TrustRequirement :: Untrusted } ;
848
+
849
+ let bad_type_decryption_result =
850
+ bob. try_decrypt_room_event ( & bad_type_event, room_id, & decryption_settings) . await . unwrap ( ) ;
851
+
852
+ assert_matches ! (
853
+ bad_type_decryption_result,
854
+ RoomEventDecryptionResult :: UnableToDecrypt ( UnableToDecryptInfo {
855
+ reason: UnableToDecryptReason :: StateKeyVerificationFailed ,
856
+ ..
857
+ } )
858
+ ) ;
859
+ }
860
+
861
+ #[ cfg( feature = "experimental-encrypted-state-events" ) ]
862
+ #[ async_test]
863
+ async fn test_megolm_state_encryption_bad_state_key ( ) {
864
+ use ruma:: events:: EmptyStateKey ;
865
+
866
+ let room_id = room_id ! ( "!test:example.org" ) ;
867
+ let ( alice, bob) = megolm_encryption_setup_helper ( room_id) . await ;
868
+
869
+ let plaintext = "It is a secret to everybody" ;
870
+ let content = RoomTopicEventContent :: new ( plaintext. to_owned ( ) ) ;
871
+ let encrypted_content =
872
+ alice. encrypt_state_event ( room_id, content, EmptyStateKey ) . await . unwrap ( ) ;
873
+
804
874
let bad_state_key_event = json ! ( {
805
875
"event_id" : "$xxxxx:example.org" ,
806
876
"origin_server_ts" : MilliSecondsSinceUnixEpoch :: now( ) ,
@@ -810,54 +880,24 @@ async fn test_megolm_state_encryption() {
810
880
"content" : encrypted_content,
811
881
} ) ;
812
882
813
- let event = json_convert ( & event) . unwrap ( ) ;
814
-
815
- let bad_type_event: Raw < Event < RoomEncryptedEventContent > > =
816
- json_convert ( & bad_type_event) . unwrap ( ) ;
817
883
let bad_state_key_event = json_convert ( & bad_state_key_event) . unwrap ( ) ;
818
884
819
885
let decryption_settings =
820
886
DecryptionSettings { sender_device_trust_requirement : TrustRequirement :: Untrusted } ;
821
887
822
- let decryption_result =
823
- bob. try_decrypt_room_event ( & event, room_id, & decryption_settings) . await . unwrap ( ) ;
824
-
825
- let bad_type_decryption_result =
826
- bob. try_decrypt_room_event ( & bad_type_event, room_id, & decryption_settings) . await . unwrap ( ) ;
827
888
let bad_state_key_decryption_result = bob
828
889
. try_decrypt_room_event ( & bad_state_key_event, room_id, & decryption_settings)
829
890
. await
830
891
. unwrap ( ) ;
831
892
832
- assert_let ! ( RoomEventDecryptionResult :: Decrypted ( decrypted_event) = decryption_result) ;
833
-
834
893
// Require malformed events fail verification
835
- assert_matches ! (
836
- bad_type_decryption_result,
837
- RoomEventDecryptionResult :: UnableToDecrypt ( UnableToDecryptInfo {
838
- reason: UnableToDecryptReason :: StateKeyVerificationFailed ,
839
- ..
840
- } )
841
- ) ;
842
894
assert_matches ! (
843
895
bad_state_key_decryption_result,
844
896
RoomEventDecryptionResult :: UnableToDecrypt ( UnableToDecryptInfo {
845
897
reason: UnableToDecryptReason :: StateKeyVerificationFailed ,
846
898
..
847
899
} )
848
900
) ;
849
-
850
- let decrypted_event = decrypted_event. event . deserialize ( ) . unwrap ( ) ;
851
-
852
- if let AnyTimelineEvent :: State ( AnyStateEvent :: RoomTopic ( StateEvent :: Original (
853
- OriginalRoomTopicEvent { sender, content, .. } ,
854
- ) ) ) = decrypted_event
855
- {
856
- assert_eq ! ( & sender, alice. user_id( ) ) ;
857
- assert_eq ! ( & content. topic, plaintext) ;
858
- } else {
859
- panic ! ( "Decrypted room event has the wrong type" ) ;
860
- }
861
901
}
862
902
863
903
#[ async_test]
0 commit comments