| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/content_settings/content_settings_pattern.h" | |
| 6 | |
| 7 #include "googleurl/src/gurl.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.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://www.google.com:443"))); | |
| 77 EXPECT_FALSE(pattern.Matches(GURL("https://www.google.com:444"))); | |
| 78 EXPECT_FALSE(pattern.Matches(GURL("http://www.google.com:443"))); | |
| 79 | |
| 80 pattern = ContentSettingsPattern::FromURL(GURL("https://127.0.0.1")); | |
| 81 EXPECT_TRUE(pattern.IsValid()); | |
| 82 EXPECT_STREQ("https://127.0.0.1:443", pattern.ToString().c_str()); | |
| 83 | |
| 84 pattern = ContentSettingsPattern::FromURL(GURL("http://[::1]")); | |
| 85 EXPECT_TRUE(pattern.IsValid()); | |
| 86 | |
| 87 pattern = ContentSettingsPattern::FromURL(GURL("file:///foo/bar.html")); | |
| 88 EXPECT_TRUE(pattern.IsValid()); | |
| 89 EXPECT_STREQ("file:///foo/bar.html", pattern.ToString().c_str()); | |
| 90 } | |
| 91 | |
| 92 TEST(ContentSettingsPatternTest, FromURLNoWildcard) { | |
| 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 | |
| 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 | |
| 97 // scheme, host and port part of the pattern. | |
| 98 // Creating content settings patterns from strings behaves different. Pattern | |
| 99 // parts that are omitted in pattern specifications (strings), are completed | |
| 100 // with a wildcard. | |
| 101 ContentSettingsPattern pattern = ContentSettingsPattern::FromURLNoWildcard( | |
| 102 GURL("http://www.example.com")); | |
| 103 EXPECT_TRUE(pattern.IsValid()); | |
| 104 EXPECT_STREQ("http://www.example.com:80", pattern.ToString().c_str()); | |
| 105 EXPECT_TRUE(pattern.Matches(GURL("http://www.example.com"))); | |
| 106 EXPECT_FALSE(pattern.Matches(GURL("https://www.example.com"))); | |
| 107 EXPECT_FALSE(pattern.Matches(GURL("http://foo.www.example.com"))); | |
| 108 | |
| 109 pattern = ContentSettingsPattern::FromURLNoWildcard( | |
| 110 GURL("https://www.example.com")); | |
| 111 EXPECT_TRUE(pattern.IsValid()); | |
| 112 EXPECT_STREQ("https://www.example.com:443", pattern.ToString().c_str()); | |
| 113 EXPECT_FALSE(pattern.Matches(GURL("http://www.example.com"))); | |
| 114 EXPECT_TRUE(pattern.Matches(GURL("https://www.example.com"))); | |
| 115 EXPECT_FALSE(pattern.Matches(GURL("http://foo.www.example.com"))); | |
| 116 | |
| 117 pattern = ContentSettingsPattern::FromURLNoWildcard( | |
| 118 GURL("https://www.example.com")); | |
| 119 } | |
| 120 | |
| 121 TEST(ContentSettingsPatternTest, Wildcard) { | |
| 122 EXPECT_TRUE(ContentSettingsPattern::Wildcard().IsValid()); | |
| 123 | |
| 124 EXPECT_TRUE(ContentSettingsPattern::Wildcard().Matches( | |
| 125 GURL("http://www.google.com"))); | |
| 126 EXPECT_TRUE(ContentSettingsPattern::Wildcard().Matches( | |
| 127 GURL("https://www.google.com"))); | |
| 128 EXPECT_TRUE(ContentSettingsPattern::Wildcard().Matches( | |
| 129 GURL("https://myhost:8080"))); | |
| 130 EXPECT_TRUE(ContentSettingsPattern::Wildcard().Matches( | |
| 131 GURL("file:///foo/bar.txt"))); | |
| 132 | |
| 133 EXPECT_STREQ("*", ContentSettingsPattern::Wildcard().ToString().c_str()); | |
| 134 | |
| 135 EXPECT_EQ(ContentSettingsPattern::IDENTITY, | |
| 136 ContentSettingsPattern::Wildcard().Compare( | |
| 137 ContentSettingsPattern::Wildcard())); | |
| 138 } | |
| 139 | |
| 140 TEST(ContentSettingsPatternTest, TrimEndingDotFromHost) { | |
| 141 EXPECT_TRUE(Pattern("www.example.com").IsValid()); | |
| 142 EXPECT_TRUE(Pattern("www.example.com").Matches( | |
| 143 GURL("http://www.example.com"))); | |
| 144 EXPECT_TRUE(Pattern("www.example.com").Matches( | |
| 145 GURL("http://www.example.com."))); | |
| 146 | |
| 147 EXPECT_TRUE(Pattern("www.example.com.").IsValid()); | |
| 148 EXPECT_STREQ("www.example.com", | |
| 149 Pattern("www.example.com.").ToString().c_str()); | |
| 150 | |
| 151 EXPECT_TRUE(Pattern("www.example.com.") == Pattern("www.example.com")); | |
| 152 } | |
| 153 | |
| 154 TEST(ContentSettingsPatternTest, FromString_WithNoWildcards) { | |
| 155 // HTTP patterns with default port. | |
| 156 EXPECT_TRUE(Pattern("http://www.example.com:80").IsValid()); | |
| 157 EXPECT_STREQ("http://www.example.com:80", | |
| 158 Pattern("http://www.example.com:80").ToString().c_str()); | |
| 159 // HTTP patterns with none default port. | |
| 160 EXPECT_TRUE(Pattern("http://www.example.com:81").IsValid()); | |
| 161 EXPECT_STREQ("http://www.example.com:81", | |
| 162 Pattern("http://www.example.com:81").ToString().c_str()); | |
| 163 | |
| 164 // HTTPS patterns with default port. | |
| 165 EXPECT_TRUE(Pattern("https://www.example.com:443").IsValid()); | |
| 166 EXPECT_STREQ("https://www.example.com:443", | |
| 167 Pattern("https://www.example.com:443").ToString().c_str()); | |
| 168 // HTTPS patterns with none default port. | |
| 169 EXPECT_TRUE(Pattern("https://www.example.com:8080").IsValid()); | |
| 170 EXPECT_STREQ("https://www.example.com:8080", | |
| 171 Pattern("https://www.example.com:8080").ToString().c_str()); | |
| 172 } | |
| 173 | |
| 174 TEST(ContentSettingsPatternTest, FromString_FilePatterns) { | |
| 175 EXPECT_TRUE(Pattern("file:///").IsValid()); | |
| 176 EXPECT_STREQ("file:///", | |
| 177 Pattern("file:///").ToString().c_str()); | |
| 178 EXPECT_TRUE(Pattern("file:///").Matches( | |
| 179 GURL("file:///"))); | |
| 180 EXPECT_FALSE(Pattern("file:///").Matches( | |
| 181 GURL("file:///tmp/test.html"))); | |
| 182 | |
| 183 EXPECT_TRUE(Pattern("file:///tmp/test.html").IsValid()); | |
| 184 EXPECT_STREQ("file:///tmp/file.html", | |
| 185 Pattern("file:///tmp/file.html").ToString().c_str()); | |
| 186 EXPECT_TRUE(Pattern("file:///tmp/test.html").Matches( | |
| 187 GURL("file:///tmp/test.html"))); | |
| 188 EXPECT_FALSE(Pattern("file:///tmp/test.html").Matches( | |
| 189 GURL("file:///tmp/other.html"))); | |
| 190 EXPECT_FALSE(Pattern("file:///tmp/test.html").Matches( | |
| 191 GURL("http://example.org/"))); | |
| 192 } | |
| 193 | |
| 194 TEST(ContentSettingsPatternTest, FromString_ExtensionPatterns) { | |
| 195 EXPECT_TRUE(Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/") | |
| 196 .IsValid()); | |
| 197 EXPECT_STREQ("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/", | |
| 198 Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/") | |
| 199 .ToString().c_str()); | |
| 200 EXPECT_TRUE(Pattern("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/") | |
| 201 .Matches(GURL("chrome-extension://peoadpeiejnhkmpaakpnompolbglelel/"))); | |
| 202 } | |
| 203 | |
| 204 TEST(ContentSettingsPatternTest, FromString_WithIPAdresses) { | |
| 205 // IPv4 | |
| 206 EXPECT_TRUE(Pattern("192.168.0.1").IsValid()); | |
| 207 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()); | |
| 209 EXPECT_STREQ("https://192.168.0.1:8080", | |
| 210 Pattern("https://192.168.0.1:8080").ToString().c_str()); | |
| 211 // IPv6 | |
| 212 EXPECT_TRUE(Pattern("[::1]").IsValid()); | |
| 213 EXPECT_STREQ("[::1]", Pattern("[::1]").ToString().c_str()); | |
| 214 EXPECT_TRUE(Pattern("https://[::1]:8080").IsValid()); | |
| 215 EXPECT_STREQ("https://[::1]:8080", | |
| 216 Pattern("https://[::1]:8080").ToString().c_str()); | |
| 217 } | |
| 218 | |
| 219 TEST(ContentSettingsPatternTest, FromString_WithWildcards) { | |
| 220 // Creating content settings patterns from strings completes pattern parts | |
| 221 // that are omitted in pattern specifications (strings) with a wildcard. | |
| 222 | |
| 223 // The wildcard pattern. | |
| 224 EXPECT_TRUE(Pattern("*").IsValid()); | |
| 225 EXPECT_STREQ("*", Pattern("*").ToString().c_str()); | |
| 226 EXPECT_EQ(ContentSettingsPattern::IDENTITY, | |
| 227 Pattern("*").Compare(ContentSettingsPattern::Wildcard())); | |
| 228 | |
| 229 // Patterns with port wildcard. | |
| 230 EXPECT_TRUE(Pattern("http://example.com:*").IsValid()); | |
| 231 EXPECT_STREQ("http://example.com", | |
| 232 Pattern("http://example.com:*").ToString().c_str()); | |
| 233 | |
| 234 EXPECT_TRUE(Pattern("https://example.com").IsValid()); | |
| 235 EXPECT_STREQ("https://example.com", | |
| 236 Pattern("https://example.com").ToString().c_str()); | |
| 237 | |
| 238 EXPECT_TRUE(Pattern("*://www.google.com.com:8080").IsValid()); | |
| 239 EXPECT_STREQ("www.google.com:8080", | |
| 240 Pattern("*://www.google.com:8080").ToString().c_str()); | |
| 241 EXPECT_TRUE(Pattern("*://www.google.com:8080").Matches( | |
| 242 GURL("http://www.google.com:8080"))); | |
| 243 EXPECT_TRUE(Pattern("*://www.google.com:8080").Matches( | |
| 244 GURL("https://www.google.com:8080"))); | |
| 245 EXPECT_FALSE( | |
| 246 Pattern("*://www.google.com").Matches(GURL("file:///foo/bar.html"))); | |
| 247 | |
| 248 EXPECT_TRUE(Pattern("www.example.com:8080").IsValid()); | |
| 249 | |
| 250 // Patterns with port and scheme wildcard. | |
| 251 EXPECT_TRUE(Pattern("*://www.example.com:*").IsValid()); | |
| 252 EXPECT_STREQ("www.example.com", | |
| 253 Pattern("*://www.example.com:*").ToString().c_str()); | |
| 254 | |
| 255 EXPECT_TRUE(Pattern("*://www.example.com").IsValid()); | |
| 256 EXPECT_STREQ("www.example.com", | |
| 257 Pattern("*://www.example.com").ToString().c_str()); | |
| 258 | |
| 259 EXPECT_TRUE(Pattern("www.example.com:*").IsValid()); | |
| 260 EXPECT_STREQ("www.example.com", | |
| 261 Pattern("www.example.com:*").ToString().c_str()); | |
| 262 | |
| 263 EXPECT_TRUE(Pattern("www.example.com").IsValid()); | |
| 264 EXPECT_STREQ("www.example.com", | |
| 265 Pattern("www.example.com").ToString().c_str()); | |
| 266 EXPECT_TRUE(Pattern("www.example.com").Matches( | |
| 267 GURL("http://www.example.com/"))); | |
| 268 EXPECT_FALSE(Pattern("example.com").Matches( | |
| 269 GURL("http://example.org/"))); | |
| 270 | |
| 271 // Patterns with domain wildcard. | |
| 272 EXPECT_TRUE(Pattern("[*.]example.com").IsValid()); | |
| 273 EXPECT_STREQ("[*.]example.com", | |
| 274 Pattern("[*.]example.com").ToString().c_str()); | |
| 275 EXPECT_TRUE(Pattern("[*.]example.com").Matches( | |
| 276 GURL("http://example.com/"))); | |
| 277 EXPECT_TRUE(Pattern("[*.]example.com").Matches( | |
| 278 GURL("http://foo.example.com/"))); | |
| 279 EXPECT_FALSE(Pattern("[*.]example.com").Matches( | |
| 280 GURL("http://example.org/"))); | |
| 281 | |
| 282 EXPECT_TRUE(Pattern("[*.]google.com:80").Matches( | |
| 283 GURL("http://mail.google.com:80"))); | |
| 284 EXPECT_FALSE(Pattern("[*.]google.com:80").Matches( | |
| 285 GURL("http://mail.google.com:81"))); | |
| 286 EXPECT_TRUE(Pattern("[*.]google.com:80").Matches( | |
| 287 GURL("http://www.google.com"))); | |
| 288 | |
| 289 EXPECT_TRUE(Pattern("[*.]google.com:8080").Matches( | |
| 290 GURL("http://mail.google.com:8080"))); | |
| 291 | |
| 292 EXPECT_TRUE(Pattern("[*.]google.com:443").Matches( | |
| 293 GURL("https://mail.google.com:443"))); | |
| 294 EXPECT_TRUE(Pattern("[*.]google.com:443").Matches( | |
| 295 GURL("https://www.google.com"))); | |
| 296 | |
| 297 EXPECT_TRUE(Pattern("[*.]google.com:4321").Matches( | |
| 298 GURL("https://mail.google.com:4321"))); | |
| 299 EXPECT_TRUE(Pattern("[*.]example.com").Matches( | |
| 300 GURL("http://example.com/"))); | |
| 301 EXPECT_TRUE(Pattern("[*.]example.com").Matches( | |
| 302 GURL("http://www.example.com/"))); | |
| 303 | |
| 304 // Patterns with host wildcard | |
| 305 // TODO(markusheintz): Should these patterns be allowed? | |
| 306 // EXPECT_TRUE(Pattern("http://*").IsValid()); | |
| 307 // EXPECT_TRUE(Pattern("http://*:8080").IsValid()); | |
| 308 EXPECT_TRUE(Pattern("*://*").IsValid()); | |
| 309 EXPECT_STREQ("*", Pattern("*://*").ToString().c_str()); | |
| 310 } | |
| 311 | |
| 312 TEST(ContentSettingsPatternTest, FromString_Canonicalized) { | |
| 313 // UTF-8 patterns. | |
| 314 EXPECT_TRUE(Pattern("[*.]\xC4\x87ira.com").IsValid()); | |
| 315 EXPECT_STREQ("[*.]xn--ira-ppa.com", | |
| 316 Pattern("[*.]\xC4\x87ira.com").ToString().c_str()); | |
| 317 EXPECT_TRUE(Pattern("\xC4\x87ira.com").IsValid()); | |
| 318 EXPECT_STREQ("xn--ira-ppa.com", | |
| 319 Pattern("\xC4\x87ira.com").ToString().c_str()); | |
| 320 EXPECT_TRUE(Pattern("file:///\xC4\x87ira.html").IsValid()); | |
| 321 EXPECT_STREQ("file:///%C4%87ira.html", | |
| 322 Pattern("file:///\xC4\x87ira.html").ToString().c_str()); | |
| 323 | |
| 324 // File path normalization. | |
| 325 EXPECT_TRUE(Pattern("file:///tmp/bar/../test.html").IsValid()); | |
| 326 EXPECT_STREQ("file:///tmp/test.html", | |
| 327 Pattern("file:///tmp/bar/../test.html").ToString().c_str()); | |
| 328 } | |
| 329 | |
| 330 TEST(ContentSettingsPatternTest, InvalidPatterns) { | |
| 331 // StubObserver expects an empty pattern top be returned as empty string. | |
| 332 EXPECT_FALSE(ContentSettingsPattern().IsValid()); | |
| 333 EXPECT_STREQ("", ContentSettingsPattern().ToString().c_str()); | |
| 334 | |
| 335 // Empty pattern string | |
| 336 EXPECT_FALSE(Pattern("").IsValid()); | |
| 337 EXPECT_STREQ("", Pattern("").ToString().c_str()); | |
| 338 | |
| 339 // Pattern strings with invalid scheme part. | |
| 340 EXPECT_FALSE(Pattern("ftp://myhost.org").IsValid()); | |
| 341 EXPECT_STREQ("", Pattern("ftp://myhost.org").ToString().c_str()); | |
| 342 | |
| 343 // Pattern strings with invalid host part. | |
| 344 EXPECT_FALSE(Pattern("*example.com").IsValid()); | |
| 345 EXPECT_STREQ("", Pattern("*example.com").ToString().c_str()); | |
| 346 EXPECT_FALSE(Pattern("example.*").IsValid()); | |
| 347 EXPECT_STREQ("", Pattern("example.*").ToString().c_str()); | |
| 348 EXPECT_FALSE(Pattern("*\xC4\x87ira.com").IsValid()); | |
| 349 EXPECT_STREQ("", Pattern("*\xC4\x87ira.com").ToString().c_str()); | |
| 350 EXPECT_FALSE(Pattern("\xC4\x87ira.*").IsValid()); | |
| 351 EXPECT_STREQ("", Pattern("\xC4\x87ira.*").ToString().c_str()); | |
| 352 | |
| 353 // Pattern strings with invalid port parts. | |
| 354 EXPECT_FALSE(Pattern("example.com:abc").IsValid()); | |
| 355 EXPECT_STREQ("", Pattern("example.com:abc").ToString().c_str()); | |
| 356 | |
| 357 // Invalid file pattern strings. | |
| 358 EXPECT_FALSE(Pattern("file://").IsValid()); | |
| 359 EXPECT_STREQ("", Pattern("file://").ToString().c_str()); | |
| 360 EXPECT_FALSE(Pattern("file:///foo/bar.html:8080").IsValid()); | |
| 361 EXPECT_STREQ("", Pattern("file:///foo/bar.html:8080").ToString().c_str()); | |
| 362 } | |
| 363 | |
| 364 TEST(ContentSettingsPatternTest, UnequalOperator) { | |
| 365 EXPECT_TRUE(Pattern("http://www.foo.com") != Pattern("http://www.foo.com*")); | |
| 366 EXPECT_TRUE(Pattern("http://www.foo.com*") != | |
| 367 ContentSettingsPattern::Wildcard()); | |
| 368 | |
| 369 EXPECT_TRUE(Pattern("http://www.foo.com") != | |
| 370 ContentSettingsPattern::Wildcard()); | |
| 371 | |
| 372 EXPECT_TRUE(Pattern("http://www.foo.com") != Pattern("www.foo.com")); | |
| 373 EXPECT_TRUE(Pattern("http://www.foo.com") != | |
| 374 Pattern("http://www.foo.com:80")); | |
| 375 | |
| 376 EXPECT_FALSE(Pattern("http://www.foo.com") != Pattern("http://www.foo.com")); | |
| 377 EXPECT_TRUE(Pattern("http://www.foo.com") == Pattern("http://www.foo.com")); | |
| 378 } | |
| 379 | |
| 380 TEST(ContentSettingsPatternTest, Compare) { | |
| 381 // Test identical patterns patterns. | |
| 382 ContentSettingsPattern pattern1 = | |
| 383 Pattern("http://www.google.com"); | |
| 384 EXPECT_EQ(ContentSettingsPattern::IDENTITY, pattern1.Compare(pattern1)); | |
| 385 EXPECT_EQ(ContentSettingsPattern::IDENTITY, | |
| 386 Pattern("http://www.google.com:80").Compare( | |
| 387 Pattern("http://www.google.com:80"))); | |
| 388 EXPECT_EQ(ContentSettingsPattern::IDENTITY, | |
| 389 Pattern("*://[*.]google.com:*").Compare( | |
| 390 Pattern("*://[*.]google.com:*"))); | |
| 391 | |
| 392 ContentSettingsPattern invalid_pattern1; | |
| 393 ContentSettingsPattern invalid_pattern2 = | |
| 394 ContentSettingsPattern::FromString("google.com*"); | |
| 395 | |
| 396 // Compare invalid patterns. | |
| 397 EXPECT_TRUE(!invalid_pattern1.IsValid()); | |
| 398 EXPECT_TRUE(!invalid_pattern2.IsValid()); | |
| 399 EXPECT_EQ(ContentSettingsPattern::IDENTITY, | |
| 400 invalid_pattern1.Compare(invalid_pattern2)); | |
| 401 EXPECT_TRUE(invalid_pattern1 == invalid_pattern2); | |
| 402 | |
| 403 // Compare a pattern with an IPv4 addresse to a pattern with a domain name. | |
| 404 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_POST, | |
| 405 Pattern("http://www.google.com").Compare( | |
| 406 Pattern("127.0.0.1"))); | |
| 407 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE, | |
| 408 Pattern("127.0.0.1").Compare( | |
| 409 Pattern("http://www.google.com"))); | |
| 410 EXPECT_TRUE(Pattern("127.0.0.1") > Pattern("http://www.google.com")); | |
| 411 EXPECT_TRUE(Pattern("http://www.google.com") < Pattern("127.0.0.1")); | |
| 412 | |
| 413 // Compare a pattern with an IPv6 address to a patterns with a domain name. | |
| 414 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_POST, | |
| 415 Pattern("http://www.google.com").Compare( | |
| 416 Pattern("[::1]"))); | |
| 417 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE, | |
| 418 Pattern("[::1]").Compare( | |
| 419 Pattern("http://www.google.com"))); | |
| 420 EXPECT_TRUE(Pattern("[::1]") > Pattern("http://www.google.com")); | |
| 421 EXPECT_TRUE(Pattern("http://www.google.com") < Pattern("[::1]")); | |
| 422 | |
| 423 // Compare a pattern with an IPv6 addresse to a pattern with an IPv4 addresse. | |
| 424 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE, | |
| 425 Pattern("127.0.0.1").Compare( | |
| 426 Pattern("[::1]"))); | |
| 427 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_POST, | |
| 428 Pattern("[::1]").Compare( | |
| 429 Pattern("127.0.0.1"))); | |
| 430 EXPECT_TRUE(Pattern("[::1]") < Pattern("127.0.0.1")); | |
| 431 EXPECT_TRUE(Pattern("127.0.0.1") > Pattern("[::1]")); | |
| 432 | |
| 433 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE, | |
| 434 Pattern("http://www.google.com").Compare( | |
| 435 Pattern("http://www.youtube.com"))); | |
| 436 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE, | |
| 437 Pattern("http://[*.]google.com").Compare( | |
| 438 Pattern("http://[*.]youtube.com"))); | |
| 439 | |
| 440 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_POST, | |
| 441 Pattern("http://[*.]host.com").Compare( | |
| 442 Pattern("http://[*.]evilhost.com"))); | |
| 443 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_POST, | |
| 444 Pattern("*://www.google.com:80").Compare( | |
| 445 Pattern("*://www.google.com:8080"))); | |
| 446 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE, | |
| 447 Pattern("https://www.google.com:80").Compare( | |
| 448 Pattern("http://www.google.com:80"))); | |
| 449 | |
| 450 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE, | |
| 451 Pattern("http://[*.]google.com:90").Compare( | |
| 452 Pattern("http://mail.google.com:80"))); | |
| 453 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE, | |
| 454 Pattern("https://[*.]google.com:80").Compare( | |
| 455 Pattern("http://mail.google.com:80"))); | |
| 456 EXPECT_EQ(ContentSettingsPattern::DISJOINT_ORDER_PRE, | |
| 457 Pattern("https://mail.google.com:*").Compare( | |
| 458 Pattern("http://mail.google.com:80"))); | |
| 459 | |
| 460 // Test patterns with different precedences. | |
| 461 EXPECT_EQ(ContentSettingsPattern::PREDECESSOR, | |
| 462 Pattern("mail.google.com").Compare( | |
| 463 Pattern("[*.]google.com"))); | |
| 464 EXPECT_EQ(ContentSettingsPattern::SUCCESSOR, | |
| 465 Pattern("[*.]google.com").Compare( | |
| 466 Pattern("mail.google.com"))); | |
| 467 | |
| 468 EXPECT_EQ(ContentSettingsPattern::PREDECESSOR, | |
| 469 Pattern("[*.]mail.google.com").Compare( | |
| 470 Pattern("[*.]google.com"))); | |
| 471 EXPECT_EQ(ContentSettingsPattern::SUCCESSOR, | |
| 472 Pattern("[*.]google.com").Compare( | |
| 473 Pattern("[*.]mail.google.com"))); | |
| 474 | |
| 475 EXPECT_EQ(ContentSettingsPattern::PREDECESSOR, | |
| 476 Pattern("mail.google.com:80").Compare( | |
| 477 Pattern("mail.google.com:*"))); | |
| 478 EXPECT_EQ(ContentSettingsPattern::SUCCESSOR, | |
| 479 Pattern("mail.google.com:*").Compare( | |
| 480 Pattern("mail.google.com:80"))); | |
| 481 | |
| 482 EXPECT_EQ(ContentSettingsPattern::PREDECESSOR, | |
| 483 Pattern("https://mail.google.com:*").Compare( | |
| 484 Pattern("*://mail.google.com:*"))); | |
| 485 EXPECT_EQ(ContentSettingsPattern::SUCCESSOR, | |
| 486 Pattern("*://mail.google.com:*").Compare( | |
| 487 Pattern("https://mail.google.com:*"))); | |
| 488 | |
| 489 EXPECT_EQ(ContentSettingsPattern::PREDECESSOR, | |
| 490 Pattern("*://mail.google.com:80").Compare( | |
| 491 Pattern("https://mail.google.com:*"))); | |
| 492 EXPECT_EQ(ContentSettingsPattern::SUCCESSOR, | |
| 493 Pattern("https://mail.google.com:*").Compare( | |
| 494 Pattern("*://mail.google.com:80"))); | |
| 495 | |
| 496 // Test the wildcard pattern. | |
| 497 EXPECT_EQ(ContentSettingsPattern::IDENTITY, | |
| 498 ContentSettingsPattern::Wildcard().Compare( | |
| 499 ContentSettingsPattern::Wildcard())); | |
| 500 | |
| 501 EXPECT_EQ(ContentSettingsPattern::PREDECESSOR, | |
| 502 Pattern("[*.]google.com").Compare( | |
| 503 ContentSettingsPattern::Wildcard())); | |
| 504 EXPECT_EQ(ContentSettingsPattern::SUCCESSOR, | |
| 505 ContentSettingsPattern::Wildcard().Compare( | |
| 506 Pattern("[*.]google.com"))); | |
| 507 | |
| 508 EXPECT_EQ(ContentSettingsPattern::PREDECESSOR, | |
| 509 Pattern("mail.google.com").Compare( | |
| 510 ContentSettingsPattern::Wildcard())); | |
| 511 EXPECT_EQ(ContentSettingsPattern::SUCCESSOR, | |
| 512 ContentSettingsPattern::Wildcard().Compare( | |
| 513 Pattern("mail.google.com"))); | |
| 514 } | |
| 515 | |
| 516 // Legacy tests to ensure backwards compatibility. | |
| 517 | |
| 518 TEST(ContentSettingsPatternTest, PatternSupport_Legacy) { | |
| 519 EXPECT_TRUE(Pattern("[*.]example.com").IsValid()); | |
| 520 EXPECT_TRUE(Pattern("example.com").IsValid()); | |
| 521 EXPECT_TRUE(Pattern("192.168.0.1").IsValid()); | |
| 522 EXPECT_TRUE(Pattern("[::1]").IsValid()); | |
| 523 EXPECT_TRUE( | |
| 524 Pattern("file:///tmp/test.html").IsValid()); | |
| 525 EXPECT_FALSE(Pattern("*example.com").IsValid()); | |
| 526 EXPECT_FALSE(Pattern("example.*").IsValid()); | |
| 527 | |
| 528 EXPECT_TRUE( | |
| 529 Pattern("http://example.com").IsValid()); | |
| 530 EXPECT_TRUE( | |
| 531 Pattern("https://example.com").IsValid()); | |
| 532 | |
| 533 EXPECT_TRUE(Pattern("[*.]example.com").Matches( | |
| 534 GURL("http://example.com/"))); | |
| 535 EXPECT_TRUE(Pattern("[*.]example.com").Matches( | |
| 536 GURL("http://www.example.com/"))); | |
| 537 EXPECT_TRUE(Pattern("www.example.com").Matches( | |
| 538 GURL("http://www.example.com/"))); | |
| 539 EXPECT_TRUE( | |
| 540 Pattern("file:///tmp/test.html").Matches( | |
| 541 GURL("file:///tmp/test.html"))); | |
| 542 EXPECT_FALSE(Pattern("").Matches( | |
| 543 GURL("http://www.example.com/"))); | |
| 544 EXPECT_FALSE(Pattern("[*.]example.com").Matches( | |
| 545 GURL("http://example.org/"))); | |
| 546 EXPECT_FALSE(Pattern("example.com").Matches( | |
| 547 GURL("http://example.org/"))); | |
| 548 EXPECT_FALSE( | |
| 549 Pattern("file:///tmp/test.html").Matches( | |
| 550 GURL("file:///tmp/other.html"))); | |
| 551 EXPECT_FALSE( | |
| 552 Pattern("file:///tmp/test.html").Matches( | |
| 553 GURL("http://example.org/"))); | |
| 554 } | |
| 555 | |
| 556 TEST(ContentSettingsPatternTest, CanonicalizePattern_Legacy) { | |
| 557 // Basic patterns. | |
| 558 EXPECT_STREQ("[*.]ikea.com", Pattern("[*.]ikea.com").ToString().c_str()); | |
| 559 EXPECT_STREQ("example.com", Pattern("example.com").ToString().c_str()); | |
| 560 EXPECT_STREQ("192.168.1.1", Pattern("192.168.1.1").ToString().c_str()); | |
| 561 EXPECT_STREQ("[::1]", Pattern("[::1]").ToString().c_str()); | |
| 562 EXPECT_STREQ("file:///tmp/file.html", | |
| 563 Pattern("file:///tmp/file.html").ToString().c_str()); | |
| 564 | |
| 565 // UTF-8 patterns. | |
| 566 EXPECT_STREQ("[*.]xn--ira-ppa.com", | |
| 567 Pattern("[*.]\xC4\x87ira.com").ToString().c_str()); | |
| 568 EXPECT_STREQ("xn--ira-ppa.com", | |
| 569 Pattern("\xC4\x87ira.com").ToString().c_str()); | |
| 570 EXPECT_STREQ("file:///%C4%87ira.html", | |
| 571 Pattern("file:///\xC4\x87ira.html").ToString().c_str()); | |
| 572 | |
| 573 // file:/// normalization. | |
| 574 EXPECT_STREQ("file:///tmp/test.html", | |
| 575 Pattern("file:///tmp/bar/../test.html").ToString().c_str()); | |
| 576 | |
| 577 // Invalid patterns. | |
| 578 EXPECT_STREQ("", Pattern("*example.com").ToString().c_str()); | |
| 579 EXPECT_STREQ("", Pattern("example.*").ToString().c_str()); | |
| 580 EXPECT_STREQ("", Pattern("*\xC4\x87ira.com").ToString().c_str()); | |
| 581 EXPECT_STREQ("", Pattern("\xC4\x87ira.*").ToString().c_str()); | |
| 582 } | |
| OLD | NEW |