Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2006-2009 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/common/extensions/url_pattern.h" | |
| 6 #include "testing/gtest/include/gtest/gtest.h" | |
| 7 | |
| 8 // See url_pattern.h for examples of valid and invalid patterns. | |
| 9 | |
| 10 TEST(URLPatternTest, ParseInvalid) { | |
| 11 const char* kInvalidPatterns[] = { | |
| 12 "http", // no scheme | |
| 13 "http://", // no path separator | |
| 14 "http://foo", // no path separator | |
| 15 "http://*foo/bar", // not allowed as substring of host component | |
| 16 "http://foo.*.bar/baz", // must be first component | |
| 17 "http:/bar", // scheme separator not found | |
| 18 "foo://*", // invalid scheme | |
| 19 }; | |
| 20 | |
| 21 for (size_t i = 0; i < arraysize(kInvalidPatterns); ++i) { | |
| 22 URLPattern pattern; | |
| 23 EXPECT_FALSE(pattern.Parse(kInvalidPatterns[i])); | |
| 24 } | |
| 25 }; | |
| 26 | |
| 27 // all pages for a given scheme | |
| 28 TEST(URLPatternTest, Match1) { | |
| 29 URLPattern pattern; | |
| 30 EXPECT_TRUE(pattern.Parse("http://*/*")); | |
| 31 EXPECT_EQ("http", pattern.scheme()); | |
| 32 EXPECT_EQ("", pattern.host()); | |
| 33 EXPECT_TRUE(pattern.match_subdomains()); | |
| 34 EXPECT_EQ("/*", pattern.path()); | |
| 35 EXPECT_TRUE(pattern.MatchesUrl(GURL("http://google.com"))); | |
| 36 EXPECT_TRUE(pattern.MatchesUrl(GURL("http://yahoo.com"))); | |
| 37 EXPECT_TRUE(pattern.MatchesUrl(GURL("http://google.com/foo"))); | |
| 38 EXPECT_FALSE(pattern.MatchesUrl(GURL("https://google.com"))); | |
| 39 } | |
| 40 | |
| 41 // all domains | |
| 42 TEST(URLPatternTest, Match2) { | |
| 43 URLPattern pattern; | |
| 44 EXPECT_TRUE(pattern.Parse("https://*/foo*")); | |
| 45 EXPECT_EQ("https", pattern.scheme()); | |
| 46 EXPECT_EQ("", pattern.host()); | |
| 47 EXPECT_TRUE(pattern.match_subdomains()); | |
| 48 EXPECT_EQ("/foo*", pattern.path()); | |
| 49 EXPECT_TRUE(pattern.MatchesUrl(GURL("https://www.google.com/foo"))); | |
| 50 EXPECT_TRUE(pattern.MatchesUrl(GURL("https://www.google.com/foobar"))); | |
| 51 EXPECT_FALSE(pattern.MatchesUrl(GURL("http://www.google.com/foo"))); | |
| 52 EXPECT_FALSE(pattern.MatchesUrl(GURL("https://www.google.com/"))); | |
| 53 } | |
| 54 | |
| 55 // subdomains | |
| 56 TEST(URLPatternTest, Match3) { | |
| 57 URLPattern pattern; | |
| 58 EXPECT_TRUE(pattern.Parse("http://*.google.com/foo*bar")); | |
| 59 EXPECT_EQ("http", pattern.scheme()); | |
| 60 EXPECT_EQ("google.com", pattern.host()); | |
| 61 EXPECT_TRUE(pattern.match_subdomains()); | |
| 62 EXPECT_EQ("/foo*bar", pattern.path()); | |
| 63 EXPECT_TRUE(pattern.MatchesUrl(GURL("http://google.com/foobar"))); | |
| 64 EXPECT_TRUE(pattern.MatchesUrl(GURL("http://www.google.com/foo?bar"))); | |
| 65 EXPECT_TRUE(pattern.MatchesUrl( | |
| 66 GURL("http://monkey.images.google.com/foooobar"))); | |
| 67 EXPECT_FALSE(pattern.MatchesUrl(GURL("http://yahoo.com/foobar"))); | |
| 68 } | |
| 69 | |
| 70 // odd schemes and normalization | |
| 71 TEST(URLPatternTest, Match4) { | |
| 72 URLPattern pattern; | |
| 73 EXPECT_TRUE(pattern.Parse("chrome-ui://thinger/*")); | |
| 74 EXPECT_EQ("chrome-ui", pattern.scheme()); | |
| 75 EXPECT_EQ("thinger", pattern.host()); | |
| 76 EXPECT_FALSE(pattern.match_subdomains()); | |
| 77 EXPECT_EQ("/*", pattern.path()); | |
| 78 EXPECT_TRUE(pattern.MatchesUrl(GURL("chrome-ui://thinger/foobar"))); | |
| 79 EXPECT_TRUE(pattern.MatchesUrl(GURL("CHROME-UI://thinger/"))); | |
| 80 EXPECT_FALSE(pattern.MatchesUrl(GURL("http://thinger/"))); | |
| 81 } | |
| 82 | |
| 83 // glob escaping | |
| 84 TEST(URLPatternTest, Match5) { | |
| 85 URLPattern pattern; | |
| 86 EXPECT_TRUE(pattern.Parse("file:///foo?bar\\*baz")); | |
| 87 EXPECT_EQ("file", pattern.scheme()); | |
| 88 EXPECT_EQ("", pattern.host()); | |
| 89 EXPECT_FALSE(pattern.match_subdomains()); | |
| 90 EXPECT_EQ("/foo?bar\\*baz", pattern.path()); | |
| 91 EXPECT_TRUE(pattern.MatchesUrl(GURL("file:///foo?bar\\hellobaz"))); | |
| 92 EXPECT_FALSE(pattern.MatchesUrl(GURL("file:///fooXbar\\hellobaz"))); | |
| 93 } | |
| 94 | |
| 95 // ip addresses | |
| 96 TEST(URLPatternTest, Match6) { | |
| 97 URLPattern pattern; | |
| 98 EXPECT_TRUE(pattern.Parse("http://127.0.0.1/*")); | |
| 99 EXPECT_EQ("http", pattern.scheme()); | |
| 100 EXPECT_EQ("127.0.0.1", pattern.host()); | |
| 101 EXPECT_FALSE(pattern.match_subdomains()); | |
| 102 EXPECT_EQ("/*", pattern.path()); | |
| 103 EXPECT_TRUE(pattern.MatchesUrl(GURL("http://127.0.0.1"))); | |
| 104 } | |
| 105 | |
| 106 // subdomain matching with ip addresses | |
| 107 TEST(URLPatternTest, Match7) { | |
| 108 URLPattern pattern; | |
| 109 EXPECT_TRUE(pattern.Parse("http://*.0.0.1/*")); // allowed, but useless | |
| 110 EXPECT_EQ("http", pattern.scheme()); | |
| 111 EXPECT_EQ("0.0.1", pattern.host()); | |
| 112 EXPECT_TRUE(pattern.match_subdomains()); | |
| 113 EXPECT_EQ("/*", pattern.path()); | |
| 114 // Subdomain matching is never done if the argument has an IP address host. | |
| 115 EXPECT_FALSE(pattern.MatchesUrl(GURL("http://127.0.0.1"))); | |
| 116 }; | |
| 117 | |
| 118 // unicode | |
| 119 TEST(URLPatternTest, Match8) { | |
| 120 URLPattern pattern; | |
| 121 // The below is the ASCII encoding of the following URL: | |
| 122 // http://*.\xe1\x80\xbf/a\xc2\x81\xe1* | |
| 123 EXPECT_TRUE(pattern.Parse("http://*.xn--gkd/a%C2%81%E1*")); | |
| 124 EXPECT_EQ("http", pattern.scheme()); | |
| 125 EXPECT_EQ("xn--gkd", pattern.host()); | |
| 126 EXPECT_TRUE(pattern.match_subdomains()); | |
| 127 EXPECT_EQ("/a%C2%81%E1*", pattern.path()); | |
| 128 EXPECT_TRUE(pattern.MatchesUrl( | |
| 129 GURL("http://abc.\xe1\x80\xbf/a\xc2\x81\xe1xyz"))); | |
| 130 EXPECT_TRUE(pattern.MatchesUrl( | |
| 131 GURL("http://\xe1\x80\xbf/a\xc2\x81\xe1\xe1"))); | |
|
Matt Perry
2009/01/31 01:29:14
interesting. what does pattern.MatchesUrl(GURL("h
| |
| 132 }; | |
| OLD | NEW |