From 381c02d21e75ae9b5b9bab30e543133cd8d533f5 Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Thu, 18 Apr 2024 13:47:36 +0100 Subject: [PATCH] crypto: Test that the sqlite store empties its session cache when asked --- .../src/store/integration_tests.rs | 2 +- crates/matrix-sdk-sqlite/src/crypto_store.rs | 33 ++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/crates/matrix-sdk-crypto/src/store/integration_tests.rs b/crates/matrix-sdk-crypto/src/store/integration_tests.rs index 025fc054d57..4331d0573bf 100644 --- a/crates/matrix-sdk-crypto/src/store/integration_tests.rs +++ b/crates/matrix-sdk-crypto/src/store/integration_tests.rs @@ -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()); diff --git a/crates/matrix-sdk-sqlite/src/crypto_store.rs b/crates/matrix-sdk-sqlite/src/crypto_store.rs index 3ebb26dbd29..27ee1c5a5b0 100644 --- a/crates/matrix-sdk-sqlite/src/crypto_store.rs +++ b/crates/matrix-sdk-sqlite/src/crypto_store.rs @@ -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}; @@ -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!(); }