Bug 1329366 - Avoid the reuse of the same chunk numbers in classifierHelper.js. r?francois draft
authorDimiL <dlee@mozilla.com>
Mon, 12 Jun 2017 17:04:59 +0800
changeset 594361 5a895b339a7ab9c5614fa85216ca223b843ff621
parent 592285 981da978f1f686ad024fa958c9d27d2f8acc5ad0
child 633414 1154f2118a95e1abc8708a3c94ec886e88379e43
push id64019
push userbmo:dlee@mozilla.com
push dateThu, 15 Jun 2017 00:38:50 +0000
reviewersfrancois
bugs1329366
milestone55.0a1
Bug 1329366 - Avoid the reuse of the same chunk numbers in classifierHelper.js. r?francois This patch includes following fix in classifierHelper.js: 1. Avoid the reuse of same chunk numbers 2. Remove unused removeUrlFromDB function MozReview-Commit-ID: XK1oHBa8gf
toolkit/components/url-classifier/tests/browser/classifierHelper.js
toolkit/components/url-classifier/tests/mochitest/classifierHelper.js
--- a/toolkit/components/url-classifier/tests/browser/classifierHelper.js
+++ b/toolkit/components/url-classifier/tests/browser/classifierHelper.js
@@ -9,26 +9,26 @@
 
 let dbService = Cc["@mozilla.org/url-classifier/dbservice;1"]
                 .getService(Ci.nsIUrlClassifierDBService);
 
 if (typeof(classifierHelper) == "undefined") {
   var classifierHelper = {};
 }
 
-const ADD_CHUNKNUM = 524;
-const SUB_CHUNKNUM = 523;
 const HASHLEN = 32;
 
 const PREFS = {
   PROVIDER_LISTS : "browser.safebrowsing.provider.mozilla.lists",
   DISALLOW_COMPLETIONS : "urlclassifier.disallow_completions",
   PROVIDER_GETHASHURL : "browser.safebrowsing.provider.mozilla.gethashURL"
 };
 
+classifierHelper._curAddChunkNum = 1;
+
 // Keep urls added to database, those urls should be automatically
 // removed after test complete.
 classifierHelper._updatesToCleanup = [];
 
 // This function returns a Promise resolved when SafeBrowsing.jsm is initialized.
 // SafeBrowsing.jsm is initialized after mozEntries are added. Add observer
 // to receive "finished" event. For the case when this function is called
 // after the event had already been notified, we lookup entries to see if
@@ -98,67 +98,41 @@ classifierHelper.allowCompletion = funct
 classifierHelper.addUrlToDB = function(updateData) {
   let testUpdate = "";
   for (let update of updateData) {
     let LISTNAME = update.db;
     let CHUNKDATA = update.url;
     let CHUNKLEN = CHUNKDATA.length;
     let HASHLEN = update.len ? update.len : 32;
 
+    update.addChunk = classifierHelper._curAddChunkNum;
+    classifierHelper._curAddChunkNum += 1;
+
     classifierHelper._updatesToCleanup.push(update);
     testUpdate +=
       "n:1000\n" +
       "i:" + LISTNAME + "\n" +
       "ad:1\n" +
-      "a:" + ADD_CHUNKNUM + ":" + HASHLEN + ":" + CHUNKLEN + "\n" +
+      "a:" + update.addChunk + ":" + HASHLEN + ":" + CHUNKLEN + "\n" +
       CHUNKDATA;
   }
 
   return classifierHelper._update(testUpdate);
 }
 
-// Pass { url: ..., db: ... } to remove url from database,
-// Returns a Promise.
-classifierHelper.removeUrlFromDB = function(updateData) {
-  var testUpdate = "";
-  for (var update of updateData) {
-    var LISTNAME = update.db;
-    var CHUNKDATA = ADD_CHUNKNUM + ":" + update.url;
-    var CHUNKLEN = CHUNKDATA.length;
-    var HASHLEN = update.len ? update.len : 32;
-
-    testUpdate +=
-      "n:1000\n" +
-      "i:" + LISTNAME + "\n" +
-      "s:" + SUB_CHUNKNUM + ":" + HASHLEN + ":" + CHUNKLEN + "\n" +
-      CHUNKDATA;
-  }
-
-  classifierHelper._updatesToCleanup =
-    classifierHelper._updatesToCleanup.filter((v) => {
-      return updateData.indexOf(v) == -1;
-    });
-
-  return classifierHelper._update(testUpdate);
-};
-
 // This API is used to expire all add/sub chunks we have updated
