Bug 1347728 - Fix JSON dump of blocklists r=mgoodwin draft
authorMathieu Leplatre <mathieu@mozilla.com>
Wed, 22 Mar 2017 15:46:55 +0100
changeset 570201 7a2aba200352dcb05394bd099dcc6974ebab1036
parent 569138 0b77ed3f26c5335503bc16e85b8c067382e7bb1e
child 626433 3304c3d5d39362fdd1bfb134028c76526f990c61
push id56425
push usermleplatre@mozilla.com
push dateFri, 28 Apr 2017 15:24:36 +0000
reviewersmgoodwin
bugs1347728
milestone55.0a1
Bug 1347728 - Fix JSON dump of blocklists r=mgoodwin MozReview-Commit-ID: rwX5Nn9d6b
services/common/blocklist-clients.js
services/common/tests/unit/test_blocklist_clients.js
--- a/services/common/blocklist-clients.js
+++ b/services/common/blocklist-clients.js
@@ -97,29 +97,37 @@ class BlocklistClient {
     this.signerName = signerName;
 
     this._kinto = new Kinto({
       bucket: bucketName,
       adapter: FirefoxAdapter,
     });
   }
 
+  get identifier() {
+    return `${this.bucketName}/${this.collectionName}`;
+  }
+
   get filename() {
-    return `${this.bucketName}/${this.collectionName}.json`;
+    // Replace slash by OS specific path separator (eg. Windows)
+    const identifier = OS.Path.join(...this.identifier.split("/"));
+    return `${identifier}.json`;
   }
 
   /**
    * Load the the JSON file distributed with the release for this blocklist.
    *
    * For Bug 1257565 this method will have to try to load the file from the profile,
    * in order to leverage the updateJSONBlocklist() below, which writes a new
    * dump each time the collection changes.
    */
   loadDumpFile() {
-    const fileURI = `resource://app/defaults/${this.filename}`;
+    // Replace OS specific path separator by / for URI.
+    const { components: folderFile } = OS.Path.split(this.filename);
+    const fileURI = `resource://app/defaults/${folderFile.join("/")}`;
     return Task.spawn(function* loadFile() {
       const response = yield fetch(fileURI);
       if (!response.ok) {
         throw new Error(`Could not read from '${fileURI}'`);
       }
       // Will be rejected if JSON is invalid.
       return yield response.json();
     });
--- a/services/common/tests/unit/test_blocklist_clients.js
+++ b/services/common/tests/unit/test_blocklist_clients.js
@@ -1,12 +1,10 @@
 const { Constructor: CC } = Components;
 
-const KEY_PROFILEDIR = "ProfD";
-
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://testing-common/httpd.js");
 Cu.import("resource://gre/modules/Timer.jsm");
 const { FileUtils } = Cu.import("resource://gre/modules/FileUtils.jsm", {});
 const { OS } = Cu.import("resource://gre/modules/osfile.jsm", {});
 
 const { Kinto } = Cu.import("resource://services-common/kinto-offline-client.js", {});
 const { FirefoxAdapter } = Cu.import("resource://services-common/kinto-storage-adapter.js", {});
@@ -53,19 +51,20 @@ function* clear_state() {
     try {
       sqliteHandle = yield FirefoxAdapter.openConnection({path: kintoFilename});
       const collection = kintoCollection(client.collectionName, sqliteHandle);
       yield collection.clear();
     } finally {
       yield sqliteHandle.close();
     }
 
-    // Remove profile data.
-    const path = OS.Path.join(OS.Constants.Path.profileDir, client.filename);
-    yield OS.File.remove(path, { ignoreAbsent: true });
+    // Remove JSON dumps folders in profile dir.
+    const dumpFile = OS.Path.join(OS.Constants.Path.profileDir, client.filename);
+    const folder = OS.Path.dirname(dumpFile);
+    yield OS.File.removeDir(folder, { ignoreAbsent: true });
   }
 }
 
 
 function run_test() {
   // Set up an HTTP Server
   server = new HttpServer();
   server.start(-1);
@@ -127,17 +126,18 @@ add_task(function* test_records_obtained
     equal(list.data.length, 1);
     yield sqliteHandle.close();
   }
 });
 add_task(clear_state);
 
 add_task(function* test_list_is_written_to_file_in_profile() {
   for (let {client, testData} of gBlocklistClients) {
-    const profFile = FileUtils.getFile(KEY_PROFILEDIR, client.filename.split("/"));
+    const filePath = OS.Path.join(OS.Constants.Path.profileDir, client.filename);
+    const profFile = new FileUtils.File(filePath);
     strictEqual(profFile.exists(), false);
 
     yield client.maybeSync(2000, Date.now(), {loadDump: false});
 
     strictEqual(profFile.exists(), true);
     const content = yield readJSON(profFile.path);
     equal(content.data[0].blockID, testData[testData.length - 1]);
   }
@@ -152,17 +152,18 @@ add_task(function* test_current_server_t
     equal(after, Math.round(serverTime / 1000));
   }
 });
 add_task(clear_state);
 
 add_task(function* test_update_json_file_when_addons_has_changes() {
   for (let {client, testData} of gBlocklistClients) {
     yield client.maybeSync(2000, Date.now() - 1000, {loadDump: false});
-    const profFile = FileUtils.getFile(KEY_PROFILEDIR, client.filename.split("/"));
+    const filePath = OS.Path.join(OS.Constants.Path.profileDir, client.filename);
+    const profFile = new FileUtils.File(filePath);
     const fileLastModified = profFile.lastModifiedTime = profFile.lastModifiedTime - 1000;
     const serverTime = Date.now();
 
     yield client.maybeSync(3001, serverTime);
 
     // File was updated.
     notEqual(fileLastModified, profFile.lastModifiedTime);
     const content = yield readJSON(profFile.path);
@@ -187,17 +188,18 @@ add_task(function* test_sends_reload_mes
     equal(received.data.filename, client.filename);
   }
 });
 add_task(clear_state);
 
 add_task(function* test_do_nothing_when_blocklist_is_up_to_date() {
   for (let {client} of gBlocklistClients) {
     yield client.maybeSync(2000, Date.now() - 1000, {loadDump: false});
-    const profFile = FileUtils.getFile(KEY_PROFILEDIR, client.filename.split("/"));
+    const filePath = OS.Path.join(OS.Constants.Path.profileDir, client.filename);
+    const profFile = new FileUtils.File(filePath);
     const fileLastModified = profFile.lastModifiedTime = profFile.lastModifiedTime - 1000;
     const serverTime = Date.now();
 
     yield client.maybeSync(3000, serverTime);
 
     // File was not updated.
     equal(fileLastModified, profFile.lastModifiedTime);
     // Server time was updated.