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::{ |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 7 | EncryptedSessionStore, EngineConfig, Event, PasswordLogin, 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(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 60 | let session = Session::restore(&sessions).await.unwrap().unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 61 | let (engine, handle, mut events) = session.build(config).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 62 | let running = tokio::spawn(engine.run()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 63 | support::receive_until(&mut events, |event| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 64 | matches!(event, Event::Authenticated { .. }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 65 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 66 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 67 | support::shut_down(&handle, &mut events, running).await; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 68 | drop(handle); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 69 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 70 | sessions.discard().await.unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 71 | assert!(!record_path.exists()); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 72 | assert!(!store_path.exists()); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 73 | assert!(Session::restore(&sessions).await.unwrap().is_none()); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 74 | std::fs::remove_dir_all(root).unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 75 | } |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 76 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 77 | #[tokio::test] |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 78 | async fn a_torn_or_failed_save_leaves_the_old_record() { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 79 | let root = temporary_path(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 80 | let record_path = root.join("session"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 81 | let sessions = || { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 82 | EncryptedSessionStore::file( |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 83 | &record_path, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 84 | StoreConfig::sqlite(root.join("store"), "test store passphrase"), |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 85 | ) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 86 | }; |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 87 | let temporary = root.join("session.tmp"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 88 | sessions() |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 89 | .save(&record("https://old.example")) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 90 | .await |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 91 | .unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 92 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 93 | std::fs::write(&temporary, b"half a rec").unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 94 | let loaded = sessions().load().await.unwrap().unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 95 | assert_eq!(loaded.homeserver(), "https://old.example"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 96 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 97 | std::fs::remove_file(&temporary).unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 98 | std::fs::create_dir(&temporary).unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 99 | assert!( |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 100 | sessions() |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 101 | .save(&record("https://new.example")) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 102 | .await |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 103 | .is_err() |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 104 | ); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 105 | let loaded = sessions().load().await.unwrap().unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 106 | assert_eq!(loaded.homeserver(), "https://old.example"); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 107 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 108 | std::fs::remove_dir_all(root).unwrap(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 109 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 110 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 111 | fn record(homeserver: &str) -> SessionRecord { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 112 | serde_json::from_value(serde_json::json!({ |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 113 | "homeserver": homeserver, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 114 | "authentication": { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 115 | "type": "matrix", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 116 | "session": { |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 117 | "user_id": "@example:example.org", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 118 | "device_id": "DEVICE", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 119 | "access_token": "secret", |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 120 | }, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 121 | }, |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 122 | })) |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 123 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 124 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 125 | |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 126 | fn temporary_path() -> PathBuf { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 127 | let nonce = SystemTime::now() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 128 | .duration_since(SystemTime::UNIX_EPOCH) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 129 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 130 | .as_nanos(); |
713205a95af2 | atagen | 2026-09-26 09:24 UTC | 131 | PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join(format!("macltok-account-{nonce}")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 132 | } |