Chris Mumford | d8ed9f8 | 2018-05-01 15:43:13 | [diff] [blame] | 1 | // Copyright 2018 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 | #include "net/cookies/cookie_deletion_info.h" |
| 6 | |
| 7 | #include "testing/gtest/include/gtest/gtest.h" |
| 8 | |
| 9 | namespace net { |
| 10 | |
| 11 | using TimeRange = CookieDeletionInfo::TimeRange; |
| 12 | |
| 13 | TEST(CookieDeletionInfoTest, TimeRangeValues) { |
| 14 | TimeRange range; |
| 15 | EXPECT_EQ(base::Time(), range.start()); |
| 16 | EXPECT_EQ(base::Time(), range.end()); |
| 17 | |
| 18 | const base::Time kTestStart = base::Time::FromDoubleT(1000); |
| 19 | const base::Time kTestEnd = base::Time::FromDoubleT(10000); |
| 20 | |
| 21 | EXPECT_EQ(kTestStart, TimeRange(kTestStart, base::Time()).start()); |
| 22 | EXPECT_EQ(base::Time(), TimeRange(kTestStart, base::Time()).end()); |
| 23 | |
| 24 | EXPECT_EQ(kTestStart, TimeRange(kTestStart, kTestEnd).start()); |
| 25 | EXPECT_EQ(kTestEnd, TimeRange(kTestStart, kTestEnd).end()); |
| 26 | |
| 27 | TimeRange range2; |
| 28 | range2.SetStart(kTestStart); |
| 29 | EXPECT_EQ(kTestStart, range2.start()); |
| 30 | EXPECT_EQ(base::Time(), range2.end()); |
| 31 | range2.SetEnd(kTestEnd); |
| 32 | EXPECT_EQ(kTestStart, range2.start()); |
| 33 | EXPECT_EQ(kTestEnd, range2.end()); |
| 34 | } |
| 35 | |
| 36 | TEST(CookieDeletionInfoTest, TimeRangeContains) { |
| 37 | // Default TimeRange matches all time values. |
| 38 | TimeRange range; |
| 39 | EXPECT_TRUE(range.Contains(base::Time::Now())); |
| 40 | EXPECT_TRUE(range.Contains(base::Time::Max())); |
| 41 | |
| 42 | // With a start, but no end. |
| 43 | const double kTestMinEpoch = 1000; |
| 44 | range.SetStart(base::Time::FromDoubleT(kTestMinEpoch)); |
| 45 | EXPECT_FALSE(range.Contains(base::Time::Min())); |
| 46 | EXPECT_FALSE(range.Contains(base::Time::FromDoubleT(kTestMinEpoch - 1))); |
| 47 | EXPECT_TRUE(range.Contains(base::Time::FromDoubleT(kTestMinEpoch))); |
| 48 | EXPECT_TRUE(range.Contains(base::Time::FromDoubleT(kTestMinEpoch + 1))); |
| 49 | EXPECT_TRUE(range.Contains(base::Time::Max())); |
| 50 | |
| 51 | // With an end, but no start. |
| 52 | const double kTestMaxEpoch = 10000000; |
| 53 | range = TimeRange(); |
| 54 | range.SetEnd(base::Time::FromDoubleT(kTestMaxEpoch)); |
| 55 | EXPECT_TRUE(range.Contains(base::Time::Min())); |
| 56 | EXPECT_TRUE(range.Contains(base::Time::FromDoubleT(kTestMaxEpoch - 1))); |
| 57 | EXPECT_FALSE(range.Contains(base::Time::FromDoubleT(kTestMaxEpoch))); |
| 58 | EXPECT_FALSE(range.Contains(base::Time::FromDoubleT(kTestMaxEpoch + 1))); |
| 59 | EXPECT_FALSE(range.Contains(base::Time::Max())); |
| 60 | |
| 61 | // With both a start and an end. |
| 62 | range.SetStart(base::Time::FromDoubleT(kTestMinEpoch)); |
| 63 | EXPECT_FALSE(range.Contains(base::Time::Min())); |
| 64 | EXPECT_FALSE(range.Contains(base::Time::FromDoubleT(kTestMinEpoch - 1))); |
| 65 | EXPECT_TRUE(range.Contains(base::Time::FromDoubleT(kTestMinEpoch))); |
| 66 | EXPECT_TRUE(range.Contains(base::Time::FromDoubleT(kTestMinEpoch + 1))); |
| 67 | EXPECT_TRUE(range.Contains(base::Time::FromDoubleT(kTestMaxEpoch - 1))); |
| 68 | EXPECT_FALSE(range.Contains(base::Time::FromDoubleT(kTestMaxEpoch))); |
| 69 | EXPECT_FALSE(range.Contains(base::Time::FromDoubleT(kTestMaxEpoch + 1))); |
| 70 | EXPECT_FALSE(range.Contains(base::Time::Max())); |
| 71 | |
| 72 | // And where start==end. |
| 73 | range = TimeRange(base::Time::FromDoubleT(kTestMinEpoch), |
| 74 | base::Time::FromDoubleT(kTestMinEpoch)); |
| 75 | EXPECT_FALSE(range.Contains(base::Time::Min())); |
| 76 | EXPECT_FALSE(range.Contains(base::Time::FromDoubleT(kTestMinEpoch - 1))); |
| 77 | EXPECT_TRUE(range.Contains(base::Time::FromDoubleT(kTestMinEpoch))); |
| 78 | EXPECT_FALSE(range.Contains(base::Time::FromDoubleT(kTestMinEpoch + 1))); |
| 79 | } |
| 80 | |
| 81 | TEST(CookieDeletionInfoTest, CookieDeletionInfoMatchSessionControl) { |
| 82 | CanonicalCookie persistent_cookie("persistent-cookie", "persistent-value", |
| 83 | "persistent-domain", "persistent-path", |
| 84 | /*creation=*/base::Time::Now(), |
| 85 | /*expiration=*/base::Time::Max(), |
| 86 | /*last_access=*/base::Time::Now(), |
| 87 | /*secure=*/false, |
| 88 | /*httponly=*/false, |
Lily Chen | 92eec70 | 2019-04-17 16:17:42 | [diff] [blame] | 89 | CookieSameSite::NO_RESTRICTION, |
Chris Mumford | d8ed9f8 | 2018-05-01 15:43:13 | [diff] [blame] | 90 | CookiePriority::COOKIE_PRIORITY_DEFAULT); |
| 91 | |
| 92 | CanonicalCookie session_cookie( |
| 93 | "session-cookie", "session-value", "session-domain", "session-path", |
| 94 | /*creation=*/base::Time::Now(), |
| 95 | /*expiration=*/base::Time(), |
| 96 | /*last_access=*/base::Time::Now(), |
| 97 | /*secure=*/false, |
Lily Chen | 92eec70 | 2019-04-17 16:17:42 | [diff] [blame] | 98 | /*httponly=*/false, CookieSameSite::NO_RESTRICTION, |
Chris Mumford | d8ed9f8 | 2018-05-01 15:43:13 | [diff] [blame] | 99 | CookiePriority::COOKIE_PRIORITY_DEFAULT); |
| 100 | |
| 101 | CookieDeletionInfo delete_info; |
| 102 | EXPECT_TRUE(delete_info.Matches(persistent_cookie)); |
| 103 | EXPECT_TRUE(delete_info.Matches(session_cookie)); |
| 104 | |
| 105 | delete_info.session_control = |
| 106 | CookieDeletionInfo::SessionControl::PERSISTENT_COOKIES; |
| 107 | EXPECT_TRUE(delete_info.Matches(persistent_cookie)); |
| 108 | EXPECT_FALSE(delete_info.Matches(session_cookie)); |
| 109 | |
| 110 | delete_info.session_control = |
| 111 | CookieDeletionInfo::SessionControl::SESSION_COOKIES; |
| 112 | EXPECT_FALSE(delete_info.Matches(persistent_cookie)); |
| 113 | EXPECT_TRUE(delete_info.Matches(session_cookie)); |
| 114 | } |
| 115 | |
| 116 | TEST(CookieDeletionInfoTest, CookieDeletionInfoMatchHost) { |
| 117 | CanonicalCookie domain_cookie("domain-cookie", "domain-cookie-value", |
| 118 | /*domain=*/".example.com", "/path", |
| 119 | /*creation=*/base::Time::Now(), |
| 120 | /*expiration=*/base::Time::Max(), |
| 121 | /*last_access=*/base::Time::Now(), |
| 122 | /*secure=*/false, |
| 123 | /*httponly=*/false, |
Lily Chen | 92eec70 | 2019-04-17 16:17:42 | [diff] [blame] | 124 | CookieSameSite::NO_RESTRICTION, |
Chris Mumford | d8ed9f8 | 2018-05-01 15:43:13 | [diff] [blame] | 125 | CookiePriority::COOKIE_PRIORITY_DEFAULT); |
| 126 | |
| 127 | CanonicalCookie host_cookie("host-cookie", "host-cookie-value", |
| 128 | /*domain=*/"thehost.hosting.com", "/path", |
| 129 | /*creation=*/base::Time::Now(), |
| 130 | /*expiration=*/base::Time::Max(), |
| 131 | /*last_access=*/base::Time::Now(), |
| 132 | /*secure=*/false, |
Lily Chen | 92eec70 | 2019-04-17 16:17:42 | [diff] [blame] | 133 | /*httponly=*/false, |
| 134 | CookieSameSite::NO_RESTRICTION, |
Chris Mumford | d8ed9f8 | 2018-05-01 15:43:13 | [diff] [blame] | 135 | CookiePriority::COOKIE_PRIORITY_DEFAULT); |
| 136 | |
| 137 | EXPECT_TRUE(domain_cookie.IsDomainCookie()); |
| 138 | EXPECT_TRUE(host_cookie.IsHostCookie()); |
| 139 | |
| 140 | CookieDeletionInfo delete_info; |
| 141 | EXPECT_TRUE(delete_info.Matches(domain_cookie)); |
| 142 | EXPECT_TRUE(delete_info.Matches(host_cookie)); |
| 143 | |
| 144 | delete_info.host = "thehost.hosting.com"; |
| 145 | EXPECT_FALSE(delete_info.Matches(domain_cookie)); |
| 146 | EXPECT_TRUE(delete_info.Matches(host_cookie)); |
| 147 | |
| 148 | delete_info.host = "otherhost.hosting.com"; |
| 149 | EXPECT_FALSE(delete_info.Matches(domain_cookie)); |
| 150 | EXPECT_FALSE(delete_info.Matches(host_cookie)); |
| 151 | |
| 152 | delete_info.host = "thehost.otherhosting.com"; |
| 153 | EXPECT_FALSE(delete_info.Matches(domain_cookie)); |
| 154 | EXPECT_FALSE(delete_info.Matches(host_cookie)); |
| 155 | } |
| 156 | |
| 157 | TEST(CookieDeletionInfoTest, CookieDeletionInfoMatchName) { |
| 158 | CanonicalCookie cookie1("cookie1-name", "cookie1-value", |
| 159 | /*domain=*/".example.com", "/path", |
| 160 | /*creation=*/base::Time::Now(), |
| 161 | /*expiration=*/base::Time::Max(), |
| 162 | /*last_access=*/base::Time::Now(), |
| 163 | /*secure=*/false, |
Lily Chen | 92eec70 | 2019-04-17 16:17:42 | [diff] [blame] | 164 | /*httponly=*/false, CookieSameSite::NO_RESTRICTION, |
Chris Mumford | d8ed9f8 | 2018-05-01 15:43:13 | [diff] [blame] | 165 | CookiePriority::COOKIE_PRIORITY_DEFAULT); |
| 166 | CanonicalCookie cookie2("cookie2-name", "cookie2-value", |
| 167 | /*domain=*/".example.com", "/path", |
| 168 | /*creation=*/base::Time::Now(), |
| 169 | /*expiration=*/base::Time::Max(), |
| 170 | /*last_access=*/base::Time::Now(), |
| 171 | /*secure=*/false, |
Lily Chen | 92eec70 | 2019-04-17 16:17:42 | [diff] [blame] | 172 | /*httponly=*/false, CookieSameSite::NO_RESTRICTION, |
Chris Mumford | d8ed9f8 | 2018-05-01 15:43:13 | [diff] [blame] | 173 | CookiePriority::COOKIE_PRIORITY_DEFAULT); |
| 174 | |
| 175 | CookieDeletionInfo delete_info; |
| 176 | delete_info.name = "cookie1-name"; |
| 177 | EXPECT_TRUE(delete_info.Matches(cookie1)); |
| 178 | EXPECT_FALSE(delete_info.Matches(cookie2)); |
| 179 | } |
| 180 | |
| 181 | TEST(CookieDeletionInfoTest, CookieDeletionInfoMatchValue) { |
| 182 | CanonicalCookie cookie1("cookie1-name", "cookie1-value", |
| 183 | /*domain=*/".example.com", "/path", |
| 184 | /*creation=*/base::Time::Now(), |
| 185 | /*expiration=*/base::Time::Max(), |
| 186 | /*last_access=*/base::Time::Now(), |
| 187 | /*secure=*/false, |
Lily Chen | 92eec70 | 2019-04-17 16:17:42 | [diff] [blame] | 188 | /*httponly=*/false, CookieSameSite::NO_RESTRICTION, |
Chris Mumford | d8ed9f8 | 2018-05-01 15:43:13 | [diff] [blame] | 189 | CookiePriority::COOKIE_PRIORITY_DEFAULT); |
| 190 | CanonicalCookie cookie2("cookie2-name", "cookie2-value", |
| 191 | /*domain=*/".example.com", "/path", |
| 192 | /*creation=*/base::Time::Now(), |
| 193 | /*expiration=*/base::Time::Max(), |
| 194 | /*last_access=*/base::Time::Now(), |
| 195 | /*secure=*/false, |
Lily Chen | 92eec70 | 2019-04-17 16:17:42 | [diff] [blame] | 196 | /*httponly=*/false, CookieSameSite::NO_RESTRICTION, |
Chris Mumford | d8ed9f8 | 2018-05-01 15:43:13 | [diff] [blame] | 197 | CookiePriority::COOKIE_PRIORITY_DEFAULT); |
| 198 | |
| 199 | CookieDeletionInfo delete_info; |
| 200 | delete_info.value_for_testing = "cookie2-value"; |
| 201 | EXPECT_FALSE(delete_info.Matches(cookie1)); |
| 202 | EXPECT_TRUE(delete_info.Matches(cookie2)); |
| 203 | } |
| 204 | |
| 205 | TEST(CookieDeletionInfoTest, CookieDeletionInfoMatchUrl) { |
| 206 | CanonicalCookie cookie("cookie-name", "cookie-value", |
| 207 | /*domain=*/"www.example.com", "/path", |
| 208 | /*creation=*/base::Time::Now(), |
| 209 | /*expiration=*/base::Time::Max(), |
| 210 | /*last_access=*/base::Time::Now(), |
| 211 | /*secure=*/false, |
| 212 | /*httponly=*/false, CookieSameSite::NO_RESTRICTION, |
| 213 | CookiePriority::COOKIE_PRIORITY_DEFAULT); |
| 214 | |
| 215 | CookieDeletionInfo delete_info; |
| 216 | delete_info.url = GURL("https://www.example.com/path"); |
| 217 | EXPECT_TRUE(delete_info.Matches(cookie)); |
| 218 | |
| 219 | delete_info.url = GURL("https://www.example.com/another/path"); |
| 220 | EXPECT_FALSE(delete_info.Matches(cookie)); |
| 221 | } |
| 222 | |
| 223 | TEST(CookieDeletionInfoTest, CookieDeletionInfoDomainMatchesDomain) { |
| 224 | CookieDeletionInfo delete_info; |
| 225 | |
| 226 | const double kTestMinEpoch = 1000; |
| 227 | const double kTestMaxEpoch = 10000000; |
| 228 | delete_info.creation_range.SetStart(base::Time::FromDoubleT(kTestMinEpoch)); |
| 229 | delete_info.creation_range.SetEnd(base::Time::FromDoubleT(kTestMaxEpoch)); |
| 230 | |
| 231 | auto create_cookie = [kTestMinEpoch](std::string cookie_domain) { |
| 232 | CanonicalCookie cookie( |
| 233 | /*name=*/"test-cookie", |
| 234 | /*value=*/"cookie-value", cookie_domain, |
| 235 | /*path=*/"cookie/path", |
| 236 | /*creation=*/base::Time::FromDoubleT(kTestMinEpoch + 1), |
| 237 | /*expiration=*/base::Time::Max(), |
| 238 | /*last_access=*/base::Time::FromDoubleT(kTestMinEpoch + 1), |
| 239 | /*secure=*/false, |
| 240 | /*httponly=*/false, |
Lily Chen | 92eec70 | 2019-04-17 16:17:42 | [diff] [blame] | 241 | /*same_site=*/CookieSameSite::NO_RESTRICTION, |
Chris Mumford | d8ed9f8 | 2018-05-01 15:43:13 | [diff] [blame] | 242 | /*priority=*/CookiePriority::COOKIE_PRIORITY_DEFAULT); |
| 243 | return cookie; |
| 244 | }; |
| 245 | |
| 246 | // by default empty domain list and default match action will match. |
| 247 | EXPECT_TRUE(delete_info.Matches(create_cookie("example.com"))); |
| 248 | |
| 249 | const char kExtensionHostname[] = "mgndgikekgjfcpckkfioiadnlibdjbkf"; |
| 250 | |
| 251 | // Only using the inclusion list because this is only testing |
| 252 | // DomainMatchesDomainSet and not CookieDeletionInfo::Matches. |
| 253 | delete_info.domains_and_ips_to_delete = |
| 254 | std::set<std::string>({"example.com", "another.com", "192.168.0.1"}); |
| 255 | EXPECT_TRUE(delete_info.Matches(create_cookie(".example.com"))); |
| 256 | EXPECT_TRUE(delete_info.Matches(create_cookie("example.com"))); |
| 257 | EXPECT_TRUE(delete_info.Matches(create_cookie(".another.com"))); |
| 258 | EXPECT_TRUE(delete_info.Matches(create_cookie("192.168.0.1"))); |
| 259 | EXPECT_FALSE(delete_info.Matches(create_cookie(".nomatch.com"))); |
| 260 | EXPECT_FALSE(delete_info.Matches(create_cookie("192.168.0.2"))); |
| 261 | EXPECT_FALSE(delete_info.Matches(create_cookie(kExtensionHostname))); |
| 262 | } |
| 263 | |
| 264 | TEST(CookieDeletionInfoTest, CookieDeletionInfoMatchesDomainList) { |
| 265 | CookieDeletionInfo delete_info; |
| 266 | |
| 267 | auto create_cookie = [](std::string cookie_domain) { |
| 268 | CanonicalCookie cookie( |
| 269 | /*name=*/"test-cookie", |
| 270 | /*value=*/"cookie-value", cookie_domain, |
| 271 | /*path=*/"cookie/path", |
| 272 | /*creation=*/base::Time::Now(), |
| 273 | /*expiration=*/base::Time::Max(), |
| 274 | /*last_access=*/base::Time::Now(), |
| 275 | /*secure=*/false, |
| 276 | /*httponly=*/false, |
Lily Chen | 92eec70 | 2019-04-17 16:17:42 | [diff] [blame] | 277 | /*same_site=*/CookieSameSite::NO_RESTRICTION, |
Chris Mumford | d8ed9f8 | 2018-05-01 15:43:13 | [diff] [blame] | 278 | /*priority=*/CookiePriority::COOKIE_PRIORITY_DEFAULT); |
| 279 | return cookie; |
| 280 | }; |
| 281 | |
| 282 | // With two empty lists (default) should match any domain. |
| 283 | EXPECT_TRUE(delete_info.Matches(create_cookie("anything.com"))); |
| 284 | |
| 285 | // With only an "to_delete" list. |
| 286 | delete_info.domains_and_ips_to_delete = |
| 287 | std::set<std::string>({"includea.com", "includeb.com"}); |
| 288 | EXPECT_TRUE(delete_info.Matches(create_cookie("includea.com"))); |
| 289 | EXPECT_TRUE(delete_info.Matches(create_cookie("includeb.com"))); |
| 290 | EXPECT_FALSE(delete_info.Matches(create_cookie("anything.com"))); |
| 291 | |
| 292 | // With only an "to_ignore" list. |
| 293 | delete_info.domains_and_ips_to_delete.clear(); |
| 294 | delete_info.domains_and_ips_to_ignore.insert("exclude.com"); |
| 295 | EXPECT_TRUE(delete_info.Matches(create_cookie("anything.com"))); |
| 296 | EXPECT_FALSE(delete_info.Matches(create_cookie("exclude.com"))); |
| 297 | |
| 298 | // Now with both lists populated. |
| 299 | // |
| 300 | // +----------------------+ |
| 301 | // | to_delete | outside.com |
| 302 | // | | |
| 303 | // | left.com +---------------------+ |
| 304 | // | | mid.com | to_ignore | |
| 305 | // | | | | |
| 306 | // +------------|---------+ | |
| 307 | // | right.com | |
| 308 | // | | |
| 309 | // +---------------------+ |
| 310 | delete_info.domains_and_ips_to_delete = |
| 311 | std::set<std::string>({"left.com", "mid.com"}); |
| 312 | delete_info.domains_and_ips_to_ignore = |
| 313 | std::set<std::string>({"mid.com", "right.com"}); |
| 314 | |
| 315 | EXPECT_TRUE(delete_info.Matches(create_cookie("left.com"))); |
| 316 | EXPECT_FALSE(delete_info.Matches(create_cookie("mid.com"))); |
| 317 | EXPECT_FALSE(delete_info.Matches(create_cookie("right.com"))); |
| 318 | EXPECT_FALSE(delete_info.Matches(create_cookie("outside.com"))); |
| 319 | } |
| 320 | |
| 321 | } // namespace net |