Skip to content

[TestRunner] Toger5/widget to device with encryption #4993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion bindings/matrix-sdk-ffi/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use async_compat::get_runtime_handle;
use language_tags::LanguageTag;
use matrix_sdk::{
async_trait,
widget::{MessageLikeEventFilter, StateEventFilter},
widget::{MessageLikeEventFilter, StateEventFilter, ToDeviceEventFilter},
};
use ruma::events::MessageLikeEventType;
use tracing::error;
Expand Down Expand Up @@ -321,7 +321,9 @@ pub fn get_element_call_required_permissions(
event_type: "org.matrix.rageshake_request".to_owned(),
},
// To read and send encryption keys
WidgetEventFilter::ToDevice { event_type: "io.element.call.encryption_keys".to_owned() },
// TODO change this to the appropriate to-device version once ready
// remove this once all calling supports to-device encryption
WidgetEventFilter::MessageLikeWithType {
event_type: "io.element.call.encryption_keys".to_owned(),
},
Expand Down Expand Up @@ -488,6 +490,8 @@ pub enum WidgetEventFilter {
StateWithType { event_type: String },
/// Matches state events with the given `type` and `state_key`.
StateWithTypeAndStateKey { event_type: String, state_key: String },
/// Matches to-device events with the given `event_type`.
ToDevice { event_type: String },
}

impl From<WidgetEventFilter> for matrix_sdk::widget::Filter {
Expand All @@ -505,6 +509,9 @@ impl From<WidgetEventFilter> for matrix_sdk::widget::Filter {
WidgetEventFilter::StateWithTypeAndStateKey { event_type, state_key } => {
Self::State(StateEventFilter::WithTypeAndStateKey(event_type.into(), state_key))
}
WidgetEventFilter::ToDevice { event_type } => {
Self::ToDevice(ToDeviceEventFilter { event_type: event_type.into() })
}
}
}
}
Expand All @@ -526,6 +533,9 @@ impl From<matrix_sdk::widget::Filter> for WidgetEventFilter {
F::State(StateEventFilter::WithTypeAndStateKey(event_type, state_key)) => {
Self::StateWithTypeAndStateKey { event_type: event_type.to_string(), state_key }
}
F::ToDevice(ToDeviceEventFilter { event_type }) => {
Self::ToDevice { event_type: event_type.to_string() }
}
}
}
}
Expand Down
61 changes: 61 additions & 0 deletions crates/matrix-sdk/src/test_utils/mocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,56 @@ impl MatrixMockServer {
self.mock_endpoint(mock, DeleteRoomKeysVersionEndpoint).expect_default_access_token()
}

/// Creates a prebuilt mock for the `/sendToDevice` endpoint.
///
/// This mock can be used to simulate sending to-device messages in tests.
/// # Examples
///
/// ```
/// # #[cfg(feature = "e2e-encryption")]
/// # {
/// # tokio_test::block_on(async {
/// use std::collections::BTreeMap;
/// use matrix_sdk::{
/// ruma::{
/// serde::Raw,
/// api::client::to_device::send_event_to_device::v3::Request as ToDeviceRequest,
/// to_device::DeviceIdOrAllDevices,
/// user_id,owned_device_id
/// },
/// test_utils::mocks::MatrixMockServer,
/// };
/// use serde_json::json;
///
/// let mock_server = MatrixMockServer::new().await;
/// let client = mock_server.client_builder().build().await;
///
/// mock_server.mock_send_to_device().ok().mock_once().mount().await;
///
/// let request = ToDeviceRequest::new_raw(
/// "m.custom.event".into(),
/// "txn_id".into(),
/// BTreeMap::from([
/// (user_id!("@alice:localhost").to_owned(), BTreeMap::from([(
/// DeviceIdOrAllDevices::AllDevices,
/// Raw::new(&ruma::events::AnyToDeviceEventContent::Dummy(ruma::events::dummy::ToDeviceDummyEventContent {})).unwrap(),
/// )])),
/// ])
/// );
///
/// client
/// .send(request)
/// .await
/// .expect("We should be able to send a to-device message");
/// # anyhow::Ok(()) });
/// # }
/// ```
pub fn mock_send_to_device(&self) -> MockEndpoint<'_, SendToDeviceEndpoint> {
let mock =
Mock::given(method("PUT")).and(path_regex(r"^/_matrix/client/v3/sendToDevice/.*/.*"));
self.mock_endpoint(mock, SendToDeviceEndpoint).expect_default_access_token()
}

