summaryrefslogtreecommitdiffstats
path: root/chrome/browser/content_settings/content_settings_pattern_unittest.cc
blob: 88b42dcfe998e00a3016e768cabe27866d8cd1f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/content_settings/content_settings_pattern.h"

#include "googleurl/src/gurl.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

TEST(ContentSettingsPatternTest, PatternSupport) {
  EXPECT_TRUE(ContentSettingsPattern("[*.]example.com").IsValid());
  EXPECT_TRUE(ContentSettingsPattern("example.com").IsValid());
  EXPECT_TRUE(ContentSettingsPattern("192.168.0.1").IsValid());
  EXPECT_TRUE(ContentSettingsPattern("[::1]").IsValid());
  EXPECT_FALSE(ContentSettingsPattern("*example.com").IsValid());
  EXPECT_FALSE(ContentSettingsPattern("example.*").IsValid());
  EXPECT_FALSE(ContentSettingsPattern("http://example.com").IsValid());

  EXPECT_TRUE(ContentSettingsPattern("[*.]example.com").Matches(
              GURL("http://example.com/")));
  EXPECT_TRUE(ContentSettingsPattern("[*.]example.com").Matches(
              GURL("http://www.example.com/")));
  EXPECT_TRUE(ContentSettingsPattern("www.example.com").Matches(
              GURL("http://www.example.com/")));
  EXPECT_FALSE(ContentSettingsPattern("").Matches(
               GURL("http://www.example.com/")));
  EXPECT_FALSE(ContentSettingsPattern("[*.]example.com").Matches(
               GURL("http://example.org/")));
  EXPECT_FALSE(ContentSettingsPattern("example.com").Matches(
               GURL("http://example.org/")));
}

TEST(ContentSettingsPatternTest, CanonicalizePattern) {
  // Basic patterns.
  EXPECT_STREQ("[*.]ikea.com", ContentSettingsPattern("[*.]ikea.com")
      .CanonicalizePattern().c_str());
  EXPECT_STREQ("example.com", ContentSettingsPattern("example.com")
      .CanonicalizePattern().c_str());
  EXPECT_STREQ("192.168.1.1", ContentSettingsPattern("192.168.1.1")
      .CanonicalizePattern().c_str());
  EXPECT_STREQ("[::1]", ContentSettingsPattern("[::1]")
      .CanonicalizePattern().c_str());
  // IsValid returns false for file:/// patterns.
  EXPECT_STREQ("", ContentSettingsPattern(
      "file:///temp/file.html").CanonicalizePattern().c_str());

  // UTF-8 patterns.
  EXPECT_STREQ("[*.]xn--ira-ppa.com", ContentSettingsPattern(
      "[*.]\xC4\x87ira.com").CanonicalizePattern().c_str());
  EXPECT_STREQ("xn--ira-ppa.com", ContentSettingsPattern(
      "\xC4\x87ira.com").CanonicalizePattern().c_str());
  // IsValid returns false for file:/// patterns.
  EXPECT_STREQ("", ContentSettingsPattern(
      "file:///\xC4\x87ira.html").CanonicalizePattern().c_str());

  // Invalid patterns.
  EXPECT_STREQ("", ContentSettingsPattern(
      "*example.com").CanonicalizePattern().c_str());
  EXPECT_STREQ("", ContentSettingsPattern(
      "example.*").CanonicalizePattern().c_str());
  EXPECT_STREQ("", ContentSettingsPattern(
      "*\xC4\x87ira.com").CanonicalizePattern().c_str());
  EXPECT_STREQ("", ContentSettingsPattern(
      "\xC4\x87ira.*").CanonicalizePattern().c_str());
}

}  // namespace