michael@0: Cu.import("resource://services-sync/util.js"); michael@0: michael@0: const base64url = michael@0: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"; michael@0: michael@0: function run_test() { michael@0: _("Make sure makeGUID makes guids of the right length/characters"); michael@0: _("Create a bunch of guids to make sure they don't conflict"); michael@0: let guids = []; michael@0: for (let i = 0; i < 1000; i++) { michael@0: let newGuid = Utils.makeGUID(); michael@0: _("Generated " + newGuid); michael@0: michael@0: // Verify that the GUID's length is correct, even when it's URL encoded. michael@0: do_check_eq(newGuid.length, 12); michael@0: do_check_eq(encodeURIComponent(newGuid).length, 12); michael@0: michael@0: // Verify that the GUID only contains base64url characters michael@0: do_check_true(Array.every(newGuid, function(chr) { michael@0: return base64url.indexOf(chr) != -1; michael@0: })); michael@0: michael@0: // Verify that Utils.checkGUID() correctly identifies them as valid. michael@0: do_check_true(Utils.checkGUID(newGuid)); michael@0: michael@0: // Verify uniqueness within our sample of 1000. This could cause random michael@0: // failures, but they should be extremely rare. Otherwise we'd have a michael@0: // problem with GUID collisions. michael@0: do_check_true(guids.every(function(g) { return g != newGuid; })); michael@0: guids.push(newGuid); michael@0: } michael@0: michael@0: _("Make sure checkGUID fails for invalid GUIDs"); michael@0: do_check_false(Utils.checkGUID(undefined)); michael@0: do_check_false(Utils.checkGUID(null)); michael@0: do_check_false(Utils.checkGUID("")); michael@0: do_check_false(Utils.checkGUID("blergh")); michael@0: do_check_false(Utils.checkGUID("ThisGUIDisWayTooLong")); michael@0: do_check_false(Utils.checkGUID("Invalid!!!!!")); michael@0: }