Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(586)

Side by Side Diff: chrome/common/content_settings_pattern_unittest.cc

Issue 541813002: Componentize chrome/common/content_settings files. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: GN fix Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "components/content_settings/core/common/content_settings_pattern.h"
6
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "url/gurl.h"
9
10 namespace {
11
12 ContentSettingsPattern Pattern(const std::string& str) {
13 return ContentSettingsPattern::FromString(str);
14 }
15
16 } // namespace
17
18 TEST(ContentSettingsPatternTest, RealWorldPatterns) {
19 // This is the place for real world patterns that unveiled bugs.
20 EXPECT_STREQ("[*.]ikea.com",
21 Pattern("[*.]ikea.com").ToString().c_str());
22 }
23
24 TEST(ContentSettingsPatternTest, GURL) {
25 // Document and verify GURL behavior.
26 GURL url("http://mail.google.com:80");
27 EXPECT_EQ(-1, url.IntPort());
28 EXPECT_EQ("", url.port());
29
30 url = GURL("http://mail.google.com");
31 EXPECT_EQ(-1, url.IntPort());
32 EXPECT_EQ("", url.port());
33
34 url = GURL("https://mail.google.com:443");
35 EXPECT_EQ(-1, url.IntPort());
36 EXPECT_EQ("", url.port());
37
38 url = GURL("https://mail.google.com");
39 EXPECT_EQ(-1, url.IntPort());
40 EXPECT_EQ("", url.port());
41
42 url = GURL("http://mail.google.com");
43 EXPECT_EQ(-1, url.IntPort());
44 EXPECT_EQ("", url.port());
45 }
46
47 TEST(ContentSettingsPatternTest, FromURL) {
48 // NOTICE: When content settings pattern are created from a GURL the following
49 // happens:
50 // - If the GURL scheme is "http" the scheme wildcard is used. Otherwise the
51 // GURL scheme is used.
52 // - A domain wildcard is added to the GURL host.
53 // - A port wildcard is used instead of the schemes default port.
54 // In case of non-default ports the specific GURL port is used.
55 ContentSettingsPattern pattern = ContentSettingsPattern::FromURL(
56 GURL("http://www.youtube.com"));
57 EXPECT_TRUE(pattern.IsValid());
58 EXPECT_STREQ("[*.]www.youtube.com", pattern.ToString().c_str());
59
60 // Patterns created from a URL.
61 pattern = ContentSettingsPattern::FromURL(GURL("http://www.google.com"));
62 EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com")));
63 EXPECT_TRUE(pattern.Matches(GURL("http://foo.www.google.com")));
64 EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com:80")));
65 EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com:81")));
66 EXPECT_FALSE(pattern.Matches(GURL("https://mail.google.com")));
67 EXPECT_TRUE(pattern.Matches(GURL("https://www.google.com")));
68
69 pattern = ContentSettingsPattern::FromURL(GURL("http://www.google.com:80"));
70 EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com")));
71 EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com:80")));
72 EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com:81")));
73
74 pattern = ContentSettingsPattern::FromURL(GURL("https://www.google.com:443"));
75 EXPECT_TRUE(pattern.Matches(GURL("https://www.google.com")));
76 EXPECT_TRUE(pattern.Matches(GURL("https://foo.www.google.com")));
77 EXPECT_TRUE(pattern.Matches(GURL("https://www.google.com:443")));
78 EXPECT_FALSE(pattern.Matches(GURL("https://www.google.com:444")));
79 EXPECT_FALSE(pattern.Matches(GURL("http://www.google.com:443")));
80
81 pattern = ContentSettingsPattern::FromURL(GURL("https://127.0.0.1"));
82 EXPECT_TRUE(pattern.IsValid());
83 EXPECT_STREQ("https://127.0.0.1:443", pattern.ToString().c_str());
84
85 pattern = ContentSettingsPattern::FromURL(GURL("http://[::1]"));
86 EXPECT_TRUE(pattern.IsValid());
87
88 pattern = ContentSettingsPattern::FromURL(GURL("file:///foo/bar.html"));
89 EXPECT_TRUE(pattern.IsValid());
90 EXPECT_EQ("file:///foo/bar.html", pattern.ToString());
91 }
92
93 TEST(ContentSettingsPatternTest, FilesystemUrls) {
94 ContentSettingsPattern pattern =
95 ContentSettingsPattern::FromURL(GURL("http://www.google.com"));
96 EXPECT_TRUE(pattern.Matches(
97 GURL("filesystem:http://www.google.com/temporary/")));
98 EXPECT_TRUE(pattern.Matches(
99 GURL("filesystem:http://foo.www.google.com/temporary/")));
100 EXPECT_TRUE(pattern.Matches(
101 GURL("filesystem:http://www.google.com:80/temporary/")));
102 EXPECT_TRUE(pattern.Matches(
103 GURL("filesystem:http://www.google.com:81/temporary/")));
104
105 pattern = ContentSettingsPattern::FromURL(GURL("https://www.google.com"));
106 EXPECT_TRUE(pattern.Matches(
107 GURL("filesystem:https://www.google.com/temporary/")));
108 EXPECT_TRUE(pattern.Matches(
109 GURL("filesystem:https://www.google.com:443/temporary/")));
110 EXPECT_TRUE(pattern.Matches(
111 GURL("filesystem:https://foo.www.google.com/temporary/")));
112 EXPECT_FALSE(pattern.Matches(
113 GURL("filesystem:https://www.google.com:81/temporary/")));
114
115 // A pattern from a filesystem URLs is equivalent to a pattern from the inner
116 // URL of the filesystem URL.
117 ContentSettingsPattern pattern2 = ContentSettingsPattern::FromURL(
118 GURL("filesystem:https://www.google.com/temporary/"));
119 EXPECT_EQ(ContentSettingsPattern::IDENTITY, pattern.Compare(pattern2));
120
121 EXPECT_STREQ("https://[*.]www.google.com:443", pattern2.ToString().c_str());
122
123 pattern =
124 ContentSettingsPattern::FromURL(
125 GURL("filesystem:file:///temporary/foo/bar"));
126 EXPECT_TRUE(pattern.Matches(GURL("filesystem:file:///temporary/")));
127 EXPECT_TRUE(pattern.Matches(GURL("filesystem:file:///temporary/test.txt")));
128 EXPECT_TRUE(pattern.Matches(GURL("file:///temporary")));
129 EXPECT_FALSE(pattern.Matches(GURL("file://foo/bar")));
130 pattern2 =
131 ContentSettingsPattern::FromURL(
132 GURL("filesystem:file:///persistent/foo2/bar2"));
133 EXPECT_EQ(ContentSettingsPattern::IDENTITY, pattern.Compare(pattern2));
134 }
135
136 TEST(ContentSettingsPatternTest, FromURLNoWildcard) {
137 // If no port is specifed GURLs always use the default port for the schemes
138 // HTTP and HTTPS. Hence a GURL always carries a port specification either
139 // explicitly or implicitly. Therefore if a content settings pattern is
140 // created from a GURL with no wildcard, specific values are used for the
141 // scheme, host and port part of the pattern.
142 // Creating content settings patterns from strings behaves different. Pattern
143 // parts that are omitted in pattern specifications (strings), are completed
144 // with a wildcard.
145 ContentSettingsPattern pattern = ContentSettingsPattern::FromURLNoWildcard(
146 GURL("http://www.example.com"));
147 EXPECT_TRUE(pattern.IsValid());
148 EXPECT_STREQ("http://www.example.com:80", pattern.ToString().c_str());
149 EXPECT_TRUE(pattern.Matches(GURL("http://www.example.com")));
150 EXPECT_FALSE(pattern.Matches(GURL("https://www.example.com")));
151 EXPECT_FALSE(pattern.Matches(GURL("http://foo.www.example.com")));
152
153 pattern = ContentSettingsPattern::FromURLNoWildcard(
154 GURL("https://www.example.com"));
155 EXPECT_TRUE(pattern.IsValid());
156 EXPECT_STREQ("https://www.example.com:443", pattern.ToString().c_str());
157 EXPECT_FALSE(pattern.Matches(GURL("http://www.example.com")));
158 EXPECT_TRUE(pattern.Matches(GURL("https://www.example.com")));
159 EXPECT_FALSE(pattern.Matches(GURL("http://foo.www.example.com")));
160
161 // Pattern for filesystem URLs
162 pattern =
163 ContentSettingsPattern::FromURLNoWildcard(
164 GURL("filesystem:http://www.google.com/temporary/"));
165 EXPECT_TRUE(pattern.IsValid());
166 EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com")));
167 EXPECT_FALSE(pattern.Matches(GURL("http://foo.www.google.com")));
168 EXPECT_TRUE(pattern.Matches(
169 GURL("filesystem:http://www.google.com/persistent/")));
170 EXPECT_FALSE(pattern.Matches(
171 GURL("filesystem:https://www.google.com/persistent/")));
172 EXPECT_FALSE(pattern.Matches(
173 GURL("filesystem:https://www.google.com:81/temporary/")));
174 EXPECT_FALSE(pattern.Matches(
175 GURL("filesystem:https://foo.www.google.com/temporary/")));
176 }
177
178 TEST(ContentSettingsPatternTest, Wildcard) {
179 EXPECT_TRUE(ContentSettingsPattern::Wildcard().IsValid());
180
181 EXPECT_TRUE(ContentSettingsPattern::Wildcard().Matches(
182 GURL("http://www.google.com")));
183 EXPECT_TRUE(ContentSettingsPattern::Wildcard().Matches(
184 GURL("https://www.google.com")));
185 EXPECT_TRUE(ContentSettingsPattern::Wildcard().Matches(
186 GURL("https://myhost:8080")));
187 EXPECT_TRUE(ContentSettingsPattern::Wildcard().Matches(
188 GURL("file:///foo/bar.txt")));
189
190 EXPECT_STREQ("*", ContentSettingsPattern::Wildcard().ToString().c_str());
191
192 EXPECT_EQ(ContentSettingsPattern::IDENTITY,
193 ContentSettingsPattern::Wildcard().Compare(
194 ContentSettingsPattern::Wildcard()));
195 }
196
197 TEST(ContentSettingsPatternTest, TrimEndingDotFromHost) {
198 EXPECT_TRUE(Pattern("www.example.com").IsValid());
199 EXPECT_TRUE(Pattern("www.example.com").Matches(
200 GURL("http://www.example.com")));
201 EXPECT_TRUE(Pattern("www.example.com").Matches(
202 GURL("http://www.example.com.")));
203
204 EXPECT_TRUE(Pattern("www.example.com.").IsValid());
205 EXPECT_STREQ("www.example.com",
206 Pattern("www.example.com.").ToString().c_str());
207
208 EXPECT_TRUE(Pattern("www.example.com.") == Pattern("www.example.com"));
209
210 EXPECT_TRUE(Pattern(".").IsValid());
211 EXPECT_STREQ(".", Pattern(".").ToString().c_str());
212 }
213
214 TEST(ContentSettingsPatternTest, FromString_WithNoWildcards) {
215 // HTTP patterns with default port.
216 EXPECT_TRUE(Pattern("http://www.example.com:80").IsValid());
217 EXPECT_STREQ("http://www.example.com:80",
218 Pattern("http://www.example.com:80").ToString().c_str());
219 // HTTP patterns with none default port.
220 EXPECT_TRUE(Pattern("http://www.example.com:81").IsValid());
221 EXPECT_STREQ("http://www.example.com:81",
222 Pattern("http://www.example.com:81").ToString().c_str());
223
224 // HTTPS patterns with default port.
225 EXPECT_TRUE(Pattern("https://www.example.com:443").IsValid());
226 EXPECT_STREQ("https://www.example.com:443",
227 Pattern("https://www.example.com:443").ToString().c_str());
228 // HTTPS patterns with none default port.
229 EXPECT_TRUE(Pattern("https://www.example.com:8080").IsValid());
230 EXPECT_STREQ("https://www.example.com:8080",
231 Pattern("https://www.example.com:8080").ToString().c_str());
232 }
233
234 TEST(ContentSettingsPatternTest, FromString_FilePatterns) {
235 // "/" is an invalid file path.
236 EXPECT_FALSE(Pattern("file:///").IsValid());
237
238 // Non-empty domains aren't allowed in file patterns.
239 EXPECT_FALSE(Pattern("file://foo/").IsValid());
240 EXPECT_FALSE(Pattern("file://localhost/foo/bar/test.html").IsValid());
241 EXPECT_FALSE(Pattern("file://*").IsValid());
242 EXPECT_FALSE(Pattern("file://*/").IsValid());
243 EXPECT_FALSE(Pattern("file://*/*").IsValid());
244 EXPECT_FALSE(Pattern("file://*/foo/bar/test.html").IsValid());
245 EXPECT_FALSE(Pattern("file://[*.]/").IsValid());
246
247 // This is the only valid file path wildcard format.
248 EXPECT_TRUE(Pattern("file:///*").IsValid());
249 EXPECT_EQ("file:///*", Pattern("file:///*").ToString());
250
251 // Wildcards are not allowed anywhere in the file path.
252 EXPECT_FALSE(Pattern("file:///f*o/bar/file.html").IsValid());
253 EXPECT_FALSE(Pattern("file:///*/bar/file.html").IsValid());
254 EXPECT_FALSE(Pattern("file:///foo/*").IsValid());
255 EXPECT_FALSE(Pattern("file:///foo/bar/*").IsValid());
256 EXPECT_FALSE(Pattern("file:///foo/*/file.html").IsValid());
257 EXPECT_FALSE(Pattern("file:///foo/bar/*.html").IsValid());
258 EXPECT_FALSE(Pattern("file:///foo/bar/file.*").IsValid());
259
260 EXPECT_TRUE(Pattern("file:///tmp/test.html").IsValid());
261 EXPECT_EQ("file:///tmp/file.html",
262 Pattern("file:///tmp/file.html").ToString());
263 EXPECT_TRUE(Pattern("file:///tmp/test.html").Matches(
264 GURL("file:///tmp/test.html")));
265 EXPECT_FALSE(Pattern("file:///tmp/test.html").Matches(
266 GURL("file:///tmp/other.html")));
267 EXPECT_FALSE(Pattern("file:///tmp/test.html").Matches(
268 GURL("http://example.org/")));
269
270 EXPECT_TRUE(Pattern("file:///*").Matches(GURL("file:///tmp/test.html")));
271 EXPECT_TRUE(Pattern("file:///*").Matches(
272 GURL("file://localhost/tmp/test.html")));
273 }
274
275 TEST(ContentSettingsPatternTest, FromString_ExtensionPatterns) {
276 EXPECT_TRUE(Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/")
277 .IsValid());
278 EXPECT_EQ("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/",
279 Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/")
280 .ToString());
281 EXPECT_TRUE(Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/")
282 .Matches(GURL("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/")));
283 }
284
285 TEST(ContentSettingsPatternTest, FromString_WithIPAdresses) {
286 // IPv4
287 EXPECT_TRUE(Pattern("192.168.0.1").IsValid());
288 EXPECT_STREQ("192.168.1.1", Pattern("192.168.1.1").ToString().c_str());
289 EXPECT_TRUE(Pattern("https://192.168.0.1:8080").IsValid());
290 EXPECT_STREQ("https://192.168.0.1:8080",
291 Pattern("https://192.168.0.1:8080").ToString().c_str());
292
293 // Subdomain wildcards should be only valid for hosts, not for IP addresses.
294 EXPECT_FALSE(Pattern("[*.]127.0.0.1").IsValid());
295
296 // IPv6
297 EXPECT_TRUE(Pattern("[::1]").IsValid());
298 EXPECT_STREQ("[::1]", Pattern("[::1]").ToString().c_str());
299 EXPECT_TRUE(Pattern("https://[::1]:8080").IsValid());
300 EXPECT_STREQ("https://[::1]:8080",
301 Pattern("https://[::1]:8080").ToString().c_str());
302 }
303
304 TEST(ContentSettingsPatternTest, FromString_WithWildcards) {
305 // Creating content settings patterns from strings completes pattern parts
306 // that are omitted in pattern specifications (strings) with a wildcard.
307
308 // The wildcard pattern.
309 EXPECT_TRUE(Pattern("*").IsValid());
310 EXPECT_STREQ("*", Pattern("*").ToString().c_str());
311 EXPECT_EQ(ContentSettingsPattern::IDENTITY,
312 Pattern("*").Compare(ContentSettingsPattern::Wildcard()));
313
314 // Patterns with port wildcard.
315 EXPECT_TRUE(Pattern("http://example.com:*").IsValid());
316 EXPECT_STREQ("http://example.com",
317 Pattern("http://example.com:*").ToString().c_str());
318
319 EXPECT_TRUE(Pattern("https://example.com").IsValid());
320 EXPECT_STREQ("https://example.com",
321 Pattern("https://example.com").ToString().c_str());
322
323 EXPECT_TRUE(Pattern("*://www.google.com.com:8080").IsValid());
324 EXPECT_STREQ("www.google.com:8080",
325 Pattern("*://www.google.com:8080").ToString().c_str());
326 EXPECT_TRUE(Pattern("*://www.google.com:8080").Matches(
327 GURL("http://www.google.com:8080")));
328 EXPECT_TRUE(Pattern("*://www.google.com:8080").Matches(
329 GURL("https://www.google.com:8080")));
330 EXPECT_FALSE(
331 Pattern("*://www.google.com").Matches(GURL("file:///foo/bar.html")));
332
333 EXPECT_TRUE(Pattern("www.example.com:8080").IsValid());
334
335 // Patterns with port and scheme wildcard.
336 EXPECT_TRUE(Pattern("*://www.example.com:*").IsValid());
337 EXPECT_STREQ("www.example.com",
338 Pattern("*://www.example.com:*").ToString().c_str());
339
340 EXPECT_TRUE(Pattern("*://www.example.com").IsValid());
341 EXPECT_STREQ("www.example.com",
342 Pattern("*://www.example.com").ToString().c_str());
343
344 EXPECT_TRUE(Pattern("www.example.com:*").IsValid());
345 EXPECT_STREQ("www.example.com",
346 Pattern("www.example.com:*").ToString().c_str());
347
348 EXPECT_TRUE(Pattern("www.example.com").IsValid());
349 EXPECT_STREQ("www.example.com",
350 Pattern("www.example.com").ToString().c_str());
351 EXPECT_TRUE(Pattern("www.example.com").Matches(
352 GURL("http://www.example.com/")));
353 EXPECT_FALSE(Pattern("example.com").Matches(
354 GURL("http://example.org/")));
355
356 // Patterns with domain wildcard.
357 EXPECT_TRUE(Pattern("[*.]example.com").IsValid());
358 EXPECT_STREQ("[*.]example.com",
359 Pattern("[*.]example.com").ToString().c_str());
360 EXPECT_TRUE(Pattern("[*.]example.com").Matches(
361 GURL("http://example.com/")));
362 EXPECT_TRUE(Pattern("[*.]example.com").Matches(
363 GURL("http://foo.example.com/")));
364 EXPECT_FALSE(Pattern("[*.]example.com").Matches(
365 GURL("http://example.org/")));
366
367 EXPECT_TRUE(Pattern("[*.]google.com:80").Matches(
368 GURL("http://mail.google.com:80")));
369 EXPECT_FALSE(Pattern("[*.]google.com:80").Matches(
370 GURL("http://mail.google.com:81")));
371 EXPECT_TRUE(Pattern("[*.]google.com:80").Matches(
372 GURL("http://www.google.com")));
373
374 EXPECT_TRUE(Pattern("[*.]google.com:8080").Matches(
375 GURL("http://mail.google.com:8080")));
376
377 EXPECT_TRUE(Pattern("[*.]google.com:443").Matches(
378 GURL("https://mail.google.com:443")));
379 EXPECT_TRUE(Pattern("[*.]google.com:443").Matches(
380 GURL("https://www.google.com")));
381
382 EXPECT_TRUE(Pattern("[*.]google.com:4321").Matches(
383 GURL("https://mail.google.com:4321")));
384 EXPECT_TRUE(Pattern("[*.]example.com").Matches(
385 GURL("http://example.com/")));
386 EXPECT_TRUE(Pattern("[*.]example.com").Matches(
387 GURL("http://www.example.com/")));
388
389 // Patterns with host wildcard
390 EXPECT_TRUE(Pattern("[*.]").IsValid());
391 EXPECT_TRUE(Pattern("http://*").IsValid());
392 EXPECT_TRUE(Pattern("http://[*.]").IsValid());
393 EXPECT_EQ(std::string("http://*"), Pattern("http://[*.]").ToString());
394 EXPECT_TRUE(Pattern("http://*:8080").IsValid());
395 EXPECT_TRUE(Pattern("*://*").IsValid());
396 EXPECT_STREQ("*", Pattern("*://*").ToString().c_str());
397 }
398
399 TEST(ContentSettingsPatternTest, FromString_Canonicalized) {
400 // UTF-8 patterns.
401 EXPECT_TRUE(Pattern("[*.]\xC4\x87ira.com").IsValid());
402 EXPECT_STREQ("[*.]xn--ira-ppa.com",
403 Pattern("[*.]\xC4\x87ira.com").ToString().c_str());
404 EXPECT_TRUE(Pattern("\xC4\x87ira.com").IsValid());
405 EXPECT_STREQ("xn--ira-ppa.com",
406 Pattern("\xC4\x87ira.com").ToString().c_str());
407 EXPECT_TRUE(Pattern("file:///\xC4\x87ira.html").IsValid());
408 EXPECT_STREQ("file:///%C4%87ira.html",
409 Pattern("file:///\xC4\x87ira.html").ToString().c_str());
410
411 // File path normalization.
412 EXPECT_TRUE(Pattern("file:///tmp/bar/../test.html").IsValid());
413 EXPECT_STREQ("file:///tmp/test.html",
414 Pattern("file:///tmp/bar/../test.html").ToString().c_str());
415 }
416
417 TEST(ContentSettingsPatternTest, InvalidPatterns) {
418 // StubObserver expects an empty pattern top be returned as empty string.
419 EXPECT_FALSE(ContentSettingsPattern().IsValid());
420 EXPECT_STREQ("", ContentSettingsPattern().ToString().c_str());
421
422 // Empty pattern string
423 EXPECT_FALSE(Pattern(std::string()).IsValid());
424 EXPECT_STREQ("", Pattern(std::string()).ToString().c_str());
425
426 // Pattern strings with invalid scheme part.
427 EXPECT_FALSE(Pattern("ftp://myhost.org").IsValid());
428 EXPECT_STREQ("", Pattern("ftp://myhost.org").ToString().c_str());
429
430 // Pattern strings with invalid host part.
431 EXPECT_FALSE(Pattern("*example.com").IsValid());
432 EXPECT_STREQ("", Pattern("*example.com").ToString().c_str());
433 EXPECT_FALSE(Pattern("example.*").IsValid());
434 EXPECT_STREQ("", Pattern("example.*").ToString().c_str());
435 EXPECT_FALSE(Pattern("*\xC4\x87ira.com").IsValid());
436 EXPECT_STREQ("", Pattern("*\xC4\x87ira.com").ToString().c_str());
437 EXPECT_FALSE(Pattern("\xC4\x87ira.*").IsValid());
438 EXPECT_STREQ("", Pattern("\xC4\x87ira.*").ToString().c_str());
439
440 // Pattern strings with invalid port parts.
441 EXPECT_FALSE(Pattern("example.com:abc").IsValid());
442 EXPECT_STREQ("", Pattern("example.com:abc").ToString().c_str());
443
444 // Invalid file pattern strings.
445 EXPECT_FALSE(Pattern("file://").IsValid());
446 EXPECT_STREQ("", Pattern("file://").ToString().c_str());
447 EXPECT_FALSE(Pattern("file:///foo/bar.html:8080").IsValid());
448 EXPECT_STREQ("", Pattern("file:///foo/bar.html:8080").ToString().c_str());
449
450 // Host having multiple ending dots.
451 EXPECT_FALSE(Pattern("www.example.com..").IsValid());
452 EXPECT_STREQ("", Pattern("www.example.com..").ToString().c_str());
453 }
454
455 TEST(ContentSettingsPatternTest, UnequalOperator) {
456 EXPECT_TRUE(Pattern("http://www.foo.com") != Pattern("http://www.foo.com*"));
457 EXPECT_TRUE(Pattern("http://www.foo.com*") !=
458 ContentSettingsPattern::Wildcard());
459
460 EXPECT_TRUE(Pattern("http://www.foo.com") !=
461 ContentSettingsPattern::Wildcard());
462
463 EXPECT_TRUE(Pattern("http://www.foo.com") != Pattern("www.foo.com"));
464 EXPECT_TRUE(Pattern("http://www.foo.com") !=
465 Pattern("http://www.foo.com:80"));
466
467 EXPECT_FALSE(Pattern("http://www.foo.com") != Pattern("http://www.foo.com"));
468 EXPECT_TRUE(Pattern("http://www.foo.com") == Pattern("http://www.foo.com"));
469 }
470
471 TEST(ContentSettingsPatternTest, Compare) {
472 // Test identical patterns patterns.
473 ContentSettingsPattern pattern1 =
474 Pattern("http://www.google.com");
475 EXPECT_EQ(ContentSettingsPattern::IDENTITY, pattern1.Compare(pattern1));
476 EXPECT_EQ(ContentSettingsPattern::IDENTITY,
477 Pattern("http://www.google.com:80").Compare(
478 Pattern("http://www.google.com:80")));
479 EXPECT_EQ(ContentSettingsPattern::IDENTITY,
480 Pattern("*://[*.]google.com:*").Compare(
481 Pattern("*://[*.]google.com:*")));
482
483 ContentSettingsPattern invalid_pattern1;
484 ContentSettingsPattern invalid_pattern2 =
485 ContentSettingsPattern::FromString("google.com*");
486
487 // Compare invalid patterns.
488 EXPECT_TRUE(!invalid_pattern1.IsValid());
489 EXPECT_TRUE(!invalid_pattern2.IsValid());
490 EXPECT_EQ(ContentSettingsPattern::IDENTITY,
491 invalid_pattern1.Compare(invalid_pattern2));
492 EXPECT_TRUE(invalid_pattern1 == invalid_pattern2);
493
494 // Compare a pattern with an IPv4 addresse to a pattern with a domain name.
495 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_POST,
496 Pattern("http://www.google.com").Compare(
497 Pattern("127.0.0.1")));
498 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE,
499 Pattern("127.0.0.1").Compare(
500 Pattern("http://www.google.com")));
501 EXPECT_TRUE(Pattern("127.0.0.1") > Pattern("http://www.google.com"));
502 EXPECT_TRUE(Pattern("http://www.google.com") < Pattern("127.0.0.1"));
503
504 // Compare a pattern with an IPv6 address to a patterns with a domain name.
505 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_POST,
506 Pattern("http://www.google.com").Compare(
507 Pattern("[::1]")));
508 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE,
509 Pattern("[::1]").Compare(
510 Pattern("http://www.google.com")));
511 EXPECT_TRUE(Pattern("[::1]") > Pattern("http://www.google.com"));
512 EXPECT_TRUE(Pattern("http://www.google.com") < Pattern("[::1]"));
513
514 // Compare a pattern with an IPv6 addresse to a pattern with an IPv4 addresse.
515 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE,
516 Pattern("127.0.0.1").Compare(
517 Pattern("[::1]")));
518 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_POST,
519 Pattern("[::1]").Compare(
520 Pattern("127.0.0.1")));
521 EXPECT_TRUE(Pattern("[::1]") < Pattern("127.0.0.1"));
522 EXPECT_TRUE(Pattern("127.0.0.1") > Pattern("[::1]"));
523
524 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE,
525 Pattern("http://www.google.com").Compare(
526 Pattern("http://www.youtube.com")));
527 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE,
528 Pattern("http://[*.]google.com").Compare(
529 Pattern("http://[*.]youtube.com")));
530
531 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_POST,
532 Pattern("http://[*.]host.com").Compare(
533 Pattern("http://[*.]evilhost.com")));
534 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_POST,
535 Pattern("*://www.google.com:80").Compare(
536 Pattern("*://www.google.com:8080")));
537 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE,
538 Pattern("https://www.google.com:80").Compare(
539 Pattern("http://www.google.com:80")));
540
541 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE,
542 Pattern("http://[*.]google.com:90").Compare(
543 Pattern("http://mail.google.com:80")));
544 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE,
545 Pattern("https://[*.]google.com:80").Compare(
546 Pattern("http://mail.google.com:80")));
547 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE,
548 Pattern("https://mail.google.com:*").Compare(
549 Pattern("http://mail.google.com:80")));
550
551 // Test patterns with different precedences.
552 EXPECT_EQ(ContentSettingsPattern::PREDECESSOR,
553 Pattern("mail.google.com").Compare(
554 Pattern("[*.]google.com")));
555 EXPECT_EQ(ContentSettingsPattern::SUCCESSOR,
556 Pattern("[*.]google.com").Compare(
557 Pattern("mail.google.com")));
558
559 EXPECT_EQ(ContentSettingsPattern::PREDECESSOR,
560 Pattern("[*.]mail.google.com").Compare(
561 Pattern("[*.]google.com")));
562 EXPECT_EQ(ContentSettingsPattern::SUCCESSOR,
563 Pattern("[*.]google.com").Compare(
564 Pattern("[*.]mail.google.com")));
565
566 EXPECT_EQ(ContentSettingsPattern::PREDECESSOR,
567 Pattern("mail.google.com:80").Compare(
568 Pattern("mail.google.com:*")));
569 EXPECT_EQ(ContentSettingsPattern::SUCCESSOR,
570 Pattern("mail.google.com:*").Compare(
571 Pattern("mail.google.com:80")));
572
573 EXPECT_EQ(ContentSettingsPattern::PREDECESSOR,
574 Pattern("https://mail.google.com:*").Compare(
575 Pattern("*://mail.google.com:*")));
576 EXPECT_EQ(ContentSettingsPattern::SUCCESSOR,
577 Pattern("*://mail.google.com:*").Compare(
578 Pattern("https://mail.google.com:*")));
579
580 EXPECT_EQ(ContentSettingsPattern::PREDECESSOR,
581 Pattern("*://mail.google.com:80").Compare(
582 Pattern("https://mail.google.com:*")));
583 EXPECT_EQ(ContentSettingsPattern::SUCCESSOR,
584 Pattern("https://mail.google.com:*").Compare(
585 Pattern("*://mail.google.com:80")));
586
587 // Test the wildcard pattern.
588 EXPECT_EQ(ContentSettingsPattern::IDENTITY,
589 ContentSettingsPattern::Wildcard().Compare(
590 ContentSettingsPattern::Wildcard()));
591
592 EXPECT_EQ(ContentSettingsPattern::PREDECESSOR,
593 Pattern("[*.]google.com").Compare(
594 ContentSettingsPattern::Wildcard()));
595 EXPECT_EQ(ContentSettingsPattern::SUCCESSOR,
596 ContentSettingsPattern::Wildcard().Compare(
597 Pattern("[*.]google.com")));
598
599 EXPECT_EQ(ContentSettingsPattern::PREDECESSOR,
600 Pattern("mail.google.com").Compare(
601 ContentSettingsPattern::Wildcard()));
602 EXPECT_EQ(ContentSettingsPattern::SUCCESSOR,
603 ContentSettingsPattern::Wildcard().Compare(
604 Pattern("mail.google.com")));
605 }
606
607 // Legacy tests to ensure backwards compatibility.
608
609 TEST(ContentSettingsPatternTest, PatternSupport_Legacy) {
610 EXPECT_TRUE(Pattern("[*.]example.com").IsValid());
611 EXPECT_TRUE(Pattern("example.com").IsValid());
612 EXPECT_TRUE(Pattern("192.168.0.1").IsValid());
613 EXPECT_TRUE(Pattern("[::1]").IsValid());
614 EXPECT_TRUE(
615 Pattern("file:///tmp/test.html").IsValid());
616 EXPECT_FALSE(Pattern("*example.com").IsValid());
617 EXPECT_FALSE(Pattern("example.*").IsValid());
618
619 EXPECT_TRUE(
620 Pattern("http://example.com").IsValid());
621 EXPECT_TRUE(
622 Pattern("https://example.com").IsValid());
623
624 EXPECT_TRUE(Pattern("[*.]example.com").Matches(
625 GURL("http://example.com/")));
626 EXPECT_TRUE(Pattern("[*.]example.com").Matches(
627 GURL("http://www.example.com/")));
628 EXPECT_TRUE(Pattern("www.example.com").Matches(
629 GURL("http://www.example.com/")));
630 EXPECT_TRUE(
631 Pattern("file:///tmp/test.html").Matches(
632 GURL("file:///tmp/test.html")));
633 EXPECT_FALSE(Pattern(std::string()).Matches(GURL("http://www.example.com/")));
634 EXPECT_FALSE(Pattern("[*.]example.com").Matches(
635 GURL("http://example.org/")));
636 EXPECT_FALSE(Pattern("example.com").Matches(
637 GURL("http://example.org/")));
638 EXPECT_FALSE(
639 Pattern("file:///tmp/test.html").Matches(
640 GURL("file:///tmp/other.html")));
641 EXPECT_FALSE(
642 Pattern("file:///tmp/test.html").Matches(
643 GURL("http://example.org/")));
644 }
645
646 TEST(ContentSettingsPatternTest, CanonicalizePattern_Legacy) {
647 // Basic patterns.
648 EXPECT_STREQ("[*.]ikea.com", Pattern("[*.]ikea.com").ToString().c_str());
649 EXPECT_STREQ("example.com", Pattern("example.com").ToString().c_str());
650 EXPECT_STREQ("192.168.1.1", Pattern("192.168.1.1").ToString().c_str());
651 EXPECT_STREQ("[::1]", Pattern("[::1]").ToString().c_str());
652 EXPECT_STREQ("file:///tmp/file.html",
653 Pattern("file:///tmp/file.html").ToString().c_str());
654
655 // UTF-8 patterns.
656 EXPECT_STREQ("[*.]xn--ira-ppa.com",
657 Pattern("[*.]\xC4\x87ira.com").ToString().c_str());
658 EXPECT_STREQ("xn--ira-ppa.com",
659 Pattern("\xC4\x87ira.com").ToString().c_str());
660 EXPECT_STREQ("file:///%C4%87ira.html",
661 Pattern("file:///\xC4\x87ira.html").ToString().c_str());
662
663 // file:/// normalization.
664 EXPECT_STREQ("file:///tmp/test.html",
665 Pattern("file:///tmp/bar/../test.html").ToString().c_str());
666
667 // Invalid patterns.
668 EXPECT_STREQ("", Pattern("*example.com").ToString().c_str());
669 EXPECT_STREQ("", Pattern("example.*").ToString().c_str());
670 EXPECT_STREQ("", Pattern("*\xC4\x87ira.com").ToString().c_str());
671 EXPECT_STREQ("", Pattern("\xC4\x87ira.*").ToString().c_str());
672 }
OLDNEW
« no previous file with comments | « chrome/common/content_settings_pattern_parser_unittest.cc ('k') | chrome/common/render_messages.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698