summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/LoginTestUtils.sys.mjs
blob: 7a7e3835f058852dd062c35144896fbeea8eb108 (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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Shared functions generally available for testing login components.
 */

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  RemoteSettings: "resource://services-settings/remote-settings.sys.mjs",
});

import { Assert as AssertCls } from "resource://testing-common/Assert.sys.mjs";

let Assert = AssertCls;

import { TestUtils } from "resource://testing-common/TestUtils.sys.mjs";
import { setTimeout } from "resource://gre/modules/Timer.sys.mjs";
import { FileTestUtils } from "resource://testing-common/FileTestUtils.sys.mjs";

const LoginInfo = Components.Constructor(
  "@mozilla.org/login-manager/loginInfo;1",
  "nsILoginInfo",
  "init"
);

export const LoginTestUtils = {
  setAssertReporter(reporterFunc) {
    Assert = new AssertCls(Cu.waiveXrays(reporterFunc));
  },

  /**
   * Forces the storage module to save all data, and the Login Manager service
   * to replace the storage module with a newly initialized instance.
   */
  async reloadData() {
    Services.obs.notifyObservers(null, "passwordmgr-storage-replace");
    await TestUtils.topicObserved("passwordmgr-storage-replace-complete");
  },

  /**
   * Erases all the data stored by the Login Manager service.
   */
  clearData() {
    Services.logins.removeAllUserFacingLogins();
    for (let origin of Services.logins.getAllDisabledHosts()) {
      Services.logins.setLoginSavingEnabled(origin, true);
    }
  },

  /**
   * Add a new login to the store
   */
  async addLogin({
    username,
    password,
    origin = "https://example.com",
    formActionOrigin,
  }) {
    const login = LoginTestUtils.testData.formLogin({
      origin,
      formActionOrigin: formActionOrigin || origin,
      username,
      password,
    });
    return Services.logins.addLoginAsync(login);
  },

  async modifyLogin(oldLogin, newLogin) {
    const storageChangedPromise = TestUtils.topicObserved(
      "passwordmgr-storage-changed",
      (_, data) => data == "modifyLogin"
    );
    Services.logins.modifyLogin(oldLogin, newLogin);
    await storageChangedPromise;
  },

  resetGeneratedPasswordsCache() {
    let { LoginManagerParent } = ChromeUtils.importESModule(
      "resource://gre/modules/LoginManagerParent.sys.mjs"
    );
    LoginManagerParent.getGeneratedPasswordsByPrincipalOrigin().clear();
  },

  /**
   * Checks that the currently stored list of nsILoginInfo matches the provided
   * array.  If no `checkFn` is provided, the comparison uses the "equals"
   * method of nsILoginInfo, that does not include nsILoginMetaInfo properties in the test.
   */
  checkLogins(expectedLogins, msg = "checkLogins", checkFn = undefined) {
    this.assertLoginListsEqual(
      Services.logins.getAllLogins(),
      expectedLogins,
      msg,
      checkFn
    );
  },

  /**
   * Checks that the two provided arrays of nsILoginInfo have the same length,
   * and every login in "expected" is also found in "actual".  If no `checkFn`
   * is provided, the comparison uses the "equals" method of nsILoginInfo, that
   * does not include nsILoginMetaInfo properties in the test.
   */
  assertLoginListsEqual(
    actual,
    expected,
    msg = "assertLoginListsEqual",
    checkFn = undefined
  ) {
    Assert.equal(expected.length, actual.length, msg);
    Assert.ok(
      expected.every(e =>
        actual.some(a => {
          return checkFn ? checkFn(a, e) : a.equals(e);
        })
      ),
      msg
    );
  },

  /**
   * Checks that the two provided arrays of strings contain the same values,
   * maybe in a different order, case-sensitively.
   */
  assertDisabledHostsEqual(actual, expected) {
    Assert.deepEqual(actual.sort(), expected.sort());
  },

  /**
   * Checks whether the given time, expressed as the number of milliseconds
   * since January 1, 1970, 00:00:00 UTC, falls within 30 seconds of now.
   */
  assertTimeIsAboutNow(timeMs) {
    Assert.ok(Math.abs(timeMs - Date.now()) < 30000);
  },
};

