matrix_sdk_base/room/
encryption.rs

1// Copyright 2025 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use ruma::events::room::encryption::RoomEncryptionEventContent;
16
17use super::Room;
18
19impl Room {
20    /// Get the encryption state of this room.
21    pub fn encryption_state(&self) -> EncryptionState {
22        self.inner.read().encryption_state()
23    }
24
25    /// Get the `m.room.encryption` content that enabled end to end encryption
26    /// in the room.
27    pub fn encryption_settings(&self) -> Option<RoomEncryptionEventContent> {
28        self.inner.read().base_info.encryption.clone()
29    }
30}
31
32/// Represents the state of a room encryption.
33#[derive(Debug)]
34#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
35pub enum EncryptionState {
36    /// The room is encrypted.
37    Encrypted,
38
39    /// The room is not encrypted.
40    NotEncrypted,
41
42    /// The state of the room encryption is unknown, probably because the
43    /// `/sync` did not provide all data needed to decide.
44    Unknown,
45}
46
47impl EncryptionState {
48    /// Check whether `EncryptionState` is [`Encrypted`][Self::Encrypted].
49    pub fn is_encrypted(&self) -> bool {
50        matches!(self, Self::Encrypted)
51    }
52
53    /// Check whether `EncryptionState` is [`Unknown`][Self::Unknown].
54    pub fn is_unknown(&self) -> bool {
55        matches!(self, Self::Unknown)
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use std::{
62        ops::{Not, Sub},
63        str::FromStr,
64        sync::Arc,
65        time::Duration,
66    };
67
68    use assert_matches::assert_matches;
69    use matrix_sdk_test::ALICE;
70    use ruma::{
71        events::{
72            room::encryption::{OriginalSyncRoomEncryptionEvent, RoomEncryptionEventContent},
73            AnySyncStateEvent, EmptyStateKey, StateUnsigned, SyncStateEvent,
74        },
75        room_id,
76        time::SystemTime,
77        user_id, EventEncryptionAlgorithm, MilliSecondsSinceUnixEpoch, OwnedEventId,
78    };
79
80    use super::{EncryptionState, Room};
81    use crate::{store::MemoryStore, RoomState};
82
83    fn make_room_test_helper(room_type: RoomState) -> (Arc<MemoryStore>, Room) {
84        let store = Arc::new(MemoryStore::new());
85        let user_id = user_id!("@me:example.org");
86        let room_id = room_id!("!test:localhost");
87        let (sender, _receiver) = tokio::sync::broadcast::channel(1);
88
89        (store.clone(), Room::new(user_id, store, room_id, room_type, sender))
90    }
91
92    fn timestamp(minutes_ago: u32) -> MilliSecondsSinceUnixEpoch {
93        MilliSecondsSinceUnixEpoch::from_system_time(
94            SystemTime::now().sub(Duration::from_secs((60 * minutes_ago).into())),
95        )
96        .expect("date out of range")
97    }
98
99    fn receive_state_events(room: &Room, events: Vec<&AnySyncStateEvent>) {
100        room.inner.update_if(|info| {
101            let mut res = false;
102            for ev in events {
103                res |= info.handle_state_event(ev);
104            }
105            res
106        });
107    }
108
109    #[test]
110    fn test_encryption_is_set_when_encryption_event_is_received_encrypted() {
111        let (_store, room) = make_room_test_helper(RoomState::Joined);
112
113        assert_matches!(room.encryption_state(), EncryptionState::Unknown);
114
115        let encryption_content =
116            RoomEncryptionEventContent::new(EventEncryptionAlgorithm::MegolmV1AesSha2);
117        let encryption_event = AnySyncStateEvent::RoomEncryption(SyncStateEvent::Original(
118            OriginalSyncRoomEncryptionEvent {
119                content: encryption_content,
120                event_id: OwnedEventId::from_str("$1234_1").unwrap(),
121                sender: ALICE.to_owned(),
122                // we can simply use now here since this will be dropped when using a
123                // MinimalStateEvent in the roomInfo
124                origin_server_ts: timestamp(0),
125                state_key: EmptyStateKey,
126                unsigned: StateUnsigned::new(),
127            },
128        ));
129        receive_state_events(&room, vec![&encryption_event]);
130
131        assert_matches!(room.encryption_state(), EncryptionState::Encrypted);
132    }
133
134    #[test]
135    fn test_encryption_is_set_when_encryption_event_is_received_not_encrypted() {
136        let (_store, room) = make_room_test_helper(RoomState::Joined);
137
138        assert_matches!(room.encryption_state(), EncryptionState::Unknown);
139        room.inner.update_if(|info| {
140            info.mark_encryption_state_synced();
141
142            false
143        });
144
145        assert_matches!(room.encryption_state(), EncryptionState::NotEncrypted);
146    }
147
148    #[test]
149    fn test_encryption_state() {
150        assert!(EncryptionState::Unknown.is_unknown());
151        assert!(EncryptionState::Encrypted.is_unknown().not());
152        assert!(EncryptionState::NotEncrypted.is_unknown().not());
153
154        assert!(EncryptionState::Unknown.is_encrypted().not());
155        assert!(EncryptionState::Encrypted.is_encrypted());
156        assert!(EncryptionState::NotEncrypted.is_encrypted().not());
157    }
158}