Skip to content

Commit

Permalink
crypto: Test that the sqlite store empties its session cache when asked
Browse files Browse the repository at this point in the history
  • Loading branch information
andybalaam committed Apr 18, 2024
1 parent 558d133 commit 381c02d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion crates/matrix-sdk-crypto/src/store/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ macro_rules! cryptostore_integration_tests {
Account::with_device_id(alice_id(), alice_device_id())
}

async fn get_account_and_session() -> (Account, Session) {
pub(crate) async fn get_account_and_session() -> (Account, Session) {
let alice = Account::with_device_id(alice_id(), alice_device_id());
let mut bob = Account::with_device_id(bob_id(), bob_device_id());

Expand Down
33 changes: 32 additions & 1 deletion crates/matrix-sdk-sqlite/src/crypto_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1311,7 +1311,11 @@ mod tests {

#[cfg(test)]
mod encrypted_tests {
use matrix_sdk_crypto::{cryptostore_integration_tests, cryptostore_integration_tests_time};
use matrix_sdk_crypto::{
cryptostore_integration_tests, cryptostore_integration_tests_time,
store::{Changes, CryptoStore as _, PendingChanges},
};
use matrix_sdk_test::async_test;
use once_cell::sync::Lazy;
use tempfile::{tempdir, TempDir};

Expand All @@ -1328,6 +1332,33 @@ mod encrypted_tests {
.expect("Can't create a passphrase protected store")
}

#[async_test]
async fn cache_cleared() {
let store = get_store("cache_cleared", None).await;
// Given we created a session and saved it in the store
let (account, session) = cryptostore_integration_tests::get_account_and_session().await;
let sender_key = session.sender_key.to_base64();

store
.save_pending_changes(PendingChanges { account: Some(account.deep_clone()) })
.await
.expect("Can't save account");

let changes = Changes { sessions: vec![session.clone()], ..Default::default() };
store.save_changes(changes).await.unwrap();

store.session_cache.get(&sender_key).expect("We should have a session");

// When we clear the caches
store.clear_caches().await;

// Then the session is no longer in the cache
assert!(
store.session_cache.get(&sender_key).is_none(),
"Session should not be in the cache!"
);
}

cryptostore_integration_tests!();
cryptostore_integration_tests_time!();
}

0 comments on commit 381c02d

Please sign in to comment.