Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(328)

Side by Side Diff: chrome/common/content_settings_pattern_unittest.cc

Issue 8676020: Detect invalid content settings pattern that were not detected yet. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/common/content_settings_pattern_parser.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 GURL("https://www.google.com"))); 305 GURL("https://www.google.com")));
296 306
297 EXPECT_TRUE(Pattern("[*.]google.com:4321").Matches( 307 EXPECT_TRUE(Pattern("[*.]google.com:4321").Matches(
298 GURL("https://mail.google.com:4321"))); 308 GURL("https://mail.google.com:4321")));
299 EXPECT_TRUE(Pattern("[*.]example.com").Matches( 309 EXPECT_TRUE(Pattern("[*.]example.com").Matches(
300 GURL("http://example.com/"))); 310 GURL("http://example.com/")));
301 EXPECT_TRUE(Pattern("[*.]example.com").Matches( 311 EXPECT_TRUE(Pattern("[*.]example.com").Matches(
302 GURL("http://www.example.com/"))); 312 GURL("http://www.example.com/")));
303 313
304 // Patterns with host wildcard 314 // Patterns with host wildcard
305 // TODO(markusheintz): Should these patterns be allowed? 315 EXPECT_TRUE(Pattern("[*.]").IsValid());
306 // EXPECT_TRUE(Pattern("http://*").IsValid()); 316 EXPECT_TRUE(Pattern("http://*").IsValid());
307 // EXPECT_TRUE(Pattern("http://*:8080").IsValid()); 317 EXPECT_TRUE(Pattern("http://[*.]").IsValid());
318 EXPECT_EQ(std::string("http://*"), Pattern("http://[*.]").ToString());
319 EXPECT_TRUE(Pattern("http://*:8080").IsValid());
308 EXPECT_TRUE(Pattern("*://*").IsValid()); 320 EXPECT_TRUE(Pattern("*://*").IsValid());
309 EXPECT_STREQ("*", Pattern("*://*").ToString().c_str()); 321 EXPECT_STREQ("*", Pattern("*://*").ToString().c_str());
310 } 322 }
311 323
312 TEST(ContentSettingsPatternTest, FromString_Canonicalized) { 324 TEST(ContentSettingsPatternTest, FromString_Canonicalized) {
313 // UTF-8 patterns. 325 // UTF-8 patterns.
314 EXPECT_TRUE(Pattern("[*.]\xC4\x87ira.com").IsValid()); 326 EXPECT_TRUE(Pattern("[*.]\xC4\x87ira.com").IsValid());
315 EXPECT_STREQ("[*.]xn--ira-ppa.com", 327 EXPECT_STREQ("[*.]xn--ira-ppa.com",
316 Pattern("[*.]\xC4\x87ira.com").ToString().c_str()); 328 Pattern("[*.]\xC4\x87ira.com").ToString().c_str());
317 EXPECT_TRUE(Pattern("\xC4\x87ira.com").IsValid()); 329 EXPECT_TRUE(Pattern("\xC4\x87ira.com").IsValid());
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 }
OLDNEW
« no previous file with comments | « chrome/common/content_settings_pattern_parser.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698