summaryrefslogtreecommitdiffstats
path: root/services/fxaccounts/tests/xpcshell/test_storage_manager.js
blob: 702cd34f3cd29dd3fbb5f299841f91dcd2a7f083 (plain)
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Tests for the FxA storage manager.

const { FxAccountsStorageManager } = ChromeUtils.importESModule(
  "resource://gre/modules/FxAccountsStorage.sys.mjs"
);
const { DATA_FORMAT_VERSION, log } = ChromeUtils.import(
  "resource://gre/modules/FxAccountsCommon.js"
);

initTestLogging("Trace");
log.level = Log.Level.Trace;

const DEVICE_REGISTRATION_VERSION = 42;

// A couple of mocks we can use.
function MockedPlainStorage(accountData) {
  let data = null;
  if (accountData) {
    data = {
      version: DATA_FORMAT_VERSION,
      accountData,
    };
  }
  this.data = data;
  this.numReads = 0;
}
MockedPlainStorage.prototype = {
  async get() {
    this.numReads++;
    Assert.equal(this.numReads, 1, "should only ever be 1 read of acct data");
    return this.data;
  },

  async set(data) {
    this.data = data;
  },
};

function MockedSecureStorage(accountData) {
  let data = null;
  if (accountData) {
    data = {
      version: DATA_FORMAT_VERSION,
      accountData,
    };
  }
  this.data = data;
  this.numReads = 0;
}

MockedSecureStorage.prototype = {
  fetchCount: 0,
  locked: false,
  /* eslint-disable object-shorthand */
  // This constructor must be declared without
  // object shorthand or we get an exception of
  // "TypeError: this.STORAGE_LOCKED is not a constructor"
  STORAGE_LOCKED: function () {},
  /* eslint-enable object-shorthand */
  async get(uid, email) {
    this.fetchCount++;
    if (this.locked) {
      throw new this.STORAGE_LOCKED();
    }
    this.numReads++;
    Assert.equal(
      this.numReads,
      1,
      "should only ever be 1 read of unlocked data"
    );
    return this.data;
  },

  async set(uid, contents) {
    this.data = contents;
  },
};

function add_storage_task(testFunction) {
  add_task(async function () {
    print("Starting test with secure storage manager");
    await testFunction(new FxAccountsStorageManager());
  });
  add_task(async function () {
    print("Starting test with simple storage manager");
    await testFunction(new FxAccountsStorageManager({ useSecure: false }));
  });
}

// initialized without account data and there's nothing to read. Not logged in.
add_storage_task(async function checkInitializedEmpty(sm) {
  if (sm.secureStorage) {
    sm.secureStorage = new MockedSecureStorage(null);
  }
  await sm.initialize();
  Assert.strictEqual(await sm.getAccountData(), null);
  await Assert.rejects(
    sm.updateAccountData({ kXCS: "kXCS" }),
    /No user is logged in/
  );
});

// Initialized with account data (ie, simulating a new user being logged in).
// Should reflect the initial data and be written to storage.
add_storage_task(async function checkNewUser(sm) {
  let initialAccountData = {
    uid: "uid",
    email: "someone@somewhere.com",
    kXCS: "kXCS",
    device: {
      id: "device id",
    },
  };
  sm.plainStorage = new MockedPlainStorage();
  if (sm.secureStorage) {
    sm.secureStorage = new MockedSecureStorage(null);
  }
  await sm.initialize(initialAccountData);
  let accountData = await sm.getAccountData();
  Assert.equal(accountData.uid, initialAccountData.uid);
  Assert.equal(accountData.email, initialAccountData.email);
  Assert.equal(accountData.kXCS, initialAccountData.kXCS);
  Assert.deepEqual(accountData.device, initialAccountData.device);

  // and it should have been written to storage.
  Assert.equal(sm.plainStorage.data.accountData.uid, initialAccountData.uid);
  Assert.equal(
    sm.plainStorage.data.accountData.email,
    initialAccountData.email
  );
  Assert.deepEqual(
    sm.plainStorage.data.accountData.device,
    initialAccountData.device
  );
  // check secure
  if (sm.secureStorage) {
    Assert.equal(
      sm.secureStorage.data.accountData.kXCS,
      initialAccountData.kXCS
    );
  } else {
    Assert.equal(
      sm.plainStorage.data.accountData.kXCS,
      initialAccountData.kXCS
    );
  }
});

