Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andybalaam committed Apr 19, 2024
1 parent f7329c7 commit 1966446
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 7 deletions.
33 changes: 26 additions & 7 deletions testing/matrix-sdk-integration-testing/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{
collections::HashMap,
ops::Deref,
option_env,
path::{Path, PathBuf},
sync::{Arc, Mutex as StdMutex},
time::Duration,
};
Expand All @@ -21,9 +22,14 @@ use tokio::sync::Mutex;

static USERS: Lazy<Mutex<HashMap<String, (Client, TempDir)>>> = Lazy::new(Mutex::default);

enum SqlitePath {
Random,
Path(PathBuf),
}

pub struct TestClientBuilder {
username: String,
use_sqlite: bool,
use_sqlite_dir: Option<SqlitePath>,
encryption_settings: EncryptionSettings,
http_proxy: Option<String>,
}
Expand All @@ -32,7 +38,7 @@ impl TestClientBuilder {
pub fn new(username: impl Into<String>) -> Self {
Self {
username: username.into(),
use_sqlite: false,
use_sqlite_dir: None,
encryption_settings: Default::default(),
http_proxy: None,
}
Expand All @@ -45,7 +51,16 @@ impl TestClientBuilder {
}

pub fn use_sqlite(mut self) -> Self {
self.use_sqlite = true;
self.use_sqlite_dir = Some(SqlitePath::Random);
self
}

/// Create or re-use a Sqlite store (with no passphrase) in the supplied
/// directory. Note: this path must remain valid throughout the use of
/// the constructed Client, so if you created a TempDir you must hang on
/// to a reference to it throughout the test.
pub fn use_sqlite_dir(mut self, path: &Path) -> Self {
self.use_sqlite_dir = Some(SqlitePath::Path(path.to_owned()));
self
}

Expand Down Expand Up @@ -83,10 +98,14 @@ impl TestClientBuilder {
client_builder = client_builder.proxy(proxy);
}

let client = if self.use_sqlite {
client_builder.sqlite_store(tmp_dir.path(), None).build().await?
} else {
client_builder.build().await?
let client = match self.use_sqlite_dir {
None => client_builder.build().await?,
Some(SqlitePath::Random) => {
client_builder.sqlite_store(tmp_dir.path(), None).build().await?
}
Some(SqlitePath::Path(path_buf)) => {
client_builder.sqlite_store(&path_buf, None).build().await?
}
};

// safe to assume we have not registered this user yet, but ignore if we did
Expand Down
125 changes: 125 additions & 0 deletions testing/matrix-sdk-integration-testing/src/tests/e2ee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ use matrix_sdk::{
SyncRoomMessageEvent,
},
},
EventId,
},
Client,
};
use tempfile::tempdir;
use tracing::warn;

use crate::helpers::{SyncTokenAwareClient, TestClientBuilder};
Expand Down Expand Up @@ -289,6 +291,129 @@ async fn test_mutual_sas_verification() -> Result<()> {
Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_multiple_clients_share_crypto_state() -> Result<()> {
let encryption_settings =
EncryptionSettings { auto_enable_cross_signing: true, ..Default::default() };
let alice_sqlite_dir = tempdir()?;
let alice1 = SyncTokenAwareClient::new(
TestClientBuilder::new("alice1")
.use_sqlite_dir(alice_sqlite_dir.path())
.encryption_settings(encryption_settings)
.build()
.await?,
);
let alice2 = SyncTokenAwareClient::new(
TestClientBuilder::new("alice2")
.use_sqlite_dir(alice_sqlite_dir.path())
.encryption_settings(encryption_settings)
.build()
.await?,
);
let bob = SyncTokenAwareClient::new(
TestClientBuilder::new("bob")
.randomize_username()
.use_sqlite()
.encryption_settings(encryption_settings)
.build()
.await?,
);

warn!("alice's device: {}", alice1.device_id().unwrap());
warn!("bob's device: {}", bob.device_id().unwrap());

// Both alice clients share the same device ID because they are sharing the same DB.
//assert_eq!(alice1.device_id(), alice2.device_id());

let invite = vec![bob.user_id().unwrap().to_owned()];
let request = assign!(CreateRoomRequest::new(), {
invite,
is_direct: true,
});

let alice1_room = alice1.create_room(request).await?;
alice1_room.enable_encryption().await?;
let room_id = alice1_room.room_id();

let alice1_events = Arc::new(Mutex::new(Vec::new()));
let alice1_events_clone = alice1_events.clone();
alice1.add_event_handler(|ev: OriginalSyncRoomMessageEvent, _client: Client| async move {
alice1_events_clone.lock().unwrap().push(ev.event_id.clone())
});
let alice1_received = move |event_id: &EventId| {
alice1_events.lock().unwrap().iter().find(|&id| id == event_id).is_some()
};

let alice2_events = Arc::new(Mutex::new(Vec::new()));
let alice2_events_clone = alice2_events.clone();
alice2.add_event_handler(|ev: OriginalSyncRoomMessageEvent, _client: Client| async move {
alice2_events_clone.lock().unwrap().push(ev.event_id.clone())
});
let alice2_received = move |event_id: &EventId| {
alice2_events.lock().unwrap().iter().find(|&id| id == event_id).is_some()
};

let bob_events = Arc::new(Mutex::new(Vec::new()));
let bob_events_clone = bob_events.clone();
bob.add_event_handler(|ev: OriginalSyncRoomMessageEvent, _client: Client| async move {
bob_events_clone.lock().unwrap().push(ev.event_id.clone())
});
let bob_received = move |event_id: &EventId| {
bob_events.lock().unwrap().iter().find(|&id| id == event_id).is_some()
};

warn!("alice1 has created and enabled encryption in the room");

bob.sync_once().await?;
let bob_room = bob.get_room(room_id).unwrap();
bob_room.join().await?;

alice1.sync_once().await?;

warn!("alice1 and bob are both aware of each other in the e2ee room");

let msg_01_bob_alice =
bob_room.send(RoomMessageEventContent::text_plain("msg_01_bob_alice")).await?;

while !alice1_received(&msg_01_bob_alice.event_id) {
alice1.sync_once().await?;
}

warn!("alice1 received msg_01_bob_alice from bob");

let msg_02_bob_alice =
bob_room.send(RoomMessageEventContent::text_plain("msg_02_bob_alice")).await?;

while !alice2_received(&msg_02_bob_alice.event_id) {
alice2.sync_once().await?;
}

warn!("alice2 received msg_02_bob_alice from bob");

let msg_03_alice_bob =
alice1_room.send(RoomMessageEventContent::text_plain("msg_03_alice_bob")).await?;

while !bob_received(&msg_03_alice_bob.event_id) {
bob.sync_once().await?;
}

warn!("bob received msg_03_alice_bob from alice1");

let msg_04_bob_alice =
bob_room.send(RoomMessageEventContent::text_plain("msg_04_bob_alice")).await?;

while !alice1_received(&msg_04_bob_alice.event_id) {
alice1.sync_once().await?;
}
while !alice2_received(&msg_04_bob_alice.event_id) {
alice2.sync_once().await?;
}

warn!("alice1 and alice2 both received msg_04_bob_alice from bob");

Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_mutual_qrcode_verification() -> Result<()> {
let encryption_settings =
Expand Down

0 comments on commit 1966446

Please sign in to comment.