1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

use crate::auth::TestClient;
use crate::testing::TestGroup;
use anyhow::Result;
use logins::{Login, PasswordEngine, Result as LoginResult};
// helpers...

// Doesn't check metadata fields
pub fn assert_logins_equiv(a: &Login, b: &Login) {
    assert_eq!(b.guid, a.guid, "id mismatch");
    assert_eq!(b.hostname, a.hostname, "hostname mismatch");
    assert_eq!(
        b.form_submit_url, a.form_submit_url,
        "form_submit_url mismatch"
    );
    assert_eq!(b.http_realm, a.http_realm, "http_realm mismatch");
    assert_eq!(b.username, a.username, "username mismatch");
    assert_eq!(b.password, a.password, "password mismatch");
    assert_eq!(
        b.username_field, a.username_field,
        "username_field mismatch"
    );
    assert_eq!(
        b.password_field, a.password_field,
        "password_field mismatch"
    );
}

pub fn times_used_for_id(e: &PasswordEngine, id: &str) -> i64 {
    e.get(id)
        .expect("get() failed")
        .expect("Login doesn't exist")
        .times_used
}

pub fn add_login(e: &PasswordEngine, l: Login) -> LoginResult<Login> {
    let id = e.add(l)?;
    Ok(e.get(&id)?.expect("Login we just added to exist"))
}

pub fn verify_login(e: &PasswordEngine, l: &Login) {
    let equivalent = e
        .get(&l.guid)
        .expect("get() to succeed")
        .expect("Expected login to be present");
    assert_logins_equiv(&equivalent, l);
}

pub fn verify_missing_login(e: &PasswordEngine, id: &str) {
    assert!(
        e.get(id).expect("get() to succeed").is_none(),
        "Login {} should not exist",
        id
    );
}

pub fn update_login<F: FnMut(&mut Login)>(
    e: &PasswordEngine,
    id: &str,
    mut callback: F,
) -> LoginResult<Login> {
    let mut login = e.get(id)?.expect("No such login!");
    callback(&mut login);
    e.update(login)?;
    Ok(e.get(id)?.expect("Just updated this"))
}

pub fn touch_login(e: &PasswordEngine, id: &str, times: usize) -> LoginResult<Login> {
    for _ in 0..times {
        e.touch(&id)?;
    }
    Ok(e.get(&id)?.unwrap())
}

pub fn sync_logins(client: &mut TestClient) -> Result<()> {
    let (init, key, _device_id) = client.data_for_sync()?;
    client.logins_engine.sync(&init, &key)?;
    Ok(())
}

// Actual tests.

fn test_login_general(c0: &mut TestClient, c1: &mut TestClient) {
    log::info!("Add some logins to client0");

    let l0id = "aaaaaaaaaaaa";
    let l1id = "bbbbbbbbbbbb";

    add_login(
        &c0.logins_engine,
        Login {
            guid: l0id.into(),
            hostname: "http://www.example.com".into(),
            form_submit_url: Some("http://login.example.com".into()),
            username: "cool_username".into(),
            password: "hunter2".into(),
            username_field: "uname".into(),
            password_field: "pword".into(),
            ..Login::default()
        },
    )
    .expect("add l0");

    let login0_c0 = touch_login(&c0.logins_engine, l0id, 2).expect("touch0 c0");
    assert_eq!(login0_c0.times_used, 3);

    let login1_c0 = add_login(
        &c0.logins_engine,
        Login {
            guid: l1id.into(),
            hostname: "http://www.example.com".into(),
            http_realm: Some("Login".into()),
            username: "cool_username".into(),
            password: "sekret".into(),
            ..Login::default()
        },
    )
    .expect("add l1");

    log::info!("Syncing client0");
    sync_logins(c0).expect("c0 sync to work");

    // Should be the same after syncing.
    verify_login(&c0.logins_engine, &login0_c0);
    verify_login(&c0.logins_engine, &login1_c0);

    log::info!("Syncing client1");
    sync_logins(c1).expect("c1 sync to work");

    log::info!("Check state");

    verify_login(&c1.logins_engine, &login0_c0);
    verify_login(&c1.logins_engine, &login1_c0);

    assert_eq!(
        times_used_for_id(&c1.logins_engine, l0id),
        3,
        "Times used is wrong (first sync)"
    );

    log::info!("Update logins");

    // Change login0 on both
    update_login(&c1.logins_engine, l0id, |l| {
        l.password = "testtesttest".into();
    })
    .unwrap();

    let login0_c0 = update_login(&c0.logins_engine, l0id, |l| {
        l.username_field = "users_name".into();
    })
    .unwrap();

    // and login1 on remote.
    let login1_c1 = update_login(&c1.logins_engine, l1id, |l| {
        l.username = "less_cool_username".into();
    })
    .unwrap();

    log::info!("Sync again");

    sync_logins(c1).expect("c1 sync 2");
    sync_logins(c0).expect("c0 sync 2");

    log::info!("Check state again");

    // Ensure the remotely changed password change made it through
    verify_login(&c0.logins_engine, &login1_c1);

    // And that the conflicting one did too.
    verify_login(
        &c0.logins_engine,
        &Login {
            username_field: "users_name".into(),
            password: "testtesttest".into(),
            ..login0_c0
        },
    );

    assert_eq!(
        c0.logins_engine.get(l0id).unwrap().unwrap().times_used,
        5, // initially 1, touched twice, updated twice (on two accounts!
        // doing this right requires 3WM)
        "Times used is wrong (final)"
    );
}

