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.
6 #include "base/bind_helpers.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "chrome/browser/managed_mode/managed_mode_url_filter.h"
10 #include "testing/gtest/include/gtest/gtest.h"
13 class ManagedModeURLFilterTest : public ::testing::Test,
14 public ManagedModeURLFilter::Observer {
16 ManagedModeURLFilterTest() : filter_(new ManagedModeURLFilter) {
17 filter_->SetDefaultFilteringBehavior(ManagedModeURLFilter::BLOCK);
18 filter_->AddObserver(this);
21 virtual ~ManagedModeURLFilterTest() {
22 filter_->RemoveObserver(this);
25 // ManagedModeURLFilter::Observer:
26 virtual void OnSiteListUpdated() OVERRIDE {
31 bool IsURLWhitelisted(const std::string& url) {
32 return filter_->GetFilteringBehaviorForURL(GURL(url)) ==
33 ManagedModeURLFilter::ALLOW;
36 base::MessageLoop message_loop_;
37 base::RunLoop run_loop_;
38 scoped_refptr<ManagedModeURLFilter> filter_;
41 TEST_F(ManagedModeURLFilterTest, Basic) {
42 std::vector<std::string> list;
43 // Allow domain and all subdomains, for any filtered scheme.
44 list.push_back("google.com");
45 filter_->SetFromPatterns(list);
48 EXPECT_TRUE(IsURLWhitelisted("http://google.com"));
49 EXPECT_TRUE(IsURLWhitelisted("http://google.com/"));
50 EXPECT_TRUE(IsURLWhitelisted("http://google.com/whatever"));
51 EXPECT_TRUE(IsURLWhitelisted("https://google.com/"));
52 EXPECT_FALSE(IsURLWhitelisted("http://notgoogle.com/"));
53 EXPECT_TRUE(IsURLWhitelisted("http://mail.google.com"));
54 EXPECT_TRUE(IsURLWhitelisted("http://x.mail.google.com"));
55 EXPECT_TRUE(IsURLWhitelisted("https://x.mail.google.com/"));
56 EXPECT_TRUE(IsURLWhitelisted("http://x.y.google.com/a/b"));
57 EXPECT_FALSE(IsURLWhitelisted("http://youtube.com/"));
58 EXPECT_TRUE(IsURLWhitelisted("bogus://youtube.com/"));
59 EXPECT_TRUE(IsURLWhitelisted("chrome://youtube.com/"));
62 TEST_F(ManagedModeURLFilterTest, Inactive) {
63 filter_->SetDefaultFilteringBehavior(ManagedModeURLFilter::ALLOW);
65 std::vector<std::string> list;
66 list.push_back("google.com");
67 filter_->SetFromPatterns(list);
70 // If the filter is inactive, every URL should be whitelisted.
71 EXPECT_TRUE(IsURLWhitelisted("http://google.com"));
72 EXPECT_TRUE(IsURLWhitelisted("https://www.example.com"));
75 TEST_F(ManagedModeURLFilterTest, Scheme) {
76 std::vector<std::string> list;
77 // Filter only http, ftp and ws schemes.
78 list.push_back("http://secure.com");
79 list.push_back("ftp://secure.com");
80 list.push_back("ws://secure.com");
81 filter_->SetFromPatterns(list);
84 EXPECT_TRUE(IsURLWhitelisted("http://secure.com"));
85 EXPECT_TRUE(IsURLWhitelisted("http://secure.com/whatever"));
86 EXPECT_TRUE(IsURLWhitelisted("ftp://secure.com/"));
87 EXPECT_TRUE(IsURLWhitelisted("ws://secure.com"));
88 EXPECT_FALSE(IsURLWhitelisted("https://secure.com/"));
89 EXPECT_FALSE(IsURLWhitelisted("wss://secure.com"));
90 EXPECT_TRUE(IsURLWhitelisted("http://www.secure.com"));
91 EXPECT_FALSE(IsURLWhitelisted("https://www.secure.com"));
92 EXPECT_FALSE(IsURLWhitelisted("wss://www.secure.com"));
95 TEST_F(ManagedModeURLFilterTest, Path) {
96 std::vector<std::string> list;
97 // Filter only a certain path prefix.
98 list.push_back("path.to/ruin");
99 filter_->SetFromPatterns(list);
102 EXPECT_TRUE(IsURLWhitelisted("http://path.to/ruin"));
103 EXPECT_TRUE(IsURLWhitelisted("https://path.to/ruin"));
104 EXPECT_TRUE(IsURLWhitelisted("http://path.to/ruins"));
105 EXPECT_TRUE(IsURLWhitelisted("http://path.to/ruin/signup"));
106 EXPECT_TRUE(IsURLWhitelisted("http://www.path.to/ruin"));
107 EXPECT_FALSE(IsURLWhitelisted("http://path.to/fortune"));
110 TEST_F(ManagedModeURLFilterTest, PathAndScheme) {
111 std::vector<std::string> list;
112 // Filter only a certain path prefix and scheme.
113 list.push_back("https://s.aaa.com/path");
114 filter_->SetFromPatterns(list);
117 EXPECT_TRUE(IsURLWhitelisted("https://s.aaa.com/path"));
118 EXPECT_TRUE(IsURLWhitelisted("https://s.aaa.com/path/bbb"));
119 EXPECT_FALSE(IsURLWhitelisted("http://s.aaa.com/path"));
120 EXPECT_FALSE(IsURLWhitelisted("https://aaa.com/path"));
121 EXPECT_FALSE(IsURLWhitelisted("https://x.aaa.com/path"));
122 EXPECT_FALSE(IsURLWhitelisted("https://s.aaa.com/bbb"));
123 EXPECT_FALSE(IsURLWhitelisted("https://s.aaa.com/"));
126 TEST_F(ManagedModeURLFilterTest, Host) {
127 std::vector<std::string> list;
128 // Filter only a certain hostname, without subdomains.
129 list.push_back(".www.example.com");
130 filter_->SetFromPatterns(list);
133 EXPECT_TRUE(IsURLWhitelisted("http://www.example.com"));
134 EXPECT_FALSE(IsURLWhitelisted("http://example.com"));
135 EXPECT_FALSE(IsURLWhitelisted("http://subdomain.example.com"));
138 TEST_F(ManagedModeURLFilterTest, IPAddress) {
139 std::vector<std::string> list;
140 // Filter an ip address.
141 list.push_back("123.123.123.123");
142 filter_->SetFromPatterns(list);
145 EXPECT_TRUE(IsURLWhitelisted("http://123.123.123.123/"));
146 EXPECT_FALSE(IsURLWhitelisted("http://123.123.123.124/"));
149 TEST_F(ManagedModeURLFilterTest, Canonicalization) {
150 // We assume that the hosts and URLs are already canonicalized.
151 std::map<std::string, bool> hosts;
152 hosts["www.moose.org"] = true;
153 hosts["www.xn--n3h.net"] = true;
154 std::map<GURL, bool> urls;
155 urls[GURL("http://www.example.com/foo/")] = true;
156 urls[GURL("http://www.example.com/%C3%85t%C3%B8mstr%C3%B6m")] = true;
157 filter_->SetManualHosts(&hosts);
158 filter_->SetManualURLs(&urls);
161 EXPECT_TRUE(IsURLWhitelisted("http://www.example.com/foo/"));
162 EXPECT_TRUE(IsURLWhitelisted(
163 "http://www.example.com/%C3%85t%C3%B8mstr%C3%B6m"));
165 // Verify that non-URI characters are escaped.
166 EXPECT_TRUE(IsURLWhitelisted(
167 "http://www.example.com/\xc3\x85t\xc3\xb8mstr\xc3\xb6m"));
169 // Verify that unnecessary URI escapes are unescaped.
170 EXPECT_TRUE(IsURLWhitelisted("http://www.example.com/%66%6F%6F/"));
172 // Verify that the default port are removed.
173 EXPECT_TRUE(IsURLWhitelisted("http://www.example.com:80/foo/"));
175 // Verify that scheme and hostname are lowercased.
176 EXPECT_TRUE(IsURLWhitelisted("htTp://wWw.eXamPle.com/foo/"));
177 EXPECT_TRUE(IsURLWhitelisted("HttP://WwW.mOOsE.orG/blurp/"));
179 // Verify that UTF-8 in hostnames are converted to punycode.
180 EXPECT_TRUE(IsURLWhitelisted("http://www.\xe2\x98\x83\x0a.net/bla/"));
182 // Verify that query and ref are stripped.
183 EXPECT_TRUE(IsURLWhitelisted("http://www.example.com/foo/?bar=baz#ref"));
186 TEST_F(ManagedModeURLFilterTest, HasStandardScheme) {
188 ManagedModeURLFilter::HasStandardScheme(GURL("http://example.com")));
190 ManagedModeURLFilter::HasStandardScheme(GURL("https://example.com")));
192 ManagedModeURLFilter::HasStandardScheme(GURL("ftp://example.com")));
194 ManagedModeURLFilter::HasStandardScheme(GURL("gopher://example.com")));
196 ManagedModeURLFilter::HasStandardScheme(GURL("ws://example.com")));
198 ManagedModeURLFilter::HasStandardScheme(GURL("wss://example.com")));
200 ManagedModeURLFilter::HasStandardScheme(GURL("wtf://example.com")));
203 TEST_F(ManagedModeURLFilterTest, HostMatchesPattern) {
205 ManagedModeURLFilter::HostMatchesPattern("www.google.com",
208 ManagedModeURLFilter::HostMatchesPattern("google.com", "*.google.com"));
210 ManagedModeURLFilter::HostMatchesPattern("accounts.google.com",
213 ManagedModeURLFilter::HostMatchesPattern("www.google.de",
216 ManagedModeURLFilter::HostMatchesPattern("notgoogle.com",
221 ManagedModeURLFilter::HostMatchesPattern("www.google.com",
224 ManagedModeURLFilter::HostMatchesPattern("www.google.de",
227 ManagedModeURLFilter::HostMatchesPattern("www.google.co.uk",
230 ManagedModeURLFilter::HostMatchesPattern("www.google.blogspot.com",
233 ManagedModeURLFilter::HostMatchesPattern("www.google", "www.google.*"));
235 ManagedModeURLFilter::HostMatchesPattern("google.com", "www.google.*"));
237 ManagedModeURLFilter::HostMatchesPattern("mail.google.com",
240 ManagedModeURLFilter::HostMatchesPattern("www.googleplex.com",
243 ManagedModeURLFilter::HostMatchesPattern("www.googleco.uk",
248 ManagedModeURLFilter::HostMatchesPattern("www.google.com", "*.google.*"));
250 ManagedModeURLFilter::HostMatchesPattern("google.com", "*.google.*"));
252 ManagedModeURLFilter::HostMatchesPattern("accounts.google.com",
255 ManagedModeURLFilter::HostMatchesPattern("mail.google.com",
258 ManagedModeURLFilter::HostMatchesPattern("www.google.de",
261 ManagedModeURLFilter::HostMatchesPattern("google.de",
264 ManagedModeURLFilter::HostMatchesPattern("google.blogspot.com",
267 ManagedModeURLFilter::HostMatchesPattern("google", "*.google.*"));
269 ManagedModeURLFilter::HostMatchesPattern("notgoogle.com", "*.google.*"));
271 ManagedModeURLFilter::HostMatchesPattern("www.googleplex.com",
274 // Now test a few invalid patterns. They should never match.
276 ManagedModeURLFilter::HostMatchesPattern("www.google.com", ""));
278 ManagedModeURLFilter::HostMatchesPattern("www.google.com", "."));
280 ManagedModeURLFilter::HostMatchesPattern("www.google.com", "*"));
282 ManagedModeURLFilter::HostMatchesPattern("www.google.com", ".*"));
284 ManagedModeURLFilter::HostMatchesPattern("www.google.com", "*."));
286 ManagedModeURLFilter::HostMatchesPattern("www.google.com", "*.*"));
288 ManagedModeURLFilter::HostMatchesPattern("www.google..com", "*..*"));
290 ManagedModeURLFilter::HostMatchesPattern("www.google.com", "*.*.com"));
292 ManagedModeURLFilter::HostMatchesPattern("www.google.com", "www.*.*"));
293 EXPECT_FALSE(ManagedModeURLFilter::HostMatchesPattern("www.google.com",
296 ManagedModeURLFilter::HostMatchesPattern("www.google.com", "*google*"));
297 EXPECT_FALSE(ManagedModeURLFilter::HostMatchesPattern("www.google.com",
298 "www.*.google.com"));
301 TEST_F(ManagedModeURLFilterTest, Patterns) {
302 std::map<std::string, bool> hosts;
304 // Initally, the second rule is ignored because has the same value as the
305 // default (block). When we change the default to allow, the first rule is
307 hosts["*.google.com"] = true;
308 hosts["www.google.*"] = false;
310 hosts["accounts.google.com"] = false;
311 hosts["mail.google.com"] = true;
312 filter_->SetManualHosts(&hosts);
314 // Initially, the default filtering behavior is BLOCK.
315 EXPECT_TRUE(IsURLWhitelisted("http://www.google.com/foo/"));
316 EXPECT_FALSE(IsURLWhitelisted("http://accounts.google.com/bar/"));
317 EXPECT_FALSE(IsURLWhitelisted("http://www.google.co.uk/blurp/"));
318 EXPECT_TRUE(IsURLWhitelisted("http://mail.google.com/moose/"));
320 filter_->SetDefaultFilteringBehavior(ManagedModeURLFilter::ALLOW);
321 EXPECT_FALSE(IsURLWhitelisted("http://www.google.com/foo/"));
322 EXPECT_FALSE(IsURLWhitelisted("http://accounts.google.com/bar/"));
323 EXPECT_FALSE(IsURLWhitelisted("http://www.google.co.uk/blurp/"));
324 EXPECT_TRUE(IsURLWhitelisted("http://mail.google.com/moose/"));