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 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 4 | use std::time::SystemTime; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 5 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 6 | use macltok::{EngineConfig, Event, PasswordLogin, Session, SessionRecord, StoreConfig}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 7 | use matrix_sdk::{ruma::api::client::sync::sync_events::v5, test_utils::mocks::MatrixMockServer}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 8 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 9 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 10 | async fn password_session_round_trips_through_an_encrypted_store() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 11 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 12 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 13 | .mock_versions() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 14 | .with_simplified_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 15 | .ok() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 16 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 17 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 18 | server.mock_login().ok().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 19 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 20 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 21 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 22 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 23 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 24 | let store_path = temporary_store_path(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 25 | std::fs::create_dir_all(&store_path).unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 26 | let config = EngineConfig::default().with_initial_history_limit(0); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 27 | let session = Session::login_password( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 28 | PasswordLogin::new(server.uri(), "example", "wordpass") |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 29 | .with_device_display_name("macltok test"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 30 | StoreConfig::new(&store_path, "test store passphrase"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 31 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 32 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 33 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 34 | let encoded = serde_json::to_vec(&session.record()).unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 35 | let record: SessionRecord = serde_json::from_slice(&encoded).unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 36 | assert_eq!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 37 | record.homeserver().trim_end_matches('/'), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 38 | server.uri().trim_end_matches('/') |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 39 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 40 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 41 | let (engine, handle, mut events) = session.build(config.clone()).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 42 | let running = tokio::spawn(engine.run()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 43 | support::receive_until(&mut events, |event| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 44 | matches!(event, Event::Authenticated { .. }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 45 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 46 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 47 | support::shut_down(&handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 48 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 49 | let session = Session::restore( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 50 | record, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 51 | StoreConfig::new(&store_path, "test store passphrase"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 52 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 53 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 54 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 55 | let (engine, handle, mut events) = session.build(config).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 56 | let running = tokio::spawn(engine.run()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 57 | support::receive_until(&mut events, |event| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 58 | matches!(event, Event::Authenticated { .. }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 59 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 60 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 61 | support::shut_down(&handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 62 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 63 | std::fs::remove_dir_all(store_path).unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 64 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 65 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 66 | fn temporary_store_path() -> std::path::PathBuf { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 67 | let nonce = SystemTime::now() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 68 | .duration_since(SystemTime::UNIX_EPOCH) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 69 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 70 | .as_nanos(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 71 | std::env::temp_dir().join(format!("macltok-account-{nonce}")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 72 | } |