Bug 1544214 - Ensure all engines in list.json can be loaded. r=Standard8
authorDale Harvey <dale@arandomurl.com>
Fri, 19 Apr 2019 07:51:48 +0000
changeset 528996 e07782ebf0f2cb5ce8a58ff23eb4939bce6b6dd5
parent 528995 8e76e4330c6de858eb1b45f85bdbb2815007fc88
child 528997 176a74bf9fe22def92fd0fb9286c708d3514bdc4
push id11265
push userffxbld-merge
push dateMon, 13 May 2019 10:53:39 +0000
treeherdermozilla-beta@77e0fe8dbdd3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersStandard8
bugs1544214
milestone68.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 1544214 - Ensure all engines in list.json can be loaded. r=Standard8 Differential Revision: https://phabricator.services.mozilla.com/D28044
toolkit/components/search/SearchService.jsm
toolkit/components/search/tests/xpcshell/test_validate_engines.js
toolkit/components/search/tests/xpcshell/xpcshell.ini
--- a/toolkit/components/search/SearchService.jsm
+++ b/toolkit/components/search/SearchService.jsm
@@ -2857,16 +2857,18 @@ SearchService.prototype = {
     let val = this.getGlobalAttr(name);
     if (val && this.getGlobalAttr(name + "Hash") != getVerificationHash(val)) {
       LOG("getVerifiedGlobalAttr, invalid hash for " + name);
       return "";
     }
     return val;
   },
 
+  _listJSONURL: ((AppConstants.platform == "android") ? APP_SEARCH_PREFIX : EXT_SEARCH_PREFIX) + "list.json",
+
   _engines: { },
   __sortedEngines: null,
   _visibleDefaultEngines: [],
   _searchDefault: null,
   _searchOrder: [],
   // Stores a map of the built in engines installed and their params so
   // they can be reconstructed in restarts.
   _extensions: new Map(),
@@ -3462,38 +3464,36 @@ SearchService.prototype = {
    * Loads jar engines asynchronously.
    *
    * @returns {Promise} A promise, resolved successfully if finding jar engines
    * succeeds.
    */
   async _findEngines() {
     LOG("_findEngines: looking for engines in JARs");
 
-    let prefix = AppConstants.platform == "android" ? APP_SEARCH_PREFIX : EXT_SEARCH_PREFIX;
-    let listURL = prefix + "list.json";
-    let chan = makeChannel(listURL);
+    let chan = makeChannel(this._listJSONURL);
     if (!chan) {
-      LOG("_findEngines: " + prefix + " isn't registered");
+      LOG("_findEngines: " + this._listJSONURL + " isn't registered");
       return [];
     }
 
     let uris = [];
 
     // Read list.json to find the engines we need to load.
     let request = new XMLHttpRequest();
     request.overrideMimeType("text/plain");
     let list = await new Promise(resolve => {
       request.onload = function(event) {
         resolve(event.target.responseText);
       };
       request.onerror = function(event) {
-        LOG("_findEngines: failed to read " + listURL);
+        LOG("_findEngines: failed to read " + this._listJSONURL);
         resolve();
       };
-      request.open("GET", Services.io.newURI(listURL).spec, true);
+      request.open("GET", Services.io.newURI(this._listJSONURL).spec, true);
       request.send();
     });
 
     this._parseListJSON(list, uris);
     return uris;
   },
 
   _parseListJSON(list, uris) {
new file mode 100644
--- /dev/null
+++ b/toolkit/components/search/tests/xpcshell/test_validate_engines.js
@@ -0,0 +1,43 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+// Ensure all the engines defined in list.json are valid by
+// creating a new list.json that contains every engine and
+// loading them all.
+
+"use strict";
+
+Cu.importGlobalProperties(["fetch"]);
+
+const {SearchService} = ChromeUtils.import("resource://gre/modules/SearchService.jsm");
+const LIST_JSON_URL = "resource://search-extensions/list.json";
+
+function traverse(obj, fun) {
+  for (var i in obj) {
+    fun.apply(this, [i, obj[i]]);
+    if (obj[i] !== null && typeof(obj[i]) == "object") {
+      traverse(obj[i], fun);
+    }
+  }
+}
+
+const ss = new SearchService();
+
+add_task(async function test_validate_engines() {
+  let engines = await fetch(LIST_JSON_URL).then(req => req.json());
+
+  let visibleDefaultEngines = new Set();
+  traverse(engines, (key, val) => {
+    if (key === "visibleDefaultEngines") {
+      val.forEach(engine => visibleDefaultEngines.add(engine));
+    }
+  });
+
+  let listjson = {default: {
+    visibleDefaultEngines: Array.from(visibleDefaultEngines),
+  }};
+  ss._listJSONURL = "data:application/json," + JSON.stringify(listjson);
+
+  await AddonTestUtils.promiseStartupManager();
+  await ss.init();
+});
--- a/toolkit/components/search/tests/xpcshell/xpcshell.ini
+++ b/toolkit/components/search/tests/xpcshell/xpcshell.ini
@@ -102,11 +102,12 @@ skip-if = (verify && !debug && (os == 'l
 [test_addEngineWithDetails.js]
 [test_addEngineWithDetailsObject.js]
 [test_addEngineWithExtensionID.js]
 [test_chromeresource_icon2.js]
 [test_engineUpdate.js]
 [test_paramSubstitution.js]
 [test_migrateWebExtensionEngine.js]
 [test_sendSubmissionURL.js]
+[test_validate_engines.js]
 [test_validate_manifests.js]
 [test_webextensions_install.js]
 [test_purpose.js]