/**
 * This object contains functions that return new instances of nsILoginInfo for
 * every call.  The returned instances can be compared using their "equals" or
 * "matches" methods, or modified for the needs of the specific test being run.
 *
 * Any modification to the test data requires updating the tests accordingly, in
 * particular the search tests.
 */
LoginTestUtils.testData = {
  /**
   * Returns a new nsILoginInfo for use with form submits.
   *
   * @param modifications
   *        Each property of this object replaces the property of the same name
   *        in the returned nsILoginInfo or nsILoginMetaInfo.
   */
  formLogin(modifications) {
    let loginInfo = new LoginInfo(
      "http://www3.example.com",
      "http://www.example.com",
      null,
      "the username",
      "the password",
      "form_field_username",
      "form_field_password"
    );
    loginInfo.QueryInterface(Ci.nsILoginMetaInfo);
    if (modifications) {
      for (let [name, value] of Object.entries(modifications)) {
        if (name == "httpRealm" && value !== null) {
          throw new Error("httpRealm not supported for form logins");
        }
        loginInfo[name] = value;
      }
    }
    return loginInfo;
  },

  /**
   * Returns a new nsILoginInfo for use with HTTP authentication.
   *
   * @param modifications
   *        Each property of this object replaces the property of the same name
   *        in the returned nsILoginInfo or nsILoginMetaInfo.
   */
  authLogin(modifications) {
    let loginInfo = new LoginInfo(
      "http://www.example.org",
      null,
      "The HTTP Realm",
      "the username",
      "the password"
    );
    loginInfo.QueryInterface(Ci.nsILoginMetaInfo);
    if (modifications) {
      for (let [name, value] of Object.entries(modifications)) {
        if (name == "formActionOrigin" && value !== null) {
          throw new Error(
            "formActionOrigin not supported for HTTP auth. logins"
          );
        }
        loginInfo[name] = value;
      }
    }
    return loginInfo;
  },

  /**
   * Returns an array of typical nsILoginInfo that could be stored in the
   * database.
   */
  loginList() {
    return [
      // --- Examples of form logins (subdomains of example.com) ---

      // Simple form login with named fields for username and password.
      new LoginInfo(
        "http://www.example.com",
        "http://www.example.com",
        null,
        "the username",
        "the password for www.example.com",
        "form_field_username",
        "form_field_password"
      ),

      // Different schemes are treated as completely different sites.
      new LoginInfo(
        "https://www.example.com",
        "https://www.example.com",
        null,
        "the username",
        "the password for https",
        "form_field_username",
        "form_field_password"
      ),

      // Subdomains can be treated as completely different sites depending on the UI invoked.
      new LoginInfo(
        "https://example.com",
        "https://example.com",
        null,
        "the username",
        "the password for example.com",
        "form_field_username",
        "form_field_password"
      ),

      // Forms found on the same origin, but with different origins in the
      // "action" attribute, are handled independently.
      new LoginInfo(
        "http://www3.example.com",
        "http://www.example.com",
        null,
        "the username",
        "the password",
        "form_field_username",
        "form_field_password"
      ),
      new LoginInfo(
        "http://www3.example.com",
        "https://www.example.com",
        null,
        "the username",
        "the password",
        "form_field_username",
        "form_field_password"
      ),
      new LoginInfo(
        "http://www3.example.com",
        "http://example.com",
        null,
        "the username",
        "the password",
        "form_field_username",
        "form_field_password"
      ),

      // It is not possible to store multiple passwords for the same username,
      // however multiple passwords can be stored when the usernames differ.
      // An empty username is a valid case and different from the others.
      new LoginInfo(
        "http://www4.example.com",
        "http://www4.example.com",
        null,
        "username one",
        "password one",
        "form_field_username",
        "form_field_password"
      ),
      new LoginInfo(
        "http://www4.example.com",
        "http://www4.example.com",
        null,
        "username two",
        "password two",
        "form_field_username",
        "form_field_password"
      ),
      new LoginInfo(
        "http://www4.example.com",
        "http://www4.example.com",
        null,
        "",
        "password three",
        "form_field_username",
        "form_field_password"
      ),

      // Username and passwords fields in forms may have no "name" attribute.
      new LoginInfo(
        "http://www5.example.com",
        "http://www5.example.com",
        null,
        "multi username",
        "multi password",
        "",
        ""
      ),

      // Forms with PIN-type authentication will typically have no username.
      new LoginInfo(
        "http://www6.example.com",
        "http://www6.example.com",
        null,
        "",
        "12345",
        "",
        "form_field_password"
      ),

      // Logins can be saved on non-default ports
      new LoginInfo(
        "https://www7.example.com:8080",
        "https://www7.example.com:8080",
        null,
        "8080_username",
        "8080_pass"
      ),

      new LoginInfo(
        "https://www7.example.com:8080",
        null,
        "My dev server",
        "8080_username2",
        "8080_pass2"
      ),

      // --- Examples of authentication logins (subdomains of example.org) ---

      // Simple HTTP authentication login.
      new LoginInfo(
        "http://www.example.org",
        null,
        "The HTTP Realm",
        "the username",
        "the password"
      ),

      // Simple FTP authentication login.
      new LoginInfo(
        "ftp://ftp.example.org",
        null,
        "ftp://ftp.example.org",
        "the username",
        "the password"
      ),

      // Multiple HTTP authentication logins can be stored for different realms.
      new LoginInfo(
        "http://www2.example.org",
        null,
        "The HTTP Realm",
        "the username",
        "the password"
      ),
      new LoginInfo(
        "http://www2.example.org",
        null,
        "The HTTP Realm Other",
        "the username other",
        "the password other"
      ),

      // --- Both form and authentication logins (example.net) ---

      new LoginInfo(
        "http://example.net",
        "http://example.net",
        null,
        "the username",
        "the password",
        "form_field_username",
        "form_field_password"
      ),
      new LoginInfo(
        "http://example.net",
        "http://www.example.net",
        null,
        "the username",
        "the password",
        "form_field_username",
        "form_field_password"
      ),
      new LoginInfo(
        "http://example.net",
        "http://www.example.net",
        null,
        "username two",
        "the password",
        "form_field_username",
        "form_field_password"
      ),
      new LoginInfo(
        "http://example.net",
        null,
        "The HTTP Realm",
        "the username",
        "the password"
      ),
      new LoginInfo(
        "http://example.net",
        null,
        "The HTTP Realm Other",
        "username two",
        "the password"
      ),
      new LoginInfo(
        "ftp://example.net",
        null,
        "ftp://example.net",
        "the username",
        "the password"
      ),

      // --- Examples of logins added by extensions (chrome scheme) ---

      new LoginInfo(
        "chrome://example_extension",
        null,
        "Example Login One",
        "the username",
        "the password one",
        "",
        ""
      ),
      new LoginInfo(
        "chrome://example_extension",
        null,
        "Example Login Two",
        "the username",
        "the password two"
      ),

      // -- file:// URIs throw accessing nsIURI.host

      new LoginInfo(
        "file://",
        "file://",
        null,
        "file: username",
        "file: password"
      ),

      // -- javascript: URIs throw accessing nsIURI.host.
      // They should only be used for the formActionOrigin.
      new LoginInfo(
        "https://js.example.com",
        "javascript:",
        null,
        "javascript: username",
        "javascript: password"
      ),
    ];
  },
};