/// Create a prebuilt mock for getting the room members in a room.
///
/// # Examples
Expand Down Expand Up @@ -2315,6 +2365,17 @@ impl<'a> MockEndpoint<'a, DeleteRoomKeysVersionEndpoint> {
}
}

/// A prebuilt mock for the `/sendToDevice` endpoint.
///
/// This mock can be used to simulate sending to-device messages in tests.
pub struct SendToDeviceEndpoint;
impl<'a> MockEndpoint<'a, SendToDeviceEndpoint> {
/// Returns a successful response with default data.
pub fn ok(self) -> MatrixMock<'a> {
self.respond_with(ResponseTemplate::new(200).set_body_json(json!({})))
}
}

/// A prebuilt mock for `GET /members` request.
pub struct GetRoomMembersEndpoint;

Expand Down
44 changes: 36 additions & 8 deletions crates/matrix-sdk/src/widget/capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use serde::{ser::SerializeSeq, Deserialize, Deserializer, Serialize, Serializer}
use tracing::{debug, warn};

use super::{
filter::{Filter, FilterInput},
filter::{Filter, FilterInput, ToDeviceEventFilter},
MessageLikeEventFilter, StateEventFilter,
};

Expand Down Expand Up @@ -60,7 +60,7 @@ pub struct Capabilities {
impl Capabilities {
/// Checks if a given event is allowed to be forwarded to the widget.
///
/// - `event_filter_input` is a minimized event respresntation that contains
/// - `event_filter_input` is a minimized event representation that contains
/// only the information needed to check if the widget is allowed to
/// receive the event. (See [`FilterInput`])
pub(super) fn allow_reading<'a>(
Expand All @@ -78,7 +78,7 @@ impl Capabilities {

/// Checks if a given event is allowed to be sent by the widget.
///
/// - `event_filter_input` is a minimized event respresntation that contains
/// - `event_filter_input` is a minimized event representation that contains
/// only the information needed to check if the widget is allowed to send
/// the event to a matrix room. (See [`FilterInput`])
pub(super) fn allow_sending<'a>(
Expand All @@ -102,11 +102,13 @@ impl Capabilities {
}
}

const SEND_EVENT: &str = "org.matrix.msc2762.send.event";
const READ_EVENT: &str = "org.matrix.msc2762.receive.event";
const SEND_STATE: &str = "org.matrix.msc2762.send.state_event";
const READ_STATE: &str = "org.matrix.msc2762.receive.state_event";
const REQUIRES_CLIENT: &str = "io.element.requires_client";
pub(super) const SEND_EVENT: &str = "org.matrix.msc2762.send.event";
pub(super) const READ_EVENT: &str = "org.matrix.msc2762.receive.event";
pub(super) const SEND_STATE: &str = "org.matrix.msc2762.send.state_event";
pub(super) const READ_STATE: &str = "org.matrix.msc2762.receive.state_event";
pub(super) const SEND_TODEVICE: &str = "org.matrix.msc3819.send.to_device";
pub(super) const READ_TODEVICE: &str = "org.matrix.msc3819.receive.to_device";
pub(super) const REQUIRES_CLIENT: &str = "io.element.requires_client";
pub(super) const SEND_DELAYED_EVENT: &str = "org.matrix.msc4157.send.delayed_event";
pub(super) const UPDATE_DELAYED_EVENT: &str = "org.matrix.msc4157.update_delayed_event";

Expand All @@ -121,6 +123,7 @@ impl Serialize for Capabilities {
match self.0 {
Filter::MessageLike(filter) => PrintMessageLikeEventFilter(filter).fmt(f),
Filter::State(filter) => PrintStateEventFilter(filter).fmt(f),
Filter::ToDevice(filter) => filter.fmt(f),
}
}
}
Expand Down Expand Up @@ -168,13 +171,15 @@ impl Serialize for Capabilities {
let name = match filter {
Filter::MessageLike(_) => READ_EVENT,
Filter::State(_) => READ_STATE,
Filter::ToDevice(_) => READ_TODEVICE,
};
seq.serialize_element(&format!("{name}:{}", PrintEventFilter(filter)))?;
}
for filter in &self.send {
let name = match filter {
Filter::MessageLike(_) => SEND_EVENT,
Filter::State(_) => SEND_STATE,
Filter::ToDevice(_) => SEND_TODEVICE,
};
seq.serialize_element(&format!("{name}:{}", PrintEventFilter(filter)))?;
}
Expand Down Expand Up @@ -226,6 +231,12 @@ impl<'de> Deserialize<'de> for Capabilities {
Some((SEND_STATE, filter_s)) => {
Ok(Permission::Send(Filter::State(parse_state_event_filter(filter_s))))
}
Some((READ_TODEVICE, filter_s)) => Ok(Permission::Read(Filter::ToDevice(
parse_to_device_event_filter(filter_s),
))),
Some((SEND_TODEVICE, filter_s)) => Ok(Permission::Send(Filter::ToDevice(
parse_to_device_event_filter(filter_s),
))),
_ => {
debug!("Unknown capability `{s}`");
Ok(Self::Unknown)
Expand All @@ -252,6 +263,10 @@ impl<'de> Deserialize<'de> for Capabilities {
}
}

fn parse_to_device_event_filter(s: &str) -> ToDeviceEventFilter {
ToDeviceEventFilter::new(s.into())
}

let mut capabilities = Capabilities::default();
for capability in Vec::<Permission>::deserialize(deserializer)? {
match capability {
Expand All @@ -274,6 +289,7 @@ mod tests {
use ruma::events::StateEventType;

use super::*;
use crate::widget::filter::ToDeviceEventFilter;

#[test]
fn deserialization_of_no_capabilities() {
Expand All @@ -293,8 +309,10 @@ mod tests {
"org.matrix.msc2762.receive.event:org.matrix.rageshake_request",
"org.matrix.msc2762.receive.state_event:m.room.member",
"org.matrix.msc2762.receive.state_event:org.matrix.msc3401.call.member",
"org.matrix.msc3819.receive.to_device:io.element.call.encryption_keys",
"org.matrix.msc2762.send.event:org.matrix.rageshake_request",
"org.matrix.msc2762.send.state_event:org.matrix.msc3401.call.member#@user:matrix.server",
"org.matrix.msc3819.send.to_device:io.element.call.encryption_keys",
"org.matrix.msc4157.send.delayed_event",
"org.matrix.msc4157.update_delayed_event"
]"#;
Expand All @@ -307,6 +325,9 @@ mod tests {
)),
Filter::State(StateEventFilter::WithType(StateEventType::RoomMember)),
Filter::State(StateEventFilter::WithType("org.matrix.msc3401.call.member".into())),
Filter::ToDevice(ToDeviceEventFilter::new(
"io.element.call.encryption_keys".into(),
)),
],
send: vec![
Filter::MessageLike(MessageLikeEventFilter::WithType(
Expand All @@ -316,6 +337,9 @@ mod tests {
"org.matrix.msc3401.call.member".into(),
"@user:matrix.server".into(),
)),
Filter::ToDevice(ToDeviceEventFilter::new(
"io.element.call.encryption_keys".into(),
)),
],
requires_client: true,
update_delayed_event: true,
Expand All @@ -335,13 +359,17 @@ mod tests {
"org.matrix.msc3401.call.member".into(),
"@user:matrix.server".into(),
)),
Filter::ToDevice(ToDeviceEventFilter::new(
"io.element.call.encryption_keys".into(),
)),
],
send: vec![
Filter::MessageLike(MessageLikeEventFilter::WithType("io.element.custom".into())),
Filter::State(StateEventFilter::WithTypeAndStateKey(
"org.matrix.msc3401.call.member".into(),
"@user:matrix.server".into(),
)),
Filter::ToDevice(ToDeviceEventFilter::new("my.org.other.to_device_event".into())),
],
requires_client: true,
update_delayed_event: false,
Expand Down
Loading
Loading