diff --git a/crates/matrix-sdk-crypto/src/store/integration_tests.rs b/crates/matrix-sdk-crypto/src/store/integration_tests.rs index 37b32f4dc..871e1a336 100644 --- a/crates/matrix-sdk-crypto/src/store/integration_tests.rs +++ b/crates/matrix-sdk-crypto/src/store/integration_tests.rs @@ -1,30 +1,29 @@ #[allow(unused_macros)] #[macro_export] macro_rules! cryptostore_integration_tests { - ($($name:ident)*) => { - $( - mod $name { - use super::get_store; - + () => { + mod cryptostore_integration_tests { use matrix_sdk_test::async_test; use ruma::{ - encryption::SignedKey, - serde::Base64, user_id, TransactionId, DeviceId, UserId, - room_id, device_id, + device_id, encryption::SignedKey, room_id, serde::Base64, user_id, DeviceId, + TransactionId, UserId, }; - use $crate::{ - SecretInfo, - testing::{get_device, get_other_identity, get_own_identity}, - types::events::room_key_request::MegolmV1AesSha2Content, - ReadOnlyDevice, olm::{ Curve25519PublicKey, InboundGroupSession, OlmMessageHash, PrivateCrossSigningIdentity, ReadOnlyAccount, Session, }, - store::{CryptoStore, GossipRequest, Changes, DeviceChanges, IdentityChanges, RecoveryKey}, + store::{ + Changes, CryptoStore, DeviceChanges, GossipRequest, IdentityChanges, + RecoveryKey, + }, + testing::{get_device, get_other_identity, get_own_identity}, + types::events::room_key_request::MegolmV1AesSha2Content, + ReadOnlyDevice, SecretInfo, }; + use super::get_store; + fn alice_id() -> &'static UserId { user_id!("@alice:example.org") } @@ -60,8 +59,14 @@ macro_rules! cryptostore_integration_tests { bob.generate_one_time_keys_helper(1).await; let one_time_key = *bob.one_time_keys().await.values().next().unwrap(); let sender_key = bob.identity_keys().curve25519; - let session = - alice.create_outbound_session_helper(Default::default(), sender_key, one_time_key, false).await; + let session = alice + .create_outbound_session_helper( + Default::default(), + sender_key, + one_time_key, + false, + ) + .await; (alice, session) } @@ -73,7 +78,10 @@ macro_rules! cryptostore_integration_tests { assert!(store.load_account().await.unwrap().is_none()); let account = get_account(); - store.save_changes(Changes { account: Some(account), ..Default::default() } ).await.expect("Can't save account"); + store + .save_changes(Changes { account: Some(account), ..Default::default() }) + .await + .expect("Can't save account"); assert!(store.get_account_info().is_some()); } @@ -103,7 +111,8 @@ macro_rules! cryptostore_integration_tests { #[async_test] async fn load_account_with_passphrase() { - let store = get_store("load_account_with_passphrase", Some("secret_passphrase")).await; + let store = + get_store("load_account_with_passphrase", Some("secret_passphrase")).await; let account = get_account(); store.save_account(account.clone()).await.expect("Can't save account"); @@ -143,8 +152,11 @@ macro_rules! cryptostore_integration_tests { store.save_changes(changes).await.unwrap(); - let sessions = - store.get_sessions(&session.sender_key.to_base64()).await.expect("Can't load sessions").unwrap(); + let sessions = store + .get_sessions(&session.sender_key.to_base64()) + .await + .expect("Can't load sessions") + .unwrap(); let loaded_session = sessions.lock().await.get(0).cloned().unwrap(); assert_eq!(&session, &loaded_session); @@ -190,11 +202,12 @@ macro_rules! cryptostore_integration_tests { let room_id = room_id!("!test:localhost"); assert!(store.get_outbound_group_sessions(&room_id).await.unwrap().is_none()); - let (session, _) = account.create_group_session_pair_with_defaults(&room_id) - .await; + let (session, _) = account.create_group_session_pair_with_defaults(&room_id).await; - let changes = - Changes { outbound_group_sessions: vec![session.clone()], ..Default::default() }; + let changes = Changes { + outbound_group_sessions: vec![session.clone()], + ..Default::default() + }; store.save_changes(changes).await.expect("Can't save group session"); @@ -204,11 +217,7 @@ macro_rules! cryptostore_integration_tests { store.load_account().await.unwrap(); - assert!(store - .get_outbound_group_sessions(&room_id) - .await - .unwrap() - .is_some()); + assert!(store.get_outbound_group_sessions(&room_id).await.unwrap().is_some()); } #[async_test] @@ -218,24 +227,31 @@ macro_rules! cryptostore_integration_tests { let room_id = &room_id!("!test:localhost"); let (_, session) = account.create_group_session_pair_with_defaults(room_id).await; - let changes = Changes { inbound_group_sessions: vec![session], ..Default::default() }; + let changes = + Changes { inbound_group_sessions: vec![session], ..Default::default() }; store.save_changes(changes).await.expect("Can't save group session"); } #[async_test] async fn save_inbound_group_session_for_backup() { - let (account, store) = get_loaded_store("save_inbound_group_session_for_backup").await; + let (account, store) = + get_loaded_store("save_inbound_group_session_for_backup").await; let room_id = &room_id!("!test:localhost"); let (_, session) = account.create_group_session_pair_with_defaults(room_id).await; - let changes = Changes { inbound_group_sessions: vec![session.clone()], ..Default::default() }; + let changes = + Changes { inbound_group_sessions: vec![session.clone()], ..Default::default() }; store.save_changes(changes).await.expect("Can't save group session"); let loaded_session = store - .get_inbound_group_session(&session.room_id, &session.sender_key.to_base64(), session.session_id()) + .get_inbound_group_session( + &session.room_id, + &session.sender_key.to_base64(), + session.session_id(), + ) .await .unwrap() .unwrap(); @@ -244,14 +260,14 @@ macro_rules! cryptostore_integration_tests { assert_eq!(store.inbound_group_session_counts().await.unwrap().total, 1); assert_eq!(store.inbound_group_session_counts().await.unwrap().backed_up, 0); - let to_back_up = store.inbound_group_sessions_for_backup(1).await.unwrap(); assert_eq!(to_back_up, vec![session]) } #[async_test] async fn reset_inbound_group_session_for_backup() { - let (account, store) = get_loaded_store("reset_inbound_group_session_for_backup").await; + let (account, store) = + get_loaded_store("reset_inbound_group_session_for_backup").await; assert_eq!(store.inbound_group_session_counts().await.unwrap().total, 0); let room_id = &room_id!("!test:localhost"); @@ -259,7 +275,8 @@ macro_rules! cryptostore_integration_tests { session.mark_as_backed_up(); - let changes = Changes { inbound_group_sessions: vec![session.clone()], ..Default::default() }; + let changes = + Changes { inbound_group_sessions: vec![session.clone()], ..Default::default() }; store.save_changes(changes).await.expect("Can't save group session"); @@ -275,7 +292,6 @@ macro_rules! cryptostore_integration_tests { assert_eq!(to_back_up, vec![session]); } - #[async_test] async fn load_inbound_group_session() { let dir = "load_inbound_group_session"; @@ -287,7 +303,9 @@ macro_rules! cryptostore_integration_tests { let mut export = session.export().await; - let curve_key = Curve25519PublicKey::from_base64("Nn0L2hkcCMFKqynTjyGsJbth7QrVmX3lbrksMkrGOAw").unwrap(); + let curve_key = + Curve25519PublicKey::from_base64("Nn0L2hkcCMFKqynTjyGsJbth7QrVmX3lbrksMkrGOAw") + .unwrap(); export.forwarding_curve25519_key_chain = vec![curve_key]; let session = InboundGroupSession::from_export(&export).unwrap(); @@ -304,7 +322,11 @@ macro_rules! cryptostore_integration_tests { store.load_account().await.unwrap(); let loaded_session = store - .get_inbound_group_session(&session.room_id, &session.sender_key.to_base64(), session.session_id()) + .get_inbound_group_session( + &session.room_id, + &session.sender_key.to_base64(), + session.session_id(), + ) .await .unwrap() .unwrap(); @@ -322,13 +344,28 @@ macro_rules! cryptostore_integration_tests { let (_account, store) = get_loaded_store(dir.clone()).await; let device = get_device(); - assert!(store.update_tracked_user(device.user_id(), false).await.unwrap(), "We were not tracked"); - assert!(!store.update_tracked_user(device.user_id(), false).await.unwrap(), "We were still tracked "); + assert!( + store.update_tracked_user(device.user_id(), false).await.unwrap(), + "We were not tracked" + ); + assert!( + !store.update_tracked_user(device.user_id(), false).await.unwrap(), + "We were still tracked" + ); assert!(store.is_user_tracked(device.user_id())); - assert!(!store.users_for_key_query().contains(device.user_id()), "Unexpectedly key found"); - assert!(!store.update_tracked_user(device.user_id(), true).await.unwrap(), "User was there?"); - assert!(store.users_for_key_query().contains(device.user_id()), "Didn't find the key despite tracking"); + assert!( + !store.users_for_key_query().contains(device.user_id()), + "Unexpectedly key found" + ); + assert!( + !store.update_tracked_user(device.user_id(), true).await.unwrap(), + "User was there?" + ); + assert!( + store.users_for_key_query().contains(device.user_id()), + "Didn't find the key despite tracking" + ); drop(store); let store = get_store(dir.clone(), None).await; @@ -336,17 +373,26 @@ macro_rules! cryptostore_integration_tests { store.load_account().await.unwrap(); assert!(store.is_user_tracked(device.user_id()), "Reopened didn't track"); - assert!(store.users_for_key_query().contains(device.user_id()), "Reopened doesn't have the key"); + assert!( + store.users_for_key_query().contains(device.user_id()), + "Reopened doesn't have the key" + ); store.update_tracked_user(device.user_id(), false).await.unwrap(); - assert!(!store.users_for_key_query().contains(device.user_id()), "Reopened has the key despite us not tracking"); + assert!( + !store.users_for_key_query().contains(device.user_id()), + "Reopened has the key despite us not tracking" + ); drop(store); let store = get_store(dir, None).await; store.load_account().await.unwrap(); - assert!(!store.users_for_key_query().contains(device.user_id()), "Reloaded store has the account"); + assert!( + !store.users_for_key_query().contains(device.user_id()), + "Reloaded store has the account" + ); } #[async_test] @@ -354,24 +400,23 @@ macro_rules! cryptostore_integration_tests { let dir = "device_saving"; let (_account, store) = get_loaded_store(dir.clone()).await; - let alice_device_1 = ReadOnlyDevice::from_account( - &ReadOnlyAccount::new( - "@alice:localhost".try_into().unwrap(), - "FIRSTDEVICE".into() - )).await; + let alice_device_1 = ReadOnlyDevice::from_account(&ReadOnlyAccount::new( + "@alice:localhost".try_into().unwrap(), + "FIRSTDEVICE".into(), + )) + .await; - let alice_device_2 = ReadOnlyDevice::from_account( - &ReadOnlyAccount::new( - "@alice:localhost".try_into().unwrap(), - "SECONDDEVICE".into() - )).await; + let alice_device_2 = ReadOnlyDevice::from_account(&ReadOnlyAccount::new( + "@alice:localhost".try_into().unwrap(), + "SECONDDEVICE".into(), + )) + .await; let changes = Changes { devices: DeviceChanges { - new: vec![ - alice_device_1.clone(), - alice_device_2.clone() - ], ..Default::default() }, + new: vec![alice_device_1.clone(), alice_device_2.clone()], + ..Default::default() + }, ..Default::default() }; @@ -383,10 +428,11 @@ macro_rules! cryptostore_integration_tests { store.load_account().await.unwrap(); - let loaded_device = store.get_device( - alice_device_1.user_id(), - alice_device_1.device_id() - ).await.unwrap().unwrap(); + let loaded_device = store + .get_device(alice_device_1.user_id(), alice_device_1.device_id()) + .await + .unwrap() + .unwrap(); assert_eq!(alice_device_1, loaded_device); @@ -425,7 +471,8 @@ macro_rules! cryptostore_integration_tests { store.load_account().await.unwrap(); - let loaded_device = store.get_device(device.user_id(), device.device_id()).await.unwrap(); + let loaded_device = + store.get_device(device.user_id(), device.device_id()).await.unwrap(); assert!(loaded_device.is_none()); } @@ -461,7 +508,8 @@ macro_rules! cryptostore_integration_tests { store.load_account().await.unwrap(); - let loaded_user = store.get_user_identity(own_identity.user_id()).await.unwrap().unwrap(); + let loaded_user = + store.get_user_identity(own_identity.user_id()).await.unwrap().unwrap(); assert_eq!(loaded_user.master_key(), own_identity.master_key()); assert_eq!(loaded_user.self_signing_key(), own_identity.self_signing_key()); @@ -479,7 +527,8 @@ macro_rules! cryptostore_integration_tests { store.save_changes(changes).await.unwrap(); - let loaded_user = store.get_user_identity(other_identity.user_id()).await.unwrap().unwrap(); + let loaded_user = + store.get_user_identity(other_identity.user_id()).await.unwrap().unwrap(); assert_eq!(loaded_user.master_key(), other_identity.master_key()); assert_eq!(loaded_user.self_signing_key(), other_identity.self_signing_key()); @@ -506,7 +555,8 @@ macro_rules! cryptostore_integration_tests { assert!(store.load_identity().await.unwrap().is_none()); let identity = PrivateCrossSigningIdentity::new(alice_id().to_owned()).await; - let changes = Changes { private_identity: Some(identity.clone()), ..Default::default() }; + let changes = + Changes { private_identity: Some(identity.clone()), ..Default::default() }; store.save_changes(changes).await.unwrap(); let loaded_identity = store.load_identity().await.unwrap().unwrap(); @@ -517,8 +567,10 @@ macro_rules! cryptostore_integration_tests { async fn olm_hash_saving() { let (_, store) = get_loaded_store("olm_hash_saving").await; - let hash = - OlmMessageHash { sender_key: "test_sender".to_owned(), hash: "test_hash".to_owned() }; + let hash = OlmMessageHash { + sender_key: "test_sender".to_owned(), + hash: "test_hash".to_owned(), + }; let mut changes = Changes::default(); changes.message_hashes.push(hash.clone()); @@ -531,7 +583,9 @@ macro_rules! cryptostore_integration_tests { #[async_test] async fn key_request_saving() { let (account, store) = get_loaded_store("key_request_saving").await; - let sender_key = Curve25519PublicKey::from_base64("Nn0L2hkcCMFKqynTjyGsJbth7QrVmX3lbrksMkrGOAw").unwrap(); + let sender_key = + Curve25519PublicKey::from_base64("Nn0L2hkcCMFKqynTjyGsJbth7QrVmX3lbrksMkrGOAw") + .unwrap(); let id = TransactionId::new(); let info: SecretInfo = MegolmV1AesSha2Content { @@ -621,6 +675,5 @@ macro_rules! cryptostore_integration_tests { ); } } - )* - } + }; } diff --git a/crates/matrix-sdk-indexeddb/src/crypto_store.rs b/crates/matrix-sdk-indexeddb/src/crypto_store.rs index ee69e6822..766a84c4c 100644 --- a/crates/matrix-sdk-indexeddb/src/crypto_store.rs +++ b/crates/matrix-sdk-indexeddb/src/crypto_store.rs @@ -1092,7 +1092,7 @@ mod tests { .expect("Can't create store without passphrase"), } } - cryptostore_integration_tests! { integration } + cryptostore_integration_tests!(); } #[cfg(all(test, target_arch = "wasm32"))] @@ -1112,5 +1112,5 @@ mod encrypted_tests { // FIXME: the tests pass, if run one by one, but run all together locally, // as well as CI fails... see matrix-org/matrix-rust-sdk#661 - // cryptostore_integration_tests! { integration } + // cryptostore_integration_tests!(); } diff --git a/crates/matrix-sdk-sled/src/crypto_store.rs b/crates/matrix-sdk-sled/src/crypto_store.rs index eaacb2e97..a51e793a5 100644 --- a/crates/matrix-sdk-sled/src/crypto_store.rs +++ b/crates/matrix-sdk-sled/src/crypto_store.rs @@ -1047,7 +1047,7 @@ mod tests { store } - cryptostore_integration_tests! { integration } + cryptostore_integration_tests!(); } #[cfg(test)] @@ -1070,5 +1070,5 @@ mod encrypted_tests { store } - cryptostore_integration_tests! { integration } + cryptostore_integration_tests!(); }