// Initialized without account data but storage has it available.
add_storage_task(async function checkEverythingRead(sm) {
  sm.plainStorage = new MockedPlainStorage({
    uid: "uid",
    email: "someone@somewhere.com",
    device: {
      id: "wibble",
      registrationVersion: null,
    },
  });
  if (sm.secureStorage) {
    sm.secureStorage = new MockedSecureStorage(null);
  }
  await sm.initialize();
  let accountData = await sm.getAccountData();
  Assert.ok(accountData, "read account data");
  Assert.equal(accountData.uid, "uid");
  Assert.equal(accountData.email, "someone@somewhere.com");
  Assert.deepEqual(accountData.device, {
    id: "wibble",
    registrationVersion: null,
  });
  // Update the data - we should be able to fetch it back and it should appear
  // in our storage.
  await sm.updateAccountData({
    verified: true,
    kSync: "kSync",
    kXCS: "kXCS",
    kExtSync: "kExtSync",
    kExtKbHash: "kExtKbHash",
    device: {
      id: "wibble",
      registrationVersion: DEVICE_REGISTRATION_VERSION,
    },
  });
  accountData = await sm.getAccountData();
  Assert.equal(accountData.kSync, "kSync");
  Assert.equal(accountData.kXCS, "kXCS");
  Assert.equal(accountData.kExtSync, "kExtSync");
  Assert.equal(accountData.kExtKbHash, "kExtKbHash");
  Assert.deepEqual(accountData.device, {
    id: "wibble",
    registrationVersion: DEVICE_REGISTRATION_VERSION,
  });
  // Check the new value was written to storage.
  await sm._promiseStorageComplete; // storage is written in the background.
  Assert.equal(sm.plainStorage.data.accountData.verified, true);
  Assert.deepEqual(sm.plainStorage.data.accountData.device, {
    id: "wibble",
    registrationVersion: DEVICE_REGISTRATION_VERSION,
  });
  // derive keys are secure
  if (sm.secureStorage) {
    Assert.equal(sm.secureStorage.data.accountData.kExtKbHash, "kExtKbHash");
    Assert.equal(sm.secureStorage.data.accountData.kExtSync, "kExtSync");
    Assert.equal(sm.secureStorage.data.accountData.kXCS, "kXCS");
    Assert.equal(sm.secureStorage.data.accountData.kSync, "kSync");
  } else {
    Assert.equal(sm.plainStorage.data.accountData.kExtKbHash, "kExtKbHash");
    Assert.equal(sm.plainStorage.data.accountData.kExtSync, "kExtSync");
    Assert.equal(sm.plainStorage.data.accountData.kXCS, "kXCS");
    Assert.equal(sm.plainStorage.data.accountData.kSync, "kSync");
  }
});

add_storage_task(async function checkInvalidUpdates(sm) {
  sm.plainStorage = new MockedPlainStorage({
    uid: "uid",
    email: "someone@somewhere.com",
  });
  if (sm.secureStorage) {
    sm.secureStorage = new MockedSecureStorage(null);
  }
  await sm.initialize();

  await Assert.rejects(
    sm.updateAccountData({ uid: "another" }),
    /Can't change uid/
  );
});

