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