Skip to content

Commit a5517ff

Browse files
committed
crypto: Helpers for NSE test
1 parent bca8452 commit a5517ff

File tree

4 files changed

+69
-9
lines changed

4 files changed

+69
-9
lines changed

crates/matrix-sdk-crypto/src/machine.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use tokio::sync::Mutex;
4848
use tracing::{
4949
debug, error,
5050
field::{debug, display},
51-
info, instrument, warn, Span,
51+
info, instrument, trace, warn, Span,
5252
};
5353
use vodozemac::{
5454
megolm::{DecryptionError, SessionOrdering},
@@ -1918,9 +1918,12 @@ impl OlmMachine {
19181918
&self,
19191919
generation: &Mutex<Option<u64>>,
19201920
) -> StoreResult<()> {
1921+
trace!("initialize_crypto_store_generation()");
1922+
19211923
// Avoid reentrant initialization by taking the lock for the entire's function
19221924
// scope.
19231925
let mut gen_guard = generation.lock().await;
1926+
trace!(" generation={gen_guard:?}");
19241927

19251928
let prev_generation =
19261929
self.inner.store.get_custom_value(Self::CURRENT_GENERATION_STORE_KEY).await?;

crates/matrix-sdk-crypto/src/olm/account.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,7 @@ impl Account {
12771277
// session in lieu of an OTK.)
12781278

12791279
warn!(
1280-
session_id = session.session_id(),
1280+
?session,
12811281
"Failed to decrypt a pre-key message with the corresponding session"
12821282
);
12831283

crates/matrix-sdk/src/encryption/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,8 @@ impl Encryption {
12151215
///
12161216
/// The provided `lock_value` must be a unique identifier for this process.
12171217
pub async fn enable_cross_process_store_lock(&self, lock_value: String) -> Result<(), Error> {
1218+
trace!("enable_cross_process_store_lock({lock_value})");
1219+
12181220
// If the lock has already been created, don't recreate it from scratch.
12191221
if let Some(prev_lock) = self.client.locks().cross_process_crypto_store_lock.get() {
12201222
let prev_holder = prev_lock.lock_holder();

testing/matrix-sdk-integration-testing/src/helpers.rs

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::{
22
collections::HashMap,
33
ops::Deref,
44
option_env,
5+
path::{Path, PathBuf},
56
sync::{Arc, Mutex as StdMutex},
67
time::Duration,
78
};
@@ -21,9 +22,14 @@ use tokio::sync::Mutex;
2122

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

25+
enum SqlitePath {
26+
Random,
27+
Path(PathBuf),
28+
}
29+
2430
pub struct TestClientBuilder {
2531
username: String,
26-
use_sqlite: bool,
32+
use_sqlite_dir: Option<SqlitePath>,
2733
encryption_settings: EncryptionSettings,
2834
http_proxy: Option<String>,
2935
}
@@ -32,7 +38,7 @@ impl TestClientBuilder {
3238
pub fn new(username: impl Into<String>) -> Self {
3339
Self {
3440
username: username.into(),
35-
use_sqlite: false,
41+
use_sqlite_dir: None,
3642
encryption_settings: Default::default(),
3743
http_proxy: None,
3844
}
@@ -45,7 +51,16 @@ impl TestClientBuilder {
4551
}
4652

4753
pub fn use_sqlite(mut self) -> Self {
48-
self.use_sqlite = true;
54+
self.use_sqlite_dir = Some(SqlitePath::Random);
55+
self
56+
}
57+
58+
/// Create or re-use a Sqlite store (with no passphrase) in the supplied
59+
/// directory. Note: this path must remain valid throughout the use of
60+
/// the constructed Client, so if you created a TempDir you must hang on
61+
/// to a reference to it throughout the test.
62+
pub fn use_sqlite_dir(mut self, path: &Path) -> Self {
63+
self.use_sqlite_dir = Some(SqlitePath::Path(path.to_owned()));
4964
self
5065
}
5166

@@ -59,6 +74,42 @@ impl TestClientBuilder {
5974
self
6075
}
6176

77+
pub async fn duplicate(self, other: &Client) -> Result<Client> {
78+
let homeserver_url =
79+
option_env!("HOMESERVER_URL").unwrap_or("http://localhost:8228").to_owned();
80+
let sliding_sync_proxy_url =
81+
option_env!("SLIDING_SYNC_PROXY_URL").unwrap_or("http://localhost:8338").to_owned();
82+
83+
let mut client_builder = Client::builder()
84+
.user_agent("matrix-sdk-integration-tests")
85+
.homeserver_url(homeserver_url)
86+
.sliding_sync_proxy(sliding_sync_proxy_url)
87+
.with_encryption_settings(self.encryption_settings)
88+
.request_config(RequestConfig::short_retry());
89+
90+
if let Some(proxy) = self.http_proxy {
91+
client_builder = client_builder.proxy(proxy);
92+
}
93+
94+
let client = match self.use_sqlite_dir {
95+
Some(SqlitePath::Path(path_buf)) => {
96+
client_builder.sqlite_store(&path_buf, None).build().await?
97+
}
98+
_ => {
99+
panic!("You must call use_sqlite_dir for a duplicate client!");
100+
}
101+
};
102+
103+
client
104+
.restore_session(
105+
other.session().expect("Session must be logged in before we can duplicate it"),
106+
)
107+
.await
108+
.expect("Failed to restore session");
109+
110+
Ok(client)
111+
}
112+
62113
pub async fn build(self) -> Result<Client> {
63114
let mut users = USERS.lock().await;
64115
if let Some((client, _)) = users.get(&self.username) {
@@ -83,10 +134,14 @@ impl TestClientBuilder {
83134
client_builder = client_builder.proxy(proxy);
84135
}
85136

86-
let client = if self.use_sqlite {
87-
client_builder.sqlite_store(tmp_dir.path(), None).build().await?
88-
} else {
89-
client_builder.build().await?
137+
let client = match self.use_sqlite_dir {
138+
None => client_builder.build().await?,
139+
Some(SqlitePath::Random) => {
140+
client_builder.sqlite_store(tmp_dir.path(), None).build().await?
141+
}
142+
Some(SqlitePath::Path(path_buf)) => {
143+
client_builder.sqlite_store(&path_buf, None).build().await?
144+
}
90145
};
91146

92147
// safe to assume we have not registered this user yet, but ignore if we did

0 commit comments

Comments
 (0)