add_storage_task(async function checkNullUpdatesRemovedUnlocked(sm) {
  if (sm.secureStorage) {
    sm.plainStorage = new MockedPlainStorage({
      uid: "uid",
      email: "someone@somewhere.com",
    });
    sm.secureStorage = new MockedSecureStorage({
      kSync: "kSync",
      kXCS: "kXCS",
      kExtSync: "kExtSync",
      kExtKbHash: "kExtKbHash",
    });
  } else {
    sm.plainStorage = new MockedPlainStorage({
      uid: "uid",
      email: "someone@somewhere.com",
      kSync: "kSync",
      kXCS: "kXCS",
      kExtSync: "kExtSync",
      kExtKbHash: "kExtKbHash",
    });
  }
  await sm.initialize();

  await sm.updateAccountData({ kXCS: null });
  let accountData = await sm.getAccountData();
  Assert.ok(!accountData.kXCS);
  Assert.equal(accountData.kSync, "kSync");
});

add_storage_task(async function checkNullRemovesUnlistedFields(sm) {
  // kA and kB are not listed in FXA_PWDMGR_*_FIELDS, but we still want to
  // be able to delete them (migration case).
  if (sm.secureStorage) {
    sm.plainStorage = new MockedPlainStorage({
      uid: "uid",
      email: "someone@somewhere.com",
    });
    sm.secureStorage = new MockedSecureStorage({ kA: "kA", kb: "kB" });
  } else {
    sm.plainStorage = new MockedPlainStorage({
      uid: "uid",
      email: "someone@somewhere.com",
      kA: "kA",
      kb: "kB",
    });
  }
  await sm.initialize();

  await sm.updateAccountData({ kA: null, kB: null });
  let accountData = await sm.getAccountData();
  Assert.ok(!accountData.kA);
  Assert.ok(!accountData.kB);
});

add_storage_task(async function checkDelete(sm) {
  if (sm.secureStorage) {
    sm.plainStorage = new MockedPlainStorage({
      uid: "uid",
      email: "someone@somewhere.com",
    });
    sm.secureStorage = new MockedSecureStorage({
      kSync: "kSync",
      kXCS: "kXCS",
      kExtSync: "kExtSync",
      kExtKbHash: "kExtKbHash",
    });
  } else {
    sm.plainStorage = new MockedPlainStorage({
      uid: "uid",
      email: "someone@somewhere.com",
      kSync: "kSync",
      kXCS: "kXCS",
      kExtSync: "kExtSync",
      kExtKbHash: "kExtKbHash",
    });
  }
  await sm.initialize();

  await sm.deleteAccountData();
  // Storage should have been reset to null.
  Assert.equal(sm.plainStorage.data, null);
  if (sm.secureStorage) {
    Assert.equal(sm.secureStorage.data, null);
  }
  // And everything should reflect no user.
  Assert.equal(await sm.getAccountData(), null);
});

// Some tests only for the secure storage manager.
add_task(async function checkNullUpdatesRemovedLocked() {
  let sm = new FxAccountsStorageManager();
  sm.plainStorage = new MockedPlainStorage({
    uid: "uid",
    email: "someone@somewhere.com",
  });
  sm.secureStorage = new MockedSecureStorage({
    kSync: "kSync",
    kXCS: "kXCS",
    kExtSync: "kExtSync",
    kExtKbHash: "kExtKbHash",
  });
  sm.secureStorage.locked = true;
  await sm.initialize();

  await sm.updateAccountData({ kSync: null });
  let accountData = await sm.getAccountData();
  Assert.ok(!accountData.kSync);
  // still no kXCS as we are locked.
  Assert.ok(!accountData.kXCS);

  // now unlock - should still be no kSync but kXCS should appear.
  sm.secureStorage.locked = false;
  accountData = await sm.getAccountData();
  Assert.ok(!accountData.kSync);
  Assert.equal(accountData.kXCS, "kXCS");
  // And secure storage should have been written with our previously-cached
  // data.
  Assert.strictEqual(sm.secureStorage.data.accountData.kSync, undefined);
  Assert.strictEqual(sm.secureStorage.data.accountData.kXCS, "kXCS");
});

