| 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 154 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 EXPECT_TRUE(Pattern("file:///").IsValid()); | 175 EXPECT_FALSE(Pattern("file:///").IsValid()); |
| 176 EXPECT_STREQ("file:///", | 176 |
| 177 Pattern("file:///").ToString().c_str()); | 177 // Non-empty domains aren't allowed in file patterns. |
| 178 EXPECT_TRUE(Pattern("file:///").Matches( | 178 EXPECT_FALSE(Pattern("file://foo/").IsValid()); |
| 179 GURL("file:///"))); | 179 |
| 180 EXPECT_FALSE(Pattern("file:///").Matches( | 180 // Domain wildcards aren't allowed in file patterns. |
| 181 GURL("file:///tmp/test.html"))); | 181 EXPECT_FALSE(Pattern("file://*/").IsValid()); |
| 182 EXPECT_FALSE(Pattern("file://[*.]/").IsValid()); |
| 183 |
| 184 // These specify a path that contains '*', which isn't allowed to avoid |
| 185 // user confusion. |
| 186 EXPECT_FALSE(Pattern("file:///*").IsValid()); |
| 187 EXPECT_FALSE(Pattern("file:///foo/bar/*").IsValid()); |
| 182 | 188 |
| 183 EXPECT_TRUE(Pattern("file:///tmp/test.html").IsValid()); | 189 EXPECT_TRUE(Pattern("file:///tmp/test.html").IsValid()); |
| 184 EXPECT_STREQ("file:///tmp/file.html", | 190 EXPECT_STREQ("file:///tmp/file.html", |
| 185 Pattern("file:///tmp/file.html").ToString().c_str()); | 191 Pattern("file:///tmp/file.html").ToString().c_str()); |
| 186 EXPECT_TRUE(Pattern("file:///tmp/test.html").Matches( | 192 EXPECT_TRUE(Pattern("file:///tmp/test.html").Matches( |
| 187 GURL("file:///tmp/test.html"))); | 193 GURL("file:///tmp/test.html"))); |
| 188 EXPECT_FALSE(Pattern("file:///tmp/test.html").Matches( | 194 EXPECT_FALSE(Pattern("file:///tmp/test.html").Matches( |
| 189 GURL("file:///tmp/other.html"))); | 195 GURL("file:///tmp/other.html"))); |
| 190 EXPECT_FALSE(Pattern("file:///tmp/test.html").Matches( | 196 EXPECT_FALSE(Pattern("file:///tmp/test.html").Matches( |
| 191 GURL("http://example.org/"))); | 197 GURL("http://example.org/"))); |
| 192 } | 198 } |
| 193 | 199 |
| 194 TEST(ContentSettingsPatternTest, FromString_ExtensionPatterns) { | 200 TEST(ContentSettingsPatternTest, FromString_ExtensionPatterns) { |
| 195 EXPECT_TRUE(Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/") | 201 EXPECT_TRUE(Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/") |
| 196 .IsValid()); | 202 .IsValid()); |
| 197 EXPECT_STREQ("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/", | 203 EXPECT_STREQ("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/", |
| 198 Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/") | 204 Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/") |
| 199 .ToString().c_str()); | 205 .ToString().c_str()); |
| 200 EXPECT_TRUE(Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/") | 206 EXPECT_TRUE(Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/") |
| 201 .Matches(GURL("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/"))); | 207 .Matches(GURL("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/"))); |
| 202 } | 208 } |
| 203 | 209 |
| 204 TEST(ContentSettingsPatternTest, FromString_WithIPAdresses) { | 210 TEST(ContentSettingsPatternTest, FromString_WithIPAdresses) { |
| 205 // IPv4 | 211 // IPv4 |
| 206 EXPECT_TRUE(Pattern("192.168.0.1").IsValid()); | 212 EXPECT_TRUE(Pattern("192.168.0.1").IsValid()); |
| 207 EXPECT_STREQ("192.168.1.1", Pattern("192.168.1.1").ToString().c_str()); | 213 EXPECT_STREQ("192.168.1.1", Pattern("192.168.1.1").ToString().c_str()); |
| 208 EXPECT_TRUE(Pattern("https://192.168.0.1:8080").IsValid()); | 214 EXPECT_TRUE(Pattern("https://192.168.0.1:8080").IsValid()); |
| 209 EXPECT_STREQ("https://192.168.0.1:8080", | 215 EXPECT_STREQ("https://192.168.0.1:8080", |
| 210 Pattern("https://192.168.0.1:8080").ToString().c_str()); | 216 Pattern("https://192.168.0.1:8080").ToString().c_str()); |
| 217 |
| 218 // Subdomain wildcards should be only valid for hosts, not for IP addresses. |
| 219 EXPECT_FALSE(Pattern("[*.]127.0.0.1").IsValid()); |
| 220 |
| 211 // IPv6 | 221 // IPv6 |
| 212 EXPECT_TRUE(Pattern("[::1]").IsValid()); | 222 EXPECT_TRUE(Pattern("[::1]").IsValid()); |
| 213 EXPECT_STREQ("[::1]", Pattern("[::1]").ToString().c_str()); | 223 EXPECT_STREQ("[::1]", Pattern("[::1]").ToString().c_str()); |
| 214 EXPECT_TRUE(Pattern("https://[::1]:8080").IsValid()); | 224 EXPECT_TRUE(Pattern("https://[::1]:8080").IsValid()); |
| 215 EXPECT_STREQ("https://[::1]:8080", | 225 EXPECT_STREQ("https://[::1]:8080", |
| 216 Pattern("https://[::1]:8080").ToString().c_str()); | 226 Pattern("https://[::1]:8080").ToString().c_str()); |
| 217 } | 227 } |
| 218 | 228 |
| 219 TEST(ContentSettingsPatternTest, FromString_WithWildcards) { | 229 TEST(ContentSettingsPatternTest, FromString_WithWildcards) { |
| 220 // Creating content settings patterns from strings completes pattern parts | 230 // Creating content settings patterns from strings completes pattern parts |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 | 272 |
| 263 EXPECT_TRUE(Pattern("www.example.com").IsValid()); | 273 EXPECT_TRUE(Pattern("www.example.com").IsValid()); |
| 264 EXPECT_STREQ("www.example.com", | 274 EXPECT_STREQ("www.example.com", |
| 265 Pattern("www.example.com").ToString().c_str()); | 275 Pattern("www.example.com").ToString().c_str()); |
| 266 EXPECT_TRUE(Pattern("www.example.com").Matches( | 276 EXPECT_TRUE(Pattern("www.example.com").Matches( |
| 267 GURL("http://www.example.com/"))); | 277 GURL("http://www.example.com/"))); |
| 268 EXPECT_FALSE(Pattern("example.com").Matches( | 278 EXPECT_FALSE(Pattern("example.com").Matches( |
| 269 GURL("http://example.org/"))); | 279 GURL("http://example.org/"))); |
| 270 | 280 |
| 271 // Patterns with domain wildcard. | 281 // Patterns with domain wildcard. |
| 282 EXPECT_FALSE(Pattern("[*.]").IsValid()); |
| 283 EXPECT_FALSE(Pattern("http://[*.]").IsValid()); |
| 272 EXPECT_TRUE(Pattern("[*.]example.com").IsValid()); | 284 EXPECT_TRUE(Pattern("[*.]example.com").IsValid()); |
| 273 EXPECT_STREQ("[*.]example.com", | 285 EXPECT_STREQ("[*.]example.com", |
| 274 Pattern("[*.]example.com").ToString().c_str()); | 286 Pattern("[*.]example.com").ToString().c_str()); |
| 275 EXPECT_TRUE(Pattern("[*.]example.com").Matches( | 287 EXPECT_TRUE(Pattern("[*.]example.com").Matches( |
| 276 GURL("http://example.com/"))); | 288 GURL("http://example.com/"))); |
| 277 EXPECT_TRUE(Pattern("[*.]example.com").Matches( | 289 EXPECT_TRUE(Pattern("[*.]example.com").Matches( |
| 278 GURL("http://foo.example.com/"))); | 290 GURL("http://foo.example.com/"))); |
| 279 EXPECT_FALSE(Pattern("[*.]example.com").Matches( | 291 EXPECT_FALSE(Pattern("[*.]example.com").Matches( |
| 280 GURL("http://example.org/"))); | 292 GURL("http://example.org/"))); |
| 281 | 293 |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 573 // file:/// normalization. | 585 // file:/// normalization. |
| 574 EXPECT_STREQ("file:///tmp/test.html", | 586 EXPECT_STREQ("file:///tmp/test.html", |
| 575 Pattern("file:///tmp/bar/../test.html").ToString().c_str()); | 587 Pattern("file:///tmp/bar/../test.html").ToString().c_str()); |
| 576 | 588 |
| 577 // Invalid patterns. | 589 // Invalid patterns. |
| 578 EXPECT_STREQ("", Pattern("*example.com").ToString().c_str()); | 590 EXPECT_STREQ("", Pattern("*example.com").ToString().c_str()); |
| 579 EXPECT_STREQ("", Pattern("example.*").ToString().c_str()); | 591 EXPECT_STREQ("", Pattern("example.*").ToString().c_str()); |
| 580 EXPECT_STREQ("", Pattern("*\xC4\x87ira.com").ToString().c_str()); | 592 EXPECT_STREQ("", Pattern("*\xC4\x87ira.com").ToString().c_str()); |
| 581 EXPECT_STREQ("", Pattern("\xC4\x87ira.*").ToString().c_str()); | 593 EXPECT_STREQ("", Pattern("\xC4\x87ira.*").ToString().c_str()); |
| 582 } | 594 } |
| OLD | NEW |