fn test_login_deletes(c0: &mut TestClient, c1: &mut TestClient) {
    log::info!("Add some logins to client0");

    let l0id = "aaaaaaaaaaaa";
    let l1id = "bbbbbbbbbbbb";
    let l2id = "cccccccccccc";
    let l3id = "dddddddddddd";

    let login0 = add_login(
        &c0.logins_engine,
        Login {
            guid: l0id.into(),
            hostname: "http://www.example.com".into(),
            form_submit_url: Some("http://login.example.com".into()),
            username: "cool_username".into(),
            password: "hunter2".into(),
            username_field: "uname".into(),
            password_field: "pword".into(),
            ..Login::default()
        },
    )
    .expect("add l0");

    let login1 = add_login(
        &c0.logins_engine,
        Login {
            guid: l1id.into(),
            hostname: "http://www.example.com".into(),
            http_realm: Some("Login".into()),
            username: "cool_username".into(),
            password: "sekret".into(),
            ..Login::default()
        },
    )
    .expect("add l1");

    let login2 = add_login(
        &c0.logins_engine,
        Login {
            guid: l2id.into(),
            hostname: "https://www.example.org".into(),
            http_realm: Some("Test".into()),
            username: "cool_username100".into(),
            password: "123454321".into(),
            ..Login::default()
        },
    )
    .expect("add l2");

    let login3 = add_login(
        &c0.logins_engine,
        Login {
            guid: l3id.into(),
            hostname: "https://www.example.net".into(),
            http_realm: Some("Http Realm".into()),
            username: "cool_username99".into(),
            password: "aaaaa".into(),
            ..Login::default()
        },
    )
    .expect("add l3");

    log::info!("Syncing client0");

    sync_logins(c0).expect("c0 sync to work");

    // Should be the same after syncing.
    verify_login(&c0.logins_engine, &login0);
    verify_login(&c0.logins_engine, &login1);
    verify_login(&c0.logins_engine, &login2);
    verify_login(&c0.logins_engine, &login3);

    log::info!("Syncing client1");
    sync_logins(c1).expect("c1 sync to work");

    log::info!("Check state");
    verify_login(&c1.logins_engine, &login0);
    verify_login(&c1.logins_engine, &login1);
    verify_login(&c1.logins_engine, &login2);
    verify_login(&c1.logins_engine, &login3);

    // The 4 logins are for the for possible scenarios. All of them should result in the record
    // being deleted.

    // 1. Client A deletes record, client B has no changes (should delete).
    // 2. Client A deletes record, client B has also deleted record (should delete).
    // 3. Client A deletes record, client B has modified record locally (should delete).
    // 4. Same as #3 but in reverse order.

    // case 1. (c1 deletes record, c0 should have deleted on the other side)
    log::info!("Deleting {} from c1", l0id);
    assert!(c1.logins_engine.delete(l0id).expect("Delete should work"));
    verify_missing_login(&c1.logins_engine, l0id);

    // case 2. Both delete l1 separately
    log::info!("Deleting {} from both", l1id);
    assert!(c0.logins_engine.delete(l1id).expect("Delete should work"));
    assert!(c1.logins_engine.delete(l1id).expect("Delete should work"));

    // case 3a. c0 modifies record (c1 will delete it after c0 syncs so the timestamps line up)
    log::info!("Updating {} on c0", l2id);
    let login2_new = update_login(&c0.logins_engine, l2id, |l| {
        l.username = "foobar".into();
    })
    .unwrap();

    // case 4a. c1 deletes record (c0 will modify it after c1 syncs so the timestamps line up)
    assert!(c1.logins_engine.delete(l3id).expect("Delete should work"));

    // Sync c1
    log::info!("Syncing c1");
    sync_logins(c1).expect("c1 sync to work");
    log::info!("Checking c1 state after sync");

    verify_missing_login(&c1.logins_engine, l0id);
    verify_missing_login(&c1.logins_engine, l1id);
    verify_login(&c1.logins_engine, &login2);
    verify_missing_login(&c1.logins_engine, l3id);

    log::info!("Update {} on c0", l3id);
    // 4b
    update_login(&c0.logins_engine, l3id, |l| {
        l.password = "quux".into();
    })
    .unwrap();

    // Sync c0
    log::info!("Syncing c0");
    sync_logins(c0).expect("c0 sync to work");

    log::info!("Checking c0 state after sync");

    verify_missing_login(&c0.logins_engine, l0id);
    verify_missing_login(&c0.logins_engine, l1id);
    verify_login(&c0.logins_engine, &login2_new);
    verify_missing_login(&c0.logins_engine, l3id);

    log::info!("Delete {} on c1", l2id);
    // 3b
    assert!(c1.logins_engine.delete(l2id).expect("Delete should work"));

    log::info!("Syncing c1");
    sync_logins(c1).expect("c1 sync to work");

    log::info!("{} should stay dead", l2id);
    // Ensure we didn't revive it.
    verify_missing_login(&c1.logins_engine, l2id);

    log::info!("Syncing c0");
    sync_logins(c0).expect("c0 sync to work");
    log::info!("Should delete {}", l2id);
    verify_missing_login(&c0.logins_engine, l2id);
}

pub fn get_test_group() -> TestGroup {
    TestGroup::new(
        "logins",
        vec![
            ("test_login_general", test_login_general),
            ("test_login_deletes", test_login_deletes),
        ],
    )
}