-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathlogins.rs
More file actions
464 lines (388 loc) · 14.6 KB
/
Copy pathlogins.rs
File metadata and controls
464 lines (388 loc) · 14.6 KB
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/* 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::{
encryption::{create_key, ManagedEncryptorDecryptor, StaticKeyManager},
ApiResult as LoginResult, Login, LoginEntry, LoginStore,
};
use std::sync::Arc;
use std::{collections::hash_map::RandomState, collections::HashMap};
// 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.origin, a.origin);
assert_eq!(b.form_action_origin, a.form_action_origin);
assert_eq!(b.http_realm, a.http_realm);
assert_eq!(b.username_field, a.username_field);
assert_eq!(b.password_field, a.password_field);
assert_eq!(b.username, a.username);
assert_eq!(b.password, a.password);
}
pub fn times_used_for_id(s: &LoginStore, id: &str) -> i64 {
s.get(id)
.expect("get() failed")
.expect("Login doesn't exist")
.times_used
}
pub fn add_login(s: &LoginStore, l: LoginEntry) -> LoginResult<Login> {
let login = s.add(l)?;
let fetched = s.get(&login.guid())?.expect("Login we just added to exist");
Ok(fetched)
}
pub fn verify_login(s: &LoginStore, l: &Login) {
let equivalent = s
.get(&l.guid())
.expect("get() to succeed")
.expect("Expected login to be present");
assert_logins_equiv(&equivalent, l);
}
pub fn verify_missing_login(s: &LoginStore, id: &str) {
assert!(
s.get(id).expect("get() to succeed").is_none(),
"Login {} should not exist",
id
);
}
pub fn update_login<F: FnMut(&mut Login)>(
s: &LoginStore,
id: &str,
mut callback: F,
) -> LoginResult<Login> {
let mut login = s.get(id)?.expect("No such login!");
callback(&mut login);
let to_update = login.entry();
s.update(id, to_update)?;
Ok(s.get(id)?.expect("Just updated this"))
}
pub fn touch_login(s: &LoginStore, id: &str, times: usize) -> LoginResult<Login> {
for _ in 0..times {
s.touch(id)?;
}
Ok(s.get(id)?.unwrap())
}
pub fn sync_logins(client: &mut TestClient) -> Result<()> {
let local_encryption_keys = HashMap::new();
client.sync(&["passwords".to_string()], local_encryption_keys)
}
pub fn sync_logins_with_failure(
client: &mut TestClient,
) -> Result<HashMap<String, String, RandomState>> {
let local_encryption_keys = HashMap::new();
client.sync_with_failure(&["passwords".to_string()], local_encryption_keys)
}
// Actual tests.
fn test_login_general(c0: &mut TestClient, c1: &mut TestClient) {
log::info!("Add some logins to client0");
let l0id = add_login(
&c0.logins_store,
LoginEntry {
origin: "http://www.example.com".into(),
form_action_origin: Some("http://login.example.com".into()),
username_field: "uname".into(),
password_field: "pword".into(),
username: "cool_username".into(),
password: "hunter2".into(),
..Default::default()
},
)
.expect("add l0")
.guid();
let login0_c0 = touch_login(&c0.logins_store, &l0id, 2).expect("touch0 c0");
assert_eq!(login0_c0.times_used, 3);
let login1_c0 = add_login(
&c0.logins_store,
LoginEntry {
origin: "http://www.example.com".into(),
http_realm: Some("Login".into()),
username: "cool_username".into(),
password: "sekret".into(),
..Default::default()
},
)
.expect("add l1");
let l1id = login1_c0.guid();
log::info!("Syncing client0");
sync_logins(c0).expect("c0 sync to work");
// Should be the same after syncing.
verify_login(&c0.logins_store, &login0_c0);
verify_login(&c0.logins_store, &login1_c0);
log::info!("Syncing client1");
sync_logins(c1).expect("c1 sync to work");
log::info!("Check state");
verify_login(&c1.logins_store, &login0_c0);
verify_login(&c1.logins_store, &login1_c0);
assert_eq!(
times_used_for_id(&c1.logins_store, &l0id),
3,
"Times used is wrong (first sync)"
);
log::info!("Update logins");
// Change login0 on both
update_login(&c1.logins_store, &l0id, |l| {
l.password = "testtesttest".into();
})
.unwrap();
let login0_c0 = update_login(&c0.logins_store, &l0id, |l| {
l.username_field = "users_name".into();
})
.unwrap();
// and login1 on remote.
let login1_c1 = update_login(&c1.logins_store, &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_store, &login1_c1);
// And that the conflicting one did too.
verify_login(
&c0.logins_store,
&Login {
username_field: "users_name".into(),
password: "testtesttest".into(),
..login0_c0
},
);
assert_eq!(
c0.logins_store.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 login0 = add_login(
&c0.logins_store,
LoginEntry {
origin: "http://www.example.com".into(),
form_action_origin: Some("http://login.example.com".into()),
username_field: "uname".into(),
password_field: "pword".into(),
username: "cool_username".into(),
password: "hunter2".into(),
..Default::default()
},
)
.expect("add l0");
let l0id = login0.guid();
let login1 = add_login(
&c0.logins_store,
LoginEntry {
origin: "http://www.example.com".into(),
http_realm: Some("Login".into()),
username: "cool_username".into(),
password: "sekret".into(),
..Default::default()
},
)
.expect("add l1");
let l1id = login1.guid();
let login2 = add_login(
&c0.logins_store,
LoginEntry {
origin: "https://www.example.org".into(),
http_realm: Some("Test".into()),
username: "cool_username100".into(),
password: "123454321".into(),
..Default::default()
},
)
.expect("add l2");
let l2id = login2.guid();
let login3 = add_login(
&c0.logins_store,
LoginEntry {
origin: "https://www.example.net".into(),
http_realm: Some("Http Realm".into()),
username: "cool_username99".into(),
password: "aaaaa".into(),
..Default::default()
},
)
.expect("add l3");
let l3id = login3.guid();
log::info!("Syncing client0");
sync_logins(c0).expect("c0 sync to work");
// Should be the same after syncing.
verify_login(&c0.logins_store, &login0);
verify_login(&c0.logins_store, &login1);
verify_login(&c0.logins_store, &login2);
verify_login(&c0.logins_store, &login3);
log::info!("Syncing client1");
sync_logins(c1).expect("c1 sync to work");
log::info!("Check state");
verify_login(&c1.logins_store, &login0);
verify_login(&c1.logins_store, &login1);
verify_login(&c1.logins_store, &login2);
verify_login(&c1.logins_store, &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_store.delete(&l0id).expect("Delete should work"));
verify_missing_login(&c1.logins_store, &l0id);
// case 2. Both delete l1 separately
log::info!("Deleting {} from both", l1id);
assert!(c0.logins_store.delete(&l1id).expect("Delete should work"));
assert!(c1.logins_store.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_store, &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_store.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_store, &l0id);
verify_missing_login(&c1.logins_store, &l1id);
verify_login(&c1.logins_store, &login2);
verify_missing_login(&c1.logins_store, &l3id);
log::info!("Update {} on c0", l3id);
// 4b
update_login(&c0.logins_store, &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_store, &l0id);
verify_missing_login(&c0.logins_store, &l1id);
verify_login(&c0.logins_store, &login2_new);
verify_missing_login(&c0.logins_store, &l3id);
log::info!("Delete {} on c1", l2id);
// 3b
assert!(c1.logins_store.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_store, &l2id);
log::info!("Syncing c0");
sync_logins(c0).expect("c0 sync to work");
log::info!("Should delete {}", l2id);
verify_missing_login(&c0.logins_store, &l2id);
}
fn test_delete_undecryptable_records_for_remote_replacement(
c0: &mut TestClient,
c1: &mut TestClient,
) {
log::info!("Add a login to client0");
// Add a login
let login0 = add_login(
&c0.logins_store,
LoginEntry {
origin: "http://www.example2.com".into(),
form_action_origin: Some("http://login.example2.com".into()),
username_field: "uname".into(),
password_field: "pword".into(),
username: "cool_username".into(),
password: "hunter2".into(),
..Default::default()
},
)
.expect("add login0");
// Sync the first device where the login was added
log::info!("Syncing client0 -- inital sync");
sync_logins(c0).expect("c0 sync to work");
// Sync the second device
log::info!("Syncing client1 -- inital sync");
sync_logins(c1).expect("c0 sync to work");
// Verify that the login exists on both devices
verify_login(&c0.logins_store, &login0);
verify_login(&c1.logins_store, &login0);
// Add a login with a different EncryptorDecryptor to replicate having a stored login that cannot be decrypted
// with the EncryptorDecryptor property of the store
let key = create_key().unwrap();
let new_encdec = Arc::new(ManagedEncryptorDecryptor::new(Arc::new(
StaticKeyManager::new(key.clone()),
)));
log::info!("Add another login to client0");
// Store the login using the new/wrong encdec
//
// Note: this code has not actually been tested since the sync-tests were already failing when
// BDK made changes in #7373
let mut db = c0.logins_store.lock_db().expect("db lock retrieved");
let old_encdec = std::mem::replace(&mut db.encdec, new_encdec.clone());
let login1 = db
.add(LoginEntry {
origin: "http://www.example3.com".into(),
form_action_origin: Some("http://login.example3.com".into()),
username_field: "uname".into(),
password_field: "pword".into(),
username: "cool_username".into(),
password: "hunter2".into(),
..Default::default()
})
.expect("add login1");
db.encdec = old_encdec;
drop(db);
let l1id = login1.guid();
// Check that the corrupted login exists on first device
// The db retrieval function is being used instead of the store function so that we
// can provided our own EncryptorDecryptor.
let retrieved_login = c0
.logins_store
.lock_db()
.expect("db lock retrieved")
.get_by_id(&l1id)
.expect("get_by_id returns successfully")
.expect("login to be retrieved")
.decrypt(&*new_encdec)
.expect("decryption to succeed");
assert_eq!(retrieved_login.guid(), l1id);
// Check that syncing after adding a corrupted login fails with a decryption error
let failures = sync_logins_with_failure(c0).expect("sync to complete with failures");
let login_failures = failures.get("passwords");
assert!(login_failures.is_some());
assert!(login_failures.unwrap().contains("decryption failed"));
// Execute the verification logic to remove the corrupted login
log::info!("Verify logins");
c0.logins_store
.clone()
.delete_undecryptable_records_for_remote_replacement()
.expect("stored logins to be verified");
// Verify that the corrupted login has been removed
verify_missing_login(&c0.logins_store, &l1id);
// Sync the first device after verification
log::info!("Syncing client0 -- after verification");
sync_logins(c0).expect("c0 sync to work");
// Sync the second device after verification
log::info!("Syncing client1 -- after verification");
sync_logins(c1).expect("c0 sync to work");
// Verify that the first login record still exists on both devices
verify_login(&c0.logins_store, &login0);
verify_login(&c1.logins_store, &login0);
// Clear the stores
_ = c0.logins_store.wipe_local();
_ = c1.logins_store.wipe_local();
}
pub fn get_test_group() -> TestGroup {
TestGroup::new(
"logins",
vec![
("test_login_general", test_login_general),
("test_login_deletes", test_login_deletes),
(
"test_delete_undecryptable_records_for_remote_replacement",
test_delete_undecryptable_records_for_remote_replacement,
),
],
)
}