summaryrefslogtreecommitdiffstats
path: root/chrome/common/content_settings_pattern_parser_unittest.cc
blob: ed32f59cf226e20119b6343e16115ac7874ddcd8 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright (c) 2011 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/common/content_settings_pattern.h"
#include "chrome/common/content_settings_pattern_parser.h"

#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {
typedef ContentSettingsPattern::BuilderInterface BuilderInterface;
}  // namespace

class MockBuilder : public ContentSettingsPattern::BuilderInterface {
 public:
  MOCK_METHOD0(WithSchemeWildcard, BuilderInterface*());
  MOCK_METHOD0(WithDomainWildcard, BuilderInterface*());
  MOCK_METHOD0(WithPortWildcard, BuilderInterface*());
  MOCK_METHOD1(WithScheme, BuilderInterface*(const std::string& scheme));
  MOCK_METHOD1(WithHost, BuilderInterface*(const std::string& host));
  MOCK_METHOD1(WithPort, BuilderInterface*(const std::string& port));
  MOCK_METHOD1(WithPath, BuilderInterface*(const std::string& path));
  MOCK_METHOD0(Invalid, BuilderInterface*());
  MOCK_METHOD0(Build, ContentSettingsPattern());
};

TEST(ContentSettingsPatternParserTest, ParsePatterns) {
  // Test valid patterns
  MockBuilder builder;

  EXPECT_CALL(builder, WithScheme("http")).Times(1).WillOnce(
      ::testing::Return(&builder));
  EXPECT_CALL(builder, WithHost("www.youtube.com")).Times(1).WillOnce(
      ::testing::Return(&builder));
  EXPECT_CALL(builder, WithPort("8080")).Times(1).WillOnce(
      ::testing::Return(&builder));
  content_settings::PatternParser::Parse(
      "http://www.youtube.com:8080", &builder);
  ::testing::Mock::VerifyAndClear(&builder);

  EXPECT_CALL(builder, WithSchemeWildcard()).Times(1).WillOnce(
      ::testing::Return(&builder));
  EXPECT_CALL(builder, WithHost("www.gmail.com")).Times(1).WillOnce(
      ::testing::Return(&builder));
  EXPECT_CALL(builder, WithPort("80")).Times(1).WillOnce(
      ::testing::Return(&builder));
  content_settings::PatternParser::Parse("*://www.gmail.com:80", &builder);
  ::testing::Mock::VerifyAndClear(&builder);

  EXPECT_CALL(builder, WithScheme("http")).Times(1).WillOnce(
      ::testing::Return(&builder));
  EXPECT_CALL(builder, WithHost("www.gmail.com")).Times(1).WillOnce(
      ::testing::Return(&builder));
  EXPECT_CALL(builder, WithPortWildcard()).Times(1).WillOnce(
      ::testing::Return(&builder));
  content_settings::PatternParser::Parse("http://www.gmail.com:*", &builder);
  ::testing::Mock::VerifyAndClear(&builder);

  EXPECT_CALL(builder, WithScheme("http")).Times(1).WillOnce(
      ::testing::Return(&builder));
  EXPECT_CALL(builder, WithDomainWildcard()).WillOnce(
      ::testing::Return(&builder));
  EXPECT_CALL(builder, WithHost("google.com")).Times(1).WillOnce(
      ::testing::Return(&builder));
  EXPECT_CALL(builder, WithPort("80")).Times(1).WillOnce(
      ::testing::Return(&builder));
  content_settings::PatternParser::Parse("http://[*.]google.com:80", &builder);
  ::testing::Mock::VerifyAndClear(&builder);

  EXPECT_CALL(builder, WithScheme("https")).Times(1).WillOnce(
      ::testing::Return(&builder));
  EXPECT_CALL(builder, WithHost("[::1]")).Times(1).WillOnce(
      ::testing::Return(&builder));
  EXPECT_CALL(builder, WithPort("8080")).Times(1).WillOnce(
      ::testing::Return(&builder));
  content_settings::PatternParser::Parse("https://[::1]:8080", &builder);
  ::testing::Mock::VerifyAndClear(&builder);

  EXPECT_CALL(builder, WithScheme("http")).Times(1).WillOnce(
      ::testing::Return(&builder));
  EXPECT_CALL(builder, WithHost("127.0.0.1")).Times(1).WillOnce(
      ::testing::Return(&builder));
  EXPECT_CALL(builder, WithPort("8080")).Times(1).WillOnce(
      ::testing::Return(&builder));
  content_settings::PatternParser::Parse("http://127.0.0.1:8080", &builder);
  ::testing::Mock::VerifyAndClear(&builder);

  EXPECT_CALL(builder, WithScheme("file")).Times(1).WillOnce(
      ::testing::Return(&builder));
  EXPECT_CALL(builder, WithPath("/foo/bar/test.html")).Times(1).WillOnce(
      ::testing::Return(&builder));
  content_settings::PatternParser::Parse(
      "file:///foo/bar/test.html", &builder);
  ::testing::Mock::VerifyAndClear(&builder);

  // Test valid pattern short forms
  EXPECT_CALL(builder, WithSchemeWildcard()).Times(1).WillOnce(
      ::testing::Return(&builder));
  EXPECT_CALL(builder, WithHost("www.youtube.com")).Times(1).WillOnce(
      ::testing::Return(&builder));
  EXPECT_CALL(builder, WithPort("8080")).Times(1).WillOnce(
      ::testing::Return(&builder));
  content_settings::PatternParser::Parse("www.youtube.com:8080", &builder);
  ::testing::Mock::VerifyAndClear(&builder);

  EXPECT_CALL(builder, WithSchemeWildcard()).Times(1).WillOnce(
      ::testing::Return(&builder));
  EXPECT_CALL(builder, WithHost("www.youtube.com")).Times(1).WillOnce(
      ::testing::Return(&builder));
  EXPECT_CALL(builder, WithPortWildcard()).Times(1).WillOnce(
      ::testing::Return(&builder));
  content_settings::PatternParser::Parse("www.youtube.com", &builder);
  ::testing::Mock::VerifyAndClear(&builder);

  EXPECT_CALL(builder, WithSchemeWildcard()).Times(1).WillOnce(
      ::testing::Return(&builder));
  EXPECT_CALL(builder, WithDomainWildcard()).Times(1).WillOnce(
      ::testing::Return(&builder));
  EXPECT_CALL(builder, WithHost("youtube.com")).Times(1).WillOnce(
      ::testing::Return(&builder));
  EXPECT_CALL(builder, WithPortWildcard()).Times(1).WillOnce(
      ::testing::Return(&builder));
  content_settings::PatternParser::Parse("[*.]youtube.com", &builder);
  ::testing::Mock::VerifyAndClear(&builder);

  // Test invalid patterns
  EXPECT_CALL(builder, Invalid()).Times(1).WillOnce(
      ::testing::Return(&builder));
  content_settings::PatternParser::Parse("*youtube.com", &builder);
  ::testing::Mock::VerifyAndClear(&builder);

  EXPECT_CALL(builder, Invalid()).Times(1).WillOnce(
      ::testing::Return(&builder));
  content_settings::PatternParser::Parse("*.youtube.com", &builder);
  ::testing::Mock::VerifyAndClear(&builder);

  EXPECT_CALL(builder, Invalid()).Times(1).WillOnce(
      ::testing::Return(&builder));
  content_settings::PatternParser::Parse("www.youtube.com*", &builder);
  ::testing::Mock::VerifyAndClear(&builder);
}

TEST(ContentSettingsPatternParserTest, SerializePatterns) {
  ContentSettingsPattern::PatternParts parts;
  parts.scheme = "http";
  parts.host = "www.youtube.com";
  parts.port = "8080";
  EXPECT_STREQ("http://www.youtube.com:8080",
               content_settings::PatternParser::ToString(parts).c_str());

  parts = ContentSettingsPattern::PatternParts();
  parts.scheme = "file";
  parts.path = "/foo/bar/test.html";
  EXPECT_STREQ("file:///foo/bar/test.html",
               content_settings::PatternParser::ToString(parts).c_str());
}