From ce66ee4a168aa03e1171d4859dfeb8309115d651 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Fri, 13 Jun 2025 12:53:49 +0200 Subject: [PATCH] test(sdk): Test the conditions under which we accept a historic room key bundle --- crates/matrix-sdk/src/encryption/tasks.rs | 46 +++++++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/crates/matrix-sdk/src/encryption/tasks.rs b/crates/matrix-sdk/src/encryption/tasks.rs index 29230374d..839e7fe76 100644 --- a/crates/matrix-sdk/src/encryption/tasks.rs +++ b/crates/matrix-sdk/src/encryption/tasks.rs @@ -454,13 +454,13 @@ impl BundleReceiverTask { #[cfg(all(test, not(target_family = "wasm")))] mod test { - use matrix_sdk_test::async_test; - use ruma::{event_id, room_id}; + use matrix_sdk_test::{async_test, InvitedRoomBuilder, JoinedRoomBuilder}; + use ruma::{event_id, room_id, user_id}; use serde_json::json; use wiremock::MockServer; use super::*; - use crate::test_utils::logged_in_client; + use crate::test_utils::{logged_in_client, mocks::MatrixMockServer}; // Test that, if backups are not enabled, we don't incorrectly mark a room key // as downloaded. @@ -518,4 +518,44 @@ mod test { ) } } + + /// Test that ensures that we only accept a bundle if a certain set of + /// conditions is met. + #[async_test] + async fn test_should_accept_bundle() { + let server = MatrixMockServer::new().await; + let client = server.client_builder().logged_in_with_oauth().build().await; + + let user_id = user_id!("@alice:localhost"); + let joined_room_id = room_id!("!joined:localhost"); + let invited_rom_id = room_id!("!invited:localhost"); + + server + .mock_sync() + .ok_and_run(&client, |builder| { + builder + .add_joined_room(JoinedRoomBuilder::new(joined_room_id)) + .add_invited_room(InvitedRoomBuilder::new(invited_rom_id)); + }) + .await; + + let room = + client.get_room(joined_room_id).expect("We should have access to our joined room now"); + + let bundle_info = + RoomKeyBundleInfo { sender: user_id.to_owned(), room_id: joined_room_id.to_owned() }; + + assert!(BundleReceiverTask::should_accept_bundle(&room, &bundle_info)); + + let invited_room = + client.get_room(invited_rom_id).expect("We should have access to our invited room now"); + + assert!( + !BundleReceiverTask::should_accept_bundle(&invited_room, &bundle_info), + "We should not accept a bundle if we didn't join the room." + ); + + // TODO: Add more cases here once we figure out the correct acceptance + // rules. + } }