OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 { |
11 | 11 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 | 79 |
80 pattern = ContentSettingsPattern::FromURL(GURL("https://127.0.0.1")); | 80 pattern = ContentSettingsPattern::FromURL(GURL("https://127.0.0.1")); |
81 EXPECT_TRUE(pattern.IsValid()); | 81 EXPECT_TRUE(pattern.IsValid()); |
82 EXPECT_STREQ("https://127.0.0.1:443", pattern.ToString().c_str()); | 82 EXPECT_STREQ("https://127.0.0.1:443", pattern.ToString().c_str()); |
83 | 83 |
84 pattern = ContentSettingsPattern::FromURL(GURL("http://[::1]")); | 84 pattern = ContentSettingsPattern::FromURL(GURL("http://[::1]")); |
85 EXPECT_TRUE(pattern.IsValid()); | 85 EXPECT_TRUE(pattern.IsValid()); |
86 | 86 |
87 pattern = ContentSettingsPattern::FromURL(GURL("file:///foo/bar.html")); | 87 pattern = ContentSettingsPattern::FromURL(GURL("file:///foo/bar.html")); |
88 EXPECT_TRUE(pattern.IsValid()); | 88 EXPECT_TRUE(pattern.IsValid()); |
89 EXPECT_STREQ("file:///foo/bar.html", pattern.ToString().c_str()); | 89 EXPECT_EQ("file:///foo/bar.html", pattern.ToString()); |
90 } | 90 } |
91 | 91 |
92 TEST(ContentSettingsPatternTest, FromURLNoWildcard) { | 92 TEST(ContentSettingsPatternTest, FromURLNoWildcard) { |
93 // If no port is specifed GURLs always use the default port for the schemes | 93 // 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 | 94 // HTTP and HTTPS. Hence a GURL always carries a port specification either |
95 // explicitly or implicitly. Therefore if a content settings pattern is | 95 // explicitly or implicitly. Therefore if a content settings pattern is |
96 // created from a GURL with no wildcard, specific values are used for the | 96 // created from a GURL with no wildcard, specific values are used for the |
97 // scheme, host and port part of the pattern. | 97 // scheme, host and port part of the pattern. |
98 // Creating content settings patterns from strings behaves different. Pattern | 98 // Creating content settings patterns from strings behaves different. Pattern |
99 // parts that are omitted in pattern specifications (strings), are completed | 99 // 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()); | 165 EXPECT_TRUE(Pattern("https://www.example.com:443").IsValid()); |
166 EXPECT_STREQ("https://www.example.com:443", | 166 EXPECT_STREQ("https://www.example.com:443", |
167 Pattern("https://www.example.com:443").ToString().c_str()); | 167 Pattern("https://www.example.com:443").ToString().c_str()); |
168 // HTTPS patterns with none default port. | 168 // HTTPS patterns with none default port. |
169 EXPECT_TRUE(Pattern("https://www.example.com:8080").IsValid()); | 169 EXPECT_TRUE(Pattern("https://www.example.com:8080").IsValid()); |
170 EXPECT_STREQ("https://www.example.com:8080", | 170 EXPECT_STREQ("https://www.example.com:8080", |
171 Pattern("https://www.example.com:8080").ToString().c_str()); | 171 Pattern("https://www.example.com:8080").ToString().c_str()); |
172 } | 172 } |
173 | 173 |
174 TEST(ContentSettingsPatternTest, FromString_FilePatterns) { | 174 TEST(ContentSettingsPatternTest, FromString_FilePatterns) { |
| 175 // "/" is an invalid file path. |
175 EXPECT_FALSE(Pattern("file:///").IsValid()); | 176 EXPECT_FALSE(Pattern("file:///").IsValid()); |
176 | 177 |
177 // Non-empty domains aren't allowed in file patterns. | 178 // Non-empty domains aren't allowed in file patterns. |
178 EXPECT_FALSE(Pattern("file://foo/").IsValid()); | 179 EXPECT_FALSE(Pattern("file://foo/").IsValid()); |
179 | 180 EXPECT_FALSE(Pattern("file://localhost/foo/bar/test.html").IsValid()); |
180 // Domain wildcards aren't allowed in file patterns. | 181 EXPECT_FALSE(Pattern("file://*").IsValid()); |
181 EXPECT_FALSE(Pattern("file://*/").IsValid()); | 182 EXPECT_FALSE(Pattern("file://*/").IsValid()); |
| 183 EXPECT_FALSE(Pattern("file://*/*").IsValid()); |
| 184 EXPECT_FALSE(Pattern("file://*/foo/bar/test.html").IsValid()); |
182 EXPECT_FALSE(Pattern("file://[*.]/").IsValid()); | 185 EXPECT_FALSE(Pattern("file://[*.]/").IsValid()); |
183 | 186 |
184 // These specify a path that contains '*', which isn't allowed to avoid | 187 // This is the only valid file path wildcard format. |
185 // user confusion. | 188 EXPECT_TRUE(Pattern("file:///*").IsValid()); |
186 EXPECT_FALSE(Pattern("file:///*").IsValid()); | 189 EXPECT_EQ("file:///*", Pattern("file:///*").ToString()); |
| 190 |
| 191 // Wildcards are not allowed anywhere in the file path. |
| 192 EXPECT_FALSE(Pattern("file:///f*o/bar/file.html").IsValid()); |
| 193 EXPECT_FALSE(Pattern("file:///*/bar/file.html").IsValid()); |
| 194 EXPECT_FALSE(Pattern("file:///foo/*").IsValid()); |
187 EXPECT_FALSE(Pattern("file:///foo/bar/*").IsValid()); | 195 EXPECT_FALSE(Pattern("file:///foo/bar/*").IsValid()); |
| 196 EXPECT_FALSE(Pattern("file:///foo/*/file.html").IsValid()); |
| 197 EXPECT_FALSE(Pattern("file:///foo/bar/*.html").IsValid()); |
| 198 EXPECT_FALSE(Pattern("file:///foo/bar/file.*").IsValid()); |
188 | 199 |
189 EXPECT_TRUE(Pattern("file:///tmp/test.html").IsValid()); | 200 EXPECT_TRUE(Pattern("file:///tmp/test.html").IsValid()); |
190 EXPECT_STREQ("file:///tmp/file.html", | 201 EXPECT_EQ("file:///tmp/file.html", |
191 Pattern("file:///tmp/file.html").ToString().c_str()); | 202 Pattern("file:///tmp/file.html").ToString()); |
192 EXPECT_TRUE(Pattern("file:///tmp/test.html").Matches( | 203 EXPECT_TRUE(Pattern("file:///tmp/test.html").Matches( |
193 GURL("file:///tmp/test.html"))); | 204 GURL("file:///tmp/test.html"))); |
194 EXPECT_FALSE(Pattern("file:///tmp/test.html").Matches( | 205 EXPECT_FALSE(Pattern("file:///tmp/test.html").Matches( |
195 GURL("file:///tmp/other.html"))); | 206 GURL("file:///tmp/other.html"))); |
196 EXPECT_FALSE(Pattern("file:///tmp/test.html").Matches( | 207 EXPECT_FALSE(Pattern("file:///tmp/test.html").Matches( |
197 GURL("http://example.org/"))); | 208 GURL("http://example.org/"))); |
| 209 |
| 210 EXPECT_TRUE(Pattern("file:///*").Matches(GURL("file:///tmp/test.html"))); |
| 211 EXPECT_TRUE(Pattern("file:///*").Matches( |
| 212 GURL("file://localhost/tmp/test.html"))); |
198 } | 213 } |
199 | 214 |
200 TEST(ContentSettingsPatternTest, FromString_ExtensionPatterns) { | 215 TEST(ContentSettingsPatternTest, FromString_ExtensionPatterns) { |
201 EXPECT_TRUE(Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/") | 216 EXPECT_TRUE(Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/") |
202 .IsValid()); | 217 .IsValid()); |
203 EXPECT_STREQ("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/", | 218 EXPECT_EQ("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/", |
204 Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/") | 219 Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/") |
205 .ToString().c_str()); | 220 .ToString()); |
206 EXPECT_TRUE(Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/") | 221 EXPECT_TRUE(Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/") |
207 .Matches(GURL("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/"))); | 222 .Matches(GURL("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/"))); |
208 } | 223 } |
209 | 224 |
210 TEST(ContentSettingsPatternTest, FromString_WithIPAdresses) { | 225 TEST(ContentSettingsPatternTest, FromString_WithIPAdresses) { |
211 // IPv4 | 226 // IPv4 |
212 EXPECT_TRUE(Pattern("192.168.0.1").IsValid()); | 227 EXPECT_TRUE(Pattern("192.168.0.1").IsValid()); |
213 EXPECT_STREQ("192.168.1.1", Pattern("192.168.1.1").ToString().c_str()); | 228 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()); | 229 EXPECT_TRUE(Pattern("https://192.168.0.1:8080").IsValid()); |
215 EXPECT_STREQ("https://192.168.0.1:8080", | 230 EXPECT_STREQ("https://192.168.0.1:8080", |
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 // file:/// normalization. | 600 // file:/// normalization. |
586 EXPECT_STREQ("file:///tmp/test.html", | 601 EXPECT_STREQ("file:///tmp/test.html", |
587 Pattern("file:///tmp/bar/../test.html").ToString().c_str()); | 602 Pattern("file:///tmp/bar/../test.html").ToString().c_str()); |
588 | 603 |
589 // Invalid patterns. | 604 // Invalid patterns. |
590 EXPECT_STREQ("", Pattern("*example.com").ToString().c_str()); | 605 EXPECT_STREQ("", Pattern("*example.com").ToString().c_str()); |
591 EXPECT_STREQ("", Pattern("example.*").ToString().c_str()); | 606 EXPECT_STREQ("", Pattern("example.*").ToString().c_str()); |
592 EXPECT_STREQ("", Pattern("*\xC4\x87ira.com").ToString().c_str()); | 607 EXPECT_STREQ("", Pattern("*\xC4\x87ira.com").ToString().c_str()); |
593 EXPECT_STREQ("", Pattern("\xC4\x87ira.*").ToString().c_str()); | 608 EXPECT_STREQ("", Pattern("\xC4\x87ira.*").ToString().c_str()); |
594 } | 609 } |
OLD | NEW |