From 7607c4ef828967d6aa87159eca7ce73fa285d8e7 Mon Sep 17 00:00:00 2001 From: kaylendog Date: Thu, 2 Oct 2025 18:13:57 +0100 Subject: [PATCH] tests: Test deserializing `m.room_key.withheld` to withheld entry. Tests that a to-device `m.room_key.withheld` event can be serialized (using JSON), then deserialized as a RoomKeyWithheldEntry. Ensures compatibility with exisiting store data. --- crates/matrix-sdk-crypto/src/store/mod.rs | 55 +++++++++++++++++++++-- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/crates/matrix-sdk-crypto/src/store/mod.rs b/crates/matrix-sdk-crypto/src/store/mod.rs index dea03f523..fae04cc12 100644 --- a/crates/matrix-sdk-crypto/src/store/mod.rs +++ b/crates/matrix-sdk-crypto/src/store/mod.rs @@ -1717,17 +1717,22 @@ impl matrix_sdk_common::cross_process_lock::TryLock for LockableCryptoStore { mod tests { use std::pin::pin; + use assert_matches2::assert_matches; use futures_util::StreamExt; use insta::{_macro_support::Content, assert_json_snapshot, internals::ContentPath}; use matrix_sdk_test::async_test; - use ruma::{device_id, room_id, user_id, RoomId}; + use ruma::{device_id, owned_device_id, room_id, user_id, RoomId}; + use serde_json::json; use vodozemac::megolm::SessionKey; use crate::{ machine::test_helpers::get_machine_pair, olm::{InboundGroupSession, SenderData}, - store::types::DehydratedDeviceKey, - types::EventEncryptionAlgorithm, + store::types::{DehydratedDeviceKey, RoomKeyWithheldEntry}, + types::{ + events::room_key_withheld::{MegolmV1AesSha2WithheldContent, RoomKeyWithheldContent}, + EventEncryptionAlgorithm, + }, OlmMachine, }; @@ -1969,6 +1974,50 @@ mod tests { }); } + /// Tests that the new store format introduced in [#5737][#5737] does not + /// conflict with items already in the store that were serialised with the + /// older format. + /// + /// [#5737]: https://github.com/matrix-org/matrix-rust-sdk/pull/5737 + #[async_test] + async fn test_deserialize_room_key_withheld_entry_from_to_device_event() { + let entry: RoomKeyWithheldEntry = serde_json::from_value(json!( + { + "content": { + "algorithm": "m.megolm.v1.aes-sha2", + "code": "m.unauthorised", + "from_device": "ALICE", + "reason": "You are not authorised to read the message.", + "room_id": "!roomid:s.co", + "sender_key": "7hIcOrEroXYdzjtCBvBjUiqvT0Me7g+ymeXqoc65RS0", + "session_id": "session123" + }, + "sender": "@alice:s.co", + "type": "m.room_key.withheld" + } + )) + .unwrap(); + + assert_matches!( + entry, + RoomKeyWithheldEntry { + sender, + content: RoomKeyWithheldContent::MegolmV1AesSha2( + MegolmV1AesSha2WithheldContent::Unauthorised(withheld_content,) + ), + } + ); + + assert_eq!(sender, "@alice:s.co"); + assert_eq!(withheld_content.room_id, "!roomid:s.co"); + assert_eq!(withheld_content.session_id, "session123"); + assert_eq!( + withheld_content.sender_key.to_base64(), + "7hIcOrEroXYdzjtCBvBjUiqvT0Me7g+ymeXqoc65RS0" + ); + assert_eq!(withheld_content.from_device, Some(owned_device_id!("ALICE"))); + } + /// Create an inbound Megolm session for the given room. /// /// `olm_machine` is used to set the `sender_key` and `signing_key`