Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / managed_mode / managed_mode_url_filter_unittest.cc
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.
4
5 #include "base/bind.h"
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"
11 #include "url/gurl.h"
12
13 class ManagedModeURLFilterTest : public ::testing::Test,
14                                  public ManagedModeURLFilter::Observer {
15  public:
16   ManagedModeURLFilterTest() : filter_(new ManagedModeURLFilter) {
17     filter_->SetDefaultFilteringBehavior(ManagedModeURLFilter::BLOCK);
18     filter_->AddObserver(this);
19   }
20
21   virtual ~ManagedModeURLFilterTest() {
22     filter_->RemoveObserver(this);
23   }
24
25   // ManagedModeURLFilter::Observer:
26   virtual void OnSiteListUpdated() OVERRIDE {
27     run_loop_.Quit();
28   }
29
30  protected:
31   bool IsURLWhitelisted(const std::string& url) {
32     return filter_->GetFilteringBehaviorForURL(GURL(url)) ==
33            ManagedModeURLFilter::ALLOW;
34   }
35
36   base::MessageLoop message_loop_;
37   base::RunLoop run_loop_;
38   scoped_refptr<ManagedModeURLFilter> filter_;
39 };
40
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);
46   run_loop_.Run();
47
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
59   EXPECT_TRUE(IsURLWhitelisted("bogus://youtube.com/"));
60   EXPECT_TRUE(IsURLWhitelisted("chrome://youtube.com/"));
61   EXPECT_TRUE(IsURLWhitelisted("chrome://extensions/"));
62   EXPECT_TRUE(IsURLWhitelisted("chrome-extension://foo/main.html"));
63   EXPECT_TRUE(IsURLWhitelisted("file:///home/chronos/user/Downloads/img.jpg"));
64 }
65
66 TEST_F(ManagedModeURLFilterTest, Inactive) {
67   filter_->SetDefaultFilteringBehavior(ManagedModeURLFilter::ALLOW);
68
69   std::vector<std::string> list;
70   list.push_back("google.com");
71   filter_->SetFromPatterns(list);
72   run_loop_.Run();
73
74   // If the filter is inactive, every URL should be whitelisted.
75   EXPECT_TRUE(IsURLWhitelisted("http://google.com"));
76   EXPECT_TRUE(IsURLWhitelisted("https://www.example.com"));
77 }
78
79 TEST_F(ManagedModeURLFilterTest, Scheme) {
80   std::vector<std::string> list;
81   // Filter only http, ftp and ws schemes.
82   list.push_back("http://secure.com");
83   list.push_back("ftp://secure.com");
84   list.push_back("ws://secure.com");
85   filter_->SetFromPatterns(list);
86   run_loop_.Run();
87
88   EXPECT_TRUE(IsURLWhitelisted("http://secure.com"));
89   EXPECT_TRUE(IsURLWhitelisted("http://secure.com/whatever"));
90   EXPECT_TRUE(IsURLWhitelisted("ftp://secure.com/"));
91   EXPECT_TRUE(IsURLWhitelisted("ws://secure.com"));
92   EXPECT_FALSE(IsURLWhitelisted("https://secure.com/"));
93   EXPECT_FALSE(IsURLWhitelisted("wss://secure.com"));
94   EXPECT_TRUE(IsURLWhitelisted("http://www.secure.com"));
95   EXPECT_FALSE(IsURLWhitelisted("https://www.secure.com"));
96   EXPECT_FALSE(IsURLWhitelisted("wss://www.secure.com"));
97 }
98
99 TEST_F(ManagedModeURLFilterTest, Path) {
100   std::vector<std::string> list;
101   // Filter only a certain path prefix.
102   list.push_back("path.to/ruin");
103   filter_->SetFromPatterns(list);
104   run_loop_.Run();
105
106   EXPECT_TRUE(IsURLWhitelisted("http://path.to/ruin"));
107   EXPECT_TRUE(IsURLWhitelisted("https://path.to/ruin"));
108   EXPECT_TRUE(IsURLWhitelisted("http://path.to/ruins"));
109   EXPECT_TRUE(IsURLWhitelisted("http://path.to/ruin/signup"));
110   EXPECT_TRUE(IsURLWhitelisted("http://www.path.to/ruin"));
111   EXPECT_FALSE(IsURLWhitelisted("http://path.to/fortune"));
112 }
113
114 TEST_F(ManagedModeURLFilterTest, PathAndScheme) {
115   std::vector<std::string> list;
116   // Filter only a certain path prefix and scheme.
117   list.push_back("https://s.aaa.com/path");
118   filter_->SetFromPatterns(list);
119   run_loop_.Run();
120
121   EXPECT_TRUE(IsURLWhitelisted("https://s.aaa.com/path"));
122   EXPECT_TRUE(IsURLWhitelisted("https://s.aaa.com/path/bbb"));
123   EXPECT_FALSE(IsURLWhitelisted("http://s.aaa.com/path"));
124   EXPECT_FALSE(IsURLWhitelisted("https://aaa.com/path"));
125   EXPECT_FALSE(IsURLWhitelisted("https://x.aaa.com/path"));
126   EXPECT_FALSE(IsURLWhitelisted("https://s.aaa.com/bbb"));
127   EXPECT_FALSE(IsURLWhitelisted("https://s.aaa.com/"));
128 }
129
130 TEST_F(ManagedModeURLFilterTest, Host) {
131   std::vector<std::string> list;
132   // Filter only a certain hostname, without subdomains.
133   list.push_back(".www.example.com");
134   filter_->SetFromPatterns(list);
135   run_loop_.Run();
136
137   EXPECT_TRUE(IsURLWhitelisted("http://www.example.com"));
138   EXPECT_FALSE(IsURLWhitelisted("http://example.com"));
139   EXPECT_FALSE(IsURLWhitelisted("http://subdomain.example.com"));
140 }
141
142 TEST_F(ManagedModeURLFilterTest, IPAddress) {
143   std::vector<std::string> list;
144   // Filter an ip address.
145   list.push_back("123.123.123.123");
146   filter_->SetFromPatterns(list);
147   run_loop_.Run();
148
149   EXPECT_TRUE(IsURLWhitelisted("http://123.123.123.123/"));
150   EXPECT_FALSE(IsURLWhitelisted("http://123.123.123.124/"));
151 }
152
153 TEST_F(ManagedModeURLFilterTest, Canonicalization) {
154   // We assume that the hosts and URLs are already canonicalized.
155   std::map<std::string, bool> hosts;
156   hosts["www.moose.org"] = true;
157   hosts["www.xn--n3h.net"] = true;
158   std::map<GURL, bool> urls;
159   urls[GURL("http://www.example.com/foo/")] = true;
160   urls[GURL("http://www.example.com/%C3%85t%C3%B8mstr%C3%B6m")] = true;
161   filter_->SetManualHosts(&hosts);
162   filter_->SetManualURLs(&urls);
163
164   // Base cases.
165   EXPECT_TRUE(IsURLWhitelisted("http://www.example.com/foo/"));
166   EXPECT_TRUE(IsURLWhitelisted(
167       "http://www.example.com/%C3%85t%C3%B8mstr%C3%B6m"));
168
169   // Verify that non-URI characters are escaped.
170   EXPECT_TRUE(IsURLWhitelisted(
171       "http://www.example.com/\xc3\x85t\xc3\xb8mstr\xc3\xb6m"));
172
173   // Verify that unnecessary URI escapes are unescaped.
174   EXPECT_TRUE(IsURLWhitelisted("http://www.example.com/%66%6F%6F/"));
175
176   // Verify that the default port are removed.
177   EXPECT_TRUE(IsURLWhitelisted("http://www.example.com:80/foo/"));
178
179   // Verify that scheme and hostname are lowercased.
180   EXPECT_TRUE(IsURLWhitelisted("htTp://wWw.eXamPle.com/foo/"));
181   EXPECT_TRUE(IsURLWhitelisted("HttP://WwW.mOOsE.orG/blurp/"));
182
183   // Verify that UTF-8 in hostnames are converted to punycode.
184   EXPECT_TRUE(IsURLWhitelisted("http://www.\xe2\x98\x83\x0a.net/bla/"));
185
186   // Verify that query and ref are stripped.
187   EXPECT_TRUE(IsURLWhitelisted("http://www.example.com/foo/?bar=baz#ref"));
188 }
189
190 TEST_F(ManagedModeURLFilterTest, HasFilteredScheme) {
191   EXPECT_TRUE(
192       ManagedModeURLFilter::HasFilteredScheme(GURL("http://example.com")));
193   EXPECT_TRUE(
194       ManagedModeURLFilter::HasFilteredScheme(GURL("https://example.com")));
195   EXPECT_TRUE(
196       ManagedModeURLFilter::HasFilteredScheme(GURL("ftp://example.com")));
197   EXPECT_TRUE(
198       ManagedModeURLFilter::HasFilteredScheme(GURL("gopher://example.com")));
199   EXPECT_TRUE(
200       ManagedModeURLFilter::HasFilteredScheme(GURL("ws://example.com")));
201   EXPECT_TRUE(
202       ManagedModeURLFilter::HasFilteredScheme(GURL("wss://example.com")));
203
204   EXPECT_FALSE(
205       ManagedModeURLFilter::HasFilteredScheme(GURL("file://example.com")));
206   EXPECT_FALSE(
207       ManagedModeURLFilter::HasFilteredScheme(GURL("filesystem://80cols.com")));
208   EXPECT_FALSE(
209       ManagedModeURLFilter::HasFilteredScheme(GURL("chrome://example.com")));
210   EXPECT_FALSE(
211       ManagedModeURLFilter::HasFilteredScheme(GURL("wtf://example.com")));
212 }
213
214 TEST_F(ManagedModeURLFilterTest, HostMatchesPattern) {
215   EXPECT_TRUE(
216       ManagedModeURLFilter::HostMatchesPattern("www.google.com",
217                                                "*.google.com"));
218   EXPECT_TRUE(
219       ManagedModeURLFilter::HostMatchesPattern("google.com", "*.google.com"));
220   EXPECT_TRUE(
221       ManagedModeURLFilter::HostMatchesPattern("accounts.google.com",
222                                                "*.google.com"));
223   EXPECT_FALSE(
224       ManagedModeURLFilter::HostMatchesPattern("www.google.de",
225                                                "*.google.com"));
226   EXPECT_FALSE(
227       ManagedModeURLFilter::HostMatchesPattern("notgoogle.com",
228                                                "*.google.com"));
229
230
231   EXPECT_TRUE(
232       ManagedModeURLFilter::HostMatchesPattern("www.google.com",
233                                                "www.google.*"));
234   EXPECT_TRUE(
235       ManagedModeURLFilter::HostMatchesPattern("www.google.de",
236                                                "www.google.*"));
237   EXPECT_TRUE(
238       ManagedModeURLFilter::HostMatchesPattern("www.google.co.uk",
239                                                "www.google.*"));
240   EXPECT_FALSE(
241       ManagedModeURLFilter::HostMatchesPattern("www.google.blogspot.com",
242                                                "www.google.*"));
243   EXPECT_FALSE(
244       ManagedModeURLFilter::HostMatchesPattern("www.google", "www.google.*"));
245   EXPECT_FALSE(
246       ManagedModeURLFilter::HostMatchesPattern("google.com", "www.google.*"));
247   EXPECT_FALSE(
248       ManagedModeURLFilter::HostMatchesPattern("mail.google.com",
249                                                "www.google.*"));
250   EXPECT_FALSE(
251       ManagedModeURLFilter::HostMatchesPattern("www.googleplex.com",
252                                                "www.google.*"));
253   EXPECT_FALSE(
254       ManagedModeURLFilter::HostMatchesPattern("www.googleco.uk",
255                                                "www.google.*"));
256
257
258   EXPECT_TRUE(
259       ManagedModeURLFilter::HostMatchesPattern("www.google.com", "*.google.*"));
260   EXPECT_TRUE(
261       ManagedModeURLFilter::HostMatchesPattern("google.com", "*.google.*"));
262   EXPECT_TRUE(
263       ManagedModeURLFilter::HostMatchesPattern("accounts.google.com",
264                                                "*.google.*"));
265   EXPECT_TRUE(
266       ManagedModeURLFilter::HostMatchesPattern("mail.google.com",
267                                                "*.google.*"));
268   EXPECT_TRUE(
269       ManagedModeURLFilter::HostMatchesPattern("www.google.de",
270                                                "*.google.*"));
271   EXPECT_TRUE(
272       ManagedModeURLFilter::HostMatchesPattern("google.de",
273                                                "*.google.*"));
274   EXPECT_FALSE(
275       ManagedModeURLFilter::HostMatchesPattern("google.blogspot.com",
276                                                "*.google.*"));
277   EXPECT_FALSE(
278       ManagedModeURLFilter::HostMatchesPattern("google", "*.google.*"));
279   EXPECT_FALSE(
280       ManagedModeURLFilter::HostMatchesPattern("notgoogle.com", "*.google.*"));
281   EXPECT_FALSE(
282       ManagedModeURLFilter::HostMatchesPattern("www.googleplex.com",
283                                                "*.google.*"));
284
285   // Now test a few invalid patterns. They should never match.
286   EXPECT_FALSE(
287       ManagedModeURLFilter::HostMatchesPattern("www.google.com", ""));
288   EXPECT_FALSE(
289       ManagedModeURLFilter::HostMatchesPattern("www.google.com", "."));
290   EXPECT_FALSE(
291       ManagedModeURLFilter::HostMatchesPattern("www.google.com", "*"));
292   EXPECT_FALSE(
293       ManagedModeURLFilter::HostMatchesPattern("www.google.com", ".*"));
294   EXPECT_FALSE(
295       ManagedModeURLFilter::HostMatchesPattern("www.google.com", "*."));
296   EXPECT_FALSE(
297       ManagedModeURLFilter::HostMatchesPattern("www.google.com", "*.*"));
298   EXPECT_FALSE(
299       ManagedModeURLFilter::HostMatchesPattern("www.google..com", "*..*"));
300   EXPECT_FALSE(
301       ManagedModeURLFilter::HostMatchesPattern("www.google.com", "*.*.com"));
302   EXPECT_FALSE(
303       ManagedModeURLFilter::HostMatchesPattern("www.google.com", "www.*.*"));
304   EXPECT_FALSE(ManagedModeURLFilter::HostMatchesPattern("www.google.com",
305                                                         "*.goo.*le.*"));
306   EXPECT_FALSE(
307       ManagedModeURLFilter::HostMatchesPattern("www.google.com", "*google*"));
308   EXPECT_FALSE(ManagedModeURLFilter::HostMatchesPattern("www.google.com",
309                                                         "www.*.google.com"));
310 }
311
312 TEST_F(ManagedModeURLFilterTest, Patterns) {
313   std::map<std::string, bool> hosts;
314
315   // Initally, the second rule is ignored because has the same value as the
316   // default (block). When we change the default to allow, the first rule is
317   // ignored instead.
318   hosts["*.google.com"] = true;
319   hosts["www.google.*"] = false;
320
321   hosts["accounts.google.com"] = false;
322   hosts["mail.google.com"] = true;
323   filter_->SetManualHosts(&hosts);
324
325   // Initially, the default filtering behavior is BLOCK.
326   EXPECT_TRUE(IsURLWhitelisted("http://www.google.com/foo/"));
327   EXPECT_FALSE(IsURLWhitelisted("http://accounts.google.com/bar/"));
328   EXPECT_FALSE(IsURLWhitelisted("http://www.google.co.uk/blurp/"));
329   EXPECT_TRUE(IsURLWhitelisted("http://mail.google.com/moose/"));
330
331   filter_->SetDefaultFilteringBehavior(ManagedModeURLFilter::ALLOW);
332   EXPECT_FALSE(IsURLWhitelisted("http://www.google.com/foo/"));
333   EXPECT_FALSE(IsURLWhitelisted("http://accounts.google.com/bar/"));
334   EXPECT_FALSE(IsURLWhitelisted("http://www.google.co.uk/blurp/"));
335   EXPECT_TRUE(IsURLWhitelisted("http://mail.google.com/moose/"));
336 }