atagen

macltok

public
Star0
Watch0
96fdf90aacd6dc2b8bd3d63773234245b389d28c
3
ssh://brewery@pls.lobotomise.me:33/atagen/macltok.git
tests/account.rsRust92 lines2.9 KB
1
#![cfg(feature = "sqlite")]
2
use std::time::{Duration, SystemTime};
3
4
use macltok::{
5
    Command, EngineConfig, Event, LifecycleCommand, PasswordLogin, Session, SessionRecord,
6
    StoreConfig,
7
};
8
use matrix_sdk::{ruma::api::client::sync::sync_events::v5, test_utils::mocks::MatrixMockServer};
9
10
#[tokio::test]
11
async fn password_session_round_trips_through_an_encrypted_store() {
12
    let server = MatrixMockServer::new().await;
13
    server
14
        .mock_versions()
15
        .with_simplified_sliding_sync()
16
        .ok()
17
        .mount()
18
        .await;
19
    server.mock_login().ok().mount().await;
20
    server
21
        .mock_sliding_sync()
22
        .ok(v5::Response::new("1".to_owned()))
23
        .mount()
24
        .await;
25
    let store_path = temporary_store_path();
26
    std::fs::create_dir_all(&store_path).unwrap();
27
    let config = EngineConfig::default().with_initial_history_limit(0);
28
    let session = Session::login_password(
29
        PasswordLogin {
30
            homeserver: server.uri(),
31
            username: "example".into(),
32
            password: "wordpass".into(),
33
            device_display_name: Some("macltok test".into()),
34
        },
35
        StoreConfig::new(&store_path, "test store passphrase"),
36
    )
37
    .await
38
    .unwrap();
39
    let encoded = serde_json::to_vec(&session.record()).unwrap();
40
    let record: SessionRecord = serde_json::from_slice(&encoded).unwrap();
41
    assert_eq!(
42
        record.homeserver().trim_end_matches('/'),
43
        server.uri().trim_end_matches('/')
44
    );
45
46
    let (engine, handle, mut events) = session.build(config.clone()).await.unwrap();
47
    let running = tokio::spawn(engine.run());
48
    receive_authenticated(&mut events).await;
49
    handle
50
        .send(Command::Lifecycle(LifecycleCommand::Shutdown))
51
        .await
52
        .unwrap();
53
    running.await.unwrap().unwrap();
54
55
    let session = Session::restore(
56
        record,
57
        StoreConfig::new(&store_path, "test store passphrase"),
58
    )
59
    .await
60
    .unwrap();
61
    let (engine, handle, mut events) = session.build(config).await.unwrap();
62
    let running = tokio::spawn(engine.run());
63
    receive_authenticated(&mut events).await;
64
    handle
65
        .send(Command::Lifecycle(LifecycleCommand::Shutdown))
66
        .await
67
        .unwrap();
68
    running.await.unwrap().unwrap();
69
70
    std::fs::remove_dir_all(store_path).unwrap();
71
}
72
73
async fn receive_authenticated(events: &mut macltok::EventStream) {
74
    tokio::time::timeout(Duration::from_secs(2), async {
75
        while let Some(event) = events.recv().await {
76
            if matches!(event, Event::Authenticated { .. }) {
77
                return;
78
            }
79
        }
80
        panic!("event stream closed before authentication");
81
    })
82
    .await
83
    .expect("authentication event did not arrive");
84
}
85
86
fn temporary_store_path() -> std::path::PathBuf {
87
    let nonce = SystemTime::now()
88
        .duration_since(SystemTime::UNIX_EPOCH)
89
        .unwrap()
90
        .as_nanos();
91
    std::env::temp_dir().join(format!("macltok-account-{nonce}"))
92
}