services/sync/tests/unit/test_utils_makeGUID.js

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 Cu.import("resource://services-sync/util.js");
michael@0 2
michael@0 3 const base64url =
michael@0 4 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
michael@0 5
michael@0 6 function run_test() {
michael@0 7 _("Make sure makeGUID makes guids of the right length/characters");
michael@0 8 _("Create a bunch of guids to make sure they don't conflict");
michael@0 9 let guids = [];
michael@0 10 for (let i = 0; i < 1000; i++) {
michael@0 11 let newGuid = Utils.makeGUID();
michael@0 12 _("Generated " + newGuid);
michael@0 13
michael@0 14 // Verify that the GUID's length is correct, even when it's URL encoded.
michael@0 15 do_check_eq(newGuid.length, 12);
michael@0 16 do_check_eq(encodeURIComponent(newGuid).length, 12);
michael@0 17
michael@0 18 // Verify that the GUID only contains base64url characters
michael@0 19 do_check_true(Array.every(newGuid, function(chr) {
michael@0 20 return base64url.indexOf(chr) != -1;
michael@0 21 }));
michael@0 22
michael@0 23 // Verify that Utils.checkGUID() correctly identifies them as valid.
michael@0 24 do_check_true(Utils.checkGUID(newGuid));
michael@0 25
michael@0 26 // Verify uniqueness within our sample of 1000. This could cause random
michael@0 27 // failures, but they should be extremely rare. Otherwise we'd have a
michael@0 28 // problem with GUID collisions.
michael@0 29 do_check_true(guids.every(function(g) { return g != newGuid; }));
michael@0 30 guids.push(newGuid);
michael@0 31 }
michael@0 32
michael@0 33 _("Make sure checkGUID fails for invalid GUIDs");
michael@0 34 do_check_false(Utils.checkGUID(undefined));
michael@0 35 do_check_false(Utils.checkGUID(null));
michael@0 36 do_check_false(Utils.checkGUID(""));
michael@0 37 do_check_false(Utils.checkGUID("blergh"));
michael@0 38 do_check_false(Utils.checkGUID("ThisGUIDisWayTooLong"));
michael@0 39 do_check_false(Utils.checkGUID("Invalid!!!!!"));
michael@0 40 }

mercurial