-// by using addUrlToDB and removeUrlFromDB.
+// by using addUrlToDB.
 // Returns a Promise.
 classifierHelper.resetDatabase = function() {
   var testUpdate = "";
   for (var update of classifierHelper._updatesToCleanup) {
-    if (testUpdate.includes(update.db))
-      continue;
-
     testUpdate +=
       "n:1000\n" +
       "i:" + update.db + "\n" +
-      "ad:" + ADD_CHUNKNUM + "\n" +
-      "sd:" + SUB_CHUNKNUM + "\n"
+      "ad:" + update.addChunk + "\n";
   }
 
   return classifierHelper._update(testUpdate);
 };
 
 classifierHelper.reloadDatabase = function() {
   dbService.reloadDatabase();
 }
--- a/toolkit/components/url-classifier/tests/mochitest/classifierHelper.js
+++ b/toolkit/components/url-classifier/tests/mochitest/classifierHelper.js
@@ -1,26 +1,26 @@
 if (typeof(classifierHelper) == "undefined") {
   var classifierHelper = {};
 }
 
 const CLASSIFIER_COMMON_URL = SimpleTest.getTestFileURL("classifierCommon.js");
 var gScript = SpecialPowers.loadChromeScript(CLASSIFIER_COMMON_URL);
 
-const ADD_CHUNKNUM = 524;
-const SUB_CHUNKNUM = 523;
 const HASHLEN = 32;
 
 const PREFS = {
   PROVIDER_LISTS : "browser.safebrowsing.provider.mozilla.lists",
   DISALLOW_COMPLETIONS : "urlclassifier.disallow_completions",
   PROVIDER_GETHASHURL : "browser.safebrowsing.provider.mozilla.gethashURL"
 };
 
-// addUrlToDB & removeUrlFromDB are asynchronous, queue the task to ensure
+classifierHelper._curAddChunkNum = 1;
+
+// addUrlToDB is asynchronous, queue the task to ensure
 // the callback follow correct order.
 classifierHelper._updates = [];
 
 // Keep urls added to database, those urls should be automatically
 // removed after test complete.
 classifierHelper._updatesToCleanup = [];
 
 classifierHelper._initsCB = [];
@@ -61,71 +61,43 @@ classifierHelper.addUrlToDB = function(u
   return new Promise(function(resolve, reject) {
     var testUpdate = "";
     for (var update of updateData) {
       var LISTNAME = update.db;
       var CHUNKDATA = update.url;
       var CHUNKLEN = CHUNKDATA.length;
       var HASHLEN = update.len ? update.len : 32;
 
+      update.addChunk = classifierHelper._curAddChunkNum;
+      classifierHelper._curAddChunkNum += 1;
+
       classifierHelper._updatesToCleanup.push(update);
       testUpdate +=
         "n:1000\n" +
         "i:" + LISTNAME + "\n" +
         "ad:1\n" +
-        "a:" + ADD_CHUNKNUM + ":" + HASHLEN + ":" + CHUNKLEN + "\n" +
+        "a:" + update.addChunk + ":" + HASHLEN + ":" + CHUNKLEN + "\n" +
         CHUNKDATA;
     }
 
     classifierHelper._update(testUpdate, resolve, reject);
   });
 }
 
-// Pass { url: ..., db: ... } to remove url from database,
-// onsuccess/onerror will be called when update complete.
-classifierHelper.removeUrlFromDB = function(updateData) {
-  return new Promise(function(resolve, reject) {
-    var testUpdate = "";
-    for (var update of updateData) {
-      var LISTNAME = update.db;
-      var CHUNKDATA = ADD_CHUNKNUM + ":" + update.url;
-      var CHUNKLEN = CHUNKDATA.length;
-      var HASHLEN = update.len ? update.len : 32;
-
-      testUpdate +=
-        "n:1000\n" +
-        "i:" + LISTNAME + "\n" +
-        "s:" + SUB_CHUNKNUM + ":" + HASHLEN + ":" + CHUNKLEN + "\n" +
-        CHUNKDATA;
-    }
-
-    classifierHelper._updatesToCleanup =
-      classifierHelper._updatesToCleanup.filter((v) => {
-        return updateData.indexOf(v) == -1;
-      });
-
-    classifierHelper._update(testUpdate, resolve, reject);
-  });
-};
-
 // This API is used to expire all add/sub chunks we have updated
-// by using addUrlToDB and removeUrlFromDB.
+// by using addUrlToDB.
 classifierHelper.resetDatabase = function() {
   function removeDatabase() {
     return new Promise(function(resolve, reject) {
       var testUpdate = "";
       for (var update of classifierHelper._updatesToCleanup) {
-        if (testUpdate.includes(update.db))
-          continue;
-
         testUpdate +=
           "n:1000\n" +
           "i:" + update.db + "\n" +
-          "ad:" + ADD_CHUNKNUM + "\n" +
-          "sd:" + SUB_CHUNKNUM + "\n"
+          "ad:" + update.addChunk + "\n";
       }
 
       classifierHelper._update(testUpdate, resolve, reject);
     });
   }
 
   // Remove and then reload will ensure both database and cache will
   // be cleared.