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 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 4 | use std::{path::PathBuf, sync::Arc, time::SystemTime}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 5 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 6 | use macltok::{ |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 7 | AccountError, EncryptedSessionStore, EngineConfig, Event, PasswordLogin, Restore, Session, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 8 | SessionRecord, 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"), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 40 | Arc::new(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 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 59 | let sessions = Arc::new(sessions()); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 60 | let Some(Restore::Resumed(session)) = Session::restore(sessions.clone()).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()); |
f12377bce9b9 | atagen | 2026-09-26 12:32 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"), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 93 | Arc::new(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 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 99 | let Some(Restore::SoftLoggedOut(soft)) = Session::restore(Arc::new(sessions())).await.unwrap() |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 100 | else { |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 101 | panic!("a soft logged out record came back as something else"); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 102 | }; |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 103 | // the device matrix-sdk-test's canned login response hands out |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 104 | assert_eq!(soft.device_id, "GHTYAJCE"); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 105 | Session::login_password(soft.password_login("wordpass"), Arc::new(sessions())) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 106 | .await |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 107 | .unwrap(); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 108 | let requests = server.server().received_requests().await.unwrap(); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 109 | let relogin: serde_json::Value = requests |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 110 | .iter() |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 111 | .rfind(|request| request.url.path().ends_with("/login")) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 112 | .unwrap() |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 113 | .body_json() |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 114 | .unwrap(); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 115 | assert_eq!(relogin["device_id"], "GHTYAJCE"); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 116 | assert!(matches!( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 117 | Session::restore(Arc::new(sessions())).await.unwrap(), |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 118 | Some(Restore::Resumed(_)) |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 119 | )); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 120 | std::fs::remove_dir_all(root).unwrap(); |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 121 | } |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 122 | |
6f1d6591c10d | atagen | 2026-09-26 09:52 UTC | 123 | #[tokio::test] |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 124 | async fn a_login_whose_first_save_fails_can_still_be_saved() { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 125 | let server = MatrixMockServer::new().await; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 126 | server.mock_versions().ok().mount().await; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 127 | server.mock_login().ok().mount().await; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 128 | let root = temporary_path(); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 129 | let records = root.join("records"); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 130 | std::fs::create_dir_all(&records).unwrap(); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 131 | let sessions = || { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 132 | EncryptedSessionStore::file( |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 133 | records.join("session"), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 134 | StoreConfig::sqlite(root.join("store"), "test store passphrase"), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 135 | ) |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 136 | }; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 137 | let writable = std::fs::metadata(&records).unwrap().permissions(); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 138 | let mut read_only = writable.clone(); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 139 | read_only.set_readonly(true); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 140 | std::fs::set_permissions(&records, read_only).unwrap(); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 141 | |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 142 | let login = Session::login_password( |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 143 | PasswordLogin::new(server.uri(), "example", "wordpass"), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 144 | Arc::new(sessions()), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 145 | ) |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 146 | .await; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 147 | let Err(AccountError::Unsaved { session, .. }) = login else { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 148 | panic!("a login that couldn't save its record didn't hand the session back"); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 149 | }; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 150 | std::fs::set_permissions(&records, writable).unwrap(); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 151 | drop(session.save().await.unwrap()); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 152 | assert!(matches!( |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 153 | Session::restore(Arc::new(sessions())).await.unwrap(), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 154 | Some(Restore::Resumed(_)) |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 155 | )); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 156 | std::fs::remove_dir_all(root).unwrap(); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 157 | } |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 158 | |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 159 | #[tokio::test] |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 160 | async fn a_store_takes_one_session_at_a_time() { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 161 | let server = MatrixMockServer::new().await; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 162 | server.mock_versions().ok().mount().await; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 163 | server.mock_login().ok().mount().await; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 164 | let root = temporary_path(); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 165 | let sessions = || { |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 166 | EncryptedSessionStore::file( |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 167 | root.join("session"), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 168 | StoreConfig::sqlite(root.join("store"), "test store passphrase"), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 169 | ) |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 170 | }; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 171 | let (engine, handle, events) = Session::login_password( |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 172 | PasswordLogin::new(server.uri(), "example", "wordpass"), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 173 | Arc::new(sessions()), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 174 | ) |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 175 | .await |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 176 | .unwrap() |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 177 | .build(EngineConfig::default()) |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 178 | .await |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 179 | .unwrap(); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 180 | |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 181 | assert!(matches!( |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 182 | Session::restore(Arc::new(sessions())).await, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 183 | Err(AccountError::StoreInUse) |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 184 | )); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 185 | drop((engine, events)); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 186 | assert!(matches!( |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 187 | Session::restore(Arc::new(sessions())).await, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 188 | Err(AccountError::StoreInUse) |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 189 | )); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 190 | drop(handle); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 191 | let resumed = Session::restore(Arc::new(sessions())).await.unwrap(); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 192 | assert!(matches!(resumed, Some(Restore::Resumed(_)))); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 193 | drop(resumed); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 194 | sessions().discard().await.unwrap(); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 195 | assert!(!root.join("store").exists()); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 196 | std::fs::remove_dir_all(root).unwrap(); |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 197 | } |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 198 | |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 199 | #[tokio::test] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 200 | async fn a_torn_or_failed_save_leaves_the_old_record() { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 201 | let root = temporary_path(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 202 | let record_path = root.join("session"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 203 | let sessions = || { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 204 | EncryptedSessionStore::file( |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 205 | &record_path, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 206 | StoreConfig::sqlite(root.join("store"), "test store passphrase"), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 207 | ) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 208 | }; |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 209 | let temporary = root.join("session.1.0.tmp"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 210 | sessions() |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 211 | .save(&record("https://old.example")) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 212 | .await |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 213 | .unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 214 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 215 | std::fs::write(&temporary, b"half a rec").unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 216 | let loaded = sessions().load().await.unwrap().unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 217 | assert_eq!(loaded.homeserver(), "https://old.example"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 218 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 219 | let writable = std::fs::metadata(&root).unwrap().permissions(); |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 220 | let mut read_only = writable.clone(); |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 221 | read_only.set_readonly(true); |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 222 | std::fs::set_permissions(&root, read_only).unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 223 | assert!( |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 224 | sessions() |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 225 | .save(&record("https://new.example")) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 226 | .await |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 227 | .is_err() |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 228 | ); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 229 | let loaded = sessions().load().await.unwrap().unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 230 | assert_eq!(loaded.homeserver(), "https://old.example"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 231 | |
6db49c0ecf71 | atagen | 2026-09-26 12:41 UTC | 232 | std::fs::set_permissions(&root, writable).unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 233 | std::fs::remove_dir_all(root).unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 234 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 235 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 236 | #[tokio::test] |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 237 | async fn a_refresh_is_saved_before_anyone_hears_of_it() { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 238 | use matrix_sdk_test::test_json; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 239 | use wiremock::{ |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 240 | Mock, ResponseTemplate, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 241 | matchers::{header, method, path, path_regex}, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 242 | }; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 243 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 244 | let server = MatrixMockServer::new().await; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 245 | server.mock_versions().ok().mount().await; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 246 | server |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 247 | .mock_login() |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 248 | .respond_with( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 249 | ResponseTemplate::new(200).set_body_json(&*test_json::LOGIN_WITH_REFRESH_TOKEN), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 250 | ) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 251 | .mount() |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 252 | .await; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 253 | Mock::given(method("POST")) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 254 | .and(path_regex("sync")) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 255 | .and(header("authorization", "Bearer abc123")) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 256 | .respond_with(ResponseTemplate::new(401).set_body_json(serde_json::json!({ |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 257 | "errcode": "M_UNKNOWN_TOKEN", |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 258 | "error": "expired", |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 259 | "soft_logout": false, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 260 | }))) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 261 | .with_priority(1) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 262 | .mount(server.server()) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 263 | .await; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 264 | Mock::given(method("POST")) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 265 | .and(path("/_matrix/client/v3/refresh")) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 266 | .respond_with( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 267 | ResponseTemplate::new(200).set_body_json(&*test_json::REFRESH_TOKEN_WITH_REFRESH_TOKEN), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 268 | ) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 269 | .mount(server.server()) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 270 | .await; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 271 | server |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 272 | .mock_sliding_sync() |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 273 | .ok(v5::Response::new("1".to_owned())) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 274 | .mount() |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 275 | .await; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 276 | let root = temporary_path(); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 277 | let sessions = Arc::new(EncryptedSessionStore::file( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 278 | root.join("session"), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 279 | StoreConfig::sqlite(root.join("store"), "test store passphrase"), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 280 | )); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 281 | let session = Session::login_password( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 282 | PasswordLogin::new(server.uri(), "example", "wordpass"), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 283 | sessions.clone(), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 284 | ) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 285 | .await |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 286 | .unwrap(); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 287 | let (engine, handle, mut events) = session |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 288 | .build(EngineConfig::default().with_initial_history_limit(0)) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 289 | .await |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 290 | .unwrap(); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 291 | let running = tokio::spawn(engine.run()); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 292 | support::receive_until(&mut events, |event| { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 293 | matches!(event, Event::SessionTokensChanged) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 294 | }) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 295 | .await; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 296 | let saved = serde_json::to_value(sessions.load().await.unwrap().unwrap()).unwrap(); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 297 | assert_eq!(saved["authentication"]["session"]["refresh_token"], "wxyz"); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 298 | support::shut_down(&handle, &mut events, running).await; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 299 | drop(handle); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 300 | std::fs::remove_dir_all(root).unwrap(); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 301 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 302 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 303 | fn record(homeserver: &str) -> SessionRecord { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 304 | serde_json::from_value(serde_json::json!({ |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 305 | "homeserver": homeserver, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 306 | "authentication": { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 307 | "type": "matrix", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 308 | "session": { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 309 | "user_id": "@example:example.org", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 310 | "device_id": "DEVICE", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 311 | "access_token": "secret", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 312 | }, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 313 | }, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 314 | })) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 315 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 316 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 317 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 318 | fn temporary_path() -> PathBuf { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 319 | let nonce = SystemTime::now() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 320 | .duration_since(SystemTime::UNIX_EPOCH) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 321 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 322 | .as_nanos(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 323 | PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join(format!("macltok-account-{nonce}")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 324 | } |