Bug 606353 - Add a Utils.checkGUID() helper. r=rnewman
authorPhilipp von Weitershausen <philipp@weitershausen.de>
Fri, 28 Jan 2011 09:39:28 -0800
changeset 61606 01b0083ca0d6fbf43db38d3291b8c6c021026e68
parent 61605 ee57bd80ed9f44700868776bf3b80b58940a591e
child 61607 fbe19a7e2322678e64402c0b68a387b3f5910234
push id1
push userroot
push dateTue, 10 Dec 2013 15:46:25 +0000
reviewersrnewman
bugs606353
Bug 606353 - Add a Utils.checkGUID() helper. r=rnewman
services/sync/modules/util.js
services/sync/tests/unit/test_utils_makeGUID.js
--- a/services/sync/modules/util.js
+++ b/services/sync/modules/util.js
@@ -259,16 +259,21 @@ let Utils = {
   /**
    * GUIDs are 9 random bytes encoded with base64url (RFC 4648).
    * That makes them 12 characters long with 72 bits of entropy.
    */
   makeGUID: function makeGUID() {
     return Utils.encodeBase64url(Utils.generateRandomBytes(9));
   },
 
+  _base64url_regex: /^[-abcdefghijklmnopqrstuvwxyz0123456789_]{12}$/i,
+  checkGUID: function checkGUID(guid) {
+    return !!guid && this._base64url_regex.test(guid);
+  },
+
   anno: function anno(id, anno, val, expire) {
     // Figure out if we have a bookmark or page
     let annoFunc = (typeof id == "number" ? "Item" : "Page") + "Annotation";
 
     // Convert to a nsIURI if necessary
     if (typeof id == "string")
       id = Utils.makeURI(id);
 
--- a/services/sync/tests/unit/test_utils_makeGUID.js
+++ b/services/sync/tests/unit/test_utils_makeGUID.js
@@ -1,29 +1,40 @@
-_("Make sure makeGUID makes guids of the right length/characters");
 Cu.import("resource://services-sync/util.js");
 
 const base64url =
   "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
 
 function run_test() {
+  _("Make sure makeGUID makes guids of the right length/characters");
   _("Create a bunch of guids to make sure they don't conflict");
   let guids = [];
   for (let i = 0; i < 1000; i++) {
     let newGuid = Utils.makeGUID();
     _("Generated " + newGuid);
 
     // Verify that the GUID's length is correct, even when it's URL encoded.
     do_check_eq(newGuid.length, 12);
     do_check_eq(encodeURIComponent(newGuid).length, 12);
 
     // Verify that the GUID only contains base64url characters
     do_check_true(Array.every(newGuid, function(chr) {
       return base64url.indexOf(chr) != -1;
     }));
 
+    // Verify that Utils.checkGUID() correctly identifies them as valid.
+    do_check_true(Utils.checkGUID(newGuid));
+
     // Verify uniqueness within our sample of 1000. This could cause random
     // failures, but they should be extremely rare. Otherwise we'd have a
     // problem with GUID collisions.
     do_check_true(guids.every(function(g) { return g != newGuid; }));
     guids.push(newGuid);
   }
+
+  _("Make sure checkGUID fails for invalid GUIDs");
+  do_check_false(Utils.checkGUID(undefined));
+  do_check_false(Utils.checkGUID(null));
+  do_check_false(Utils.checkGUID(""));
+  do_check_false(Utils.checkGUID("blergh"));
+  do_check_false(Utils.checkGUID("ThisGUIDisWayTooLong"));
+  do_check_false(Utils.checkGUID("Invalid!!!!!"));
 }