OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/common/content_settings_pattern.h" | 5 #include "chrome/common/content_settings_pattern.h" |
6 | 6 |
7 #include "googleurl/src/gurl.h" | 7 #include "googleurl/src/gurl.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 namespace { | 10 namespace { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 } | 45 } |
46 | 46 |
47 TEST(ContentSettingsPatternTest, FromURL) { | 47 TEST(ContentSettingsPatternTest, FromURL) { |
48 // NOTICE: When content settings pattern are created from a GURL the following | 48 // NOTICE: When content settings pattern are created from a GURL the following |
49 // happens: | 49 // happens: |
50 // - If the GURL scheme is "http" the scheme wildcard is used. Otherwise the | 50 // - If the GURL scheme is "http" the scheme wildcard is used. Otherwise the |
51 // GURL scheme is used. | 51 // GURL scheme is used. |
52 // - A domain wildcard is added to the GURL host. | 52 // - A domain wildcard is added to the GURL host. |
53 // - A port wildcard is used instead of the schemes default port. | 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. | 54 // In case of non-default ports the specific GURL port is used. |
| 55 // - In the case of a file URI the path is set to the GURL path, unless the |
| 56 // GURL is "file:///", in which case the GURL path ("/", in this case) is |
| 57 // ignored and the wildcard flag set. |
55 ContentSettingsPattern pattern = ContentSettingsPattern::FromURL( | 58 ContentSettingsPattern pattern = ContentSettingsPattern::FromURL( |
56 GURL("http://www.youtube.com")); | 59 GURL("http://www.youtube.com")); |
57 EXPECT_TRUE(pattern.IsValid()); | 60 EXPECT_TRUE(pattern.IsValid()); |
58 EXPECT_STREQ("[*.]www.youtube.com", pattern.ToString().c_str()); | 61 EXPECT_STREQ("[*.]www.youtube.com", pattern.ToString().c_str()); |
59 | 62 |
60 // Patterns created from a URL. | 63 // Patterns created from a URL. |
61 pattern = ContentSettingsPattern::FromURL(GURL("http://www.google.com")); | 64 pattern = ContentSettingsPattern::FromURL(GURL("http://www.google.com")); |
62 EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com"))); | 65 EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com"))); |
63 EXPECT_TRUE(pattern.Matches(GURL("http://foo.www.google.com"))); | 66 EXPECT_TRUE(pattern.Matches(GURL("http://foo.www.google.com"))); |
64 EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com:80"))); | 67 EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com:80"))); |
(...skipping 12 matching lines...) Expand all Loading... |
77 EXPECT_FALSE(pattern.Matches(GURL("https://www.google.com:444"))); | 80 EXPECT_FALSE(pattern.Matches(GURL("https://www.google.com:444"))); |
78 EXPECT_FALSE(pattern.Matches(GURL("http://www.google.com:443"))); | 81 EXPECT_FALSE(pattern.Matches(GURL("http://www.google.com:443"))); |
79 | 82 |
80 pattern = ContentSettingsPattern::FromURL(GURL("https://127.0.0.1")); | 83 pattern = ContentSettingsPattern::FromURL(GURL("https://127.0.0.1")); |
81 EXPECT_TRUE(pattern.IsValid()); | 84 EXPECT_TRUE(pattern.IsValid()); |
82 EXPECT_STREQ("https://127.0.0.1:443", pattern.ToString().c_str()); | 85 EXPECT_STREQ("https://127.0.0.1:443", pattern.ToString().c_str()); |
83 | 86 |
84 pattern = ContentSettingsPattern::FromURL(GURL("http://[::1]")); | 87 pattern = ContentSettingsPattern::FromURL(GURL("http://[::1]")); |
85 EXPECT_TRUE(pattern.IsValid()); | 88 EXPECT_TRUE(pattern.IsValid()); |
86 | 89 |
| 90 GURL url("file:///foo/bar.html"); |
87 pattern = ContentSettingsPattern::FromURL(GURL("file:///foo/bar.html")); | 91 pattern = ContentSettingsPattern::FromURL(GURL("file:///foo/bar.html")); |
88 EXPECT_TRUE(pattern.IsValid()); | 92 EXPECT_TRUE(pattern.IsValid()); |
89 EXPECT_STREQ("file:///foo/bar.html", pattern.ToString().c_str()); | 93 EXPECT_EQ("file:///foo/bar.html", pattern.ToString()); |
| 94 |
| 95 pattern = ContentSettingsPattern::FromURL(GURL("file:///")); |
| 96 EXPECT_TRUE(pattern.IsValid()); |
| 97 EXPECT_TRUE(pattern.Matches(GURL("file:///"))); |
| 98 EXPECT_TRUE(pattern.Matches(GURL("file:///foo"))); |
| 99 EXPECT_TRUE(pattern.Matches(GURL("file:///foo/bar.html"))); |
| 100 EXPECT_EQ("file:///*", pattern.ToString()); |
90 } | 101 } |
91 | 102 |
92 TEST(ContentSettingsPatternTest, FromURLNoWildcard) { | 103 TEST(ContentSettingsPatternTest, FromURLNoWildcard) { |
93 // If no port is specifed GURLs always use the default port for the schemes | 104 // If no port is specifed GURLs always use the default port for the schemes |
94 // HTTP and HTTPS. Hence a GURL always carries a port specification either | 105 // HTTP and HTTPS. Hence a GURL always carries a port specification either |
95 // explicitly or implicitly. Therefore if a content settings pattern is | 106 // explicitly or implicitly. Therefore if a content settings pattern is |
96 // created from a GURL with no wildcard, specific values are used for the | 107 // created from a GURL with no wildcard, specific values are used for the |
97 // scheme, host and port part of the pattern. | 108 // scheme, host and port part of the pattern. |
98 // Creating content settings patterns from strings behaves different. Pattern | 109 // Creating content settings patterns from strings behaves different. Pattern |
99 // parts that are omitted in pattern specifications (strings), are completed | 110 // parts that are omitted in pattern specifications (strings), are completed |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 EXPECT_TRUE(Pattern("https://www.example.com:443").IsValid()); | 176 EXPECT_TRUE(Pattern("https://www.example.com:443").IsValid()); |
166 EXPECT_STREQ("https://www.example.com:443", | 177 EXPECT_STREQ("https://www.example.com:443", |
167 Pattern("https://www.example.com:443").ToString().c_str()); | 178 Pattern("https://www.example.com:443").ToString().c_str()); |
168 // HTTPS patterns with none default port. | 179 // HTTPS patterns with none default port. |
169 EXPECT_TRUE(Pattern("https://www.example.com:8080").IsValid()); | 180 EXPECT_TRUE(Pattern("https://www.example.com:8080").IsValid()); |
170 EXPECT_STREQ("https://www.example.com:8080", | 181 EXPECT_STREQ("https://www.example.com:8080", |
171 Pattern("https://www.example.com:8080").ToString().c_str()); | 182 Pattern("https://www.example.com:8080").ToString().c_str()); |
172 } | 183 } |
173 | 184 |
174 TEST(ContentSettingsPatternTest, FromString_FilePatterns) { | 185 TEST(ContentSettingsPatternTest, FromString_FilePatterns) { |
| 186 // The only valid wildcard pattern is "file:///*". (Note, however, that |
| 187 // "file:///" equates to the wildcard when constructing from a URL.) |
| 188 EXPECT_TRUE(Pattern("file:///*").IsValid()); |
| 189 EXPECT_EQ("file:///*", Pattern("file:///*").ToString()); |
175 EXPECT_FALSE(Pattern("file:///").IsValid()); | 190 EXPECT_FALSE(Pattern("file:///").IsValid()); |
176 | 191 |
177 // Non-empty domains aren't allowed in file patterns. | 192 // Non-empty domains aren't allowed in file patterns. |
178 EXPECT_FALSE(Pattern("file://foo/").IsValid()); | 193 EXPECT_FALSE(Pattern("file://foo/").IsValid()); |
179 | 194 EXPECT_FALSE(Pattern("file://localhost/foo/bar/test.html").IsValid()); |
180 // Domain wildcards aren't allowed in file patterns. | 195 EXPECT_FALSE(Pattern("file://*").IsValid()); |
181 EXPECT_FALSE(Pattern("file://*/").IsValid()); | 196 EXPECT_FALSE(Pattern("file://*/").IsValid()); |
| 197 EXPECT_FALSE(Pattern("file://*/*").IsValid()); |
| 198 EXPECT_FALSE(Pattern("file://*/foo/bar/test.html").IsValid()); |
182 EXPECT_FALSE(Pattern("file://[*.]/").IsValid()); | 199 EXPECT_FALSE(Pattern("file://[*.]/").IsValid()); |
183 | 200 |
184 // These specify a path that contains '*', which isn't allowed to avoid | 201 // Wildcards are not allowed anywhere in the file path. |
185 // user confusion. | 202 EXPECT_FALSE(Pattern("file:///f*o/bar/file.html").IsValid()); |
186 EXPECT_FALSE(Pattern("file:///*").IsValid()); | 203 EXPECT_FALSE(Pattern("file:///*/bar/file.html").IsValid()); |
| 204 EXPECT_FALSE(Pattern("file:///foo/*").IsValid()); |
187 EXPECT_FALSE(Pattern("file:///foo/bar/*").IsValid()); | 205 EXPECT_FALSE(Pattern("file:///foo/bar/*").IsValid()); |
| 206 EXPECT_FALSE(Pattern("file:///foo/*/file.html").IsValid()); |
| 207 EXPECT_FALSE(Pattern("file:///foo/bar/*.html").IsValid()); |
| 208 EXPECT_FALSE(Pattern("file:///foo/bar/file.*").IsValid()); |
188 | 209 |
189 EXPECT_TRUE(Pattern("file:///tmp/test.html").IsValid()); | 210 EXPECT_TRUE(Pattern("file:///tmp/test.html").IsValid()); |
190 EXPECT_STREQ("file:///tmp/file.html", | 211 EXPECT_EQ("file:///tmp/file.html", |
191 Pattern("file:///tmp/file.html").ToString().c_str()); | 212 Pattern("file:///tmp/file.html").ToString()); |
192 EXPECT_TRUE(Pattern("file:///tmp/test.html").Matches( | 213 EXPECT_TRUE(Pattern("file:///tmp/test.html").Matches( |
193 GURL("file:///tmp/test.html"))); | 214 GURL("file:///tmp/test.html"))); |
194 EXPECT_FALSE(Pattern("file:///tmp/test.html").Matches( | 215 EXPECT_FALSE(Pattern("file:///tmp/test.html").Matches( |
195 GURL("file:///tmp/other.html"))); | 216 GURL("file:///tmp/other.html"))); |
196 EXPECT_FALSE(Pattern("file:///tmp/test.html").Matches( | 217 EXPECT_FALSE(Pattern("file:///tmp/test.html").Matches( |
197 GURL("http://example.org/"))); | 218 GURL("http://example.org/"))); |
| 219 |
| 220 EXPECT_TRUE(Pattern("file:///*").Matches(GURL("file:///tmp/test.html"))); |
| 221 EXPECT_TRUE(Pattern("file:///*").Matches( |
| 222 GURL("file://localhost/tmp/test.html"))); |
198 } | 223 } |
199 | 224 |
200 TEST(ContentSettingsPatternTest, FromString_ExtensionPatterns) { | 225 TEST(ContentSettingsPatternTest, FromString_ExtensionPatterns) { |
201 EXPECT_TRUE(Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/") | 226 EXPECT_TRUE(Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/") |
202 .IsValid()); | 227 .IsValid()); |
203 EXPECT_STREQ("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/", | 228 EXPECT_EQ("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/", |
204 Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/") | 229 Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/") |
205 .ToString().c_str()); | 230 .ToString()); |
206 EXPECT_TRUE(Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/") | 231 EXPECT_TRUE(Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/") |
207 .Matches(GURL("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/"))); | 232 .Matches(GURL("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/"))); |
208 } | 233 } |
209 | 234 |
210 TEST(ContentSettingsPatternTest, FromString_WithIPAdresses) { | 235 TEST(ContentSettingsPatternTest, FromString_WithIPAdresses) { |
211 // IPv4 | 236 // IPv4 |
212 EXPECT_TRUE(Pattern("192.168.0.1").IsValid()); | 237 EXPECT_TRUE(Pattern("192.168.0.1").IsValid()); |
213 EXPECT_STREQ("192.168.1.1", Pattern("192.168.1.1").ToString().c_str()); | 238 EXPECT_STREQ("192.168.1.1", Pattern("192.168.1.1").ToString().c_str()); |
214 EXPECT_TRUE(Pattern("https://192.168.0.1:8080").IsValid()); | 239 EXPECT_TRUE(Pattern("https://192.168.0.1:8080").IsValid()); |
215 EXPECT_STREQ("https://192.168.0.1:8080", | 240 EXPECT_STREQ("https://192.168.0.1:8080", |
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 // file:/// normalization. | 610 // file:/// normalization. |
586 EXPECT_STREQ("file:///tmp/test.html", | 611 EXPECT_STREQ("file:///tmp/test.html", |
587 Pattern("file:///tmp/bar/../test.html").ToString().c_str()); | 612 Pattern("file:///tmp/bar/../test.html").ToString().c_str()); |
588 | 613 |
589 // Invalid patterns. | 614 // Invalid patterns. |
590 EXPECT_STREQ("", Pattern("*example.com").ToString().c_str()); | 615 EXPECT_STREQ("", Pattern("*example.com").ToString().c_str()); |
591 EXPECT_STREQ("", Pattern("example.*").ToString().c_str()); | 616 EXPECT_STREQ("", Pattern("example.*").ToString().c_str()); |
592 EXPECT_STREQ("", Pattern("*\xC4\x87ira.com").ToString().c_str()); | 617 EXPECT_STREQ("", Pattern("*\xC4\x87ira.com").ToString().c_str()); |
593 EXPECT_STREQ("", Pattern("\xC4\x87ira.*").ToString().c_str()); | 618 EXPECT_STREQ("", Pattern("\xC4\x87ira.*").ToString().c_str()); |
594 } | 619 } |
OLD | NEW |