From a90a5cba08fdf6c0ceb95101c275108a152a3aed Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 12 Jun 2024 07:35:37 +0200 Subject: Merging upstream version 127.0. Signed-off-by: Daniel Baumann --- services/settings/test/unit/head_settings.js | 25 ++++++ .../test/unit/test_attachments_downloader.js | 90 +++++++++++++++++++--- .../settings/test/unit/test_remote_settings.js | 25 +----- .../unit/test_remote_settings_dump_lastmodified.js | 4 - .../test/unit/test_remote_settings_jexl_filters.js | 10 +-- .../test/unit/test_remote_settings_offline.js | 12 +-- .../test/unit/test_remote_settings_poll.js | 33 +------- .../unit/test_remote_settings_recover_broken.js | 15 ---- .../unit/test_remote_settings_release_prefs.js | 56 ++++++++++++-- .../test/unit/test_remote_settings_signatures.js | 19 +---- .../test/unit/test_remote_settings_sync_history.js | 4 - .../test/unit/test_remote_settings_utils.js | 9 --- .../unit/test_remote_settings_utils_telemetry.js | 10 +-- .../test/unit/test_remote_settings_worker.js | 19 ----- .../settings/test/unit/test_shutdown_handling.js | 14 ---- services/settings/test/unit/xpcshell.toml | 2 +- 16 files changed, 168 insertions(+), 179 deletions(-) create mode 100644 services/settings/test/unit/head_settings.js (limited to 'services/settings/test/unit') diff --git a/services/settings/test/unit/head_settings.js b/services/settings/test/unit/head_settings.js new file mode 100644 index 0000000000..96573a9fcb --- /dev/null +++ b/services/settings/test/unit/head_settings.js @@ -0,0 +1,25 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* import-globals-from ../../../common/tests/unit/head_global.js */ +/* import-globals-from ../../../common/tests/unit/head_helpers.js */ + +"use strict"; + +ChromeUtils.defineESModuleGetters(this, { + AppConstants: "resource://gre/modules/AppConstants.sys.mjs", + Database: "resource://services-settings/Database.sys.mjs", + Policy: "resource://services-common/uptake-telemetry.sys.mjs", + RemoteSettings: "resource://services-settings/remote-settings.sys.mjs", + RemoteSettingsClient: + "resource://services-settings/RemoteSettingsClient.sys.mjs", + RemoteSettingsWorker: + "resource://services-settings/RemoteSettingsWorker.sys.mjs", + setTimeout: "resource://gre/modules/Timer.sys.mjs", + SharedUtils: "resource://services-settings/SharedUtils.sys.mjs", + SyncHistory: "resource://services-settings/SyncHistory.sys.mjs", + TelemetryTestUtils: "resource://testing-common/TelemetryTestUtils.sys.mjs", + TestUtils: "resource://testing-common/TestUtils.sys.mjs", + UptakeTelemetry: "resource://services-common/uptake-telemetry.sys.mjs", + Utils: "resource://services-settings/Utils.sys.mjs", +}); diff --git a/services/settings/test/unit/test_attachments_downloader.js b/services/settings/test/unit/test_attachments_downloader.js index 284294cfde..2f7db36d8b 100644 --- a/services/settings/test/unit/test_attachments_downloader.js +++ b/services/settings/test/unit/test_attachments_downloader.js @@ -1,17 +1,6 @@ -/* import-globals-from ../../../common/tests/unit/head_helpers.js */ - -const { RemoteSettings } = ChromeUtils.importESModule( - "resource://services-settings/remote-settings.sys.mjs" -); -const { UptakeTelemetry } = ChromeUtils.importESModule( - "resource://services-common/uptake-telemetry.sys.mjs" -); const { Downloader } = ChromeUtils.importESModule( "resource://services-settings/Attachments.sys.mjs" ); -const { TelemetryTestUtils } = ChromeUtils.importESModule( - "resource://testing-common/TelemetryTestUtils.sys.mjs" -); const RECORD = { id: "1f3a0802-648d-11ea-bd79-876a8b69c377", @@ -56,6 +45,11 @@ add_setup(() => { "/cdn/main-workspace/some-collection/", do_get_file("test_attachments_downloader") ); + + // For this test, we are using a server other than production. Force + // LOAD_DUMPS to true so that we can still load attachments from dumps. + delete Utils.LOAD_DUMPS; + Utils.LOAD_DUMPS = true; }); async function clear_state() { @@ -619,6 +613,80 @@ add_task(async function test_download_from_dump() { // but added for consistency with other tests tasks around here. add_task(clear_state); +add_task( + async function test_download_from_dump_fails_when_load_dumps_is_false() { + const client = RemoteSettings("dump-collection", { + bucketName: "dump-bucket", + }); + + // Temporarily replace the resource:-URL with another resource:-URL. + const orig_RESOURCE_BASE_URL = Downloader._RESOURCE_BASE_URL; + Downloader._RESOURCE_BASE_URL = "resource://rs-downloader-test"; + const resProto = Services.io + .getProtocolHandler("resource") + .QueryInterface(Ci.nsIResProtocolHandler); + resProto.setSubstitution( + "rs-downloader-test", + Services.io.newFileURI(do_get_file("test_attachments_downloader")) + ); + + function checkInfo( + result, + expectedSource, + expectedRecord = RECORD_OF_DUMP + ) { + Assert.equal( + new TextDecoder().decode(new Uint8Array(result.buffer)), + "This would be a RS dump.\n", + "expected content from dump" + ); + Assert.deepEqual( + result.record, + expectedRecord, + "expected record for dump" + ); + Assert.equal(result._source, expectedSource, "expected source of dump"); + } + + // Download the dump so that we can use it to fill the cache. + const dump1 = await client.attachments.download(RECORD_OF_DUMP, { + // Note: attachmentId not set, so should fall back to record.id. + fallbackToDump: true, + }); + checkInfo(dump1, "dump_match"); + + // Fill the cache with the same data as the dump for the next part. + await client.db.saveAttachment(RECORD_OF_DUMP.id, { + record: RECORD_OF_DUMP, + blob: new Blob([dump1.buffer]), + }); + + // Now turn off loading dumps, and check we no longer load from the dump, + // but use the cache instead. + Utils.LOAD_DUMPS = false; + + const dump2 = await client.attachments.download(RECORD_OF_DUMP, { + // Note: attachmentId not set, so should fall back to record.id. + fallbackToDump: true, + }); + checkInfo(dump2, "cache_match"); + + // When the record is not given, the dump would take precedence over the + // cache but we have disabled dumps, so we should load from the cache. + const dump4 = await client.attachments.download(null, { + attachmentId: RECORD_OF_DUMP.id, + fallbackToCache: true, + fallbackToDump: true, + }); + checkInfo(dump4, "cache_fallback"); + + // Restore, just in case. + Utils.LOAD_DUMPS = true; + Downloader._RESOURCE_BASE_URL = orig_RESOURCE_BASE_URL; + resProto.setSubstitution("rs-downloader-test", null); + } +); + add_task(async function test_attachment_get() { // Since get() is largely a wrapper around the same code as download(), // we only test a couple of parts to check it functions as expected, and diff --git a/services/settings/test/unit/test_remote_settings.js b/services/settings/test/unit/test_remote_settings.js index 382d1aa983..07007a755f 100644 --- a/services/settings/test/unit/test_remote_settings.js +++ b/services/settings/test/unit/test_remote_settings.js @@ -1,27 +1,8 @@ /* import-globals-from ../../../common/tests/unit/head_helpers.js */ -const { AppConstants } = ChromeUtils.importESModule( - "resource://gre/modules/AppConstants.sys.mjs" -); const { ObjectUtils } = ChromeUtils.importESModule( "resource://gre/modules/ObjectUtils.sys.mjs" ); -const { setTimeout } = ChromeUtils.importESModule( - "resource://gre/modules/Timer.sys.mjs" -); - -const { RemoteSettings } = ChromeUtils.importESModule( - "resource://services-settings/remote-settings.sys.mjs" -); -const { Utils } = ChromeUtils.importESModule( - "resource://services-settings/Utils.sys.mjs" -); -const { UptakeTelemetry, Policy } = ChromeUtils.importESModule( - "resource://services-common/uptake-telemetry.sys.mjs" -); -const { TelemetryTestUtils } = ChromeUtils.importESModule( - "resource://testing-common/TelemetryTestUtils.sys.mjs" -); const IS_ANDROID = AppConstants.platform == "android"; @@ -54,7 +35,7 @@ async function clear_state() { TelemetryTestUtils.assertEvents([], {}, { process: "dummy" }); } -function run_test() { +add_task(() => { // Set up an HTTP Server server = new HttpServer(); server.start(-1); @@ -93,13 +74,11 @@ function run_test() { ); server.registerPathHandler("/fake-x5u", handleResponse); - run_next_test(); - registerCleanupFunction(() => { Policy.getChannel = oldGetChannel; server.stop(() => {}); }); -} +}); add_task(clear_state); add_task(async function test_records_obtained_from_server_are_stored_in_db() { diff --git a/services/settings/test/unit/test_remote_settings_dump_lastmodified.js b/services/settings/test/unit/test_remote_settings_dump_lastmodified.js index 25de34c1be..6dd925281a 100644 --- a/services/settings/test/unit/test_remote_settings_dump_lastmodified.js +++ b/services/settings/test/unit/test_remote_settings_dump_lastmodified.js @@ -1,9 +1,5 @@ "use strict"; -const { Utils } = ChromeUtils.importESModule( - "resource://services-settings/Utils.sys.mjs" -); - async function getLocalDumpLastModified(bucket, collection) { let res; try { diff --git a/services/settings/test/unit/test_remote_settings_jexl_filters.js b/services/settings/test/unit/test_remote_settings_jexl_filters.js index 56d35bdd2b..b7496332dc 100644 --- a/services/settings/test/unit/test_remote_settings_jexl_filters.js +++ b/services/settings/test/unit/test_remote_settings_jexl_filters.js @@ -1,7 +1,3 @@ -const { RemoteSettings } = ChromeUtils.importESModule( - "resource://services-settings/remote-settings.sys.mjs" -); - let client; async function createRecords(records) { @@ -18,11 +14,9 @@ async function createRecords(records) { ); } -function run_test() { +add_setup(() => { client = RemoteSettings("some-key"); - - run_next_test(); -} +}); add_task(async function test_returns_all_without_target() { await createRecords([ diff --git a/services/settings/test/unit/test_remote_settings_offline.js b/services/settings/test/unit/test_remote_settings_offline.js index 0a250c3e0a..cb194677e1 100644 --- a/services/settings/test/unit/test_remote_settings_offline.js +++ b/services/settings/test/unit/test_remote_settings_offline.js @@ -1,13 +1,3 @@ -const { RemoteSettingsClient } = ChromeUtils.importESModule( - "resource://services-settings/RemoteSettingsClient.sys.mjs" -); -const { RemoteSettingsWorker } = ChromeUtils.importESModule( - "resource://services-settings/RemoteSettingsWorker.sys.mjs" -); -const { SharedUtils } = ChromeUtils.importESModule( - "resource://services-settings/SharedUtils.sys.mjs" -); - // A collection with a dump that's packaged on all builds where this test runs, // including on Android at mobile/android/installer/package-manifest.in const TEST_BUCKET = "main"; @@ -17,7 +7,7 @@ let client; let DUMP_RECORDS; let DUMP_LAST_MODIFIED; -add_task(async function setup() { +add_setup(async () => { // "services.settings.server" pref is not set. // Test defaults to an unreachable server, // and will only load from the dump if any. diff --git a/services/settings/test/unit/test_remote_settings_poll.js b/services/settings/test/unit/test_remote_settings_poll.js index c8025f4b7b..7146429427 100644 --- a/services/settings/test/unit/test_remote_settings_poll.js +++ b/services/settings/test/unit/test_remote_settings_poll.js @@ -1,34 +1,11 @@ -/* import-globals-from ../../../common/tests/unit/head_helpers.js */ - -const { AppConstants } = ChromeUtils.importESModule( - "resource://gre/modules/AppConstants.sys.mjs" -); -const { setTimeout } = ChromeUtils.importESModule( - "resource://gre/modules/Timer.sys.mjs" -); - -const { UptakeTelemetry, Policy } = ChromeUtils.importESModule( - "resource://services-common/uptake-telemetry.sys.mjs" -); -const { RemoteSettingsClient } = ChromeUtils.importESModule( - "resource://services-settings/RemoteSettingsClient.sys.mjs" -); const { pushBroadcastService } = ChromeUtils.importESModule( "resource://gre/modules/PushBroadcastService.sys.mjs" ); -const { SyncHistory } = ChromeUtils.importESModule( - "resource://services-settings/SyncHistory.sys.mjs" -); -const { RemoteSettings, remoteSettingsBroadcastHandler, BROADCAST_ID } = + +const { remoteSettingsBroadcastHandler, BROADCAST_ID } = ChromeUtils.importESModule( "resource://services-settings/remote-settings.sys.mjs" ); -const { Utils } = ChromeUtils.importESModule( - "resource://services-settings/Utils.sys.mjs" -); -const { TelemetryTestUtils } = ChromeUtils.importESModule( - "resource://testing-common/TelemetryTestUtils.sys.mjs" -); const IS_ANDROID = AppConstants.platform == "android"; @@ -80,7 +57,7 @@ function serveChangesEntries(serverTime, entriesOrFunc) { }; } -function run_test() { +add_setup(() => { // Set up an HTTP Server server = new HttpServer(); server.start(-1); @@ -89,13 +66,11 @@ function run_test() { let oldGetChannel = Policy.getChannel; Policy.getChannel = () => "nightly"; - run_next_test(); - registerCleanupFunction(() => { Policy.getChannel = oldGetChannel; server.stop(() => {}); }); -} +}); add_task(clear_state); diff --git a/services/settings/test/unit/test_remote_settings_recover_broken.js b/services/settings/test/unit/test_remote_settings_recover_broken.js index c5f82d6949..5bb047c8af 100644 --- a/services/settings/test/unit/test_remote_settings_recover_broken.js +++ b/services/settings/test/unit/test_remote_settings_recover_broken.js @@ -1,18 +1,3 @@ -/* import-globals-from ../../../common/tests/unit/head_helpers.js */ - -const { SyncHistory } = ChromeUtils.importESModule( - "resource://services-settings/SyncHistory.sys.mjs" -); -const { RemoteSettingsClient } = ChromeUtils.importESModule( - "resource://services-settings/RemoteSettingsClient.sys.mjs" -); -const { RemoteSettings } = ChromeUtils.importESModule( - "resource://services-settings/remote-settings.sys.mjs" -); -const { Utils } = ChromeUtils.importESModule( - "resource://services-settings/Utils.sys.mjs" -); - const PREF_SETTINGS_SERVER = "services.settings.server"; const CHANGES_PATH = "/v1" + Utils.CHANGES_PATH; const BROKEN_SYNC_THRESHOLD = 10; // See default pref value diff --git a/services/settings/test/unit/test_remote_settings_release_prefs.js b/services/settings/test/unit/test_remote_settings_release_prefs.js index 251c407631..5d1a9f2e28 100644 --- a/services/settings/test/unit/test_remote_settings_release_prefs.js +++ b/services/settings/test/unit/test_remote_settings_release_prefs.js @@ -1,9 +1,5 @@ "use strict"; -const { AppConstants } = ChromeUtils.importESModule( - "resource://gre/modules/AppConstants.sys.mjs" -); - var nextUniqId = 0; function getNewUtils() { const { Utils } = ChromeUtils.importESModule( @@ -12,7 +8,21 @@ function getNewUtils() { return Utils; } -function clear_state() { +// A collection with a dump that's packaged on all builds where this test runs, +// including on Android at mobile/android/installer/package-manifest.in +const TEST_BUCKET = "main"; +const TEST_COLLECTION = "password-recipes"; + +async function importData(records) { + await RemoteSettingsWorker._execute("_test_only_import", [ + TEST_BUCKET, + TEST_COLLECTION, + records, + records[0]?.last_modified || 0, + ]); +} + +async function clear_state() { Services.env.set("MOZ_REMOTE_SETTINGS_DEVTOOLS", "0"); Services.prefs.clearUserPref("services.settings.server"); Services.prefs.clearUserPref("services.settings.preview_enabled"); @@ -186,6 +196,42 @@ add_task( const Utils = getNewUtils(); Assert.ok(!Utils.LOAD_DUMPS, "Dumps won't be loaded"); + + // The section below ensures that the LOAD_DUMPS flag properly takes effect. + // The client is set up here rather than add_setup to avoid triggering the + // lazy getters that are behind the global Utils.LOAD_DUMPS. If they are + // triggered too early, then they will potentially cache different values + // for the server urls and environment variables and this test then won't be + // testing what we expect it to. + + let client = new RemoteSettingsClient(TEST_COLLECTION); + + const dump = await SharedUtils.loadJSONDump(TEST_BUCKET, TEST_COLLECTION); + let DUMP_LAST_MODIFIED = dump.timestamp; + + // Dump is updated regularly, verify that the dump matches our expectations + // before running the test. + Assert.greater( + DUMP_LAST_MODIFIED, + 1234, + "Assuming dump to be newer than dummy 1234" + ); + + await client.db.clear(); + await importData([{ last_modified: 1234, id: "dummy" }]); + + const after = await client.get(); + Assert.deepEqual( + after, + [{ last_modified: 1234, id: "dummy" }], + "Should have kept the original import" + ); + Assert.equal( + await client.getLastModified(), + 1234, + "Should have kept the import's timestamp" + ); + await client.db.clear(); } ); add_task(clear_state); diff --git a/services/settings/test/unit/test_remote_settings_signatures.js b/services/settings/test/unit/test_remote_settings_signatures.js index a730ba185e..6d3b01468e 100644 --- a/services/settings/test/unit/test_remote_settings_signatures.js +++ b/services/settings/test/unit/test_remote_settings_signatures.js @@ -1,19 +1,6 @@ /* import-globals-from ../../../common/tests/unit/head_helpers.js */ "use strict"; -const { RemoteSettings } = ChromeUtils.importESModule( - "resource://services-settings/remote-settings.sys.mjs" -); -const { RemoteSettingsClient } = ChromeUtils.importESModule( - "resource://services-settings/RemoteSettingsClient.sys.mjs" -); -const { UptakeTelemetry, Policy } = ChromeUtils.importESModule( - "resource://services-common/uptake-telemetry.sys.mjs" -); -const { TelemetryTestUtils } = ChromeUtils.importESModule( - "resource://testing-common/TelemetryTestUtils.sys.mjs" -); - const PREF_SETTINGS_SERVER = "services.settings.server"; const SIGNER_NAME = "onecrl.content-signature.mozilla.org"; const TELEMETRY_COMPONENT = "remotesettings"; @@ -42,7 +29,7 @@ function getCertChain() { let server; let client; -function run_test() { +add_setup(() => { // Signature verification is enabled by default. We use a custom signer // because these tests were originally written for OneCRL. client = RemoteSettings("signed", { signerName: SIGNER_NAME }); @@ -57,13 +44,11 @@ function run_test() { let oldGetChannel = Policy.getChannel; Policy.getChannel = () => "nightly"; - run_next_test(); - registerCleanupFunction(() => { Policy.getChannel = oldGetChannel; server.stop(() => {}); }); -} +}); add_task(async function test_check_signatures() { // First, perform a signature verification with known data and signature diff --git a/services/settings/test/unit/test_remote_settings_sync_history.js b/services/settings/test/unit/test_remote_settings_sync_history.js index 1fdc3d1adc..cc1c5c2dab 100644 --- a/services/settings/test/unit/test_remote_settings_sync_history.js +++ b/services/settings/test/unit/test_remote_settings_sync_history.js @@ -1,9 +1,5 @@ "use strict"; -const { SyncHistory } = ChromeUtils.importESModule( - "resource://services-settings/SyncHistory.sys.mjs" -); - async function clear_state() { await new SyncHistory("").clear(); } diff --git a/services/settings/test/unit/test_remote_settings_utils.js b/services/settings/test/unit/test_remote_settings_utils.js index de372b3e44..e2b4ddf8e3 100644 --- a/services/settings/test/unit/test_remote_settings_utils.js +++ b/services/settings/test/unit/test_remote_settings_utils.js @@ -1,12 +1,3 @@ -/* import-globals-from ../../../common/tests/unit/head_helpers.js */ - -const { TestUtils } = ChromeUtils.importESModule( - "resource://testing-common/TestUtils.sys.mjs" -); -const { Utils } = ChromeUtils.importESModule( - "resource://services-settings/Utils.sys.mjs" -); - const BinaryOutputStream = Components.Constructor( "@mozilla.org/binaryoutputstream;1", "nsIBinaryOutputStream", diff --git a/services/settings/test/unit/test_remote_settings_utils_telemetry.js b/services/settings/test/unit/test_remote_settings_utils_telemetry.js index f12b0d7f9a..87b72c5f4d 100644 --- a/services/settings/test/unit/test_remote_settings_utils_telemetry.js +++ b/services/settings/test/unit/test_remote_settings_utils_telemetry.js @@ -3,17 +3,9 @@ "use strict"; -/* import-globals-from ../../../common/tests/unit/head_helpers.js */ - const { TelemetryController } = ChromeUtils.importESModule( "resource://gre/modules/TelemetryController.sys.mjs" ); -const { TelemetryTestUtils } = ChromeUtils.importESModule( - "resource://testing-common/TelemetryTestUtils.sys.mjs" -); -const { Utils } = ChromeUtils.importESModule( - "resource://services-settings/Utils.sys.mjs" -); const server = new HttpServer(); server.start(-1); @@ -36,7 +28,7 @@ async function assertTelemetryEvents(expectedEvents) { }); } -add_task(async function setup() { +add_setup(async () => { await TelemetryController.testSetup(); }); diff --git a/services/settings/test/unit/test_remote_settings_worker.js b/services/settings/test/unit/test_remote_settings_worker.js index e2dcdb0063..083beb1a73 100644 --- a/services/settings/test/unit/test_remote_settings_worker.js +++ b/services/settings/test/unit/test_remote_settings_worker.js @@ -1,22 +1,3 @@ -/* import-globals-from ../../../common/tests/unit/head_helpers.js */ - -const { AppConstants } = ChromeUtils.importESModule( - "resource://gre/modules/AppConstants.sys.mjs" -); -const { TestUtils } = ChromeUtils.importESModule( - "resource://testing-common/TestUtils.sys.mjs" -); - -const { RemoteSettingsWorker } = ChromeUtils.importESModule( - "resource://services-settings/RemoteSettingsWorker.sys.mjs" -); -const { RemoteSettingsClient } = ChromeUtils.importESModule( - "resource://services-settings/RemoteSettingsClient.sys.mjs" -); -const { Database } = ChromeUtils.importESModule( - "resource://services-settings/Database.sys.mjs" -); - const IS_ANDROID = AppConstants.platform == "android"; add_task(async function test_canonicaljson() { diff --git a/services/settings/test/unit/test_shutdown_handling.js b/services/settings/test/unit/test_shutdown_handling.js index 2c98f0ab9b..418b25a62d 100644 --- a/services/settings/test/unit/test_shutdown_handling.js +++ b/services/settings/test/unit/test_shutdown_handling.js @@ -3,20 +3,6 @@ http://creativecommons.org/publicdomain/zero/1.0/ */ "use strict"; -const { TestUtils } = ChromeUtils.importESModule( - "resource://testing-common/TestUtils.sys.mjs" -); - -const { Database } = ChromeUtils.importESModule( - "resource://services-settings/Database.sys.mjs" -); -const { RemoteSettingsWorker } = ChromeUtils.importESModule( - "resource://services-settings/RemoteSettingsWorker.sys.mjs" -); -const { RemoteSettingsClient } = ChromeUtils.importESModule( - "resource://services-settings/RemoteSettingsClient.sys.mjs" -); - add_task(async function test_shutdown_abort_after_start() { // Start a forever transaction: let counter = 0; diff --git a/services/settings/test/unit/xpcshell.toml b/services/settings/test/unit/xpcshell.toml index b305ea39f3..728767e604 100644 --- a/services/settings/test/unit/xpcshell.toml +++ b/services/settings/test/unit/xpcshell.toml @@ -1,5 +1,5 @@ [DEFAULT] -head = "../../../common/tests/unit/head_global.js ../../../common/tests/unit/head_helpers.js" +head = "../../../common/tests/unit/head_global.js ../../../common/tests/unit/head_helpers.js head_settings.js" firefox-appdir = "browser" tags = "remote-settings" support-files = ["test_remote_settings_signatures/**"] -- cgit v1.2.3