Bug 1347728 - Fix JSON dump of blocklists r=kjozwiak,mgoodwin
authorMathieu Leplatre <mathieu@mozilla.com>
Wed, 22 Mar 2017 15:46:55 +0100
changeset 406492 e35828da3a2ef86c2f053c187275faf42cb775da
parent 406491 f8cf70e02740182fbbed5a650e07b01f2adc4eb6
child 406493 9233d2abdfc241478a6971be5fa44530eb18eb01
push id1490
push usermtabara@mozilla.com
push dateMon, 31 Jul 2017 14:08:16 +0000
treeherdermozilla-release@70e32e6bf15e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskjozwiak, mgoodwin
bugs1347728
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1347728 - Fix JSON dump of blocklists r=kjozwiak,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.