1 // Copyright (c) 2010 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 TEST(ContentSettingsPatternTest, PatternSupport) { 13 EXPECT_TRUE(ContentSettingsPattern("[*.]example.com").IsValid()); 14 EXPECT_TRUE(ContentSettingsPattern("example.com").IsValid()); 15 EXPECT_TRUE(ContentSettingsPattern("192.168.0.1").IsValid()); 16 EXPECT_TRUE(ContentSettingsPattern("[::1]").IsValid()); 17 EXPECT_TRUE(ContentSettingsPattern("file:///tmp/test.html").IsValid()); 18 EXPECT_FALSE(ContentSettingsPattern("*example.com").IsValid()); 19 EXPECT_FALSE(ContentSettingsPattern("example.*").IsValid()); 20 EXPECT_FALSE(ContentSettingsPattern("http://example.com").IsValid()); 21 22 EXPECT_TRUE(ContentSettingsPattern("[*.]example.com").Matches( 23 GURL("http://example.com/"))); 24 EXPECT_TRUE(ContentSettingsPattern("[*.]example.com").Matches( 25 GURL("http://www.example.com/"))); 26 EXPECT_TRUE(ContentSettingsPattern("www.example.com").Matches( 27 GURL("http://www.example.com/"))); 28 EXPECT_TRUE(ContentSettingsPattern("file:///tmp/test.html").Matches( 29 GURL("file:///tmp/test.html"))); 30 EXPECT_FALSE(ContentSettingsPattern("").Matches( 31 GURL("http://www.example.com/"))); 32 EXPECT_FALSE(ContentSettingsPattern("[*.]example.com").Matches( 33 GURL("http://example.org/"))); 34 EXPECT_FALSE(ContentSettingsPattern("example.com").Matches( 35 GURL("http://example.org/"))); 36 EXPECT_FALSE(ContentSettingsPattern("file:///tmp/test.html").Matches( 37 GURL("file:///tmp/other.html"))); 38 EXPECT_FALSE(ContentSettingsPattern("file:///tmp/test.html").Matches( 39 GURL("http://example.org/"))); 40 } 41 42 TEST(ContentSettingsPatternTest, CanonicalizePattern) { 43 // Basic patterns. 44 EXPECT_STREQ("[*.]ikea.com", ContentSettingsPattern("[*.]ikea.com") 45 .CanonicalizePattern().c_str()); 46 EXPECT_STREQ("example.com", ContentSettingsPattern("example.com") 47 .CanonicalizePattern().c_str()); 48 EXPECT_STREQ("192.168.1.1", ContentSettingsPattern("192.168.1.1") 49 .CanonicalizePattern().c_str()); 50 EXPECT_STREQ("[::1]", ContentSettingsPattern("[::1]") 51 .CanonicalizePattern().c_str()); 52 EXPECT_STREQ("file:///tmp/file.html", ContentSettingsPattern( 53 "file:///tmp/file.html").CanonicalizePattern().c_str()); 54 55 // UTF-8 patterns. 56 EXPECT_STREQ("[*.]xn--ira-ppa.com", ContentSettingsPattern( 57 "[*.]\xC4\x87ira.com").CanonicalizePattern().c_str()); 58 EXPECT_STREQ("xn--ira-ppa.com", ContentSettingsPattern( 59 "\xC4\x87ira.com").CanonicalizePattern().c_str()); 60 EXPECT_STREQ("file:///%C4%87ira.html", ContentSettingsPattern( 61 "file:///\xC4\x87ira.html").CanonicalizePattern().c_str()); 62 63 // file:/// normalization. 64 EXPECT_STREQ("file:///tmp/test.html", ContentSettingsPattern( 65 "file:///tmp/bar/../test.html").CanonicalizePattern().c_str()); 66 67 // Invalid patterns. 68 EXPECT_STREQ("", ContentSettingsPattern( 69 "*example.com").CanonicalizePattern().c_str()); 70 EXPECT_STREQ("", ContentSettingsPattern( 71 "example.*").CanonicalizePattern().c_str()); 72 EXPECT_STREQ("", ContentSettingsPattern( 73 "*\xC4\x87ira.com").CanonicalizePattern().c_str()); 74 EXPECT_STREQ("", ContentSettingsPattern( 75 "\xC4\x87ira.*").CanonicalizePattern().c_str()); 76 } 77 78 } // namespace 79