2010-09-17 08:48:06 -07:00
|
|
|
Cu.import("resource://services-sync/util.js");
|
|
|
|
|
|
|
|
function run_test() {
|
2010-11-29 16:41:17 -08:00
|
|
|
_("Generated passphrase has length 26.");
|
2010-09-17 08:48:06 -07:00
|
|
|
let pp = Utils.generatePassphrase();
|
2010-11-29 16:41:17 -08:00
|
|
|
do_check_eq(pp.length, 26);
|
2010-09-17 08:48:06 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
const key = "abcdefghijkmnpqrstuvwxyz23456789";
|
|
|
|
_("Passphrase only contains [" + key + "].");
|
|
|
|
do_check_true(pp.split('').every(function(chr) key.indexOf(chr) != -1));
|
2010-09-17 08:48:06 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
_("Hyphenated passphrase has 5 hyphens.");
|
2010-09-17 08:48:06 -07:00
|
|
|
let hyphenated = Utils.hyphenatePassphrase(pp);
|
2010-11-29 16:41:17 -08:00
|
|
|
_("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);
|
|
|
|
|
|
|
|
|
2010-09-17 08:48:06 -07:00
|
|
|
|
|
|
|
_("Normalize passphrase recognizes hyphens.");
|
|
|
|
do_check_eq(Utils.normalizePassphrase(hyphenated), pp);
|
2010-09-22 17:04:31 -07:00
|
|
|
|
|
|
|
_("Passphrase strength calculated according to the NIST algorithm.");
|
|
|
|
do_check_eq(Utils.passphraseStrength(""), 0);
|
|
|
|
do_check_eq(Utils.passphraseStrength("a"), 4);
|
|
|
|
do_check_eq(Utils.passphraseStrength("ab"), 6);
|
|
|
|
do_check_eq(Utils.passphraseStrength("abc"), 8);
|
|
|
|
do_check_eq(Utils.passphraseStrength("abcdefgh"), 18);
|
|
|
|
do_check_eq(Utils.passphraseStrength("abcdefghi"), 19.5);
|
|
|
|
do_check_eq(Utils.passphraseStrength("abcdefghij"), 21);
|
|
|
|
do_check_eq(Utils.passphraseStrength("abcdefghijklmnopqrst"), 36);
|
|
|
|
do_check_eq(Utils.passphraseStrength("abcdefghijklmnopqrstu"), 37);
|
|
|
|
do_check_eq(Utils.passphraseStrength("abcdefghijklmnopqrstuvwxyz"), 42);
|
|
|
|
do_check_eq(Utils.passphraseStrength("abcdefghijklmnopqrstuvwxyz!"), 49);
|
|
|
|
do_check_eq(Utils.passphraseStrength("1"), 10);
|
|
|
|
do_check_eq(Utils.passphraseStrength("12"), 12);
|
|
|
|
do_check_eq(Utils.passphraseStrength("a1"), 12);
|
2010-09-17 08:48:06 -07:00
|
|
|
}
|