OLD | NEW |
1 // Copyright (c) 2012 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 { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 EXPECT_FALSE(pattern.Matches(GURL("https://mail.google.com"))); | 66 EXPECT_FALSE(pattern.Matches(GURL("https://mail.google.com"))); |
67 EXPECT_TRUE(pattern.Matches(GURL("https://www.google.com"))); | 67 EXPECT_TRUE(pattern.Matches(GURL("https://www.google.com"))); |
68 | 68 |
69 pattern = ContentSettingsPattern::FromURL(GURL("http://www.google.com:80")); | 69 pattern = ContentSettingsPattern::FromURL(GURL("http://www.google.com:80")); |
70 EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com"))); | 70 EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com"))); |
71 EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com:80"))); | 71 EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com:80"))); |
72 EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com:81"))); | 72 EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com:81"))); |
73 | 73 |
74 pattern = ContentSettingsPattern::FromURL(GURL("https://www.google.com:443")); | 74 pattern = ContentSettingsPattern::FromURL(GURL("https://www.google.com:443")); |
75 EXPECT_TRUE(pattern.Matches(GURL("https://www.google.com"))); | 75 EXPECT_TRUE(pattern.Matches(GURL("https://www.google.com"))); |
| 76 EXPECT_TRUE(pattern.Matches(GURL("https://foo.www.google.com"))); |
76 EXPECT_TRUE(pattern.Matches(GURL("https://www.google.com:443"))); | 77 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("https://www.google.com:444"))); |
78 EXPECT_FALSE(pattern.Matches(GURL("http://www.google.com:443"))); | 79 EXPECT_FALSE(pattern.Matches(GURL("http://www.google.com:443"))); |
79 | 80 |
80 pattern = ContentSettingsPattern::FromURL(GURL("https://127.0.0.1")); | 81 pattern = ContentSettingsPattern::FromURL(GURL("https://127.0.0.1")); |
81 EXPECT_TRUE(pattern.IsValid()); | 82 EXPECT_TRUE(pattern.IsValid()); |
82 EXPECT_STREQ("https://127.0.0.1:443", pattern.ToString().c_str()); | 83 EXPECT_STREQ("https://127.0.0.1:443", pattern.ToString().c_str()); |
83 | 84 |
84 pattern = ContentSettingsPattern::FromURL(GURL("http://[::1]")); | 85 pattern = ContentSettingsPattern::FromURL(GURL("http://[::1]")); |
85 EXPECT_TRUE(pattern.IsValid()); | 86 EXPECT_TRUE(pattern.IsValid()); |
86 | 87 |
87 pattern = ContentSettingsPattern::FromURL(GURL("file:///foo/bar.html")); | 88 pattern = ContentSettingsPattern::FromURL(GURL("file:///foo/bar.html")); |
88 EXPECT_TRUE(pattern.IsValid()); | 89 EXPECT_TRUE(pattern.IsValid()); |
89 EXPECT_EQ("file:///foo/bar.html", pattern.ToString()); | 90 EXPECT_EQ("file:///foo/bar.html", pattern.ToString()); |
90 } | 91 } |
91 | 92 |
| 93 TEST(ContentSettingsPatternTest, FilesystemUrls) { |
| 94 ContentSettingsPattern pattern = |
| 95 ContentSettingsPattern::FromURL(GURL("http://www.google.com")); |
| 96 EXPECT_TRUE(pattern.Matches( |
| 97 GURL("filesystem:http://www.google.com/temporary/"))); |
| 98 EXPECT_TRUE(pattern.Matches( |
| 99 GURL("filesystem:http://foo.www.google.com/temporary/"))); |
| 100 EXPECT_TRUE(pattern.Matches( |
| 101 GURL("filesystem:http://www.google.com:80/temporary/"))); |
| 102 EXPECT_TRUE(pattern.Matches( |
| 103 GURL("filesystem:http://www.google.com:81/temporary/"))); |
| 104 |
| 105 pattern = ContentSettingsPattern::FromURL(GURL("https://www.google.com")); |
| 106 EXPECT_TRUE(pattern.Matches( |
| 107 GURL("filesystem:https://www.google.com/temporary/"))); |
| 108 EXPECT_TRUE(pattern.Matches( |
| 109 GURL("filesystem:https://www.google.com:443/temporary/"))); |
| 110 EXPECT_TRUE(pattern.Matches( |
| 111 GURL("filesystem:https://foo.www.google.com/temporary/"))); |
| 112 EXPECT_FALSE(pattern.Matches( |
| 113 GURL("filesystem:https://www.google.com:81/temporary/"))); |
| 114 |
| 115 // A pattern from a filesystem URLs is equivalent to a pattern from the inner |
| 116 // URL of the filesystem URL. |
| 117 ContentSettingsPattern pattern2 = ContentSettingsPattern::FromURL( |
| 118 GURL("filesystem:https://www.google.com/temporary/")); |
| 119 EXPECT_EQ(ContentSettingsPattern::IDENTITY, pattern.Compare(pattern2)); |
| 120 |
| 121 EXPECT_STREQ("https://[*.]www.google.com:443", pattern2.ToString().c_str()); |
| 122 |
| 123 pattern = |
| 124 ContentSettingsPattern::FromURL( |
| 125 GURL("filesystem:file:///temporary/foo/bar")); |
| 126 EXPECT_TRUE(pattern.Matches(GURL("filesystem:file:///temporary/"))); |
| 127 EXPECT_TRUE(pattern.Matches(GURL("filesystem:file:///temporary/test.txt"))); |
| 128 EXPECT_TRUE(pattern.Matches(GURL("file:///temporary"))); |
| 129 EXPECT_FALSE(pattern.Matches(GURL("file://foo/bar"))); |
| 130 pattern2 = |
| 131 ContentSettingsPattern::FromURL( |
| 132 GURL("filesystem:file:///persistent/foo2/bar2")); |
| 133 EXPECT_EQ(ContentSettingsPattern::IDENTITY, pattern.Compare(pattern2)); |
| 134 } |
| 135 |
92 TEST(ContentSettingsPatternTest, FromURLNoWildcard) { | 136 TEST(ContentSettingsPatternTest, FromURLNoWildcard) { |
93 // If no port is specifed GURLs always use the default port for the schemes | 137 // 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 | 138 // HTTP and HTTPS. Hence a GURL always carries a port specification either |
95 // explicitly or implicitly. Therefore if a content settings pattern is | 139 // explicitly or implicitly. Therefore if a content settings pattern is |
96 // created from a GURL with no wildcard, specific values are used for the | 140 // created from a GURL with no wildcard, specific values are used for the |
97 // scheme, host and port part of the pattern. | 141 // scheme, host and port part of the pattern. |
98 // Creating content settings patterns from strings behaves different. Pattern | 142 // Creating content settings patterns from strings behaves different. Pattern |
99 // parts that are omitted in pattern specifications (strings), are completed | 143 // parts that are omitted in pattern specifications (strings), are completed |
100 // with a wildcard. | 144 // with a wildcard. |
101 ContentSettingsPattern pattern = ContentSettingsPattern::FromURLNoWildcard( | 145 ContentSettingsPattern pattern = ContentSettingsPattern::FromURLNoWildcard( |
102 GURL("http://www.example.com")); | 146 GURL("http://www.example.com")); |
103 EXPECT_TRUE(pattern.IsValid()); | 147 EXPECT_TRUE(pattern.IsValid()); |
104 EXPECT_STREQ("http://www.example.com:80", pattern.ToString().c_str()); | 148 EXPECT_STREQ("http://www.example.com:80", pattern.ToString().c_str()); |
105 EXPECT_TRUE(pattern.Matches(GURL("http://www.example.com"))); | 149 EXPECT_TRUE(pattern.Matches(GURL("http://www.example.com"))); |
106 EXPECT_FALSE(pattern.Matches(GURL("https://www.example.com"))); | 150 EXPECT_FALSE(pattern.Matches(GURL("https://www.example.com"))); |
107 EXPECT_FALSE(pattern.Matches(GURL("http://foo.www.example.com"))); | 151 EXPECT_FALSE(pattern.Matches(GURL("http://foo.www.example.com"))); |
108 | 152 |
109 pattern = ContentSettingsPattern::FromURLNoWildcard( | 153 pattern = ContentSettingsPattern::FromURLNoWildcard( |
110 GURL("https://www.example.com")); | 154 GURL("https://www.example.com")); |
111 EXPECT_TRUE(pattern.IsValid()); | 155 EXPECT_TRUE(pattern.IsValid()); |
112 EXPECT_STREQ("https://www.example.com:443", pattern.ToString().c_str()); | 156 EXPECT_STREQ("https://www.example.com:443", pattern.ToString().c_str()); |
113 EXPECT_FALSE(pattern.Matches(GURL("http://www.example.com"))); | 157 EXPECT_FALSE(pattern.Matches(GURL("http://www.example.com"))); |
114 EXPECT_TRUE(pattern.Matches(GURL("https://www.example.com"))); | 158 EXPECT_TRUE(pattern.Matches(GURL("https://www.example.com"))); |
115 EXPECT_FALSE(pattern.Matches(GURL("http://foo.www.example.com"))); | 159 EXPECT_FALSE(pattern.Matches(GURL("http://foo.www.example.com"))); |
116 | 160 |
117 pattern = ContentSettingsPattern::FromURLNoWildcard( | 161 // Pattern for filesystem URLs |
118 GURL("https://www.example.com")); | 162 pattern = |
| 163 ContentSettingsPattern::FromURLNoWildcard( |
| 164 GURL("filesystem:http://www.google.com/temporary/")); |
| 165 EXPECT_TRUE(pattern.IsValid()); |
| 166 EXPECT_TRUE(pattern.Matches(GURL("http://www.google.com"))); |
| 167 EXPECT_FALSE(pattern.Matches(GURL("http://foo.www.google.com"))); |
| 168 EXPECT_TRUE(pattern.Matches( |
| 169 GURL("filesystem:http://www.google.com/persistent/"))); |
| 170 EXPECT_FALSE(pattern.Matches( |
| 171 GURL("filesystem:https://www.google.com/persistent/"))); |
| 172 EXPECT_FALSE(pattern.Matches( |
| 173 GURL("filesystem:https://www.google.com:81/temporary/"))); |
| 174 EXPECT_FALSE(pattern.Matches( |
| 175 GURL("filesystem:https://foo.www.google.com/temporary/"))); |
119 } | 176 } |
120 | 177 |
121 TEST(ContentSettingsPatternTest, Wildcard) { | 178 TEST(ContentSettingsPatternTest, Wildcard) { |
122 EXPECT_TRUE(ContentSettingsPattern::Wildcard().IsValid()); | 179 EXPECT_TRUE(ContentSettingsPattern::Wildcard().IsValid()); |
123 | 180 |
124 EXPECT_TRUE(ContentSettingsPattern::Wildcard().Matches( | 181 EXPECT_TRUE(ContentSettingsPattern::Wildcard().Matches( |
125 GURL("http://www.google.com"))); | 182 GURL("http://www.google.com"))); |
126 EXPECT_TRUE(ContentSettingsPattern::Wildcard().Matches( | 183 EXPECT_TRUE(ContentSettingsPattern::Wildcard().Matches( |
127 GURL("https://www.google.com"))); | 184 GURL("https://www.google.com"))); |
128 EXPECT_TRUE(ContentSettingsPattern::Wildcard().Matches( | 185 EXPECT_TRUE(ContentSettingsPattern::Wildcard().Matches( |
(...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
600 // file:/// normalization. | 657 // file:/// normalization. |
601 EXPECT_STREQ("file:///tmp/test.html", | 658 EXPECT_STREQ("file:///tmp/test.html", |
602 Pattern("file:///tmp/bar/../test.html").ToString().c_str()); | 659 Pattern("file:///tmp/bar/../test.html").ToString().c_str()); |
603 | 660 |
604 // Invalid patterns. | 661 // Invalid patterns. |
605 EXPECT_STREQ("", Pattern("*example.com").ToString().c_str()); | 662 EXPECT_STREQ("", Pattern("*example.com").ToString().c_str()); |
606 EXPECT_STREQ("", Pattern("example.*").ToString().c_str()); | 663 EXPECT_STREQ("", Pattern("example.*").ToString().c_str()); |
607 EXPECT_STREQ("", Pattern("*\xC4\x87ira.com").ToString().c_str()); | 664 EXPECT_STREQ("", Pattern("*\xC4\x87ira.com").ToString().c_str()); |
608 EXPECT_STREQ("", Pattern("\xC4\x87ira.*").ToString().c_str()); | 665 EXPECT_STREQ("", Pattern("\xC4\x87ira.*").ToString().c_str()); |
609 } | 666 } |
OLD | NEW |