add_task(async function checkEverythingReadSecure() {
  let sm = new FxAccountsStorageManager();
  sm.plainStorage = new MockedPlainStorage({
    uid: "uid",
    email: "someone@somewhere.com",
  });
  sm.secureStorage = new MockedSecureStorage({ kXCS: "kXCS" });
  await sm.initialize();

  let accountData = await sm.getAccountData();
  Assert.ok(accountData, "read account data");
  Assert.equal(accountData.uid, "uid");
  Assert.equal(accountData.email, "someone@somewhere.com");
  Assert.equal(accountData.kXCS, "kXCS");
});

add_task(async function checkExplicitGet() {
  let sm = new FxAccountsStorageManager();
  sm.plainStorage = new MockedPlainStorage({
    uid: "uid",
    email: "someone@somewhere.com",
  });
  sm.secureStorage = new MockedSecureStorage({ kXCS: "kXCS" });
  await sm.initialize();

  let accountData = await sm.getAccountData(["uid", "kXCS"]);
  Assert.ok(accountData, "read account data");
  Assert.equal(accountData.uid, "uid");
  Assert.equal(accountData.kXCS, "kXCS");
  // We didn't ask for email so shouldn't have got it.
  Assert.strictEqual(accountData.email, undefined);
});

add_task(async function checkExplicitGetNoSecureRead() {
  let sm = new FxAccountsStorageManager();
  sm.plainStorage = new MockedPlainStorage({
    uid: "uid",
    email: "someone@somewhere.com",
  });
  sm.secureStorage = new MockedSecureStorage({ kXCS: "kXCS" });
  await sm.initialize();

  Assert.equal(sm.secureStorage.fetchCount, 0);
  // request 2 fields in secure storage - it should have caused a single fetch.
  let accountData = await sm.getAccountData(["email", "uid"]);
  Assert.ok(accountData, "read account data");
  Assert.equal(accountData.uid, "uid");
  Assert.equal(accountData.email, "someone@somewhere.com");
  Assert.strictEqual(accountData.kXCS, undefined);
  Assert.equal(sm.secureStorage.fetchCount, 1);
});

add_task(async function checkLockedUpdates() {
  let sm = new FxAccountsStorageManager();
  sm.plainStorage = new MockedPlainStorage({
    uid: "uid",
    email: "someone@somewhere.com",
  });
  sm.secureStorage = new MockedSecureStorage({
    kSync: "old-kSync",
    kXCS: "kXCS",
  });
  sm.secureStorage.locked = true;
  await sm.initialize();

  let accountData = await sm.getAccountData();
  // requesting kSync and kXCS will fail as storage is locked.
  Assert.ok(!accountData.kSync);
  Assert.ok(!accountData.kXCS);
  // While locked we can still update it and see the updated value.
  sm.updateAccountData({ kSync: "new-kSync" });
  accountData = await sm.getAccountData();
  Assert.equal(accountData.kSync, "new-kSync");
  // unlock.
  sm.secureStorage.locked = false;
  accountData = await sm.getAccountData();
  // should reflect the value we updated and the one we didn't.
  Assert.equal(accountData.kSync, "new-kSync");
  Assert.equal(accountData.kXCS, "kXCS");
  // And storage should also reflect it.
  Assert.strictEqual(sm.secureStorage.data.accountData.kSync, "new-kSync");
  Assert.strictEqual(sm.secureStorage.data.accountData.kXCS, "kXCS");
});

// Some tests for the "storage queue" functionality.

// A helper for our queued tests. It creates a StorageManager and then queues
// an unresolved promise. The tests then do additional setup and checks, then
// resolves or rejects the blocked promise.
async function setupStorageManagerForQueueTest() {
  let sm = new FxAccountsStorageManager();
  sm.plainStorage = new MockedPlainStorage({
    uid: "uid",
    email: "someone@somewhere.com",
  });
  sm.secureStorage = new MockedSecureStorage({ kXCS: "kXCS" });
  sm.secureStorage.locked = true;
  await sm.initialize();

  let resolveBlocked, rejectBlocked;
  let blockedPromise = new Promise((resolve, reject) => {
    resolveBlocked = resolve;
    rejectBlocked = reject;
  });

  sm._queueStorageOperation(() => blockedPromise);
  return { sm, blockedPromise, resolveBlocked, rejectBlocked };
}