LoginTestUtils.recipes = {
  getRecipeParent() {
    let { LoginManagerParent } = ChromeUtils.importESModule(
      "resource://gre/modules/LoginManagerParent.sys.mjs"
    );
    if (!LoginManagerParent.recipeParentPromise) {
      return null;
    }
    return LoginManagerParent.recipeParentPromise.then(recipeParent => {
      return recipeParent;
    });
  },
};

LoginTestUtils.primaryPassword = {
  primaryPassword: "omgsecret!",

  _set(enable, stayLoggedIn) {
    let oldPW, newPW;
    if (enable) {
      oldPW = "";
      newPW = this.primaryPassword;
    } else {
      oldPW = this.primaryPassword;
      newPW = "";
    }
    try {
      let pk11db = Cc["@mozilla.org/security/pk11tokendb;1"].getService(
        Ci.nsIPK11TokenDB
      );
      let token = pk11db.getInternalKeyToken();
      if (token.needsUserInit) {
        dump("MP initialized to " + newPW + "\n");
        token.initPassword(newPW);
      } else {
        token.checkPassword(oldPW);
        dump("MP change from " + oldPW + " to " + newPW + "\n");
        token.changePassword(oldPW, newPW);
        if (!stayLoggedIn) {
          token.logoutSimple();
        }
      }
    } catch (e) {
      dump(
        "Tried to enable an already enabled primary password or disable an already disabled primary password!"
      );
    }
  },

  enable(stayLoggedIn = false) {
    this._set(true, stayLoggedIn);
  },

  disable() {
    this._set(false);
  },
};

