services/sync/tests/unit/test_utils_makeGUID.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/sync/tests/unit/test_utils_makeGUID.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,40 @@
     1.4 +Cu.import("resource://services-sync/util.js");
     1.5 +
     1.6 +const base64url =
     1.7 +  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
     1.8 +
     1.9 +function run_test() {
    1.10 +  _("Make sure makeGUID makes guids of the right length/characters");
    1.11 +  _("Create a bunch of guids to make sure they don't conflict");
    1.12 +  let guids = [];
    1.13 +  for (let i = 0; i < 1000; i++) {
    1.14 +    let newGuid = Utils.makeGUID();
    1.15 +    _("Generated " + newGuid);
    1.16 +
    1.17 +    // Verify that the GUID's length is correct, even when it's URL encoded.
    1.18 +    do_check_eq(newGuid.length, 12);
    1.19 +    do_check_eq(encodeURIComponent(newGuid).length, 12);
    1.20 +
    1.21 +    // Verify that the GUID only contains base64url characters
    1.22 +    do_check_true(Array.every(newGuid, function(chr) {
    1.23 +      return base64url.indexOf(chr) != -1;
    1.24 +    }));
    1.25 +
    1.26 +    // Verify that Utils.checkGUID() correctly identifies them as valid.
    1.27 +    do_check_true(Utils.checkGUID(newGuid));
    1.28 +
    1.29 +    // Verify uniqueness within our sample of 1000. This could cause random
    1.30 +    // failures, but they should be extremely rare. Otherwise we'd have a
    1.31 +    // problem with GUID collisions.
    1.32 +    do_check_true(guids.every(function(g) { return g != newGuid; }));
    1.33 +    guids.push(newGuid);
    1.34 +  }
    1.35 +
    1.36 +  _("Make sure checkGUID fails for invalid GUIDs");
    1.37 +  do_check_false(Utils.checkGUID(undefined));
    1.38 +  do_check_false(Utils.checkGUID(null));
    1.39 +  do_check_false(Utils.checkGUID(""));
    1.40 +  do_check_false(Utils.checkGUID("blergh"));
    1.41 +  do_check_false(Utils.checkGUID("ThisGUIDisWayTooLong"));
    1.42 +  do_check_false(Utils.checkGUID("Invalid!!!!!"));
    1.43 +}

mercurial