1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 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->SetIsInThirdPartyContext(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.
}
}
|