Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 1 | // 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 Cutler | 39fa227e | 2021-08-10 22:40:04 | [diff] [blame] | 9 | #include "base/test/scoped_feature_list.h" |
| 10 | #include "net/base/features.h" |
Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 11 | #include "net/cookies/cookie_constants.h" |
| 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
| 14 | namespace net { |
| 15 | |
Dylan Cutler | 39fa227e | 2021-08-10 22:40:04 | [diff] [blame] | 16 | class 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 | |
| 31 | INSTANTIATE_TEST_SUITE_P(/* no label */, |
| 32 | CookiePartitionKeyTest, |
| 33 | testing::Bool()); |
| 34 | |
| 35 | TEST_P(CookiePartitionKeyTest, Serialization) { |
Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 36 | 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 Cutler | c68e593e | 2021-09-15 22:07:27 | [diff] [blame] | 66 | 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 Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Dylan Cutler | 39fa227e | 2021-08-10 22:40:04 | [diff] [blame] | 78 | TEST_P(CookiePartitionKeyTest, Deserialization) { |
Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 79 | 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 Cutler | c68e593e | 2021-09-15 22:07:27 | [diff] [blame] | 93 | 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 Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 102 | } else { |
Dylan Cutler | c68e593e | 2021-09-15 22:07:27 | [diff] [blame] | 103 | // 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 Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
Dylan Cutler | 39fa227e | 2021-08-10 22:40:04 | [diff] [blame] | 111 | TEST_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 Cutler | 4a461f1b | 2021-11-03 01:08:36 | [diff] [blame] | 124 | EXPECT_FALSE(got->from_script()); |
Dylan Cutler | ab0b573 | 2021-09-17 00:28:34 | [diff] [blame] | 125 | EXPECT_EQ(CookiePartitionKey::FromURLForTesting(top_level_site.GetURL()), |
| 126 | got.value()); |
Dylan Cutler | 39fa227e | 2021-08-10 22:40:04 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Dylan Cutler | ab0b573 | 2021-09-17 00:28:34 | [diff] [blame] | 130 | TEST_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 Cutler | 4a461f1b | 2021-11-03 01:08:36 | [diff] [blame] | 134 | EXPECT_FALSE(got.from_script()); |
| 135 | } |
| 136 | |
| 137 | TEST_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 Cutler | ab0b573 | 2021-09-17 00:28:34 | [diff] [blame] | 142 | } |
| 143 | |
Dylan Cutler | 46cb8a9 | 2021-11-17 23:29:56 | [diff] [blame^] | 144 | TEST_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 Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 151 | } // namespace net |
| 152 | |
| 153 | #endif // NET_COOKIES_COOKIE_PARTITION_KEY_UNITTEST_H_ |