OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/browsing_data/origin_filter_builder.h" | 5 #include "chrome/browser/browsing_data/origin_filter_builder.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "components/content_settings/core/common/content_settings_pattern.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "url/gurl.h" | 14 #include "url/gurl.h" |
14 #include "url/origin.h" | 15 #include "url/origin.h" |
15 | 16 |
16 namespace url { | 17 namespace url { |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 struct TestCase { | 21 struct TestCase { |
21 std::string url; | 22 std::string url; |
22 bool should_match; | 23 bool should_match; |
23 }; | 24 }; |
24 | 25 |
25 void RunTestCase( | 26 void RunTestCase( |
26 TestCase test_case, const base::Callback<bool(const GURL&)>& filter) { | 27 TestCase test_case, 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."; | |
27 if (test_case.should_match) | 30 if (test_case.should_match) |
28 EXPECT_TRUE(filter.Run(GURL(test_case.url))); | 31 EXPECT_TRUE(filter.Run(GURL(test_case.url))); |
29 else | 32 else |
30 EXPECT_FALSE(filter.Run(GURL(test_case.url))); | 33 EXPECT_FALSE(filter.Run(GURL(test_case.url))); |
31 } | 34 } |
32 | 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 if (test_case.should_match) | |
43 EXPECT_TRUE(filter.Run(pattern)) << pattern.ToString() << " should match."; | |
44 else | |
45 EXPECT_FALSE(filter.Run(pattern)) << pattern.ToString() | |
46 << " should not match."; | |
47 } | |
48 | |
49 void RunTestCase( | |
50 TestCase test_case, | |
51 const base::Callback<bool(const net::CanonicalCookie&)>& filter) { | |
52 std::string cookie_line = "A=2"; | |
Mike West
2016/03/11 08:36:11
It would be helpful to verify that cookie attribut
dmurph
2016/04/05 00:16:28
Done.
QUESTION:::
Does this mean that 'Secure' coo
| |
53 GURL test_url(test_case.url); | |
54 EXPECT_TRUE(test_url.is_valid()) << test_case.url; | |
55 scoped_ptr<net::CanonicalCookie> cookie = net::CanonicalCookie::Create( | |
56 test_url, cookie_line, base::Time::Now(), net::CookieOptions()); | |
57 EXPECT_TRUE(cookie) << cookie_line << " from " << test_case.url | |
58 << " is not a valid cookie"; | |
59 if (cookie) { | |
60 if (test_case.should_match) | |
61 EXPECT_TRUE(filter.Run(*cookie)) << cookie->DebugString() | |
62 << " should match."; | |
63 else | |
64 EXPECT_FALSE(filter.Run(*cookie)) << cookie->DebugString() | |
65 << " should not match."; | |
Mike West
2016/03/11 08:36:11
This pattern could be simplified to `EXPECT_EQ(tes
dmurph
2016/04/05 00:16:28
Done.
| |
66 } | |
67 cookie_line = std::string("A=2;domain=") + test_url.host(); | |
68 cookie = net::CanonicalCookie::Create( | |
69 test_url, cookie_line, base::Time::Now(), net::CookieOptions()); | |
70 if (cookie) { | |
71 if (test_case.should_match) | |
72 EXPECT_TRUE(filter.Run(*cookie)) << cookie->DebugString() | |
73 << " should match."; | |
74 else | |
75 EXPECT_FALSE(filter.Run(*cookie)) << cookie->DebugString() | |
76 << " should not match."; | |
77 } | |
78 } | |
79 | |
33 } // namespace | 80 } // namespace |
34 | 81 |
35 TEST(OriginFilterBuilderTest, Noop) { | 82 TEST(OriginFilterBuilderTest, Noop) { |
36 // An no-op filter matches everything. | 83 // An no-op filter matches everything. |
37 base::Callback<bool(const GURL&)> filter = | 84 base::Callback<bool(const GURL&)> filter = |
38 OriginFilterBuilder::BuildNoopFilter(); | 85 OriginFilterBuilder::BuildNoopFilter(); |
39 | 86 |
40 TestCase test_cases[] = { | 87 TestCase test_cases[] = { |
41 { "https://www.google.com", true }, | 88 { "https://www.google.com", true }, |
42 { "https://www.chrome.com", true }, | 89 { "https://www.chrome.com", true }, |
(...skipping 27 matching lines...) Expand all Loading... | |
70 | 117 |
71 // Different host is a different origin. | 118 // Different host is a different origin. |
72 { "https://www.youtube.com", false }, | 119 { "https://www.youtube.com", false }, |
73 { "https://www.chromium.org", false }, | 120 { "https://www.chromium.org", false }, |
74 }; | 121 }; |
75 | 122 |
76 for (TestCase test_case : test_cases) | 123 for (TestCase test_case : test_cases) |
77 RunTestCase(test_case, filter); | 124 RunTestCase(test_case, filter); |
78 } | 125 } |
79 | 126 |
127 TEST(OriginFilterBuilderTest, WhitelistContentSettings) { | |
128 OriginFilterBuilder builder(OriginFilterBuilder::WHITELIST); | |
129 builder.AddOrigin(Origin(GURL("https://www.google.com"))); | |
130 builder.AddOrigin(Origin(GURL("http://www.example.com"))); | |
131 base::Callback<bool(const ContentSettingsPattern&)> filter = | |
132 builder.BuildWebsiteSettingsPatternMatchesFilter(); | |
133 | |
134 TestCase test_cases[] = { | |
135 // Whitelist matches any patterns that include the whitelist origins. | |
136 {"https://www.google.com", true}, | |
137 {"https://[*.]google.com", true}, | |
138 {"https://[*.]google.com:443", true}, | |
139 {"[*.]google.com", true}, | |
140 {"[*.]google.com/foo/bar", true}, | |
141 {"www.google.com/?q=test", true}, | |
142 {"http://www.example.com", true}, | |
143 {"[*.]example.com:80", true}, | |
144 {"http://www.example.com/index.html", true}, | |
145 {"http://www.example.com/foo/bar", true}, | |
146 {"*", true}, | |
147 {"*:80", true}, | |
148 {"*:443", true}, | |
149 | |
150 // Subdomains are different origins. | |
151 {"https://test.www.google.com", false}, | |
152 {"https://google.com", false}, | |
153 | |
154 // Different TLDs are different origins. | |
155 {"[*.]google", false}, | |
156 {"[*.]google.net", false}, | |
157 {"https://[*.]google.org", false}, | |
158 | |
159 // Different scheme or port is a different origin. | |
160 {"http://www.google.com", false}, | |
161 {"http://[*.].google.com", false}, | |
162 {"*:8000", false}, | |
163 {"https://www.example.com/index.html", false}, | |
164 | |
165 // Different host is a different origin. | |
166 {"https://www.youtube.com", false}, | |
167 {"https://www.chromium.org", false}, | |
168 {"[*.]youtube", false}, | |
169 }; | |
170 | |
171 for (TestCase test_case : test_cases) | |
172 RunTestCase(test_case, filter); | |
173 } | |
174 | |
80 TEST(OriginFilterBuilderTest, Blacklist) { | 175 TEST(OriginFilterBuilderTest, Blacklist) { |
81 OriginFilterBuilder builder(OriginFilterBuilder::BLACKLIST); | 176 OriginFilterBuilder builder(OriginFilterBuilder::BLACKLIST); |
82 builder.AddOrigin(Origin(GURL("https://www.google.com"))); | 177 builder.AddOrigin(Origin(GURL("https://www.google.com"))); |
83 builder.AddOrigin(Origin(GURL("http://www.example.com"))); | 178 builder.AddOrigin(Origin(GURL("http://www.example.com"))); |
84 base::Callback<bool(const GURL&)> filter = builder.BuildSameOriginFilter(); | 179 base::Callback<bool(const GURL&)> filter = builder.BuildSameOriginFilter(); |
85 | 180 |
86 TestCase test_cases[] = { | 181 TestCase test_cases[] = { |
87 // URLS on explicitly specified origins are not matched. | 182 // URLS on explicitly specified origins are not matched. |
88 { "https://www.google.com", false }, | 183 { "https://www.google.com", false }, |
89 { "https://www.google.com/?q=test", false }, | 184 { "https://www.google.com/?q=test", false }, |
(...skipping 11 matching lines...) Expand all Loading... | |
101 | 196 |
102 // Different hosts are not blacklisted. | 197 // Different hosts are not blacklisted. |
103 { "https://www.chrome.com", true }, | 198 { "https://www.chrome.com", true }, |
104 { "https://www.youtube.com", true }, | 199 { "https://www.youtube.com", true }, |
105 }; | 200 }; |
106 | 201 |
107 for (TestCase test_case : test_cases) | 202 for (TestCase test_case : test_cases) |
108 RunTestCase(test_case, filter); | 203 RunTestCase(test_case, filter); |
109 } | 204 } |
110 | 205 |
206 TEST(OriginFilterBuilderTest, BlacklistContentSettings) { | |
207 OriginFilterBuilder builder(OriginFilterBuilder::BLACKLIST); | |
208 builder.AddOrigin(Origin(GURL("https://www.google.com"))); | |
209 builder.AddOrigin(Origin(GURL("http://www.example.com"))); | |
210 base::Callback<bool(const ContentSettingsPattern&)> filter = | |
211 builder.BuildWebsiteSettingsPatternMatchesFilter(); | |
212 | |
213 TestCase test_cases[] = { | |
214 // Blacklist matches any patterns that don't include the origins. | |
215 {"https://www.google.com", false}, | |
216 {"https://[*.]google.com", false}, | |
217 {"https://[*.]google.com:443", false}, | |
218 {"[*.]google.com", false}, | |
219 {"[*.]google.com/foo/bar", false}, | |
220 {"www.google.com/?q=test", false}, | |
221 {"http://www.example.com", false}, | |
222 {"[*.]example.com:80", false}, | |
223 {"http://www.example.com/index.html", false}, | |
224 {"http://www.example.com/foo/bar", false}, | |
225 {"*", false}, | |
226 {"*:80", false}, | |
227 {"*:443", false}, | |
228 | |
229 // Subdomains are different origins. | |
230 {"https://test.www.google.com", true}, | |
231 {"https://google.com", true}, | |
232 | |
233 // Different TLDs are different origins. | |
234 {"[*.]google", true}, | |
235 {"[*.]google.net", true}, | |
236 {"https://[*.]google.org", true}, | |
237 | |
238 // Different scheme or port is a different origin. | |
239 {"http://www.google.com", true}, | |
240 {"http://[*.].google.com", true}, | |
241 {"*:8000", true}, | |
242 {"https://www.example.com/index.html", true}, | |
243 | |
244 // Different host is a different origin. | |
245 {"https://www.youtube.com", true}, | |
246 {"https://www.chromium.org", true}, | |
247 {"[*.]youtube", true}, | |
248 }; | |
249 | |
250 for (TestCase test_case : test_cases) | |
251 RunTestCase(test_case, filter); | |
252 } | |
253 | |
254 TEST(OriginFilterBuilderTest, MatchesCookiesWhitelist) { | |
255 OriginFilterBuilder builder(OriginFilterBuilder::WHITELIST); | |
256 // Regular site. | |
257 builder.AddOrigin(Origin(GURL("https://www.google.com"))); | |
Mike West
2016/03/11 08:36:11
I'd like to see an origin with multiple subdomains
dmurph
2016/04/05 00:16:28
Done.
| |
258 // sp.nom.br is a TLD. | |
259 builder.AddOrigin(Origin(GURL("https://www.website.sp.nom.br"))); | |
260 // misawa.aomori.jp is a TLD and possibly not. | |
Mike West
2016/03/11 08:36:11
and possibly not what?
dmurph
2016/04/05 00:16:28
Clarified.
| |
261 builder.AddOrigin(Origin(GURL("http://www.misawa.aomori.jp"))); | |
262 // Here we're doing no subdomain. | |
263 builder.AddOrigin(Origin(GURL("http://example.com"))); | |
264 builder.AddOrigin(Origin(GURL("http://192.168.1.1:80"))); | |
265 base::Callback<bool(const net::CanonicalCookie&)> filter = | |
266 builder.BuildDomainCookieFilter(); | |
267 | |
268 TestCase test_cases[] = { | |
269 // Any cookie with the same tld+1 is matched | |
270 {"https://www.google.com", true}, | |
271 {"http://www.google.com", true}, | |
272 {"http://www.google.com:300", true}, | |
273 {"https://mail.google.com", true}, | |
274 {"http://mail.google.com", true}, | |
275 {"http://google.com", true}, | |
276 {"https://website.sp.nom.br", true}, | |
277 {"https://sub.website.sp.nom.br", true}, | |
278 {"http://www.misawa.aomori.jp", true}, | |
279 {"http://www.example.com", true}, | |
280 {"http://192.168.1.1", true}, | |
281 {"http://192.168.1.1:10", true}, | |
282 | |
283 // Different tlds. | |
284 {"https://www.google.org", false}, | |
285 | |
286 // We treat tld+1 and bare tld as different domains. | |
287 {"https://www.sp.nom.br", false}, | |
Mike West
2016/03/11 08:36:11
This would be clearer if it was something like "si
dmurph
2016/04/05 00:16:28
Done.
| |
288 {"http://www.website.misawa.aomori.jp", false}, | |
289 | |
290 // Different hosts in general. | |
291 {"https://www.chrome.com", false}, | |
292 {"http://youtube.com", false}, | |
293 {"http://192.168.2.1", false} | |
294 }; | |
295 | |
296 for (TestCase test_case : test_cases) | |
297 RunTestCase(test_case, filter); | |
298 } | |
299 | |
300 TEST(OriginFilterBuilderTest, MatchesCookiesBlacklist) { | |
301 OriginFilterBuilder builder(OriginFilterBuilder::BLACKLIST); | |
302 // Regular site. | |
303 builder.AddOrigin(Origin(GURL("https://www.google.com"))); | |
304 // sp.nom.br is a TLD. | |
305 builder.AddOrigin(Origin(GURL("https://www.website.sp.nom.br"))); | |
306 // misawa.aomori.jp is a TLD and possibly not. | |
307 builder.AddOrigin(Origin(GURL("http://www.misawa.aomori.jp"))); | |
308 // Here we're doing no subdomain. | |
309 builder.AddOrigin(Origin(GURL("http://example.com"))); | |
310 builder.AddOrigin(Origin(GURL("http://192.168.1.1:80"))); | |
311 base::Callback<bool(const net::CanonicalCookie&)> filter = | |
312 builder.BuildDomainCookieFilter(); | |
313 | |
314 TestCase test_cases[] = { | |
315 // Any cookie with the same tld+1 is matched | |
316 {"https://www.google.com", false}, | |
317 {"http://www.google.com", false}, | |
318 {"http://www.google.com:300", false}, | |
319 {"https://mail.google.com", false}, | |
320 {"http://mail.google.com", false}, | |
321 {"http://google.com", false}, | |
322 {"https://website.sp.nom.br", false}, | |
323 {"https://sub.website.sp.nom.br", false}, | |
324 {"http://www.misawa.aomori.jp", false}, | |
325 {"http://www.example.com", false}, | |
326 {"http://192.168.1.1", false}, | |
327 {"http://192.168.1.1:10", false}, | |
328 | |
329 // Different tlds. | |
330 {"https://www.google.org", true}, | |
331 | |
332 // We treat tld+1 and bare tld as different domains. | |
333 {"https://www.sp.nom.br", true}, | |
334 {"http://www.website.misawa.aomori.jp", true}, | |
335 | |
336 // Different hosts in general. | |
337 {"https://www.chrome.com", true}, | |
338 {"http://youtube.com", true}, | |
339 {"http://192.168.2.1", true} | |
340 }; | |
341 | |
342 for (TestCase test_case : test_cases) | |
343 RunTestCase(test_case, filter); | |
344 } | |
345 | |
111 TEST(OriginFilterBuilderTest, MatchesURLWithSubdomain) { | 346 TEST(OriginFilterBuilderTest, MatchesURLWithSubdomain) { |
112 OriginFilterBuilder builder(OriginFilterBuilder::WHITELIST); | 347 OriginFilterBuilder builder(OriginFilterBuilder::WHITELIST); |
113 builder.AddOrigin(Origin(GURL("https://www.google.com"))); | 348 builder.AddOrigin(Origin(GURL("https://www.google.com"))); |
114 base::Callback<bool(const GURL&)> filter = builder.BuildDomainFilter(); | 349 base::Callback<bool(const GURL&)> filter = builder.BuildDomainFilter(); |
115 | 350 |
116 TestCase test_cases[] = { | 351 TestCase test_cases[] = { |
117 // Any URL on the specified origin is matched. | 352 // Any URL on the specified origin is matched. |
118 { "https://www.google.com", true }, | 353 { "https://www.google.com", true }, |
119 { "https://www.google.com/test.html", true }, | 354 { "https://www.google.com/test.html", true }, |
120 | 355 |
(...skipping 10 matching lines...) Expand all Loading... | |
131 // Different hosts are not matched. | 366 // Different hosts are not matched. |
132 { "https://www.chrome.com", false }, | 367 { "https://www.chrome.com", false }, |
133 { "https://www.youtube.com", false }, | 368 { "https://www.youtube.com", false }, |
134 }; | 369 }; |
135 | 370 |
136 for (TestCase test_case : test_cases) | 371 for (TestCase test_case : test_cases) |
137 RunTestCase(test_case, filter); | 372 RunTestCase(test_case, filter); |
138 } | 373 } |
139 | 374 |
140 } // namespace url | 375 } // namespace url |
OLD | NEW |