From 5e5d6f7b3fab5bd7b0498f7efcfdc348cd42a778 Mon Sep 17 00:00:00 2001 From: Mark Banner Date: Tue, 6 Jun 2023 16:43:14 +0000 Subject: [PATCH] Bug 1835824 - Add a test to validate search-config against the schema. r=search-reviewers,mcheang,daleharvey This will also ensure that if we add new properties to the search-config, we'll also need to add them to the schema. Differential Revision: https://phabricator.services.mozilla.com/D179496 --- .../schema/search-engine-config-schema.json | 3 + .../test_searchconfig_validates.js | 67 +++++++++++++++++++ .../tests/xpcshell/searchconfigs/xpcshell.ini | 3 + 3 files changed, 73 insertions(+) create mode 100644 toolkit/components/search/tests/xpcshell/searchconfigs/test_searchconfig_validates.js diff --git a/toolkit/components/search/schema/search-engine-config-schema.json b/toolkit/components/search/schema/search-engine-config-schema.json index 838ccbf340a3..f020204dd238 100644 --- a/toolkit/components/search/schema/search-engine-config-schema.json +++ b/toolkit/components/search/schema/search-engine-config-schema.json @@ -393,6 +393,9 @@ "type": "boolean", "title": "Override", "description": "This section will override previous appliesTo sections, but not add new locations where this engine is deployed to." + }, + "suggestExtraParams": { + "$ref": "#/definitions/extraParams" } } } diff --git a/toolkit/components/search/tests/xpcshell/searchconfigs/test_searchconfig_validates.js b/toolkit/components/search/tests/xpcshell/searchconfigs/test_searchconfig_validates.js new file mode 100644 index 000000000000..c6d82a7b9894 --- /dev/null +++ b/toolkit/components/search/tests/xpcshell/searchconfigs/test_searchconfig_validates.js @@ -0,0 +1,67 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +ChromeUtils.defineESModuleGetters(this, { + JsonSchema: "resource://gre/modules/JsonSchema.sys.mjs", +}); + +/** + * Checks to see if a value is an object or not. + * + * @param {*} value + * The value to check. + * @returns {boolean} + */ +function isObject(value) { + return value != null && typeof value == "object" && !Array.isArray(value); +} + +/** + * This function modifies the schema to prevent allowing additional properties + * on objects. This is used to enforce that the schema contains everything that + * we deliver via the search configuration. + * + * These checks are not enabled in-product, as we want to allow older versions + * to keep working if we add new properties for whatever reason. + * + * @param {object} section + * The section to check to see if an additionalProperties flag should be added. + */ +function disallowAdditionalProperties(section) { + if (section.type == "object") { + section.additionalProperties = false; + } + for (let value of Object.values(section)) { + if (isObject(value)) { + disallowAdditionalProperties(value); + } + } +} + +add_task(async function test_search_config_validates_to_schema() { + let schema = await IOUtils.readJSON( + PathUtils.join(do_get_cwd().path, "search-engine-config-schema.json") + ); + disallowAdditionalProperties(schema); + + let selector = new SearchEngineSelector(() => {}); + let searchConfig = await selector.getEngineConfiguration(); + let validator = new JsonSchema.Validator(schema); + + for (let entry of searchConfig) { + // Records in Remote Settings contain additional properties independent of + // the schema. Hence, we don't want to validate their presence. + delete entry.schema; + delete entry.id; + delete entry.last_modified; + + let result = validator.validate(entry); + let message = `Should validate ${entry.webExtension?.id}`; + if (!result.valid) { + message += `:\n${JSON.stringify(result.errors, null, 2)}`; + } + Assert.ok(result.valid, message); + } +}); diff --git a/toolkit/components/search/tests/xpcshell/searchconfigs/xpcshell.ini b/toolkit/components/search/tests/xpcshell/searchconfigs/xpcshell.ini index 17cbd5350dff..435d576f2373 100644 --- a/toolkit/components/search/tests/xpcshell/searchconfigs/xpcshell.ini +++ b/toolkit/components/search/tests/xpcshell/searchconfigs/xpcshell.ini @@ -29,6 +29,9 @@ requesttimeoutfactor = 2 [test_mailru.js] [test_qwant.js] [test_rakuten.js] +[test_searchconfig_validates.js] +support-files = + ../../../schema/search-engine-config-schema.json [test_selector_db_out_of_date.js] [test_yahoojp.js] [test_yandex.js]