From e218041b0afb473790d13cab0e307b05a7bf250c Mon Sep 17 00:00:00 2001 From: DimiL Date: Mon, 12 Jun 2017 17:04:59 +0800 Subject: [PATCH] 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 --HG-- extra : rebase_source : bf11c44ae98f34de43ac65ad4558ee53894e5568 --- .../tests/browser/classifierHelper.js | 42 ++++------------- .../tests/mochitest/classifierHelper.js | 46 ++++--------------- 2 files changed, 17 insertions(+), 71 deletions(-) diff --git a/toolkit/components/url-classifier/tests/browser/classifierHelper.js b/toolkit/components/url-classifier/tests/browser/classifierHelper.js index b2d2c341311b..728157d4f2c9 100644 --- a/toolkit/components/url-classifier/tests/browser/classifierHelper.js +++ b/toolkit/components/url-classifier/tests/browser/classifierHelper.js @@ -14,8 +14,6 @@ if (typeof(classifierHelper) == "undefined") { var classifierHelper = {}; } -const ADD_CHUNKNUM = 524; -const SUB_CHUNKNUM = 523; const HASHLEN = 32; const PREFS = { @@ -24,6 +22,8 @@ const PREFS = { 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 = []; @@ -103,57 +103,31 @@ classifierHelper.addUrlToDB = function(updateData) { 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); diff --git a/toolkit/components/url-classifier/tests/mochitest/classifierHelper.js b/toolkit/components/url-classifier/tests/mochitest/classifierHelper.js index 16266b06cb1d..40988cbf65cc 100644 --- a/toolkit/components/url-classifier/tests/mochitest/classifierHelper.js +++ b/toolkit/components/url-classifier/tests/mochitest/classifierHelper.js @@ -5,8 +5,6 @@ if (typeof(classifierHelper) == "undefined") { 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 = { @@ -15,7 +13,9 @@ const PREFS = { 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 = []; @@ -66,12 +66,15 @@ classifierHelper.addUrlToDB = function(updateData) { 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; } @@ -79,48 +82,17 @@ classifierHelper.addUrlToDB = function(updateData) { }); } -// 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);