services/sync/tests/unit/test_utils_makeGUID.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 Cu.import("resource://services-sync/util.js");
     3 const base64url =
     4   "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
     6 function run_test() {
     7   _("Make sure makeGUID makes guids of the right length/characters");
     8   _("Create a bunch of guids to make sure they don't conflict");
     9   let guids = [];
    10   for (let i = 0; i < 1000; i++) {
    11     let newGuid = Utils.makeGUID();
    12     _("Generated " + newGuid);
    14     // Verify that the GUID's length is correct, even when it's URL encoded.
    15     do_check_eq(newGuid.length, 12);
    16     do_check_eq(encodeURIComponent(newGuid).length, 12);
    18     // Verify that the GUID only contains base64url characters
    19     do_check_true(Array.every(newGuid, function(chr) {
    20       return base64url.indexOf(chr) != -1;
    21     }));
    23     // Verify that Utils.checkGUID() correctly identifies them as valid.
    24     do_check_true(Utils.checkGUID(newGuid));
    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     do_check_true(guids.every(function(g) { return g != newGuid; }));
    30     guids.push(newGuid);
    31   }
    33   _("Make sure checkGUID fails for invalid GUIDs");
    34   do_check_false(Utils.checkGUID(undefined));
    35   do_check_false(Utils.checkGUID(null));
    36   do_check_false(Utils.checkGUID(""));
    37   do_check_false(Utils.checkGUID("blergh"));
    38   do_check_false(Utils.checkGUID("ThisGUIDisWayTooLong"));
    39   do_check_false(Utils.checkGUID("Invalid!!!!!"));
    40 }

mercurial