cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1 | mod support; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 2 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 3 | use std::sync::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 4 | Arc, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 5 | atomic::{AtomicUsize, Ordering}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 6 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 7 | use std::time::Duration; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 8 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 9 | use macltok::{Engine, EngineConfig, Event, EventStream, MentionsPage}; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 10 | use matrix_sdk::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 11 | ruma::{RoomVersionId, device_id, event_id, room_id, user_id}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 12 | test_utils::mocks::MatrixMockServer, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 13 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 14 | use matrix_sdk_test::{JoinedRoomBuilder, event_factory::EventFactory}; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 15 | use serde_json::{Value, json}; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 16 | use support::shut_down; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 17 | use wiremock::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 18 | Mock, Request, Respond, ResponseTemplate, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 19 | matchers::{method, path}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 20 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 21 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 22 | const SLIDING_SYNC_PATH: &str = "/_matrix/client/unstable/org.matrix.simplified_msc3575/sync"; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 23 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 24 | struct EncryptedSlidingFeed { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 25 | room_id: String, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 26 | event: Value, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 27 | requests: Arc<AtomicUsize>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 28 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 29 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 30 | impl Clone for EncryptedSlidingFeed { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 31 | fn clone(&self) -> Self { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 32 | Self { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 33 | room_id: self.room_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 34 | event: self.event.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 35 | requests: self.requests.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 36 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 37 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 38 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 39 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 40 | impl EncryptedSlidingFeed { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 41 | async fn mount(&self, server: &MatrixMockServer) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 42 | Mock::given(method("POST")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 43 | .and(path(SLIDING_SYNC_PATH)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 44 | .respond_with(self.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 45 | .mount(server.server()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 46 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 47 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 48 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 49 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 50 | impl Respond for EncryptedSlidingFeed { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 51 | fn respond(&self, _request: &Request) -> ResponseTemplate { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 52 | let request = self.requests.fetch_add(1, Ordering::AcqRel); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 53 | ResponseTemplate::new(200) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 54 | .set_delay(Duration::from_millis(20)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 55 | .set_body_json(json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 56 | "txn_id": null, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 57 | "pos": format!("late-decryption-{request}"), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 58 | "lists": {}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 59 | "rooms": { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 60 | (self.room_id.clone()): { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 61 | "initial": request == 0, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 62 | "required_state": [], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 63 | "timeline": [self.event.clone()], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 64 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 65 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 66 | "extensions": {}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 67 | })) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 68 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 69 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 70 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 71 | async fn mentions_page( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 72 | events: &mut EventStream, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 73 | room_id: &ruma::RoomId, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 74 | predicate: impl Fn(&MentionsPage) -> bool, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 75 | ) -> MentionsPage { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 76 | tokio::time::timeout(Duration::from_secs(5), async { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 77 | while let Some(event) = events.recv().await { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 78 | match event { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 79 | Event::TimelineReset { timeline_id, .. } if timeline_id.room_id() == room_id => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 80 | panic!("the unopened room produced a timeline reset"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 81 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 82 | Event::MentionsChanged(update) if predicate(&update.snapshot) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 83 | return update.snapshot; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 84 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 85 | _ => {} |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 86 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 87 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 88 | panic!("event stream closed before the expected mentions update"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 89 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 90 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 91 | .expect("expected mentions update did not arrive") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 92 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 93 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 94 | async fn wait_for_requests(feed: &EncryptedSlidingFeed, minimum: usize) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 95 | tokio::time::timeout(Duration::from_secs(5), async { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 96 | while feed.requests.load(Ordering::Acquire) < minimum { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 97 | tokio::task::yield_now().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 98 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 99 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 100 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 101 | .expect("the sliding sync feed did not receive another request"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 102 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 103 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 104 | #[tokio::test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 105 | async fn late_room_key_updates_the_production_mentions_inbox_once() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 106 | let server = MatrixMockServer::new().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 107 | server.mock_crypto_endpoints_preset().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 108 | let alice_id = user_id!("@alice:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 109 | let bob_id = user_id!("@bob:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 110 | let room_id = room_id!("!mentions-late-decryption:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 111 | let event_id = event_id!("$late-production-mention"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 112 | let alice = server |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 113 | .client_builder_for_crypto_end_to_end(alice_id, device_id!("ALICEDEVICE")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 114 | .build() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 115 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 116 | let bob = server |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 117 | .client_builder_for_crypto_end_to_end(bob_id, device_id!("BOBDEVICE")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 118 | .build() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 119 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 120 | bob.event_cache().subscribe().unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 121 | server.exchange_e2ee_identities(&alice, &bob).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 122 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 123 | let factory = EventFactory::new().room(room_id).sender(alice_id); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 124 | let joined = JoinedRoomBuilder::new(room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 125 | .add_state_event(factory.create(alice_id, RoomVersionId::V1)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 126 | .add_state_event(factory.room_encryption()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 127 | .add_state_event(factory.member(alice_id)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 128 | .add_state_event(factory.member(bob_id)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 129 | server.sync_room(&alice, joined.clone()).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 130 | server.sync_room(&bob, joined).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 131 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 132 | let room = alice.get_room(room_id).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 133 | let (encrypted_event, send) = server.mock_room_send().ok_with_capture(event_id, alice_id); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 134 | let _send = send.mock_once().mount_as_scoped().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 135 | let (_keys, room_key) = server.mock_capture_put_to_device(alice_id).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 136 | server |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 137 | .mock_get_members() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 138 | .ok(vec![ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 139 | factory.member(alice_id).into_raw(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 140 | factory.member(bob_id).into_raw(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 141 | ]) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 142 | .mock_once() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 143 | .mount() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 144 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 145 | room.send_raw( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 146 | "m.room.message", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 147 | json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 148 | "msgtype": "m.text", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 149 | "body": "a private mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 150 | "m.mentions": { "user_ids": [bob_id] } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 151 | }), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 152 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 153 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 154 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 155 | let encrypted_event = encrypted_event.await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 156 | let room_key = room_key.await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 157 | let encrypted_event: Value = serde_json::from_str(encrypted_event.json().get()).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 158 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 159 | let feed = EncryptedSlidingFeed { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 160 | room_id: room_id.to_string(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 161 | event: encrypted_event, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 162 | requests: Arc::new(AtomicUsize::new(0)), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 163 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 164 | feed.mount(&server).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 165 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 166 | let (engine, handle, mut events) = Engine::from_client( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 167 | bob.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 168 | EngineConfig::default().with_initial_history_limit(0), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 169 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 170 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 171 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 172 | let running = tokio::spawn(engine.run()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 173 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 174 | let unresolved = mentions_page(&mut events, room_id, |page| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 175 | page.coverage.unresolved_candidates == 1 |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 176 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 177 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 178 | assert!(unresolved.entries.is_empty()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 179 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 180 | server |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 181 | .mock_sync() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 182 | .ok_and_run(&bob, |sync| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 183 | sync.add_to_device_event(room_key.deserialize_as().unwrap()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 184 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 185 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 186 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 187 | let decrypted = mentions_page(&mut events, room_id, |page| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 188 | page.entries.len() == 1 && page.entries[0].key.event_id == event_id |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 189 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 190 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 191 | assert_eq!(decrypted.entries.len(), 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 192 | assert_eq!(decrypted.entries[0].key.event_id, event_id); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 193 | assert_eq!(decrypted.coverage.unresolved_candidates, 0); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 194 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 195 | let replay_start = feed.requests.load(Ordering::Acquire); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 196 | wait_for_requests(&feed, replay_start + 3).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 197 | let replayed = handle.mentions_page(None, 50).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 198 | assert_eq!(replayed.entries.len(), 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 199 | assert_eq!(replayed.entries[0].key.event_id, event_id); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 200 | while let Ok(event) = events.try_recv() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 201 | if let Event::TimelineReset { timeline_id, .. } = event { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 202 | assert_ne!(timeline_id.room_id(), room_id); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 203 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 204 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 205 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 206 | shut_down(&handle, &mut events, running).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 207 | } |