summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/test/gtest
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /toolkit/components/antitracking/test/gtest
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/antitracking/test/gtest')
-rw-r--r--toolkit/components/antitracking/test/gtest/TestPartitioningExceptionList.cpp184
-rw-r--r--toolkit/components/antitracking/test/gtest/TestStoragePrincipalHelper.cpp215
-rw-r--r--toolkit/components/antitracking/test/gtest/TestURLQueryStringStripper.cpp176
-rw-r--r--toolkit/components/antitracking/test/gtest/moz.build19
4 files changed, 594 insertions, 0 deletions
diff --git a/toolkit/components/antitracking/test/gtest/TestPartitioningExceptionList.cpp b/toolkit/components/antitracking/test/gtest/TestPartitioningExceptionList.cpp
new file mode 100644
index 0000000000..1aa0614622
--- /dev/null
+++ b/toolkit/components/antitracking/test/gtest/TestPartitioningExceptionList.cpp
@@ -0,0 +1,184 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "gtest/gtest.h"
+
+#include "mozilla/Preferences.h"
+#include "mozilla/PartitioningExceptionList.h"
+
+using namespace mozilla;
+
+static const char kPrefPartitioningExceptionList[] =
+ "privacy.restrict3rdpartystorage.skip_list";
+
+static const char kPrefEnableWebcompat[] =
+ "privacy.antitracking.enableWebcompat";
+
+TEST(TestPartitioningExceptionList, TestPrefBasic)
+{
+ nsAutoCString oldPartitioningExceptionList;
+ Preferences::GetCString(kPrefEnableWebcompat, oldPartitioningExceptionList);
+ bool oldEnableWebcompat = Preferences::GetBool(kPrefEnableWebcompat);
+
+ for (uint32_t populateList = 0; populateList <= 1; populateList++) {
+ for (uint32_t enableWebcompat = 0; enableWebcompat <= 1;
+ enableWebcompat++) {
+ if (populateList) {
+ Preferences::SetCString(kPrefPartitioningExceptionList,
+ "https://example.com,https://example.net");
+ } else {
+ Preferences::SetCString(kPrefPartitioningExceptionList, "");
+ }
+
+ Preferences::SetBool(kPrefEnableWebcompat, enableWebcompat);
+
+ EXPECT_FALSE(
+ PartitioningExceptionList::Check(""_ns, "https://example.net"_ns));
+ EXPECT_FALSE(
+ PartitioningExceptionList::Check("https://example.com"_ns, ""_ns));
+ EXPECT_FALSE(PartitioningExceptionList::Check(""_ns, ""_ns));
+ EXPECT_FALSE(PartitioningExceptionList::Check("https://example.net"_ns,
+ "https://example.com"_ns));
+ EXPECT_FALSE(PartitioningExceptionList::Check("https://example.com"_ns,
+ "https://example.org"_ns));
+ EXPECT_FALSE(PartitioningExceptionList::Check("https://example.com"_ns,
+ "https://example.com"_ns));
+ EXPECT_FALSE(PartitioningExceptionList::Check("http://example.com"_ns,
+ "http://example.net"_ns));
+ EXPECT_FALSE(PartitioningExceptionList::Check("https://example.com"_ns,
+ "http://example.net"_ns));
+ EXPECT_FALSE(PartitioningExceptionList::Check("https://example.com."_ns,
+ "https://example.net"_ns));
+
+ bool result = PartitioningExceptionList::Check("https://example.com"_ns,
+ "https://example.net"_ns);
+ EXPECT_TRUE(result == (populateList && enableWebcompat));
+ }
+ }
+
+ Preferences::SetCString(kPrefPartitioningExceptionList,
+ oldPartitioningExceptionList);
+ Preferences::SetBool(kPrefEnableWebcompat, oldEnableWebcompat);
+}
+
+TEST(TestPartitioningExceptionList, TestPrefWildcard)
+{
+ nsAutoCString oldPartitioningExceptionList;
+ Preferences::GetCString(kPrefEnableWebcompat, oldPartitioningExceptionList);
+ bool oldEnableWebcompat = Preferences::GetBool(kPrefEnableWebcompat);
+
+ Preferences::SetCString(kPrefPartitioningExceptionList,
+ "https://example.com,https://example.net;"
+ "https://*.foo.com,https://bar.com;"
+ "https://*.foo.com,https://foobar.net;"
+ "https://test.net,https://*.example.com;"
+ "https://test.com,https://*.example.com;"
+ "https://*.test2.org,*;"
+ "*,http://notatracker.org");
+
+ Preferences::SetBool(kPrefEnableWebcompat, true);
+
+ EXPECT_TRUE(PartitioningExceptionList::Check("https://example.com"_ns,
+ "https://example.net"_ns));
+
+ EXPECT_TRUE(PartitioningExceptionList::Check("https://two.foo.com"_ns,
+ "https://bar.com"_ns));
+ EXPECT_TRUE(PartitioningExceptionList::Check("https://another.foo.com"_ns,
+ "https://bar.com"_ns));
+ EXPECT_TRUE(PartitioningExceptionList::Check("https://three.two.foo.com"_ns,
+ "https://bar.com"_ns));
+ EXPECT_FALSE(PartitioningExceptionList::Check("https://two.foo.com"_ns,
+ "https://example.com"_ns));
+ EXPECT_FALSE(PartitioningExceptionList::Check("https://foo.com"_ns,
+ "https://bar.com"_ns));
+ EXPECT_FALSE(PartitioningExceptionList::Check("https://two.foo.com"_ns,
+ "http://bar.com"_ns));
+ EXPECT_FALSE(PartitioningExceptionList::Check("http://two.foo.com"_ns,
+ "https://bar.com"_ns));
+
+ EXPECT_TRUE(PartitioningExceptionList::Check("https://a.foo.com"_ns,
+ "https://foobar.net"_ns));
+
+ EXPECT_TRUE(PartitioningExceptionList::Check("https://test.net"_ns,
+ "https://test.example.com"_ns));
+ EXPECT_TRUE(PartitioningExceptionList::Check(
+ "https://test.net"_ns, "https://foo.bar.example.com"_ns));
+ EXPECT_FALSE(PartitioningExceptionList::Check("https://test.com"_ns,
+ "https://foo.test.net"_ns));
+
+ EXPECT_TRUE(PartitioningExceptionList::Check("https://one.test2.org"_ns,
+ "https://example.net"_ns));
+ EXPECT_TRUE(PartitioningExceptionList::Check("https://two.test2.org"_ns,
+ "https://foo.example.net"_ns));
+ EXPECT_TRUE(PartitioningExceptionList::Check("https://three.test2.org"_ns,
+ "http://example.net"_ns));
+ EXPECT_TRUE(PartitioningExceptionList::Check("https://four.sub.test2.org"_ns,
+ "https://bar.com"_ns));
+ EXPECT_FALSE(PartitioningExceptionList::Check("https://four.sub.test2.com"_ns,
+ "https://bar.com"_ns));
+ EXPECT_FALSE(PartitioningExceptionList::Check("http://four.sub.test2.org"_ns,
+ "https://bar.com"_ns));
+ EXPECT_FALSE(PartitioningExceptionList::Check(
+ "https://four.sub.test2.org."_ns, "https://bar.com"_ns));
+
+ EXPECT_TRUE(PartitioningExceptionList::Check("https://example.com"_ns,
+ "http://notatracker.org"_ns));
+
+ Preferences::SetCString(kPrefPartitioningExceptionList,
+ oldPartitioningExceptionList);
+ Preferences::SetBool(kPrefEnableWebcompat, oldEnableWebcompat);
+}
+
+TEST(TestPartitioningExceptionList, TestInvalidEntries)
+{
+ nsAutoCString oldPartitioningExceptionList;
+ Preferences::GetCString(kPrefEnableWebcompat, oldPartitioningExceptionList);
+ bool oldEnableWebcompat = Preferences::GetBool(kPrefEnableWebcompat);
+
+ Preferences::SetBool(kPrefEnableWebcompat, true);
+
+ // Empty entries.
+ Preferences::SetCString(kPrefPartitioningExceptionList, ";;;,;");
+
+ EXPECT_FALSE(PartitioningExceptionList::Check("https://example.com"_ns,
+ "https://example.net"_ns));
+
+ // Schemeless entries.
+ Preferences::SetCString(kPrefPartitioningExceptionList,
+ "example.com,example.net");
+
+ EXPECT_FALSE(PartitioningExceptionList::Check("https://example.com"_ns,
+ "https://example.net"_ns));
+
+ // Invalid entry should be skipped and not break other entries.
+ Preferences::SetCString(kPrefPartitioningExceptionList,
+ "*,*;"
+ "https://example.com,https://example.net;"
+ "http://example.org,");
+
+ EXPECT_TRUE(PartitioningExceptionList::Check("https://example.com"_ns,
+ "https://example.net"_ns));
+ EXPECT_FALSE(PartitioningExceptionList::Check("https://foo.com"_ns,
+ "https://bar.net"_ns));
+
+ // Unsupported schemes should not be accepted.
+ Preferences::SetCString(kPrefPartitioningExceptionList,
+ "ftp://example.com,ftp://example.net;");
+
+ EXPECT_FALSE(PartitioningExceptionList::Check("https://example.com"_ns,
+ "https://example.net"_ns));
+ EXPECT_FALSE(PartitioningExceptionList::Check("ftp://example.com"_ns,
+ "ftp://example.net"_ns));
+
+ // Test invalid origins with trailing '/'.
+ Preferences::SetCString(kPrefPartitioningExceptionList,
+ "https://example.com/,https://example.net/");
+ EXPECT_FALSE(PartitioningExceptionList::Check("https://example.com"_ns,
+ "https://example.net"_ns));
+
+ Preferences::SetCString(kPrefPartitioningExceptionList,
+ oldPartitioningExceptionList);
+ Preferences::SetBool(kPrefEnableWebcompat, oldEnableWebcompat);
+}
diff --git a/toolkit/components/antitracking/test/gtest/TestStoragePrincipalHelper.cpp b/toolkit/components/antitracking/test/gtest/TestStoragePrincipalHelper.cpp
new file mode 100644
index 0000000000..44959e4dc8
--- /dev/null
+++ b/toolkit/components/antitracking/test/gtest/TestStoragePrincipalHelper.cpp
@@ -0,0 +1,215 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "gtest/gtest.h"
+
+#include "nsCOMPtr.h"
+#include "nsContentUtils.h"
+#include "nsIChannel.h"
+#include "nsIContentPolicy.h"
+#include "nsICookieJarSettings.h"
+#include "nsILoadInfo.h"
+#include "nsIURI.h"
+#include "nsNetUtil.h"
+#include "nsStringFwd.h"
+
+#include "mozilla/gtest/MozAssertions.h"
+#include "mozilla/NullPrincipal.h"
+#include "mozilla/Preferences.h"
+#include "mozilla/RefPtr.h"
+#include "mozilla/StoragePrincipalHelper.h"
+
+using mozilla::Preferences;
+using namespace mozilla;
+
+/**
+ * Creates a test channel with CookieJarSettings which have a partitionKey set.
+ */
+nsresult CreateMockChannel(nsIPrincipal* aPrincipal, bool isThirdParty,
+ const nsACString& aPartitionKey,
+ nsIChannel** aChannel,
+ nsICookieJarSettings** aCookieJarSettings) {
+ nsCOMPtr<nsIURI> mockUri;
+ nsresult rv = NS_NewURI(getter_AddRefs(mockUri), "http://example.com"_ns);
+ NS_ENSURE_SUCCESS(rv, rv);
+
+ nsCOMPtr<nsIChannel> mockChannel;
+ nsCOMPtr<nsIIOService> service = do_GetIOService(&rv);
+ NS_ENSURE_SUCCESS(rv, rv);
+
+ rv = service->NewChannelFromURI(mockUri, nullptr, aPrincipal, aPrincipal, 0,
+ nsContentPolicyType::TYPE_OTHER,
+ getter_AddRefs(mockChannel));
+ NS_ENSURE_SUCCESS(rv, rv);
+
+ nsCOMPtr<nsILoadInfo> mockLoadInfo = mockChannel->LoadInfo();
+ rv = mockLoadInfo->SetIsThirdPartyContextToTopWindow(isThirdParty);
+ NS_ENSURE_SUCCESS(rv, rv);
+
+ nsCOMPtr<nsICookieJarSettings> cjs;
+ rv = mockLoadInfo->GetCookieJarSettings(getter_AddRefs(cjs));
+ NS_ENSURE_SUCCESS(rv, rv);
+
+ nsCOMPtr<nsIURI> partitionKeyUri;
+ rv = NS_NewURI(getter_AddRefs(partitionKeyUri), aPartitionKey);
+ NS_ENSURE_SUCCESS(rv, rv);
+
+ rv = cjs->InitWithURI(partitionKeyUri, false);
+ NS_ENSURE_SUCCESS(rv, rv);
+
+ cjs.forget(aCookieJarSettings);
+ mockChannel.forget(aChannel);
+ return NS_OK;
+}
+
+TEST(TestStoragePrincipalHelper, TestCreateContentPrincipal)
+{
+ nsCOMPtr<nsIPrincipal> contentPrincipal =
+ BasePrincipal::CreateContentPrincipal("https://example.com"_ns);
+ EXPECT_TRUE(contentPrincipal);
+
+ nsCOMPtr<nsIChannel> mockChannel;
+ nsCOMPtr<nsICookieJarSettings> cookieJarSettings;
+ nsresult rv = CreateMockChannel(
+ contentPrincipal, false, "https://example.org"_ns,
+ getter_AddRefs(mockChannel), getter_AddRefs(cookieJarSettings));
+ ASSERT_EQ(rv, NS_OK) << "Could not create a mock channel";
+
+ nsCOMPtr<nsIPrincipal> storagePrincipal;
+ rv = StoragePrincipalHelper::Create(mockChannel, contentPrincipal, true,
+ getter_AddRefs(storagePrincipal));
+ ASSERT_EQ(rv, NS_OK) << "Should not fail for ContentPrincipal";
+ EXPECT_TRUE(storagePrincipal);
+
+ nsCOMPtr<nsIPrincipal> storagePrincipalSW;
+ rv = StoragePrincipalHelper::CreatePartitionedPrincipalForServiceWorker(
+ contentPrincipal, cookieJarSettings, getter_AddRefs(storagePrincipalSW));
+ ASSERT_EQ(rv, NS_OK) << "Should not fail for ContentPrincipal";
+ EXPECT_TRUE(storagePrincipalSW);
+}
+
+TEST(TestStoragePrincipalHelper, TestCreateNullPrincipal)
+{
+ RefPtr<NullPrincipal> nullPrincipal =
+ NullPrincipal::CreateWithoutOriginAttributes();
+ EXPECT_TRUE(nullPrincipal);
+
+ nsCOMPtr<nsIChannel> mockChannel;
+ nsCOMPtr<nsICookieJarSettings> cookieJarSettings;
+ nsresult rv = CreateMockChannel(
+ nullPrincipal, false, "https://example.org"_ns,
+ getter_AddRefs(mockChannel), getter_AddRefs(cookieJarSettings));
+ ASSERT_EQ(rv, NS_OK) << "Could not create a mock channel";
+
+ nsCOMPtr<nsIPrincipal> storagePrincipal;
+ rv = StoragePrincipalHelper::Create(mockChannel, nullPrincipal, true,
+ getter_AddRefs(storagePrincipal));
+ EXPECT_NS_FAILED(rv) << "Should fail for NullPrincipal";
+ EXPECT_FALSE(storagePrincipal);
+
+ nsCOMPtr<nsIPrincipal> storagePrincipalSW;
+ rv = StoragePrincipalHelper::CreatePartitionedPrincipalForServiceWorker(
+ nullPrincipal, cookieJarSettings, getter_AddRefs(storagePrincipalSW));
+ EXPECT_NS_FAILED(rv) << "Should fail for NullPrincipal";
+ EXPECT_FALSE(storagePrincipal);
+}
+
+TEST(TestStoragePrincipalHelper, TestGetPrincipalCookieBehavior4)
+{
+ Preferences::SetInt("network.cookie.cookieBehavior", 4);
+
+ nsCOMPtr<nsIPrincipal> contentPrincipal =
+ BasePrincipal::CreateContentPrincipal("https://example.com"_ns);
+ EXPECT_TRUE(contentPrincipal);
+
+ for (auto isThirdParty : {false, true}) {
+ nsCOMPtr<nsIChannel> mockChannel;
+ nsCOMPtr<nsICookieJarSettings> cookieJarSettings;
+ nsresult rv = CreateMockChannel(
+ contentPrincipal, isThirdParty, "https://example.org"_ns,
+ getter_AddRefs(mockChannel), getter_AddRefs(cookieJarSettings));
+ ASSERT_EQ(rv, NS_OK) << "Could not create a mock channel";
+
+ nsCOMPtr<nsIPrincipal> testPrincipal;
+ rv = StoragePrincipalHelper::GetPrincipal(
+ mockChannel, StoragePrincipalHelper::eRegularPrincipal,
+ getter_AddRefs(testPrincipal));
+ ASSERT_EQ(rv, NS_OK) << "Could not get regular principal";
+ EXPECT_TRUE(testPrincipal);
+ EXPECT_TRUE(testPrincipal->OriginAttributesRef().mPartitionKey.IsEmpty());
+
+ rv = StoragePrincipalHelper::GetPrincipal(
+ mockChannel, StoragePrincipalHelper::ePartitionedPrincipal,
+ getter_AddRefs(testPrincipal));
+ ASSERT_EQ(rv, NS_OK) << "Could not get partitioned principal";
+ EXPECT_TRUE(testPrincipal);
+ EXPECT_TRUE(
+ testPrincipal->OriginAttributesRef().mPartitionKey.EqualsLiteral(
+ "(https,example.org)"));
+
+ // We should always get regular principal if the dFPI is disabled.
+ rv = StoragePrincipalHelper::GetPrincipal(
+ mockChannel, StoragePrincipalHelper::eForeignPartitionedPrincipal,
+ getter_AddRefs(testPrincipal));
+ ASSERT_EQ(rv, NS_OK) << "Could not get foreign partitioned principal";
+ EXPECT_TRUE(testPrincipal);
+ EXPECT_TRUE(testPrincipal->OriginAttributesRef().mPartitionKey.IsEmpty());
+
+ // Note that we don't test eStorageAccessPrincipal here because it's hard to
+ // setup the right state for the storage access in gTest.
+ }
+}
+
+TEST(TestStoragePrincipalHelper, TestGetPrincipalCookieBehavior5)
+{
+ Preferences::SetInt("network.cookie.cookieBehavior", 5);
+
+ nsCOMPtr<nsIPrincipal> contentPrincipal =
+ BasePrincipal::CreateContentPrincipal("https://example.com"_ns);
+ EXPECT_TRUE(contentPrincipal);
+
+ for (auto isThirdParty : {false, true}) {
+ nsCOMPtr<nsIChannel> mockChannel;
+ nsCOMPtr<nsICookieJarSettings> cookieJarSettings;
+ nsresult rv = CreateMockChannel(
+ contentPrincipal, isThirdParty, "https://example.org"_ns,
+ getter_AddRefs(mockChannel), getter_AddRefs(cookieJarSettings));
+ ASSERT_EQ(rv, NS_OK) << "Could not create a mock channel";
+
+ nsCOMPtr<nsIPrincipal> testPrincipal;
+ rv = StoragePrincipalHelper::GetPrincipal(
+ mockChannel, StoragePrincipalHelper::eRegularPrincipal,
+ getter_AddRefs(testPrincipal));
+ ASSERT_EQ(rv, NS_OK) << "Could not get regular principal";
+ EXPECT_TRUE(testPrincipal);
+ EXPECT_TRUE(testPrincipal->OriginAttributesRef().mPartitionKey.IsEmpty());
+
+ rv = StoragePrincipalHelper::GetPrincipal(
+ mockChannel, StoragePrincipalHelper::ePartitionedPrincipal,
+ getter_AddRefs(testPrincipal));
+ ASSERT_EQ(rv, NS_OK) << "Could not get partitioned principal";
+ EXPECT_TRUE(testPrincipal);
+ EXPECT_TRUE(
+ testPrincipal->OriginAttributesRef().mPartitionKey.EqualsLiteral(
+ "(https,example.org)"));
+
+ // We should always get regular principal if the dFPI is disabled.
+ rv = StoragePrincipalHelper::GetPrincipal(
+ mockChannel, StoragePrincipalHelper::eForeignPartitionedPrincipal,
+ getter_AddRefs(testPrincipal));
+ ASSERT_EQ(rv, NS_OK) << "Could not get foreign partitioned principal";
+ EXPECT_TRUE(testPrincipal);
+ if (isThirdParty) {
+ EXPECT_TRUE(
+ testPrincipal->OriginAttributesRef().mPartitionKey.EqualsLiteral(
+ "(https,example.org)"));
+ } else {
+ EXPECT_TRUE(testPrincipal->OriginAttributesRef().mPartitionKey.IsEmpty());
+ }
+
+ // Note that we don't test eStorageAccessPrincipal here because it's hard to
+ // setup the right state for the storage access in gTest.
+ }
+}
diff --git a/toolkit/components/antitracking/test/gtest/TestURLQueryStringStripper.cpp b/toolkit/components/antitracking/test/gtest/TestURLQueryStringStripper.cpp
new file mode 100644
index 0000000000..3f77479f74
--- /dev/null
+++ b/toolkit/components/antitracking/test/gtest/TestURLQueryStringStripper.cpp
@@ -0,0 +1,176 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "gtest/gtest.h"
+
+#include "mozilla/Components.h"
+#include "nsIURLQueryStringStripper.h"
+#include "nsIURI.h"
+#include "nsNetUtil.h"
+#include "nsStringFwd.h"
+
+#include "mozilla/Preferences.h"
+#include "mozilla/SpinEventLoopUntil.h"
+#include "mozilla/URLQueryStringStripper.h"
+
+using namespace mozilla;
+
+static const char kPrefQueryStrippingEnabled[] =
+ "privacy.query_stripping.enabled";
+static const char kPrefQueryStrippingEnabledPBM[] =
+ "privacy.query_stripping.enabled.pbmode";
+static const char kPrefQueryStrippingList[] =
+ "privacy.query_stripping.strip_list";
+
+/**
+ * Waits for the strip list in the URLQueryStringStripper to match aExpected.
+ */
+void waitForStripListChange(const nsACString& aExpected) {
+ nsresult rv;
+ nsCOMPtr<nsIURLQueryStringStripper> queryStripper =
+ components::URLQueryStringStripper::Service(&rv);
+ EXPECT_TRUE(NS_SUCCEEDED(rv));
+
+ MOZ_ALWAYS_TRUE(mozilla::SpinEventLoopUntil(
+ "TestURLQueryStringStripper waitForStripListChange"_ns, [&]() -> bool {
+ nsAutoCString stripList;
+ rv = queryStripper->TestGetStripList(stripList);
+ return NS_SUCCEEDED(rv) && stripList.Equals(aExpected);
+ }));
+}
+
+void DoTest(const nsACString& aTestURL, const bool aIsPBM,
+ const nsACString& aExpectedURL, uint32_t aExpectedResult) {
+ nsCOMPtr<nsIURI> testURI;
+
+ NS_NewURI(getter_AddRefs(testURI), aTestURL);
+
+ nsresult rv;
+ nsCOMPtr<nsIURLQueryStringStripper> queryStripper =
+ components::URLQueryStringStripper::Service(&rv);
+ EXPECT_TRUE(NS_SUCCEEDED(rv));
+
+ nsCOMPtr<nsIURI> strippedURI;
+ uint32_t numStripped;
+ rv = queryStripper->Strip(testURI, aIsPBM, getter_AddRefs(strippedURI),
+ &numStripped);
+ EXPECT_TRUE(NS_SUCCEEDED(rv));
+
+ EXPECT_TRUE(numStripped == aExpectedResult);
+
+ if (!numStripped) {
+ EXPECT_TRUE(!strippedURI);
+ } else {
+ EXPECT_TRUE(strippedURI->GetSpecOrDefault().Equals(aExpectedURL));
+ }
+}
+
+TEST(TestURLQueryStringStripper, TestPrefDisabled)
+{
+ // Disable the query string stripping by the pref and make sure the stripping
+ // is disabled.
+ // Note that we don't need to run a dummy test to create the
+ // URLQueryStringStripper here because the stripper will never be created if
+ // the query stripping is disabled.
+ Preferences::SetCString(kPrefQueryStrippingList, "fooBar foobaz");
+ Preferences::SetBool(kPrefQueryStrippingEnabled, false);
+ Preferences::SetBool(kPrefQueryStrippingEnabledPBM, false);
+
+ for (bool isPBM : {false, true}) {
+ DoTest("https://example.com/"_ns, isPBM, ""_ns, 0);
+ DoTest("https://example.com/?Barfoo=123"_ns, isPBM, ""_ns, 0);
+ DoTest("https://example.com/?fooBar=123&foobaz"_ns, isPBM, ""_ns, 0);
+ }
+}
+
+TEST(TestURLQueryStringStripper, TestEmptyStripList)
+{
+ // Make sure there is no error if the strip list is empty.
+ Preferences::SetBool(kPrefQueryStrippingEnabled, true);
+ Preferences::SetBool(kPrefQueryStrippingEnabledPBM, true);
+
+ // To create the URLQueryStringStripper, we need to run a dummy test after
+ // the query stripping is enabled. By doing this, the stripper will be
+ // initiated and we are good to test.
+ DoTest("https://example.com/"_ns, false, ""_ns, 0);
+
+ // Set the strip list to empty and wait until the pref setting is set to the
+ // stripper.
+ Preferences::SetCString(kPrefQueryStrippingList, "");
+
+ waitForStripListChange(""_ns);
+
+ for (bool isPBM : {false, true}) {
+ DoTest("https://example.com/"_ns, isPBM, ""_ns, 0);
+ DoTest("https://example.com/?Barfoo=123"_ns, isPBM, ""_ns, 0);
+ DoTest("https://example.com/?fooBar=123&foobaz"_ns, isPBM, ""_ns, 0);
+ }
+}
+
+TEST(TestURLQueryStringStripper, TestStripping)
+{
+ Preferences::SetBool(kPrefQueryStrippingEnabled, true);
+ Preferences::SetBool(kPrefQueryStrippingEnabledPBM, true);
+ DoTest("https://example.com/"_ns, false, ""_ns, 0);
+
+ Preferences::SetCString(kPrefQueryStrippingList, "fooBar foobaz");
+ waitForStripListChange("foobar foobaz"_ns);
+
+ // Test all pref combinations.
+ for (bool pref : {false, true}) {
+ for (bool prefPBM : {false, true}) {
+ Preferences::SetBool(kPrefQueryStrippingEnabled, pref);
+ Preferences::SetBool(kPrefQueryStrippingEnabledPBM, prefPBM);
+
+ // If the service is enabled with the given pref config we need for the
+ // list changes to propagate as they happen async.
+ if (pref || prefPBM) {
+ waitForStripListChange("foobar foobaz"_ns);
+ }
+
+ // Test with normal and private browsing mode.
+ for (bool isPBM : {false, true}) {
+ bool expectStrip = (prefPBM && isPBM) || (pref && !isPBM);
+
+ DoTest("https://example.com/"_ns, isPBM, ""_ns, 0);
+ DoTest("https://example.com/?Barfoo=123"_ns, isPBM, ""_ns, 0);
+
+ DoTest("https://example.com/?fooBar=123"_ns, isPBM,
+ "https://example.com/"_ns, expectStrip ? 1 : 0);
+ DoTest("https://example.com/?fooBar=123&foobaz"_ns, isPBM,
+ "https://example.com/"_ns, expectStrip ? 2 : 0);
+ DoTest("https://example.com/?fooBar=123&Barfoo=456&foobaz"_ns, isPBM,
+ "https://example.com/?Barfoo=456"_ns, expectStrip ? 2 : 0);
+
+ DoTest("https://example.com/?FOOBAR=123"_ns, isPBM,
+ "https://example.com/"_ns, expectStrip ? 1 : 0);
+ DoTest("https://example.com/?barfoo=foobar"_ns, isPBM,
+ "https://example.com/?barfoo=foobar"_ns, 0);
+ DoTest("https://example.com/?foobar=123&nostrip=456&FooBar=789"_ns,
+ isPBM, "https://example.com/?nostrip=456"_ns,
+ expectStrip ? 2 : 0);
+ DoTest("https://example.com/?AfoobazB=123"_ns, isPBM,
+ "https://example.com/?AfoobazB=123"_ns, 0);
+ }
+ }
+ }
+
+ // Change the strip list pref to see if it is updated properly.
+ // We test this in normal browsing, so set the prefs accordingly.
+ Preferences::SetBool(kPrefQueryStrippingEnabled, true);
+ Preferences::SetBool(kPrefQueryStrippingEnabledPBM, false);
+
+ Preferences::SetCString(kPrefQueryStrippingList, "Barfoo bazfoo");
+
+ waitForStripListChange("barfoo bazfoo"_ns);
+
+ DoTest("https://example.com/?fooBar=123"_ns, false, ""_ns, 0);
+ DoTest("https://example.com/?fooBar=123&foobaz"_ns, false, ""_ns, 0);
+
+ DoTest("https://example.com/?bazfoo=123"_ns, false, "https://example.com/"_ns,
+ 1);
+ DoTest("https://example.com/?fooBar=123&Barfoo=456&foobaz=abc"_ns, false,
+ "https://example.com/?fooBar=123&foobaz=abc"_ns, 1);
+}
diff --git a/toolkit/components/antitracking/test/gtest/moz.build b/toolkit/components/antitracking/test/gtest/moz.build
new file mode 100644
index 0000000000..a7e8595c5e
--- /dev/null
+++ b/toolkit/components/antitracking/test/gtest/moz.build
@@ -0,0 +1,19 @@
+# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+UNIFIED_SOURCES += [
+ "TestPartitioningExceptionList.cpp",
+ "TestStoragePrincipalHelper.cpp",
+ "TestURLQueryStringStripper.cpp",
+]
+
+LOCAL_INCLUDES += [
+ "/xpcom/tests/gtest",
+]
+
+include("/ipc/chromium/chromium-config.mozbuild")
+
+FINAL_LIBRARY = "xul-gtest"