From 40d96dbf27c6b988a949be31238e2d57696bde62 Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Fri, 15 Mar 2024 11:44:51 +0000 Subject: [PATCH] crypto: Refactor integration tests in preparation for them running against MemoryStore A couple of small changes that allow us to drop locks that would otherwise persist when running the tests over the MemoryStore, and some documentation comments for assertions to make it easier to spot which assertion failed, even though we are inside a macro. --- .../src/store/integration_tests.rs | 81 ++++++++++++------- 1 file changed, 50 insertions(+), 31 deletions(-) diff --git a/crates/matrix-sdk-crypto/src/store/integration_tests.rs b/crates/matrix-sdk-crypto/src/store/integration_tests.rs index d0c1ab2d7..1ed29ecbd 100644 --- a/crates/matrix-sdk-crypto/src/store/integration_tests.rs +++ b/crates/matrix-sdk-crypto/src/store/integration_tests.rs @@ -187,26 +187,32 @@ macro_rules! cryptostore_integration_tests { #[async_test] async fn add_and_save_session() { let store_name = "add_and_save_session"; - let store = get_store(store_name, None).await; - let (account, session) = get_account_and_session().await; - let sender_key = session.sender_key.to_base64(); - let session_id = session.session_id().to_owned(); - store.save_pending_changes(PendingChanges { account: Some(account.deep_clone()), }).await.expect("Can't save account"); + // Given we created a session and saved it in the store + let (session_id, account, sender_key) = { + let store = get_store(store_name, None).await; + let (account, session) = get_account_and_session().await; + let sender_key = session.sender_key.to_base64(); + let session_id = session.session_id().to_owned(); - let changes = Changes { sessions: vec![session.clone()], ..Default::default() }; - store.save_changes(changes).await.unwrap(); + store.save_pending_changes(PendingChanges { account: Some(account.deep_clone()), }).await.expect("Can't save account"); - let sessions = store.get_sessions(&sender_key).await.unwrap().unwrap(); - let sessions_lock = sessions.lock().await; - let session = &sessions_lock[0]; + let changes = Changes { sessions: vec![session.clone()], ..Default::default() }; + store.save_changes(changes).await.unwrap(); - assert_eq!(session_id, session.session_id()); + let sessions = store.get_sessions(&sender_key).await.unwrap().unwrap(); + let sessions_lock = sessions.lock().await; + let session = &sessions_lock[0]; - drop(store); + assert_eq!(session_id, session.session_id()); + (session_id, account, sender_key) + }; + + // When we reload the store let store = get_store(store_name, None).await; + // Then the same account and session info was reloaded let loaded_account = store.load_account().await.unwrap().unwrap(); assert_eq!(account, loaded_account); @@ -220,36 +226,49 @@ macro_rules! cryptostore_integration_tests { #[async_test] async fn load_outbound_group_session() { let dir = "load_outbound_group_session"; - let (account, store) = get_loaded_store(dir.clone()).await; let room_id = room_id!("!test:localhost"); - assert!(store.get_outbound_group_session(&room_id).await.unwrap().is_none()); - let (session, _) = account.create_group_session_pair_with_defaults(&room_id).await; + // Given we saved an outbound group session + { + let (account, store) = get_loaded_store(dir.clone()).await; + assert!( + store.get_outbound_group_session(&room_id).await.unwrap().is_none(), + "Initially there should be no outbound group session" + ); - let user_id = user_id!("@example:localhost"); - let request = ToDeviceRequest::new( - user_id, - DeviceIdOrAllDevices::AllDevices, - "m.dummy", - Raw::from_json(to_raw_value(&DummyEventContent::new()).unwrap()), - ); + let (session, _) = account.create_group_session_pair_with_defaults(&room_id).await; - session.add_request(TransactionId::new(), request.into(), Default::default()); + let user_id = user_id!("@example:localhost"); + let request = ToDeviceRequest::new( + user_id, + DeviceIdOrAllDevices::AllDevices, + "m.dummy", + Raw::from_json(to_raw_value(&DummyEventContent::new()).unwrap()), + ); - let changes = Changes { - outbound_group_sessions: vec![session.clone()], - ..Default::default() - }; + session.add_request(TransactionId::new(), request.into(), Default::default()); - store.save_changes(changes).await.expect("Can't save group session"); + let changes = Changes { + outbound_group_sessions: vec![session.clone()], + ..Default::default() + }; - drop(store); + store.save_changes(changes).await.expect("Can't save group session"); + assert!( + store.get_outbound_group_session(&room_id).await.unwrap().is_some(), + "Sanity: after we've saved one, there should be an outbound_group_session" + ); + } + // When we reload the account let store = get_store(dir, None).await; - store.load_account().await.unwrap(); - assert!(store.get_outbound_group_session(&room_id).await.unwrap().is_some()); + // Then the saved session is restored + assert!( + store.get_outbound_group_session(&room_id).await.unwrap().is_some(), + "The outbound_group_session should have been loaded" + ); } #[async_test]