Bug 1347728 - Fix JSON dump of blocklists r=mgoodwin
MozReview-Commit-ID: rwX5Nn9d6b
--- 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.