OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "chrome/browser/browsing_data/browsing_data_filter_builder.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/callback.h" |
| 12 #include "components/content_settings/core/common/content_settings_pattern.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "url/gurl.h" |
| 15 #include "url/origin.h" |
| 16 |
| 17 namespace { |
| 18 const char kGoogleDomain[] = "google.com"; |
| 19 // sp.nom.br is an eTLD, so this is a regular valid registrable domain, just |
| 20 // like google.com. |
| 21 const char kLongETLDDomain[] = "website.sp.nom.br"; |
| 22 // An IP address can be a registerable domain. |
| 23 const char kIPAddress[] = "192.168.1.1"; |
| 24 |
| 25 struct TestCase { |
| 26 std::string url; |
| 27 bool should_match; |
| 28 }; |
| 29 |
| 30 void RunTestCase(TestCase test_case, |
| 31 const base::Callback<bool(const GURL&)>& filter) { |
| 32 GURL url(test_case.url); |
| 33 EXPECT_TRUE(url.is_valid()) << test_case.url << " is not valid."; |
| 34 EXPECT_EQ(test_case.should_match, filter.Run(GURL(test_case.url))) |
| 35 << test_case.url; |
| 36 } |
| 37 |
| 38 void RunTestCase( |
| 39 TestCase test_case, |
| 40 const base::Callback<bool(const ContentSettingsPattern&)>& filter) { |
| 41 ContentSettingsPattern pattern = |
| 42 ContentSettingsPattern::FromString(test_case.url); |
| 43 EXPECT_TRUE(pattern.IsValid()) << test_case.url << " is not valid."; |
| 44 EXPECT_EQ(test_case.should_match, filter.Run(pattern)) << pattern.ToString(); |
| 45 } |
| 46 |
| 47 void RunTestCase( |
| 48 TestCase test_case, |
| 49 const base::Callback<bool(const net::CanonicalCookie&)>& filter) { |
| 50 // Test with regular cookie, http only, domain, and secure. |
| 51 std::string cookie_line = "A=2"; |
| 52 GURL test_url(test_case.url); |
| 53 EXPECT_TRUE(test_url.is_valid()) << test_case.url; |
| 54 scoped_ptr<net::CanonicalCookie> cookie = net::CanonicalCookie::Create( |
| 55 test_url, cookie_line, base::Time::Now(), net::CookieOptions()); |
| 56 EXPECT_TRUE(cookie) << cookie_line << " from " << test_case.url |
| 57 << " is not a valid cookie"; |
| 58 if (cookie) |
| 59 EXPECT_EQ(test_case.should_match, filter.Run(*cookie)) |
| 60 << cookie->DebugString(); |
| 61 |
| 62 cookie_line = std::string("A=2;domain=") + test_url.host(); |
| 63 cookie = net::CanonicalCookie::Create( |
| 64 test_url, cookie_line, base::Time::Now(), net::CookieOptions()); |
| 65 if (cookie) |
| 66 EXPECT_EQ(test_case.should_match, filter.Run(*cookie)) |
| 67 << cookie->DebugString(); |
| 68 |
| 69 cookie_line = std::string("A=2; HttpOnly;") + test_url.host(); |
| 70 cookie = net::CanonicalCookie::Create( |
| 71 test_url, cookie_line, base::Time::Now(), net::CookieOptions()); |
| 72 if (cookie) |
| 73 EXPECT_EQ(test_case.should_match, filter.Run(*cookie)) |
| 74 << cookie->DebugString(); |
| 75 |
| 76 cookie_line = std::string("A=2; HttpOnly; Secure;") + test_url.host(); |
| 77 cookie = net::CanonicalCookie::Create( |
| 78 test_url, cookie_line, base::Time::Now(), net::CookieOptions()); |
| 79 if (cookie) |
| 80 EXPECT_EQ(test_case.should_match, filter.Run(*cookie)) |
| 81 << cookie->DebugString(); |
| 82 } |
| 83 |
| 84 } // namespace |
| 85 |
| 86 TEST(BrowsingDataFilterBuilderTest, Noop) { |
| 87 // An no-op filter matches everything. |
| 88 base::Callback<bool(const GURL&)> filter = |
| 89 BrowsingDataFilterBuilder::BuildNoopFilter(); |
| 90 |
| 91 TestCase test_cases[] = { |
| 92 {"https://www.google.com", true}, |
| 93 {"https://www.chrome.com", true}, |
| 94 {"http://www.google.com/foo/bar", true}, |
| 95 {"https://website.sp.nom.br", true}, |
| 96 }; |
| 97 |
| 98 for (TestCase test_case : test_cases) |
| 99 RunTestCase(test_case, filter); |
| 100 } |
| 101 |
| 102 TEST(BrowsingDataFilterBuilderTest, GURLWhitelist) { |
| 103 BrowsingDataFilterBuilder builder(BrowsingDataFilterBuilder::WHITELIST); |
| 104 builder.AddRegisterableDomain(std::string(kGoogleDomain)); |
| 105 builder.AddRegisterableDomain(std::string(kLongETLDDomain)); |
| 106 builder.AddRegisterableDomain(std::string(kIPAddress)); |
| 107 base::Callback<bool(const GURL&)> filter = builder.BuildSameDomainFilter(); |
| 108 |
| 109 TestCase test_cases[] = { |
| 110 // We matche any URL on the specified domains. |
| 111 {"http://www.google.com/foo/bar", true}, |
| 112 {"https://www.sub.google.com/foo/bar", true}, |
| 113 {"https://sub.google.com", true}, |
| 114 {"http://www.sub.google.com:8000/foo/bar", true}, |
| 115 {"https://website.sp.nom.br", true}, |
| 116 {"https://www.website.sp.nom.br", true}, |
| 117 {"http://192.168.1.1", true}, |
| 118 {"http://192.168.1.1:80", true}, |
| 119 |
| 120 // Different domains. |
| 121 {"https://www.youtube.com", false}, |
| 122 {"https://www.google.net", false}, |
| 123 {"http://192.168.1.2", false}, |
| 124 |
| 125 // Check both a bare eTLD. |
| 126 {"https://sp.nom.br", false}, |
| 127 }; |
| 128 |
| 129 for (TestCase test_case : test_cases) |
| 130 RunTestCase(test_case, filter); |
| 131 } |
| 132 |
| 133 TEST(BrowsingDataFilterBuilderTest, GURLBlacklist) { |
| 134 BrowsingDataFilterBuilder builder(BrowsingDataFilterBuilder::BLACKLIST); |
| 135 builder.AddRegisterableDomain(std::string(kGoogleDomain)); |
| 136 builder.AddRegisterableDomain(std::string(kLongETLDDomain)); |
| 137 builder.AddRegisterableDomain(std::string(kIPAddress)); |
| 138 base::Callback<bool(const GURL&)> filter = builder.BuildSameDomainFilter(); |
| 139 |
| 140 TestCase test_cases[] = { |
| 141 // We matches any URL that are not on the specified domains. |
| 142 {"http://www.google.com/foo/bar", false}, |
| 143 {"https://www.sub.google.com/foo/bar", false}, |
| 144 {"https://sub.google.com", false}, |
| 145 {"http://www.sub.google.com:8000/foo/bar", false}, |
| 146 {"https://website.sp.nom.br", false}, |
| 147 {"https://www.website.sp.nom.br", false}, |
| 148 {"http://192.168.1.1", false}, |
| 149 {"http://192.168.1.1:80", false}, |
| 150 |
| 151 // Different domains. |
| 152 {"https://www.youtube.com", true}, |
| 153 {"https://www.google.net", true}, |
| 154 {"http://192.168.1.2", true}, |
| 155 |
| 156 // Check our bare eTLD. |
| 157 {"https://sp.nom.br", true}, |
| 158 }; |
| 159 |
| 160 for (TestCase test_case : test_cases) |
| 161 RunTestCase(test_case, filter); |
| 162 } |
| 163 |
| 164 TEST(BrowsingDataFilterBuilderTest, WhitelistContentSettings) { |
| 165 BrowsingDataFilterBuilder builder(BrowsingDataFilterBuilder::WHITELIST); |
| 166 builder.AddRegisterableDomain(std::string(kGoogleDomain)); |
| 167 builder.AddRegisterableDomain(std::string(kLongETLDDomain)); |
| 168 builder.AddRegisterableDomain(std::string(kIPAddress)); |
| 169 base::Callback<bool(const ContentSettingsPattern&)> filter = |
| 170 builder.BuildWebsiteSettingsPatternMatchesFilter(); |
| 171 |
| 172 TestCase test_cases[] = { |
| 173 // Whitelist matches any patterns that include the whitelisted |
| 174 // registerable domains. |
| 175 {"https://www.google.com", true}, |
| 176 {"http://www.google.com", true}, |
| 177 {"http://www.google.com/index.html", true}, |
| 178 {"http://www.google.com/foo/bar", true}, |
| 179 {"www.sub.google.com/bar", true}, |
| 180 {"http://www.sub.google.com:8000/foo/bar", true}, |
| 181 {"https://[*.]google.com", true}, |
| 182 {"https://[*.]google.com:443", true}, |
| 183 {"[*.]google.com", true}, |
| 184 {"[*.]google.com/foo/bar", true}, |
| 185 {"[*.]google.com:80", true}, |
| 186 {"www.google.com/?q=test", true}, |
| 187 {"192.168.1.1", true}, |
| 188 {"192.168.1.1:80", true}, |
| 189 // We check that we treat the eTLDs correctly. |
| 190 {"https://website.sp.nom.br", true}, |
| 191 {"https://www.website.sp.nom.br", true}, |
| 192 |
| 193 // Different eTLDs, and a bare eTLD. |
| 194 {"[*.]google", false}, |
| 195 {"[*.]google.net", false}, |
| 196 {"https://[*.]google.org", false}, |
| 197 {"https://[*.]foo.bar.com", false}, |
| 198 {"https://sp.nom.br", false}, |
| 199 {"http://192.168.1.2", false}, |
| 200 |
| 201 // These patterns are more general than our registerable domain filter, |
| 202 // as they apply to more sites. So we don't match them. The content |
| 203 // settings categories that we'll be seeing from browsing_data_remover |
| 204 // aren't going to have these, but I test for it anyways. |
| 205 {"*", false}, |
| 206 {"*:80", false} |
| 207 }; |
| 208 |
| 209 for (TestCase test_case : test_cases) |
| 210 RunTestCase(test_case, filter); |
| 211 } |
| 212 |
| 213 TEST(BrowsingDataFilterBuilderTest, BlacklistContentSettings) { |
| 214 BrowsingDataFilterBuilder builder(BrowsingDataFilterBuilder::BLACKLIST); |
| 215 builder.AddRegisterableDomain(std::string(kGoogleDomain)); |
| 216 builder.AddRegisterableDomain(std::string(kLongETLDDomain)); |
| 217 builder.AddRegisterableDomain(std::string(kIPAddress)); |
| 218 base::Callback<bool(const ContentSettingsPattern&)> filter = |
| 219 builder.BuildWebsiteSettingsPatternMatchesFilter(); |
| 220 |
| 221 TestCase test_cases[] = { |
| 222 // Blacklist matches any patterns that isn't in the blacklist |
| 223 // registerable domains. |
| 224 {"https://www.google.com", false}, |
| 225 {"http://www.google.com", false}, |
| 226 {"http://www.google.com/index.html", false}, |
| 227 {"http://www.google.com/foo/bar", false}, |
| 228 {"www.sub.google.com/bar", false}, |
| 229 {"http://www.sub.google.com:8000/foo/bar", false}, |
| 230 {"https://[*.]google.com", false}, |
| 231 {"https://[*.]google.com:443", false}, |
| 232 {"[*.]google.com", false}, |
| 233 {"[*.]google.com/foo/bar", false}, |
| 234 {"[*.]google.com:80", false}, |
| 235 {"www.google.com/?q=test", false}, |
| 236 {"192.168.1.1", false}, |
| 237 {"192.168.1.1:80", false}, |
| 238 // We check that we treat the eTLDs correctly. |
| 239 {"https://website.sp.nom.br", false}, |
| 240 {"https://www.website.sp.nom.br", false}, |
| 241 |
| 242 // Different eTLDs, and a bare eTLD. |
| 243 {"[*.]google", true}, |
| 244 {"[*.]google.net", true}, |
| 245 {"https://[*.]google.org", true}, |
| 246 {"https://[*.]foo.bar.com", true}, |
| 247 {"https://sp.nom.br", true}, |
| 248 {"http://192.168.1.2", true}, |
| 249 |
| 250 // These patterns are more general than our registerable domain filter, |
| 251 // as they apply to more sites. So we don't match them. The content |
| 252 // settings categories that we'll be seeing from browsing_data_remover |
| 253 // aren't going to have these, but I test for it anyways. |
| 254 {"*", true}, |
| 255 {"*:80", true} |
| 256 }; |
| 257 |
| 258 for (TestCase test_case : test_cases) |
| 259 RunTestCase(test_case, filter); |
| 260 } |
| 261 |
| 262 TEST(BrowsingDataFilterBuilderTest, MatchesCookiesWhitelist) { |
| 263 BrowsingDataFilterBuilder builder(BrowsingDataFilterBuilder::WHITELIST); |
| 264 builder.AddRegisterableDomain(std::string(kGoogleDomain)); |
| 265 builder.AddRegisterableDomain(std::string(kLongETLDDomain)); |
| 266 builder.AddRegisterableDomain(std::string(kIPAddress)); |
| 267 base::Callback<bool(const net::CanonicalCookie&)> filter = |
| 268 builder.BuildDomainCookieFilter(); |
| 269 |
| 270 TestCase test_cases[] = { |
| 271 // Any cookie with the same registerable domain as the origins is matched. |
| 272 {"https://www.google.com", true}, |
| 273 {"http://www.google.com", true}, |
| 274 {"http://www.google.com:300", true}, |
| 275 {"https://mail.google.com", true}, |
| 276 {"http://mail.google.com", true}, |
| 277 {"http://google.com", true}, |
| 278 {"https://website.sp.nom.br", true}, |
| 279 {"https://sub.website.sp.nom.br", true}, |
| 280 {"http://192.168.1.1", true}, |
| 281 {"http://192.168.1.1:10", true}, |
| 282 |
| 283 // Different eTLDs. |
| 284 {"https://www.google.org", false}, |
| 285 {"https://www.google.co.uk", false}, |
| 286 |
| 287 // We treat eTLD+1 and bare eTLDs as different domains. |
| 288 {"https://www.sp.nom.br", false}, |
| 289 {"https://sp.nom.br", false}, |
| 290 |
| 291 // Different hosts in general. |
| 292 {"https://www.chrome.com", false}, |
| 293 {"http://192.168.2.1", false}}; |
| 294 |
| 295 for (TestCase test_case : test_cases) |
| 296 RunTestCase(test_case, filter); |
| 297 } |
| 298 |
| 299 TEST(BrowsingDataFilterBuilderTest, MatchesCookiesBlacklist) { |
| 300 BrowsingDataFilterBuilder builder(BrowsingDataFilterBuilder::BLACKLIST); |
| 301 builder.AddRegisterableDomain(std::string(kGoogleDomain)); |
| 302 builder.AddRegisterableDomain(std::string(kLongETLDDomain)); |
| 303 builder.AddRegisterableDomain(std::string(kIPAddress)); |
| 304 base::Callback<bool(const net::CanonicalCookie&)> filter = |
| 305 builder.BuildDomainCookieFilter(); |
| 306 |
| 307 TestCase test_cases[] = { |
| 308 // Any cookie that doesn't have the same registerable domain is matched. |
| 309 {"https://www.google.com", false}, |
| 310 {"http://www.google.com", false}, |
| 311 {"http://www.google.com:300", false}, |
| 312 {"https://mail.google.com", false}, |
| 313 {"http://mail.google.com", false}, |
| 314 {"http://google.com", false}, |
| 315 {"https://website.sp.nom.br", false}, |
| 316 {"https://sub.website.sp.nom.br", false}, |
| 317 {"http://192.168.1.1", false}, |
| 318 {"http://192.168.1.1:10", false}, |
| 319 |
| 320 // Different eTLDs. |
| 321 {"https://www.google.org", true}, |
| 322 {"https://www.google.co.uk", true}, |
| 323 |
| 324 // We treat eTLD+1 and bare eTLDs as different domains. |
| 325 {"https://www.sp.nom.br", true}, |
| 326 {"https://sp.nom.br", true}, |
| 327 |
| 328 // Different hosts in general. |
| 329 {"https://www.chrome.com", true}, |
| 330 {"http://192.168.2.1", true}}; |
| 331 |
| 332 for (TestCase test_case : test_cases) |
| 333 RunTestCase(test_case, filter); |
| 334 } |
OLD | NEW |