summaryrefslogtreecommitdiffstats
path: root/services/sync/tests/unit/test_utils_passphrase.js
blob: 6d34697be47cc773aaf6b7290dfc5450e77e9892 (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
Cu.import("resource://services-sync/util.js");

function run_test() {
  _("Generated passphrase has length 26.");
  let pp = Utils.generatePassphrase();
  do_check_eq(pp.length, 26);

  const key = "abcdefghijkmnpqrstuvwxyz23456789";
  _("Passphrase only contains [" + key + "].");
  do_check_true(pp.split('').every(chr => key.indexOf(chr) != -1));

  _("Hyphenated passphrase has 5 hyphens.");
  let hyphenated = Utils.hyphenatePassphrase(pp);
  _("H: " + hyphenated);
  do_check_eq(hyphenated.length, 31);
  do_check_eq(hyphenated[1], '-');
  do_check_eq(hyphenated[7], '-');
  do_check_eq(hyphenated[13], '-');
  do_check_eq(hyphenated[19], '-');
  do_check_eq(hyphenated[25], '-');
  do_check_eq(pp,
      hyphenated.slice(0, 1) + hyphenated.slice(2, 7)
      + hyphenated.slice(8, 13) + hyphenated.slice(14, 19)
      + hyphenated.slice(20, 25) + hyphenated.slice(26, 31));

  _("Arbitrary hyphenation.");
  // We don't allow invalid characters for our base32 character set.
  do_check_eq(Utils.hyphenatePassphrase("1234567"), "2-34567");  // Not partial, so no trailing dash.
  do_check_eq(Utils.hyphenatePassphrase("1234567890"), "2-34567-89");
  do_check_eq(Utils.hyphenatePassphrase("abcdeabcdeabcdeabcdeabcde"), "a-bcdea-bcdea-bcdea-bcdea-bcde");
  do_check_eq(Utils.hyphenatePartialPassphrase("1234567"), "2-34567-");
  do_check_eq(Utils.hyphenatePartialPassphrase("1234567890"), "2-34567-89");
  do_check_eq(Utils.hyphenatePartialPassphrase("abcdeabcdeabcdeabcdeabcde"), "a-bcdea-bcdea-bcdea-bcdea-bcde");

  do_check_eq(Utils.hyphenatePartialPassphrase("a"), "a-");
  do_check_eq(Utils.hyphenatePartialPassphrase("1234567"), "2-34567-");
  do_check_eq(Utils.hyphenatePartialPassphrase("a-bcdef-g"),
              "a-bcdef-g");
  do_check_eq(Utils.hyphenatePartialPassphrase("abcdefghijklmnop"),
              "a-bcdef-ghijk-mnp");
  do_check_eq(Utils.hyphenatePartialPassphrase("abcdefghijklmnopabcde"),
              "a-bcdef-ghijk-mnpab-cde");
  do_check_eq(Utils.hyphenatePartialPassphrase("a-bcdef-ghijk-LMNOP-ABCDE-Fg"),
              "a-bcdef-ghijk-mnpab-cdefg-");
  // Cuts off.
  do_check_eq(Utils.hyphenatePartialPassphrase("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").length, 31);

  _("Normalize passphrase recognizes hyphens.");
  do_check_eq(Utils.normalizePassphrase(hyphenated), pp);

  _("Skip whitespace.");
  do_check_eq("aaaaaaaaaaaaaaaaaaaaaaaaaa", Utils.normalizePassphrase("aaaaaaaaaaaaaaaaaaaaaaaaaa  "));
  do_check_eq("aaaaaaaaaaaaaaaaaaaaaaaaaa", Utils.normalizePassphrase("	 aaaaaaaaaaaaaaaaaaaaaaaaaa"));
  do_check_eq("aaaaaaaaaaaaaaaaaaaaaaaaaa", Utils.normalizePassphrase("    aaaaaaaaaaaaaaaaaaaaaaaaaa  "));
  do_check_eq("aaaaaaaaaaaaaaaaaaaaaaaaaa", Utils.normalizePassphrase("    a-aaaaa-aaaaa-aaaaa-aaaaa-aaaaa  "));
  do_check_true(Utils.isPassphrase("aaaaaaaaaaaaaaaaaaaaaaaaaa  "));
  do_check_true(Utils.isPassphrase("	 aaaaaaaaaaaaaaaaaaaaaaaaaa"));
  do_check_true(Utils.isPassphrase("    aaaaaaaaaaaaaaaaaaaaaaaaaa  "));
  do_check_true(Utils.isPassphrase("    a-aaaaa-aaaaa-aaaaa-aaaaa-aaaaa  "));
  do_check_false(Utils.isPassphrase("    -aaaaa-aaaaa-aaaaa-aaaaa-aaaaa  "));

  _("Normalizing 20-char passphrases.");
  do_check_eq(Utils.normalizePassphrase("abcde-abcde-abcde-abcde"),
              "abcdeabcdeabcdeabcde");
  do_check_eq(Utils.normalizePassphrase("a-bcde-abcde-abcde-abcde"),
              "a-bcde-abcde-abcde-abcde");
  do_check_eq(Utils.normalizePassphrase(" abcde-abcde-abcde-abcde "),
              "abcdeabcdeabcdeabcde");

  _("Normalizing username.");
  do_check_eq(Utils.normalizeAccount("   QA1234+boo@mozilla.com	"), "QA1234+boo@mozilla.com");
  do_check_eq(Utils.normalizeAccount("QA1234+boo@mozilla.com"), "QA1234+boo@mozilla.com");
}