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_remover_filter.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 url { | |
18 | |
19 namespace { | |
20 | |
21 struct TestCase { | |
22 std::string url; | |
23 bool should_match; | |
24 }; | |
25 | |
26 void RunTestCase(TestCase test_case, | |
27 const base::Callback<bool(const GURL&)>& filter) { | |
28 GURL url(test_case.url); | |
29 EXPECT_TRUE(url.is_valid()) << test_case.url << " is not valid."; | |
30 if (test_case.should_match) | |
31 EXPECT_TRUE(filter.Run(GURL(test_case.url))); | |
32 else | |
33 EXPECT_FALSE(filter.Run(GURL(test_case.url))); | |
34 } | |
35 | |
36 void RunTestCase( | |
37 TestCase test_case, | |
38 const base::Callback<bool(const ContentSettingsPattern&)>& filter) { | |
39 ContentSettingsPattern pattern = | |
40 ContentSettingsPattern::FromString(test_case.url); | |
41 EXPECT_TRUE(pattern.IsValid()) << test_case.url << " is not valid."; | |
42 EXPECT_EQ(test_case.should_match, filter.Run(pattern)) << pattern.ToString(); | |
43 } | |
44 | |
45 void RunTestCase( | |
46 TestCase test_case, | |
47 const base::Callback<bool(const net::CanonicalCookie&)>& filter) { | |
48 // Test with regular cookie, http only, domain, and secure. | |
49 std::string cookie_line = "A=2"; | |
50 GURL test_url(test_case.url); | |
51 EXPECT_TRUE(test_url.is_valid()) << test_case.url; | |
52 scoped_ptr<net::CanonicalCookie> cookie = net::CanonicalCookie::Create( | |
53 test_url, cookie_line, base::Time::Now(), net::CookieOptions()); | |
54 EXPECT_TRUE(cookie) << cookie_line << " from " << test_case.url | |
55 << " is not a valid cookie"; | |
56 if (cookie) | |
57 EXPECT_EQ(test_case.should_match, filter.Run(*cookie)) | |
58 << cookie->DebugString(); | |
59 | |
60 cookie_line = std::string("A=2;domain=") + test_url.host(); | |
61 cookie = net::CanonicalCookie::Create( | |
62 test_url, cookie_line, base::Time::Now(), net::CookieOptions()); | |
63 if (cookie) | |
64 EXPECT_EQ(test_case.should_match, filter.Run(*cookie)) | |
65 << cookie->DebugString(); | |
66 | |
67 cookie_line = std::string("A=2; HttpOnly;") + test_url.host(); | |
68 cookie = net::CanonicalCookie::Create( | |
69 test_url, cookie_line, base::Time::Now(), net::CookieOptions()); | |
70 if (cookie) | |
71 EXPECT_EQ(test_case.should_match, filter.Run(*cookie)) | |
72 << cookie->DebugString(); | |
73 | |
74 cookie_line = std::string("A=2; HttpOnly; Secure;") + test_url.host(); | |
75 cookie = net::CanonicalCookie::Create( | |
76 test_url, cookie_line, base::Time::Now(), net::CookieOptions()); | |
77 if (cookie) | |
78 EXPECT_EQ(test_case.should_match, filter.Run(*cookie)) | |
79 << cookie->DebugString(); | |
80 } | |
81 | |
82 } // namespace | |
83 | |
84 TEST(BrowsingDataRemoverFilterTest, Noop) { | |
85 // An no-op filter matches everything. | |
86 base::Callback<bool(const GURL&)> filter = | |
87 BrowsingDataRemoverFilter::BuildNoopFilter(); | |
88 | |
89 TestCase test_cases[] = { | |
90 {"https://www.google.com", true}, {"https://www.chrome.com", true}, | |
91 }; | |
92 | |
93 for (TestCase test_case : test_cases) | |
94 RunTestCase(test_case, filter); | |
95 } | |
96 | |
97 TEST(BrowsingDataRemoverFilterTest, Whitelist) { | |
98 BrowsingDataRemoverFilter builder(BrowsingDataRemoverFilter::WHITELIST); | |
99 builder.AddOrigin(Origin(GURL("https://www.google.com"))); | |
100 builder.AddOrigin(Origin(GURL("http://www.example.com"))); | |
101 builder.AddRegisterableDomain("foo.bar"); | |
102 base::Callback<bool(const GURL&)> filter = | |
103 builder.BuildSameOriginOrDomainFilter(); | |
104 | |
105 TestCase test_cases[] = { | |
106 // Whitelist matches any URL on the specified origins, | |
107 {"https://www.google.com", true}, | |
108 {"https://www.google.com/?q=test", true}, | |
109 {"http://www.example.com", true}, | |
110 {"http://www.example.com/index.html", true}, | |
111 {"http://www.example.com/foo/bar", true}, | |
112 | |
113 // and matches any URL on the specified domains. | |
114 {"http://www.foo.bar/foo/bar", true}, | |
115 {"https://www.sub.foo.bar/foo/bar", true}, | |
116 {"http://www.sub.foo.bar:8000/foo/bar", true}, | |
117 | |
118 // Subdomains are different origins. | |
119 {"https://test.www.google.com", false}, | |
120 | |
121 // Different scheme or port is a different origin. | |
122 {"https://www.google.com:8000", false}, | |
123 {"https://www.example.com/index.html", false}, | |
124 | |
125 // Different host is a different origin. | |
126 {"https://www.youtube.com", false}, | |
127 {"https://www.chromium.org", false}, | |
128 {"http://www.chromium.org", false}, | |
129 }; | |
130 | |
131 for (TestCase test_case : test_cases) | |
132 RunTestCase(test_case, filter); | |
133 } | |
134 | |
135 TEST(BrowsingDataRemoverFilterTest, WhitelistContentSettings) { | |
136 BrowsingDataRemoverFilter builder(BrowsingDataRemoverFilter::WHITELIST); | |
137 builder.AddOrigin(Origin(GURL("https://www.google.com"))); | |
138 builder.AddOrigin(Origin(GURL("http://www.example.com"))); | |
139 builder.AddRegisterableDomain("foo.bar"); | |
140 base::Callback<bool(const ContentSettingsPattern&)> filter = | |
141 builder.BuildWebsiteSettingsPatternMatchesFilter(); | |
142 | |
143 TestCase test_cases[] = { | |
144 // Whitelist matches any patterns that include the whitelist origins or | |
145 // registerable domains. | |
146 {"https://www.google.com", true}, | |
147 {"https://[*.]google.com", true}, | |
148 {"https://[*.]google.com:443", true}, | |
149 {"[*.]google.com", true}, | |
150 {"[*.]google.com/foo/bar", true}, | |
151 {"www.google.com/?q=test", true}, | |
152 {"http://www.example.com", true}, | |
153 {"[*.]example.com:80", true}, | |
154 {"http://www.example.com/index.html", true}, | |
155 {"http://www.example.com/foo/bar", true}, | |
156 {"*", true}, | |
157 {"*:80", true}, | |
158 {"*:443", true}, | |
159 {"http://www.foo.bar/foo/bar", true}, | |
160 {"[*.]foo.bar", true}, | |
161 {"www.sub.foo.bar/foo/bar", true}, | |
162 {"http://www.sub.foo.bar:8000/foo/bar", true}, | |
163 | |
164 // Subdomains are different origins. | |
165 {"https://test.www.google.com", false}, | |
166 {"https://google.com", false}, | |
167 | |
168 // Different TLDs are different origins. | |
169 {"[*.]google", false}, | |
170 {"[*.]google.net", false}, | |
171 {"https://[*.]google.org", false}, | |
172 | |
173 // Different scheme or port is a different origin. | |
174 {"http://www.google.com", false}, | |
175 {"http://[*.].google.com", false}, | |
176 {"*:8000", false}, | |
177 {"https://www.example.com/index.html", false}, | |
178 | |
179 // Different host is a different origin. | |
180 {"www.sub.foo.bar.bar2/foo/bar", false}, | |
181 {"https://www.youtube.com", false}, | |
182 {"https://www.chromium.org", false}, | |
183 {"[*.]youtube", false}, | |
184 }; | |
185 | |
186 for (TestCase test_case : test_cases) | |
187 RunTestCase(test_case, filter); | |
188 } | |
189 | |
190 TEST(BrowsingDataRemoverFilterTest, Blacklist) { | |
191 BrowsingDataRemoverFilter builder(BrowsingDataRemoverFilter::BLACKLIST); | |
192 builder.AddOrigin(Origin(GURL("https://www.google.com"))); | |
193 builder.AddOrigin(Origin(GURL("http://www.example.com"))); | |
194 builder.AddRegisterableDomain("foo.bar"); | |
195 base::Callback<bool(const GURL&)> filter = | |
196 builder.BuildSameOriginOrDomainFilter(); | |
197 | |
198 TestCase test_cases[] = { | |
199 // URLS on explicitly specified origins or domains are not matched. | |
200 {"https://www.google.com", false}, | |
201 {"https://www.google.com/?q=test", false}, | |
202 {"http://www.example.com", false}, | |
203 {"http://www.example.com/index.html", false}, | |
204 {"http://www.example.com/foo/bar", false}, | |
205 {"http://www.foo.bar/foo/bar", false}, | |
206 {"https://www.sub.foo.bar/foo/bar", false}, | |
207 {"http://www.sub.foo.bar:8000/foo/bar", false}, | |
208 | |
209 // Subdomains are different origins. | |
210 {"https://test.www.google.com", true}, | |
211 | |
212 // The same hosts but with different schemes and ports | |
213 // are not blacklisted. | |
214 {"https://www.google.com:8000", true}, | |
215 {"https://www.example.com/index.html", true}, | |
216 | |
217 // Different hosts are not blacklisted. | |
218 {"https://www.chrome.com", true}, | |
219 {"https://www.youtube.com", true}, | |
220 }; | |
221 | |
222 for (TestCase test_case : test_cases) | |
223 RunTestCase(test_case, filter); | |
224 } | |
225 | |
226 TEST(BrowsingDataRemoverFilterTest, BlacklistContentSettings) { | |
227 BrowsingDataRemoverFilter builder(BrowsingDataRemoverFilter::BLACKLIST); | |
228 builder.AddOrigin(Origin(GURL("https://www.google.com"))); | |
229 builder.AddOrigin(Origin(GURL("http://www.example.com"))); | |
230 builder.AddRegisterableDomain("foo.bar"); | |
231 base::Callback<bool(const ContentSettingsPattern&)> filter = | |
232 builder.BuildWebsiteSettingsPatternMatchesFilter(); | |
233 | |
234 TestCase test_cases[] = { | |
235 // Blacklist matches any patterns that don't include the origins. | |
236 {"https://www.google.com", false}, | |
237 {"https://[*.]google.com", false}, | |
238 {"https://[*.]google.com:443", false}, | |
239 {"[*.]google.com", false}, | |
240 {"[*.]google.com/foo/bar", false}, | |
241 {"www.google.com/?q=test", false}, | |
242 {"http://www.example.com", false}, | |
243 {"[*.]example.com:80", false}, | |
244 {"http://www.example.com/index.html", false}, | |
245 {"http://www.example.com/foo/bar", false}, | |
246 {"*", false}, | |
247 {"*:80", false}, | |
248 {"*:443", false}, | |
249 {"http://www.foo.bar/foo/bar", false}, | |
250 {"[*.]foo.bar", false}, | |
251 {"www.sub.foo.bar/foo/bar", false}, | |
252 {"http://www.sub.foo.bar:8000/foo/bar", false}, | |
253 | |
254 // Subdomains are different origins. | |
255 {"https://test.www.google.com", true}, | |
256 {"https://google.com", true}, | |
257 | |
258 // Different TLDs are different origins. | |
259 {"[*.]google", true}, | |
260 {"[*.]google.net", true}, | |
261 {"https://[*.]google.org", true}, | |
262 | |
263 // Different scheme or port is a different origin. | |
264 {"http://www.google.com", true}, | |
265 {"http://[*.].google.com", true}, | |
266 {"*:8000", true}, | |
267 {"https://www.example.com/index.html", true}, | |
268 | |
269 // Different host is a different origin. | |
270 {"www.sub.foo.bar.bar2/foo/bar", true}, | |
271 {"https://www.youtube.com", true}, | |
272 {"https://www.chromium.org", true}, | |
273 {"[*.]youtube", true}, | |
274 }; | |
275 | |
276 for (TestCase test_case : test_cases) | |
277 RunTestCase(test_case, filter); | |
278 } | |
279 | |
280 TEST(BrowsingDataRemoverFilterTest, MatchesCookiesWhitelist) { | |
281 BrowsingDataRemoverFilter builder(BrowsingDataRemoverFilter::WHITELIST); | |
282 // Regular site. | |
283 builder.AddOrigin(Origin(GURL("https://www.google.com"))); | |
284 // sp.nom.br is a TLD. | |
285 builder.AddOrigin(Origin(GURL("https://www.website.sp.nom.br"))); | |
286 // misawa.aomori.jp is a TLD and possibly not, as using the address without | |
287 // a subdomain is also a valid url. | |
288 builder.AddOrigin(Origin(GURL("http://www.misawa.aomori.jp"))); | |
289 builder.AddOrigin(Origin(GURL("http://example.com"))); | |
290 builder.AddOrigin(Origin(GURL("http://192.168.1.1:80"))); | |
291 builder.AddOrigin(Origin(GURL("https://sub.sub.sub.subdomaincrazy.com"))); | |
292 builder.AddRegisterableDomain("foo.bar"); | |
293 base::Callback<bool(const net::CanonicalCookie&)> filter = | |
294 builder.BuildDomainCookieFilter(); | |
295 | |
296 TestCase test_cases[] = { | |
297 // Any cookie with the same registerable domain as the origins is matched. | |
298 {"https://www.google.com", true}, | |
299 {"http://www.google.com", true}, | |
300 {"http://www.google.com:300", true}, | |
301 {"https://mail.google.com", true}, | |
302 {"http://mail.google.com", true}, | |
303 {"http://google.com", true}, | |
304 {"https://website.sp.nom.br", true}, | |
305 {"https://sub.website.sp.nom.br", true}, | |
306 {"http://www.misawa.aomori.jp", true}, | |
307 {"http://sub.www.misawa.aomori.jp", true}, | |
308 {"http://www.example.com", true}, | |
309 {"http://192.168.1.1", true}, | |
310 {"http://192.168.1.1:10", true}, | |
311 {"https://sub.sub.subdomaincrazy.com", true}, | |
312 {"http://sub.subdomaincrazy.com", true}, | |
313 {"https://subdomaincrazy.com", true}, | |
314 {"http://www.subdomaincrazy.com", true}, | |
315 {"https://www.foo.bar", true}, | |
316 {"http://www.foo.bar/foo/bar", true}, | |
317 {"https://www.sub.foo.bar/foo/bar", true}, | |
318 {"http://www.sub.foo.bar:8000/foo/bar", true}, | |
319 | |
320 // Different tlds. | |
321 {"https://www.google.org", false}, | |
322 | |
323 // We treat tld+1 and bare tld as different domains. | |
324 {"https://www.sp.nom.br", false}, | |
325 {"https://sp.nom.br", false}, | |
326 {"http://www.hospital.misawa.aomori.jp/", false}, | |
327 | |
328 // Different hosts in general. | |
329 {"https://www.chrome.com", false}, | |
330 {"https://www.foo.bar.bar2", false}, | |
331 {"http://youtube.com", false}, | |
332 {"http://192.168.2.1", false}}; | |
333 | |
334 for (TestCase test_case : test_cases) | |
335 RunTestCase(test_case, filter); | |
336 } | |
337 | |
338 TEST(BrowsingDataRemoverFilterTest, MatchesCookiesBlacklist) { | |
339 BrowsingDataRemoverFilter builder(BrowsingDataRemoverFilter::BLACKLIST); | |
340 // Regular site. | |
341 builder.AddOrigin(Origin(GURL("https://www.google.com"))); | |
342 // sp.nom.br is a TLD. | |
msramek
2016/04/07 16:19:13
nit: eTLD? Here and elsewhere.
dmurph
2016/04/08 23:33:45
Done.
| |
343 builder.AddOrigin(Origin(GURL("https://www.website.sp.nom.br"))); | |
344 // misawa.aomori.jp is a TLD and possibly not, as using the address without | |
345 // a subdomain is also a valid url. | |
346 builder.AddOrigin(Origin(GURL("http://www.misawa.aomori.jp"))); | |
347 // Here we're doing no subdomain. | |
348 builder.AddOrigin(Origin(GURL("http://example.com"))); | |
349 builder.AddOrigin(Origin(GURL("http://192.168.1.1:80"))); | |
350 builder.AddOrigin(Origin(GURL("https://sub.sub.sub.subdomaincrazy.com"))); | |
351 builder.AddRegisterableDomain("foo.bar"); | |
352 base::Callback<bool(const net::CanonicalCookie&)> filter = | |
353 builder.BuildDomainCookieFilter(); | |
354 | |
355 TestCase test_cases[] = { | |
356 // Any cookie with the same registerable domain as the origins is matched. | |
357 {"https://www.google.com", false}, | |
358 {"http://www.google.com", false}, | |
359 {"http://www.google.com:300", false}, | |
360 {"https://mail.google.com", false}, | |
361 {"http://mail.google.com", false}, | |
362 {"http://google.com", false}, | |
363 {"https://website.sp.nom.br", false}, | |
364 {"https://sub.website.sp.nom.br", false}, | |
365 {"http://www.misawa.aomori.jp", false}, | |
366 {"http://sub.www.misawa.aomori.jp", false}, | |
367 {"http://www.example.com", false}, | |
368 {"http://192.168.1.1", false}, | |
369 {"http://192.168.1.1:10", false}, | |
370 {"https://sub.sub.subdomaincrazy.com", false}, | |
371 {"http://sub.subdomaincrazy.com", false}, | |
372 {"https://subdomaincrazy.com", false}, | |
373 {"http://www.subdomaincrazy.com", false}, | |
374 {"https://www.foo.bar", false}, | |
375 {"http://www.foo.bar/foo/bar", false}, | |
376 {"https://www.sub.foo.bar/foo/bar", false}, | |
377 {"http://www.sub.foo.bar:8000/foo/bar", false}, | |
378 | |
379 // Different tlds. | |
380 {"https://www.google.org", true}, | |
381 | |
382 // We treat tld+1 and bare tld as different domains. | |
383 {"https://www.sp.nom.br", true}, | |
384 {"https://sp.nom.br", true}, | |
385 {"http://www.hospital.misawa.aomori.jp/", true}, | |
386 | |
387 // Different hosts in general. | |
388 {"https://www.chrome.com", true}, | |
389 {"https://www.foo.bar.bar2", true}, | |
390 {"http://youtube.com", true}, | |
391 {"http://192.168.2.1", true}}; | |
392 | |
393 for (TestCase test_case : test_cases) | |
394 RunTestCase(test_case, filter); | |
395 } | |
396 | |
397 } // namespace url | |
OLD | NEW |