|
1 |
| -use std::sync::{Arc, Mutex}; |
| 1 | +use std::{ |
| 2 | + sync::{Arc, Mutex}, |
| 3 | + time::Duration, |
| 4 | +}; |
2 | 5 |
|
3 | 6 | use anyhow::Result;
|
4 | 7 | use assert_matches::assert_matches;
|
@@ -26,14 +29,133 @@ use matrix_sdk::{
|
26 | 29 | secret_storage::secret::SecretEventContent,
|
27 | 30 | GlobalAccountDataEventType, OriginalSyncMessageLikeEvent,
|
28 | 31 | },
|
| 32 | + OwnedEventId, |
29 | 33 | },
|
| 34 | + timeout::timeout, |
30 | 35 | Client,
|
31 | 36 | };
|
| 37 | +use matrix_sdk_ui::{ |
| 38 | + notification_client::{NotificationClient, NotificationProcessSetup}, |
| 39 | + sync_service::SyncService, |
| 40 | +}; |
32 | 41 | use similar_asserts::assert_eq;
|
33 | 42 | use tracing::{debug, warn};
|
34 | 43 |
|
35 | 44 | use crate::helpers::{SyncTokenAwareClient, TestClientBuilder};
|
36 | 45 |
|
| 46 | +// This test reproduces a bug seen on clients that use the same `Client` |
| 47 | +// instance for both the usual sliding sync loop and for getting the event for a |
| 48 | +// notification (i.e. Element X Android). The verification events will be |
| 49 | +// processed twice, meaning incorrect verification states will be found and the |
| 50 | +// process will fail, especially with user verification. |
| 51 | +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 52 | +async fn test_mutual_sas_verification_with_notification_client_ignores_verification_events( |
| 53 | +) -> Result<()> { |
| 54 | + let encryption_settings = |
| 55 | + EncryptionSettings { auto_enable_cross_signing: true, ..Default::default() }; |
| 56 | + let alice = TestClientBuilder::new("alice") |
| 57 | + .use_sqlite() |
| 58 | + .encryption_settings(encryption_settings) |
| 59 | + .build() |
| 60 | + .await?; |
| 61 | + |
| 62 | + let alice_sync_service = |
| 63 | + Arc::new(SyncService::builder(alice.clone()).build().await.expect("Wat")); |
| 64 | + |
| 65 | + let bob = TestClientBuilder::new("bob") |
| 66 | + .use_sqlite() |
| 67 | + .encryption_settings(encryption_settings) |
| 68 | + .build() |
| 69 | + .await?; |
| 70 | + |
| 71 | + let bob_sync_service = Arc::new(SyncService::builder(bob.clone()).build().await.expect("Wat")); |
| 72 | + let bob_id = bob.user_id().expect("Bob should be logged in by now"); |
| 73 | + |
| 74 | + alice.encryption().wait_for_e2ee_initialization_tasks().await; |
| 75 | + bob.encryption().wait_for_e2ee_initialization_tasks().await; |
| 76 | + |
| 77 | + alice_sync_service.start().await; |
| 78 | + bob_sync_service.start().await; |
| 79 | + |
| 80 | + warn!("alice's device: {}", alice.device_id().unwrap()); |
| 81 | + warn!("bob's device: {}", bob.device_id().unwrap()); |
| 82 | + |
| 83 | + // Set up the test: Alice creates the DM room, and invites Bob, who joins |
| 84 | + let invite = vec![bob_id.to_owned()]; |
| 85 | + let request = assign!(CreateRoomRequest::new(), { |
| 86 | + invite, |
| 87 | + is_direct: true, |
| 88 | + }); |
| 89 | + |
| 90 | + let alice_room = alice.create_room(request).await?; |
| 91 | + alice_room.enable_encryption().await?; |
| 92 | + let room_id = alice_room.room_id(); |
| 93 | + |
| 94 | + warn!("alice has created and enabled encryption in the room"); |
| 95 | + |
| 96 | + timeout( |
| 97 | + async { |
| 98 | + loop { |
| 99 | + if let Some(room) = bob.get_room(room_id) { |
| 100 | + room.join().await.expect("We should be able to join a room"); |
| 101 | + return; |
| 102 | + } |
| 103 | + } |
| 104 | + }, |
| 105 | + Duration::from_secs(1), |
| 106 | + ) |
| 107 | + .await |
| 108 | + .expect("Bob should have joined the room"); |
| 109 | + |
| 110 | + bob_sync_service.stop().await; |
| 111 | + |
| 112 | + warn!("alice and bob are both aware of each other in the e2ee room"); |
| 113 | + |
| 114 | + alice_room |
| 115 | + .send(RoomMessageEventContent::text_plain("Hello Bob")) |
| 116 | + .await |
| 117 | + .expect("We should be able to send a message to the room"); |
| 118 | + |
| 119 | + let alice_bob_identity = alice |
| 120 | + .encryption() |
| 121 | + .get_user_identity(bob_id) |
| 122 | + .await |
| 123 | + .expect("We should be able to fetch an identity from the store") |
| 124 | + .expect("Bob's identity should be known by now"); |
| 125 | + |
| 126 | + warn!("alice has found bob's identity"); |
| 127 | + |
| 128 | + let alice_verification_request = alice_bob_identity.request_verification().await?; |
| 129 | + |
| 130 | + // The notification client must use the `SingleProcess` setup |
| 131 | + let notification_client = NotificationClient::new( |
| 132 | + bob.clone(), |
| 133 | + NotificationProcessSetup::SingleProcess { sync_service: bob_sync_service.clone() }, |
| 134 | + ) |
| 135 | + .await |
| 136 | + .expect("couldn't create notification client"); |
| 137 | + |
| 138 | + let event_id = OwnedEventId::try_from(alice_verification_request.flow_id()) |
| 139 | + .expect("We should be able to get the event id from the verification flow id"); |
| 140 | + |
| 141 | + // Simulate getting the event for a notification |
| 142 | + let _ = notification_client.get_notification_with_sliding_sync(room_id, &event_id).await; |
| 143 | + |
| 144 | + let verification = bob |
| 145 | + .encryption() |
| 146 | + .get_verification_request( |
| 147 | + alice_verification_request.own_user_id(), |
| 148 | + alice_verification_request.flow_id(), |
| 149 | + ) |
| 150 | + .await; |
| 151 | + |
| 152 | + // If the ignore_verification_events parameter is true in NotificationClient, |
| 153 | + // no verification request should have been received |
| 154 | + assert!(verification.is_none()); |
| 155 | + |
| 156 | + Ok(()) |
| 157 | +} |
| 158 | + |
37 | 159 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
38 | 160 | async fn test_mutual_sas_verification() -> Result<()> {
|
39 | 161 | let encryption_settings =
|
|
0 commit comments