/**
 * Utilities related to interacting with login fields in content.
 */
LoginTestUtils.loginField = {
  checkPasswordMasked(field, expected, msg) {
    let { editor } = field;
    let valueLength = field.value.length;
    Assert.equal(
      editor.autoMaskingEnabled,
      expected,
      `Check autoMaskingEnabled: ${msg}`
    );
    Assert.equal(editor.unmaskedStart, 0, `unmaskedStart is 0: ${msg}`);
    if (expected) {
      Assert.equal(editor.unmaskedEnd, 0, `Password is masked: ${msg}`);
    } else {
      Assert.equal(
        editor.unmaskedEnd,
        valueLength,
        `Unmasked to the end: ${msg}`
      );
    }
  },
};

LoginTestUtils.generation = {
  LENGTH: 15,
  REGEX: /^[a-km-np-zA-HJ-NP-Z2-9-~!@#$%^&*_+=)}:;"'>,.?\]]{15}$/,
};

LoginTestUtils.telemetry = {
  async waitForEventCount(
    count,
    process = "content",
    category = "pwmgr",
    method = undefined
  ) {
    // The test is already unreliable (see bug 1627419 and 1605494) and relied on
    // the implicit 100ms initial timer of waitForCondition that bug 1596165 removed.
    await new Promise(resolve => setTimeout(resolve, 100));
    let events = await TestUtils.waitForCondition(() => {
      let events = Services.telemetry.snapshotEvents(
        Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS,
        false
      )[process];

      if (!events) {
        return null;
      }

      events = events.filter(
        e => e[1] == category && (!method || e[2] == method)
      );
      dump(`Waiting for ${count} events, got ${events.length}\n`);
      return events.length == count ? events : null;
    }, "waiting for telemetry event count of: " + count);
    Assert.equal(events.length, count, "waiting for telemetry event count");
    return events;
  },
};

LoginTestUtils.file = {
  /**
   * Given an array of strings it creates a temporary CSV file that has them as content.
   *
   * @param {string[]} csvLines
   *        The lines that make up the CSV file.
   * @param {string} extension
   *        Optional parameter. Either 'csv' or 'tsv'. Default is 'csv'.
   * @returns {window.File} The File to the CSV file that was created.
   */
  async setupCsvFileWithLines(csvLines, extension = "csv") {
    let tmpFile = FileTestUtils.getTempFile(`firefox_logins.${extension}`);
    await IOUtils.writeUTF8(tmpFile.path, csvLines.join("\r\n"));
    return tmpFile;
  },
};

LoginTestUtils.remoteSettings = {
  relatedRealmsCollection: "websites-with-shared-credential-backends",
  async setupWebsitesWithSharedCredentials(
    relatedRealms = [["other-example.com", "example.com", "example.co.uk"]]
  ) {
    let db = lazy.RemoteSettings(this.relatedRealmsCollection).db;
    await db.clear();
    await db.create({
      id: "some-fake-ID-abc",
      relatedRealms,
    });
    await db.importChanges({}, Date.now());
  },
  async cleanWebsitesWithSharedCredentials() {
    let db = lazy.RemoteSettings(this.relatedRealmsCollection).db;
    await db.importChanges({}, Date.now(), [], { clear: true });
  },
  improvedPasswordRulesCollection: "password-rules",

  async setupImprovedPasswordRules(
    origin = "example.com",
    rules = "minlength: 6; maxlength: 16; required: lower, upper; required: digit; required: [&<>'\"!#$%(),:;=?[^`{|}~]]; max-consecutive: 2;"
  ) {
    let db = lazy.RemoteSettings(this.improvedPasswordRulesCollection).db;
    await db.clear();
    await db.create({
      id: "some-fake-ID",
      Domain: origin,
      "password-rules": rules,
    });
    await db.create({
      id: "some-fake-ID-2",
      Domain: origin,
      "password-rules": rules,
    });
    await db.importChanges({}, Date.now());
  },
  async cleanImprovedPasswordRules() {
    let db = lazy.RemoteSettings(this.improvedPasswordRulesCollection).db;
    await db.importChanges({}, Date.now(), [], { clear: true });
  },
};