From 086c044dc34dfc0f74fbe41f4ecb402b2cd34884 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:13:33 +0200 Subject: Merging upstream version 125.0.1. Signed-off-by: Daniel Baumann --- dom/quota/test/gtest/TestScopedLogExtraInfo.cpp | 31 +++- .../test/gtest/TestStorageOriginAttributes.cpp | 75 +++++++++ .../test/modules/system/worker/ModuleLoader.js | 2 +- .../test/xpcshell/telemetry/test_dom_quota_try.js | 180 +++++++++++++++++++++ dom/quota/test/xpcshell/telemetry/xpcshell.toml | 3 + 5 files changed, 282 insertions(+), 9 deletions(-) create mode 100644 dom/quota/test/xpcshell/telemetry/test_dom_quota_try.js (limited to 'dom/quota/test') diff --git a/dom/quota/test/gtest/TestScopedLogExtraInfo.cpp b/dom/quota/test/gtest/TestScopedLogExtraInfo.cpp index 00a3393844..395246c1fb 100644 --- a/dom/quota/test/gtest/TestScopedLogExtraInfo.cpp +++ b/dom/quota/test/gtest/TestScopedLogExtraInfo.cpp @@ -16,19 +16,23 @@ TEST(DOM_Quota_ScopedLogExtraInfo, AddAndRemove) { const auto extraInfo = - ScopedLogExtraInfo{ScopedLogExtraInfo::kTagQuery, text}; + ScopedLogExtraInfo{ScopedLogExtraInfo::kTagQueryTainted, text}; #ifdef QM_SCOPED_LOG_EXTRA_INFO_ENABLED const auto& extraInfoMap = ScopedLogExtraInfo::GetExtraInfoMap(); - EXPECT_EQ(text, *extraInfoMap.at(ScopedLogExtraInfo::kTagQuery)); + const auto& queryValueTainted = + *extraInfoMap.at(ScopedLogExtraInfo::kTagQueryTainted); + + EXPECT_EQ(text, MOZ_NO_VALIDATE(queryValueTainted, + "It's ok to use query value in tests.")); #endif } #ifdef QM_SCOPED_LOG_EXTRA_INFO_ENABLED const auto& extraInfoMap = ScopedLogExtraInfo::GetExtraInfoMap(); - EXPECT_EQ(0u, extraInfoMap.count(ScopedLogExtraInfo::kTagQuery)); + EXPECT_EQ(0u, extraInfoMap.count(ScopedLogExtraInfo::kTagQueryTainted)); #endif } @@ -39,27 +43,38 @@ TEST(DOM_Quota_ScopedLogExtraInfo, Nested) { const auto extraInfo = - ScopedLogExtraInfo{ScopedLogExtraInfo::kTagQuery, text}; + ScopedLogExtraInfo{ScopedLogExtraInfo::kTagQueryTainted, text}; { const auto extraInfo = - ScopedLogExtraInfo{ScopedLogExtraInfo::kTagQuery, nestedText}; + ScopedLogExtraInfo{ScopedLogExtraInfo::kTagQueryTainted, nestedText}; #ifdef QM_SCOPED_LOG_EXTRA_INFO_ENABLED const auto& extraInfoMap = ScopedLogExtraInfo::GetExtraInfoMap(); - EXPECT_EQ(nestedText, *extraInfoMap.at(ScopedLogExtraInfo::kTagQuery)); + + const auto& queryValueTainted = + *extraInfoMap.at(ScopedLogExtraInfo::kTagQueryTainted); + + EXPECT_EQ(nestedText, + MOZ_NO_VALIDATE(queryValueTainted, + "It's ok to use query value in tests.")); #endif } #ifdef QM_SCOPED_LOG_EXTRA_INFO_ENABLED const auto& extraInfoMap = ScopedLogExtraInfo::GetExtraInfoMap(); - EXPECT_EQ(text, *extraInfoMap.at(ScopedLogExtraInfo::kTagQuery)); + + const auto& queryValueTainted = + *extraInfoMap.at(ScopedLogExtraInfo::kTagQueryTainted); + + EXPECT_EQ(text, MOZ_NO_VALIDATE(queryValueTainted, + "It's ok to use query value in tests.")); #endif } #ifdef QM_SCOPED_LOG_EXTRA_INFO_ENABLED const auto& extraInfoMap = ScopedLogExtraInfo::GetExtraInfoMap(); - EXPECT_EQ(0u, extraInfoMap.count(ScopedLogExtraInfo::kTagQuery)); + EXPECT_EQ(0u, extraInfoMap.count(ScopedLogExtraInfo::kTagQueryTainted)); #endif } diff --git a/dom/quota/test/gtest/TestStorageOriginAttributes.cpp b/dom/quota/test/gtest/TestStorageOriginAttributes.cpp index 4529e1d53b..f7a8387305 100644 --- a/dom/quota/test/gtest/TestStorageOriginAttributes.cpp +++ b/dom/quota/test/gtest/TestStorageOriginAttributes.cpp @@ -9,6 +9,33 @@ namespace mozilla::dom::quota::test { +TEST(DOM_Quota_StorageOriginAttributes, Constructor_Default) +{ + { + StorageOriginAttributes originAttributes; + + ASSERT_FALSE(originAttributes.InIsolatedMozBrowser()); + ASSERT_EQ(originAttributes.UserContextId(), 0u); + } +} + +TEST(DOM_Quota_StorageOriginAttributes, Constructor_InIsolatedMozbrowser) +{ + { + StorageOriginAttributes originAttributes(/* aInIsolatedMozBrowser */ false); + + ASSERT_FALSE(originAttributes.InIsolatedMozBrowser()); + ASSERT_EQ(originAttributes.UserContextId(), 0u); + } + + { + StorageOriginAttributes originAttributes(/* aInIsolatedMozBrowser */ true); + + ASSERT_TRUE(originAttributes.InIsolatedMozBrowser()); + ASSERT_EQ(originAttributes.UserContextId(), 0u); + } +} + TEST(DOM_Quota_StorageOriginAttributes, PopulateFromOrigin_NoOriginAttributes) { { @@ -162,4 +189,52 @@ TEST(DOM_Quota_StorageOriginAttributes, PopulateFromOrigin_Mixed_Invalid) } } +TEST(DOM_Quota_StorageOriginAttributes, CreateSuffix_NoOriginAttributes) +{ + { + StorageOriginAttributes originAttributes; + nsCString suffix; + originAttributes.CreateSuffix(suffix); + + ASSERT_TRUE(suffix.IsEmpty()); + } +} + +TEST(DOM_Quota_StorageOriginAttributes, CreateSuffix_InIsolatedMozbrowser) +{ + { + StorageOriginAttributes originAttributes; + originAttributes.SetInIsolatedMozBrowser(true); + nsCString suffix; + originAttributes.CreateSuffix(suffix); + + ASSERT_TRUE(suffix.Equals("^inBrowser=1"_ns)); + } +} + +TEST(DOM_Quota_StorageOriginAttributes, CreateSuffix_UserContextId) +{ + { + StorageOriginAttributes originAttributes; + originAttributes.SetUserContextId(42); + nsCString suffix; + originAttributes.CreateSuffix(suffix); + + ASSERT_TRUE(suffix.Equals("^userContextId=42"_ns)); + } +} + +TEST(DOM_Quota_StorageOriginAttributes, CreateSuffix_Mixed) +{ + { + StorageOriginAttributes originAttributes; + originAttributes.SetInIsolatedMozBrowser(true); + originAttributes.SetUserContextId(42); + nsCString suffix; + originAttributes.CreateSuffix(suffix); + + ASSERT_TRUE(suffix.Equals("^inBrowser=1&userContextId=42"_ns)); + } +} + } // namespace mozilla::dom::quota::test diff --git a/dom/quota/test/modules/system/worker/ModuleLoader.js b/dom/quota/test/modules/system/worker/ModuleLoader.js index b79f3ff5b9..5354c26e34 100644 --- a/dom/quota/test/modules/system/worker/ModuleLoader.js +++ b/dom/quota/test/modules/system/worker/ModuleLoader.js @@ -3,7 +3,7 @@ * http://creativecommons.org/publicdomain/zero/1.0/ */ -function ModuleLoader(base, depth, proto) { +function ModuleLoader(base, depth) { const modules = {}; const require = async function (id) { diff --git a/dom/quota/test/xpcshell/telemetry/test_dom_quota_try.js b/dom/quota/test/xpcshell/telemetry/test_dom_quota_try.js new file mode 100644 index 0000000000..28bb2d63b4 --- /dev/null +++ b/dom/quota/test/xpcshell/telemetry/test_dom_quota_try.js @@ -0,0 +1,180 @@ +/** + * Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +const { AppConstants } = ChromeUtils.importESModule( + "resource://gre/modules/AppConstants.sys.mjs" +); + +const { TelemetryTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/TelemetryTestUtils.sys.mjs" +); + +// This is a list of all extra keys that are exposed to telemetry. Please only +// add things to this list with great care and proper code review from relevant +// module owner/peers and proper data review from data stewards. +const allowedExtraKeys = [ + "context", + "frame_id", + "process_id", + "result", + "seq", + "severity", + "source_file", + "source_line", + "stack_id", +]; + +const originSchemes = [ + "http", + "https", + "ftp", + "ws", + "wss", + "gopher", + "blob", + "file", + "data", +]; + +const testcases = [ + // Test temporary storage initialization with and without a broken origin + // directory. + { + async setup(expectedInitResult) { + Services.prefs.setBoolPref("dom.quotaManager.loadQuotaFromCache", false); + + let request = init(); + await requestFinished(request); + + request = initTemporaryStorage(); + await requestFinished(request); + + request = initTemporaryOrigin( + "default", + getPrincipal("https://example.com") + ); + await requestFinished(request); + + const usageFile = getRelativeFile( + "storage/default/https+++example.com/ls/usage" + ); + + if (!expectedInitResult) { + usageFile.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755); + } else { + usageFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666); + } + + // XXX It would be nice to have a method which shuts down temporary + // storage only. Now we have to shut down entire storage (including + // temporary storage) and then initialize storage again. + request = reset(); + await requestFinished(request); + + request = init(); + await requestFinished(request); + }, + initFunction: initTemporaryStorage, + getExpectedNumberOfEvents() { + if (AppConstants.EARLY_BETA_OR_EARLIER || AppConstants.DEBUG) { + if (AppConstants.NIGHTLY_BUILD) { + return { + initFailure: 9, + initSuccess: 0, + }; + } + + return { + initFailure: 14, + initSuccess: 0, + }; + } + + return { + initFailure: 0, + initSuccess: 0, + }; + }, + async cleanup() { + const request = clear(); + await requestFinished(request); + + Services.prefs.setBoolPref("dom.quotaManager.loadQuotaFromCache", true); + }, + }, +]; + +function verifyEvents(expectedNumberOfEvents) { + const events = TelemetryTestUtils.getEvents({ + category: "dom.quota.try", + method: "error", + }); + + is( + events.length, + expectedNumberOfEvents, + "The number of events must match the expected number of events" + ); + + for (const event of events) { + for (const extraKey in event.extra) { + ok( + allowedExtraKeys.includes(extraKey), + `The extra key ${extraKey} must be in the allow list` + ); + + const extraValue = event.extra[extraKey]; + + // These are extra paranoia checks to ensure extra values don't contain + // origin like strings. + for (const suffix of ["://", "+++"]) { + ok( + originSchemes.every( + originScheme => !extraValue.includes(originScheme + suffix) + ), + `The extra value ${extraValue} must not contain origin like strings` + ); + } + } + } +} + +async function testSteps() { + for (const testcase of testcases) { + for (const expectedInitResult of [false, true]) { + // Clear all events. + Services.telemetry.clearEvents(); + + info( + `Verifying the events when the initialization ` + + `${expectedInitResult ? "succeeds" : "fails"}` + ); + + await testcase.setup(expectedInitResult); + + const msg = `Should ${expectedInitResult ? "not " : ""} have thrown`; + + let request = testcase.initFunction(); + try { + await requestFinished(request); + ok(expectedInitResult, msg); + } catch (ex) { + ok(!expectedInitResult, msg); + } + + const expectedNumberOfEventsObject = testcase.getExpectedNumberOfEvents + ? testcase.getExpectedNumberOfEvents() + : testcase.expectedNumberOfEvents; + + const expectedNumberOfEvents = expectedInitResult + ? expectedNumberOfEventsObject.initSuccess + : expectedNumberOfEventsObject.initFailure; + + verifyEvents(expectedNumberOfEvents); + + await testcase.cleanup(); + } + } +} diff --git a/dom/quota/test/xpcshell/telemetry/xpcshell.toml b/dom/quota/test/xpcshell/telemetry/xpcshell.toml index 949ef3cb06..36a176db2a 100644 --- a/dom/quota/test/xpcshell/telemetry/xpcshell.toml +++ b/dom/quota/test/xpcshell/telemetry/xpcshell.toml @@ -13,5 +13,8 @@ support-files = [ "version2_2_profile.zip", ] +["test_dom_quota_try.js"] +skip-if = ["os == 'android' || appname == 'thunderbird'"] + ["test_qm_first_initialization_attempt.js"] skip-if = ["appname == 'thunderbird'"] -- cgit v1.2.3