blob: b3215debf10dfbc62e22632bbf9ecca92f268a73 [file] [log] [blame]
Dylan Cutler27307332021-08-02 21:00:411// Copyright 2021 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef NET_COOKIES_COOKIE_PARTITION_KEY_UNITTEST_H_
6#define NET_COOKIES_COOKIE_PARTITION_KEY_UNITTEST_H_
7
8#include "net/cookies/cookie_partition_key.h"
Dylan Cutler39fa227e2021-08-10 22:40:049#include "base/test/scoped_feature_list.h"
10#include "net/base/features.h"
Dylan Cutler27307332021-08-02 21:00:4111#include "net/cookies/cookie_constants.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
14namespace net {
15
Dylan Cutler39fa227e2021-08-10 22:40:0416class CookiePartitionKeyTest : public testing::TestWithParam<bool> {
17 protected:
18 // testing::Test
19 void SetUp() override {
20 if (PartitionedCookiesEnabled())
21 scoped_feature_list_.InitAndEnableFeature(features::kPartitionedCookies);
22 testing::TestWithParam<bool>::SetUp();
23 }
24
25 bool PartitionedCookiesEnabled() { return GetParam(); }
26
27 private:
28 base::test::ScopedFeatureList scoped_feature_list_;
29};
30
31INSTANTIATE_TEST_SUITE_P(/* no label */,
32 CookiePartitionKeyTest,
33 testing::Bool());
34
35TEST_P(CookiePartitionKeyTest, Serialization) {
Dylan Cutler27307332021-08-02 21:00:4136 struct {
37 absl::optional<CookiePartitionKey> input;
38 bool expected_ret;
39 std::string expected_output;
40 } cases[] = {
41 // No partition key
42 {absl::nullopt, true, kEmptyCookiePartitionKey},
43 // Partition key present
44 {absl::make_optional(CookiePartitionKey::FromURLForTesting(
45 GURL("https://toplevelsite.com"))),
46 true, "https://toplevelsite.com"},
47 // Local file URL
48 {absl::make_optional(CookiePartitionKey::FromURLForTesting(
49 GURL("file:///path/to/file.txt"))),
50 true, "file://"},
51 // File URL with host
52 {absl::make_optional(CookiePartitionKey::FromURLForTesting(
53 GURL("file://toplevelsite.com/path/to/file.pdf"))),
54 true, "file://toplevelsite.com"},
55 // Opaque origin
56 {absl::make_optional(CookiePartitionKey::FromURLForTesting(GURL())),
57 false, ""},
58 // Invalid partition key
59 {absl::make_optional(
60 CookiePartitionKey::FromURLForTesting(GURL("abc123foobar!!"))),
61 false, ""},
62 };
63
64 for (const auto& tc : cases) {
65 std::string got;
Dylan Cutlerc68e593e2021-09-15 22:07:2766 if (PartitionedCookiesEnabled()) {
67 EXPECT_EQ(tc.expected_ret, CookiePartitionKey::Serialize(tc.input, got));
68 EXPECT_EQ(tc.expected_output, got);
69 } else {
70 // Serialize should only return true for unpartitioned cookies if the
71 // feature is disabled.
72 EXPECT_NE(tc.input.has_value(),
73 CookiePartitionKey::Serialize(tc.input, got));
74 }
Dylan Cutler27307332021-08-02 21:00:4175 }
76}
77
Dylan Cutler39fa227e2021-08-10 22:40:0478TEST_P(CookiePartitionKeyTest, Deserialization) {
Dylan Cutler27307332021-08-02 21:00:4179 struct {
80 std::string input;
81 bool expected_ret;
82 absl::optional<CookiePartitionKey> expected_output;
83 } cases[] = {
84 {kEmptyCookiePartitionKey, true, absl::nullopt},
85 {"https://toplevelsite.com", true,
86 absl::make_optional(CookiePartitionKey::FromURLForTesting(
87 GURL("https://toplevelsite.com")))},
88 {"abc123foobar!!", false, absl::nullopt},
89 };
90
91 for (const auto& tc : cases) {
92 absl::optional<CookiePartitionKey> got;
Dylan Cutlerc68e593e2021-09-15 22:07:2793 if (PartitionedCookiesEnabled()) {
94 EXPECT_EQ(tc.expected_ret,
95 CookiePartitionKey::Deserialize(tc.input, got));
96 if (tc.expected_output.has_value()) {
97 EXPECT_TRUE(got.has_value());
98 EXPECT_EQ(tc.expected_output.value(), got.value());
99 } else {
100 EXPECT_FALSE(got.has_value());
101 }
Dylan Cutler27307332021-08-02 21:00:41102 } else {
Dylan Cutlerc68e593e2021-09-15 22:07:27103 // Deserialize should only return true for unpartitioned cookies if the
104 // feature is disabled.
105 EXPECT_EQ(tc.input == kEmptyCookiePartitionKey,
106 CookiePartitionKey::Deserialize(tc.input, got));
Dylan Cutler27307332021-08-02 21:00:41107 }
108 }
109}
110
Dylan Cutler39fa227e2021-08-10 22:40:04111TEST_P(CookiePartitionKeyTest, FromNetworkIsolationKey) {
112 EXPECT_FALSE(
113 CookiePartitionKey::FromNetworkIsolationKey(NetworkIsolationKey()));
114
115 SchemefulSite top_level_site =
116 SchemefulSite(GURL("https://toplevelsite.com"));
117 absl::optional<CookiePartitionKey> got =
118 CookiePartitionKey::FromNetworkIsolationKey(NetworkIsolationKey(
119 top_level_site, SchemefulSite(GURL("https://cookiesite.com"))));
120
121 bool partitioned_cookies_enabled = PartitionedCookiesEnabled();
122 EXPECT_EQ(partitioned_cookies_enabled, got.has_value());
123 if (partitioned_cookies_enabled) {
Dylan Cutler4a461f1b2021-11-03 01:08:36124 EXPECT_FALSE(got->from_script());
Dylan Cutlerab0b5732021-09-17 00:28:34125 EXPECT_EQ(CookiePartitionKey::FromURLForTesting(top_level_site.GetURL()),
126 got.value());
Dylan Cutler39fa227e2021-08-10 22:40:04127 }
128}
129
Dylan Cutlerab0b5732021-09-17 00:28:34130TEST_P(CookiePartitionKeyTest, FromWire) {
131 auto want = CookiePartitionKey::FromURLForTesting(GURL("https://foo.com"));
132 auto got = CookiePartitionKey::FromWire(want.site());
133 EXPECT_EQ(want, got);
Dylan Cutler4a461f1b2021-11-03 01:08:36134 EXPECT_FALSE(got.from_script());
135}
136
137TEST_P(CookiePartitionKeyTest, FromScript) {
138 auto key = CookiePartitionKey::FromScript();
139 EXPECT_TRUE(key);
140 EXPECT_TRUE(key->from_script());
141 EXPECT_TRUE(key->site().opaque());
Dylan Cutlerab0b5732021-09-17 00:28:34142}
143
Dylan Cutler46cb8a92021-11-17 23:29:56144TEST_P(CookiePartitionKeyTest, IsSerializeable) {
145 EXPECT_FALSE(CookiePartitionKey::FromURLForTesting(GURL()).IsSerializeable());
146 EXPECT_EQ(PartitionedCookiesEnabled(), CookiePartitionKey::FromURLForTesting(
147 GURL("https://www.example.com"))
148 .IsSerializeable());
149}
150
Dylan Cutler27307332021-08-02 21:00:41151} // namespace net
152
153#endif // NET_COOKIES_COOKIE_PARTITION_KEY_UNITTEST_H_