TabStripModel::GetActiveWebContents() might return NULL, and will cause a crash if...
[chromium-blink-merge.git] / chrome / common / content_settings_pattern_parser_unittest.cc
blobb6d5f34f6812abef4745058e6d14b72c113c9af4
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
3 // found in the LICENSE file.
5 #include "chrome/common/content_settings_pattern.h"
6 #include "chrome/common/content_settings_pattern_parser.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace {
12 typedef ContentSettingsPattern::BuilderInterface BuilderInterface;
13 } // namespace
15 class MockBuilder : public ContentSettingsPattern::BuilderInterface {
16 public:
17 MOCK_METHOD0(WithSchemeWildcard, BuilderInterface*());
18 MOCK_METHOD0(WithDomainWildcard, BuilderInterface*());
19 MOCK_METHOD0(WithPortWildcard, BuilderInterface*());
20 MOCK_METHOD1(WithScheme, BuilderInterface*(const std::string& scheme));
21 MOCK_METHOD1(WithHost, BuilderInterface*(const std::string& host));
22 MOCK_METHOD1(WithPort, BuilderInterface*(const std::string& port));
23 MOCK_METHOD1(WithPath, BuilderInterface*(const std::string& path));
24 MOCK_METHOD0(WithPathWildcard, BuilderInterface*());
25 MOCK_METHOD0(Invalid, BuilderInterface*());
26 MOCK_METHOD0(Build, ContentSettingsPattern());
29 TEST(ContentSettingsPatternParserTest, ParsePatterns) {
30 // Test valid patterns
31 ::testing::StrictMock<MockBuilder> builder;
33 // WithPathWildcard() is not called for "*". (Need a strict Mock for this
34 // case.)
35 EXPECT_CALL(builder, WithSchemeWildcard()).Times(1).WillOnce(
36 ::testing::Return(&builder));
37 EXPECT_CALL(builder, WithDomainWildcard()).Times(1).WillOnce(
38 ::testing::Return(&builder));
39 EXPECT_CALL(builder, WithPortWildcard()).Times(1).WillOnce(
40 ::testing::Return(&builder));
41 content_settings::PatternParser::Parse("*", &builder);
42 ::testing::Mock::VerifyAndClear(&builder);
44 EXPECT_CALL(builder, WithScheme("http")).Times(1).WillOnce(
45 ::testing::Return(&builder));
46 EXPECT_CALL(builder, WithHost("www.youtube.com")).Times(1).WillOnce(
47 ::testing::Return(&builder));
48 EXPECT_CALL(builder, WithPort("8080")).Times(1).WillOnce(
49 ::testing::Return(&builder));
50 content_settings::PatternParser::Parse(
51 "http://www.youtube.com:8080", &builder);
52 ::testing::Mock::VerifyAndClear(&builder);
54 EXPECT_CALL(builder, WithSchemeWildcard()).Times(1).WillOnce(
55 ::testing::Return(&builder));
56 EXPECT_CALL(builder, WithHost("www.gmail.com")).Times(1).WillOnce(
57 ::testing::Return(&builder));
58 EXPECT_CALL(builder, WithPort("80")).Times(1).WillOnce(
59 ::testing::Return(&builder));
60 content_settings::PatternParser::Parse("*://www.gmail.com:80", &builder);
61 ::testing::Mock::VerifyAndClear(&builder);
63 EXPECT_CALL(builder, WithScheme("http")).Times(1).WillOnce(
64 ::testing::Return(&builder));
65 EXPECT_CALL(builder, WithHost("www.gmail.com")).Times(1).WillOnce(
66 ::testing::Return(&builder));
67 EXPECT_CALL(builder, WithPortWildcard()).Times(1).WillOnce(
68 ::testing::Return(&builder));
69 content_settings::PatternParser::Parse("http://www.gmail.com:*", &builder);
70 ::testing::Mock::VerifyAndClear(&builder);
72 EXPECT_CALL(builder, WithScheme("http")).Times(1).WillOnce(
73 ::testing::Return(&builder));
74 EXPECT_CALL(builder, WithDomainWildcard()).WillOnce(
75 ::testing::Return(&builder));
76 EXPECT_CALL(builder, WithHost("google.com")).Times(1).WillOnce(
77 ::testing::Return(&builder));
78 EXPECT_CALL(builder, WithPort("80")).Times(1).WillOnce(
79 ::testing::Return(&builder));
80 content_settings::PatternParser::Parse("http://[*.]google.com:80", &builder);
81 ::testing::Mock::VerifyAndClear(&builder);
83 EXPECT_CALL(builder, WithScheme("https")).Times(1).WillOnce(
84 ::testing::Return(&builder));
85 EXPECT_CALL(builder, WithHost("[::1]")).Times(1).WillOnce(
86 ::testing::Return(&builder));
87 EXPECT_CALL(builder, WithPort("8080")).Times(1).WillOnce(
88 ::testing::Return(&builder));
89 content_settings::PatternParser::Parse("https://[::1]:8080", &builder);
90 ::testing::Mock::VerifyAndClear(&builder);
92 EXPECT_CALL(builder, WithScheme("http")).Times(1).WillOnce(
93 ::testing::Return(&builder));
94 EXPECT_CALL(builder, WithHost("127.0.0.1")).Times(1).WillOnce(
95 ::testing::Return(&builder));
96 EXPECT_CALL(builder, WithPort("8080")).Times(1).WillOnce(
97 ::testing::Return(&builder));
98 content_settings::PatternParser::Parse("http://127.0.0.1:8080", &builder);
99 ::testing::Mock::VerifyAndClear(&builder);
101 // Test valid pattern short forms
102 EXPECT_CALL(builder, WithSchemeWildcard()).Times(1).WillOnce(
103 ::testing::Return(&builder));
104 EXPECT_CALL(builder, WithHost("www.youtube.com")).Times(1).WillOnce(
105 ::testing::Return(&builder));
106 EXPECT_CALL(builder, WithPort("8080")).Times(1).WillOnce(
107 ::testing::Return(&builder));
108 content_settings::PatternParser::Parse("www.youtube.com:8080", &builder);
109 ::testing::Mock::VerifyAndClear(&builder);
111 EXPECT_CALL(builder, WithSchemeWildcard()).Times(1).WillOnce(
112 ::testing::Return(&builder));
113 EXPECT_CALL(builder, WithHost("www.youtube.com")).Times(1).WillOnce(
114 ::testing::Return(&builder));
115 EXPECT_CALL(builder, WithPortWildcard()).Times(1).WillOnce(
116 ::testing::Return(&builder));
117 content_settings::PatternParser::Parse("www.youtube.com", &builder);
118 ::testing::Mock::VerifyAndClear(&builder);
120 EXPECT_CALL(builder, WithSchemeWildcard()).Times(1).WillOnce(
121 ::testing::Return(&builder));
122 EXPECT_CALL(builder, WithDomainWildcard()).Times(1).WillOnce(
123 ::testing::Return(&builder));
124 EXPECT_CALL(builder, WithHost("youtube.com")).Times(1).WillOnce(
125 ::testing::Return(&builder));
126 EXPECT_CALL(builder, WithPortWildcard()).Times(1).WillOnce(
127 ::testing::Return(&builder));
128 content_settings::PatternParser::Parse("[*.]youtube.com", &builder);
129 ::testing::Mock::VerifyAndClear(&builder);
131 // Test invalid patterns
132 EXPECT_CALL(builder, WithSchemeWildcard()).Times(1).WillOnce(
133 ::testing::Return(&builder));
134 EXPECT_CALL(builder, Invalid()).Times(1).WillOnce(
135 ::testing::Return(&builder));
136 content_settings::PatternParser::Parse("*youtube.com", &builder);
137 ::testing::Mock::VerifyAndClear(&builder);
139 EXPECT_CALL(builder, WithSchemeWildcard()).Times(1).WillOnce(
140 ::testing::Return(&builder));
141 EXPECT_CALL(builder, Invalid()).Times(1).WillOnce(
142 ::testing::Return(&builder));
143 content_settings::PatternParser::Parse("*.youtube.com", &builder);
144 ::testing::Mock::VerifyAndClear(&builder);
146 EXPECT_CALL(builder, WithSchemeWildcard()).Times(1).WillOnce(
147 ::testing::Return(&builder));
148 EXPECT_CALL(builder, Invalid()).Times(1).WillOnce(
149 ::testing::Return(&builder));
150 content_settings::PatternParser::Parse("www.youtube.com*", &builder);
151 ::testing::Mock::VerifyAndClear(&builder);
154 TEST(ContentSettingsPatternParserTest, ParseFilePatterns) {
155 ::testing::StrictMock<MockBuilder> builder;
157 EXPECT_CALL(builder, WithScheme("file")).Times(1).WillOnce(
158 ::testing::Return(&builder));
159 EXPECT_CALL(builder, WithPath("/foo/bar/test.html")).Times(1).WillOnce(
160 ::testing::Return(&builder));
161 content_settings::PatternParser::Parse(
162 "file:///foo/bar/test.html", &builder);
163 ::testing::Mock::VerifyAndClear(&builder);
165 EXPECT_CALL(builder, WithScheme("file")).Times(1).WillOnce(
166 ::testing::Return(&builder));
167 EXPECT_CALL(builder, WithDomainWildcard()).Times(1).WillOnce(
168 ::testing::Return(&builder));
169 content_settings::PatternParser::Parse(
170 "file://*", &builder);
171 ::testing::Mock::VerifyAndClear(&builder);
173 EXPECT_CALL(builder, WithScheme("file")).Times(1).WillOnce(
174 ::testing::Return(&builder));
175 EXPECT_CALL(builder, WithDomainWildcard()).Times(1).WillOnce(
176 ::testing::Return(&builder));
177 EXPECT_CALL(builder, WithPath("/")).Times(1).WillOnce(
178 ::testing::Return(&builder));
179 content_settings::PatternParser::Parse(
180 "file://*/", &builder);
181 ::testing::Mock::VerifyAndClear(&builder);
183 EXPECT_CALL(builder, WithScheme("file")).Times(1).WillOnce(
184 ::testing::Return(&builder));
185 EXPECT_CALL(builder, WithDomainWildcard()).Times(1).WillOnce(
186 ::testing::Return(&builder));
187 EXPECT_CALL(builder, WithPathWildcard()).Times(1).WillOnce(
188 ::testing::Return(&builder));
189 content_settings::PatternParser::Parse(
190 "file://*/*", &builder);
191 ::testing::Mock::VerifyAndClear(&builder);
193 EXPECT_CALL(builder, WithScheme("file")).Times(1).WillOnce(
194 ::testing::Return(&builder));
195 EXPECT_CALL(builder, WithPathWildcard()).Times(1).WillOnce(
196 ::testing::Return(&builder));
197 content_settings::PatternParser::Parse(
198 "file:///*", &builder);
199 ::testing::Mock::VerifyAndClear(&builder);
201 // Invalid file patterns.
202 EXPECT_CALL(builder, WithScheme("file")).Times(1).WillOnce(
203 ::testing::Return(&builder));
204 EXPECT_CALL(builder, Invalid()).Times(1).WillOnce(
205 ::testing::Return(&builder));
206 content_settings::PatternParser::Parse(
207 "file://**", &builder);
208 ::testing::Mock::VerifyAndClear(&builder);
211 TEST(ContentSettingsPatternParserTest, SerializePatterns) {
212 ContentSettingsPattern::PatternParts parts;
213 parts.scheme = "http";
214 parts.host = "www.youtube.com";
215 parts.port = "8080";
216 EXPECT_STREQ("http://www.youtube.com:8080",
217 content_settings::PatternParser::ToString(parts).c_str());
219 parts = ContentSettingsPattern::PatternParts();
220 parts.scheme = "file";
221 parts.path = "/foo/bar/test.html";
222 EXPECT_STREQ("file:///foo/bar/test.html",
223 content_settings::PatternParser::ToString(parts).c_str());
225 parts = ContentSettingsPattern::PatternParts();
226 parts.scheme = "file";
227 parts.path = "";
228 parts.is_path_wildcard = true;
229 EXPECT_EQ("file:///*", content_settings::PatternParser::ToString(parts));