// First the general functionality.
add_task(async function checkQueueSemantics() {
  let { sm, resolveBlocked } = await setupStorageManagerForQueueTest();

  // We've one unresolved promise in the queue - add another promise.
  let resolveSubsequent;
  let subsequentPromise = new Promise(resolve => {
    resolveSubsequent = resolve;
  });
  let subsequentCalled = false;

  sm._queueStorageOperation(() => {
    subsequentCalled = true;
    resolveSubsequent();
    return subsequentPromise;
  });

  // Our "subsequent" function should not have been called yet.
  Assert.ok(!subsequentCalled);

  // Release our blocked promise.
  resolveBlocked();

  // Our subsequent promise should end up resolved.
  await subsequentPromise;
  Assert.ok(subsequentCalled);
  await sm.finalize();
});

// Check that a queued promise being rejected works correctly.
add_task(async function checkQueueSemanticsOnError() {
  let { sm, blockedPromise, rejectBlocked } =
    await setupStorageManagerForQueueTest();

  let resolveSubsequent;
  let subsequentPromise = new Promise(resolve => {
    resolveSubsequent = resolve;
  });
  let subsequentCalled = false;

  sm._queueStorageOperation(() => {
    subsequentCalled = true;
    resolveSubsequent();
    return subsequentPromise;
  });

  // Our "subsequent" function should not have been called yet.
  Assert.ok(!subsequentCalled);

  // Reject our blocked promise - the subsequent operations should still work
  // correctly.
  rejectBlocked("oh no");

  // Our subsequent promise should end up resolved.
  await subsequentPromise;
  Assert.ok(subsequentCalled);

  // But the first promise should reflect the rejection.
  try {
    await blockedPromise;
    Assert.ok(false, "expected this promise to reject");
  } catch (ex) {
    Assert.equal(ex, "oh no");
  }
  await sm.finalize();
});

// And some tests for the specific operations that are queued.
add_task(async function checkQueuedReadAndUpdate() {
  let { sm, resolveBlocked } = await setupStorageManagerForQueueTest();
  // Mock the underlying operations
  // _doReadAndUpdateSecure is queued by _maybeReadAndUpdateSecure
  let _doReadCalled = false;
  sm._doReadAndUpdateSecure = () => {
    _doReadCalled = true;
    return Promise.resolve();
  };

  let resultPromise = sm._maybeReadAndUpdateSecure();
  Assert.ok(!_doReadCalled);

  resolveBlocked();
  await resultPromise;
  Assert.ok(_doReadCalled);
  await sm.finalize();
});

add_task(async function checkQueuedWrite() {
  let { sm, resolveBlocked } = await setupStorageManagerForQueueTest();
  // Mock the underlying operations
  let __writeCalled = false;
  sm.__write = () => {
    __writeCalled = true;
    return Promise.resolve();
  };

  let writePromise = sm._write();
  Assert.ok(!__writeCalled);

  resolveBlocked();
  await writePromise;
  Assert.ok(__writeCalled);
  await sm.finalize();
});

add_task(async function checkQueuedDelete() {
  let { sm, resolveBlocked } = await setupStorageManagerForQueueTest();
  // Mock the underlying operations
  let _deleteCalled = false;
  sm._deleteAccountData = () => {
    _deleteCalled = true;
    return Promise.resolve();
  };

  let resultPromise = sm.deleteAccountData();
  Assert.ok(!_deleteCalled);

  resolveBlocked();
  await resultPromise;
  Assert.ok(_deleteCalled);
  await sm.finalize();
});