96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1 | #![cfg(feature = "sqlite")] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 2 | mod support; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 4 | use std::{path::PathBuf, time::SystemTime}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 5 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 6 | use macltok::{ |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 7 | EncryptedSessionStore, EngineConfig, Event, PasswordLogin, Restore, Session, SessionRecord, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 8 | SessionStore, StoreConfig, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 9 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 10 | use matrix_sdk::{ruma::api::client::sync::sync_events::v5, test_utils::mocks::MatrixMockServer}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 11 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 12 | #[tokio::test] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 13 | async fn password_session_round_trips_and_discards_with_its_store() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 14 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 15 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 16 | .mock_versions() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 17 | .with_simplified_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 18 | .ok() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 19 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 20 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 21 | server.mock_login().ok().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 22 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 23 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 24 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 25 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 26 | .await; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 27 | let root = temporary_path(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 28 | let store_path = root.join("store"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 29 | let record_path = root.join("records").join("session"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 30 | let sessions = || { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 31 | EncryptedSessionStore::file( |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 32 | &record_path, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 33 | StoreConfig::sqlite(&store_path, "test store passphrase"), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 34 | ) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 35 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 36 | let config = EngineConfig::default().with_initial_history_limit(0); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 37 | let session = Session::login_password( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 38 | PasswordLogin::new(server.uri(), "example", "wordpass") |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 39 | .with_device_display_name("macltok test"), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 40 | &sessions(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 41 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 42 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 43 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 44 | let (engine, handle, mut events) = session.build(config.clone()).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 45 | let running = tokio::spawn(engine.run()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 46 | support::receive_until(&mut events, |event| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 47 | matches!(event, Event::Authenticated { .. }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 48 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 49 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 50 | support::shut_down(&handle, &mut events, running).await; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 51 | drop(handle); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 52 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 53 | let wrong = EncryptedSessionStore::file( |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 54 | &record_path, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 55 | StoreConfig::sqlite(&store_path, "someone else's passphrase"), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 56 | ); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 57 | assert!(wrong.load().await.is_err()); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 58 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 59 | let sessions = sessions(); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 60 | let Some(Restore::Resumed(session)) = Session::restore(&sessions).await.unwrap() else { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 61 | panic!("the saved login didn't resume"); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 62 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 63 | let (engine, handle, mut events) = session.build(config).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 64 | let running = tokio::spawn(engine.run()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 65 | support::receive_until(&mut events, |event| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 66 | matches!(event, Event::Authenticated { .. }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 67 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 68 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 69 | support::shut_down(&handle, &mut events, running).await; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 70 | drop(handle); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 71 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 72 | sessions.discard().await.unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 73 | assert!(!record_path.exists()); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 74 | assert!(!store_path.exists()); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 75 | assert!(Session::restore(&sessions).await.unwrap().is_none()); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 76 | std::fs::remove_dir_all(root).unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 77 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 78 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 79 | #[tokio::test] |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 80 | async fn a_restart_after_a_soft_logout_still_knows_the_device() { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 81 | let server = MatrixMockServer::new().await; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 82 | server.mock_versions().ok().mount().await; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 83 | server.mock_login().ok().mount().await; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 84 | let root = temporary_path(); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 85 | let sessions = || { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 86 | EncryptedSessionStore::file( |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 87 | root.join("session"), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 88 | StoreConfig::sqlite(root.join("store"), "test store passphrase"), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 89 | ) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 90 | }; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 91 | Session::login_password( |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 92 | PasswordLogin::new(server.uri(), "example", "wordpass"), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 93 | &sessions(), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 94 | ) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 95 | .await |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 96 | .unwrap(); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 97 | sessions().soft_log_out().await.unwrap(); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 98 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 99 | let Some(Restore::SoftLoggedOut(soft)) = Session::restore(&sessions()).await.unwrap() else { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 100 | panic!("a soft logged out record came back as something else"); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 101 | }; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 102 | // the device matrix-sdk-test's canned login response hands out |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 103 | assert_eq!(soft.device_id, "GHTYAJCE"); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 104 | Session::login_password(soft.password_login("wordpass"), &sessions()) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 105 | .await |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 106 | .unwrap(); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 107 | let requests = server.server().received_requests().await.unwrap(); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 108 | let relogin: serde_json::Value = requests |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 109 | .iter() |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 110 | .rfind(|request| request.url.path().ends_with("/login")) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 111 | .unwrap() |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 112 | .body_json() |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 113 | .unwrap(); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 114 | assert_eq!(relogin["device_id"], "GHTYAJCE"); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 115 | assert!(matches!( |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 116 | Session::restore(&sessions()).await.unwrap(), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 117 | Some(Restore::Resumed(_)) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 118 | )); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 119 | std::fs::remove_dir_all(root).unwrap(); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 120 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 121 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 122 | #[tokio::test] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 123 | async fn a_torn_or_failed_save_leaves_the_old_record() { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 124 | let root = temporary_path(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 125 | let record_path = root.join("session"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 126 | let sessions = || { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 127 | EncryptedSessionStore::file( |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 128 | &record_path, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 129 | StoreConfig::sqlite(root.join("store"), "test store passphrase"), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 130 | ) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 131 | }; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 132 | let temporary = root.join("session.tmp"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 133 | sessions() |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 134 | .save(&record("https://old.example")) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 135 | .await |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 136 | .unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 137 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 138 | std::fs::write(&temporary, b"half a rec").unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 139 | let loaded = sessions().load().await.unwrap().unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 140 | assert_eq!(loaded.homeserver(), "https://old.example"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 141 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 142 | std::fs::remove_file(&temporary).unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 143 | std::fs::create_dir(&temporary).unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 144 | assert!( |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 145 | sessions() |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 146 | .save(&record("https://new.example")) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 147 | .await |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 148 | .is_err() |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 149 | ); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 150 | let loaded = sessions().load().await.unwrap().unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 151 | assert_eq!(loaded.homeserver(), "https://old.example"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 152 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 153 | std::fs::remove_dir_all(root).unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 154 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 155 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 156 | fn record(homeserver: &str) -> SessionRecord { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 157 | serde_json::from_value(serde_json::json!({ |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 158 | "homeserver": homeserver, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 159 | "authentication": { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 160 | "type": "matrix", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 161 | "session": { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 162 | "user_id": "@example:example.org", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 163 | "device_id": "DEVICE", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 164 | "access_token": "secret", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 165 | }, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 166 | }, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 167 | })) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 168 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 169 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 170 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 171 | fn temporary_path() -> PathBuf { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 172 | let nonce = SystemTime::now() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 173 | .duration_since(SystemTime::UNIX_EPOCH) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 174 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 175 | .as_nanos(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 176 | PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join(format!("macltok-account-{nonce}")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 177 | } |