Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / supervised_user / supervised_user_url_filter_unittest.cc
1 // Copyright 2014 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/supervised_user/supervised_user_url_filter.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "url/gurl.h"
12
13 class SupervisedUserURLFilterTest : public ::testing::Test,
14                                     public SupervisedUserURLFilter::Observer {
15  public:
16   SupervisedUserURLFilterTest() : filter_(new SupervisedUserURLFilter) {
17     filter_->SetDefaultFilteringBehavior(SupervisedUserURLFilter::BLOCK);
18     filter_->AddObserver(this);
19   }
20
21   ~SupervisedUserURLFilterTest() override { filter_->RemoveObserver(this); }
22
23   // SupervisedUserURLFilter::Observer:
24   void OnSiteListUpdated() override { run_loop_.Quit(); }
25
26  protected:
27   bool IsURLWhitelisted(const std::string& url) {
28     return filter_->GetFilteringBehaviorForURL(GURL(url)) ==
29            SupervisedUserURLFilter::ALLOW;
30   }
31
32   base::MessageLoop message_loop_;
33   base::RunLoop run_loop_;
34   scoped_refptr<SupervisedUserURLFilter> filter_;
35 };
36
37 TEST_F(SupervisedUserURLFilterTest, Basic) {
38   std::vector<std::string> list;
39   // Allow domain and all subdomains, for any filtered scheme.
40   list.push_back("google.com");
41   filter_->SetFromPatterns(list);
42   run_loop_.Run();
43
44   EXPECT_TRUE(IsURLWhitelisted("http://google.com"));
45   EXPECT_TRUE(IsURLWhitelisted("http://google.com/"));
46   EXPECT_TRUE(IsURLWhitelisted("http://google.com/whatever"));
47   EXPECT_TRUE(IsURLWhitelisted("https://google.com/"));
48   EXPECT_FALSE(IsURLWhitelisted("http://notgoogle.com/"));
49   EXPECT_TRUE(IsURLWhitelisted("http://mail.google.com"));
50   EXPECT_TRUE(IsURLWhitelisted("http://x.mail.google.com"));
51   EXPECT_TRUE(IsURLWhitelisted("https://x.mail.google.com/"));
52   EXPECT_TRUE(IsURLWhitelisted("http://x.y.google.com/a/b"));
53   EXPECT_FALSE(IsURLWhitelisted("http://youtube.com/"));
54
55   EXPECT_TRUE(IsURLWhitelisted("bogus://youtube.com/"));
56   EXPECT_TRUE(IsURLWhitelisted("chrome://youtube.com/"));
57   EXPECT_TRUE(IsURLWhitelisted("chrome://extensions/"));
58   EXPECT_TRUE(IsURLWhitelisted("chrome-extension://foo/main.html"));
59   EXPECT_TRUE(IsURLWhitelisted("file:///home/chronos/user/Downloads/img.jpg"));
60 }
61
62 TEST_F(SupervisedUserURLFilterTest, Inactive) {
63   filter_->SetDefaultFilteringBehavior(SupervisedUserURLFilter::ALLOW);
64
65   std::vector<std::string> list;
66   list.push_back("google.com");
67   filter_->SetFromPatterns(list);
68   run_loop_.Run();
69
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"));
73 }
74
75 TEST_F(SupervisedUserURLFilterTest, 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);
82   run_loop_.Run();
83
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"));
93 }
94
95 TEST_F(SupervisedUserURLFilterTest, 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);
100   run_loop_.Run();
101
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"));
108 }
109
110 TEST_F(SupervisedUserURLFilterTest, 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);
115   run_loop_.Run();
116
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/"));
124 }
125
126 TEST_F(SupervisedUserURLFilterTest, 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);
131   run_loop_.Run();
132
133   EXPECT_TRUE(IsURLWhitelisted("http://www.example.com"));
134   EXPECT_FALSE(IsURLWhitelisted("http://example.com"));
135   EXPECT_FALSE(IsURLWhitelisted("http://subdomain.example.com"));
136 }
137
138 TEST_F(SupervisedUserURLFilterTest, IPAddress) {
139   std::vector<std::string> list;
140   // Filter an ip address.
141   list.push_back("123.123.123.123");
142   filter_->SetFromPatterns(list);
143   run_loop_.Run();
144
145   EXPECT_TRUE(IsURLWhitelisted("http://123.123.123.123/"));
146   EXPECT_FALSE(IsURLWhitelisted("http://123.123.123.124/"));
147 }
148
149 TEST_F(SupervisedUserURLFilterTest, 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);
159
160   // Base cases.
161   EXPECT_TRUE(IsURLWhitelisted("http://www.example.com/foo/"));
162   EXPECT_TRUE(IsURLWhitelisted(
163       "http://www.example.com/%C3%85t%C3%B8mstr%C3%B6m"));
164
165   // Verify that non-URI characters are escaped.
166   EXPECT_TRUE(IsURLWhitelisted(
167       "http://www.example.com/\xc3\x85t\xc3\xb8mstr\xc3\xb6m"));
168
169   // Verify that unnecessary URI escapes are unescaped.
170   EXPECT_TRUE(IsURLWhitelisted("http://www.example.com/%66%6F%6F/"));
171
172   // Verify that the default port are removed.
173   EXPECT_TRUE(IsURLWhitelisted("http://www.example.com:80/foo/"));
174
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/"));
178
179   // Verify that UTF-8 in hostnames are converted to punycode.
180   EXPECT_TRUE(IsURLWhitelisted("http://www.\xe2\x98\x83\x0a.net/bla/"));
181
182   // Verify that query and ref are stripped.
183   EXPECT_TRUE(IsURLWhitelisted("http://www.example.com/foo/?bar=baz#ref"));
184 }
185
186 TEST_F(SupervisedUserURLFilterTest, HasFilteredScheme) {
187   EXPECT_TRUE(
188       SupervisedUserURLFilter::HasFilteredScheme(GURL("http://example.com")));
189   EXPECT_TRUE(
190       SupervisedUserURLFilter::HasFilteredScheme(GURL("https://example.com")));
191   EXPECT_TRUE(
192       SupervisedUserURLFilter::HasFilteredScheme(GURL("ftp://example.com")));
193   EXPECT_TRUE(
194       SupervisedUserURLFilter::HasFilteredScheme(GURL("gopher://example.com")));
195   EXPECT_TRUE(
196       SupervisedUserURLFilter::HasFilteredScheme(GURL("ws://example.com")));
197   EXPECT_TRUE(
198       SupervisedUserURLFilter::HasFilteredScheme(GURL("wss://example.com")));
199
200   EXPECT_FALSE(
201       SupervisedUserURLFilter::HasFilteredScheme(GURL("file://example.com")));
202   EXPECT_FALSE(
203       SupervisedUserURLFilter::HasFilteredScheme(
204           GURL("filesystem://80cols.com")));
205   EXPECT_FALSE(
206       SupervisedUserURLFilter::HasFilteredScheme(GURL("chrome://example.com")));
207   EXPECT_FALSE(
208       SupervisedUserURLFilter::HasFilteredScheme(GURL("wtf://example.com")));
209 }
210
211 TEST_F(SupervisedUserURLFilterTest, HostMatchesPattern) {
212   EXPECT_TRUE(
213       SupervisedUserURLFilter::HostMatchesPattern("www.google.com",
214                                                   "*.google.com"));
215   EXPECT_TRUE(
216       SupervisedUserURLFilter::HostMatchesPattern("google.com",
217                                                   "*.google.com"));
218   EXPECT_TRUE(
219       SupervisedUserURLFilter::HostMatchesPattern("accounts.google.com",
220                                                   "*.google.com"));
221   EXPECT_FALSE(
222       SupervisedUserURLFilter::HostMatchesPattern("www.google.de",
223                                                   "*.google.com"));
224   EXPECT_FALSE(
225       SupervisedUserURLFilter::HostMatchesPattern("notgoogle.com",
226                                                   "*.google.com"));
227
228
229   EXPECT_TRUE(
230       SupervisedUserURLFilter::HostMatchesPattern("www.google.com",
231                                                   "www.google.*"));
232   EXPECT_TRUE(
233       SupervisedUserURLFilter::HostMatchesPattern("www.google.de",
234                                                   "www.google.*"));
235   EXPECT_TRUE(
236       SupervisedUserURLFilter::HostMatchesPattern("www.google.co.uk",
237                                                   "www.google.*"));
238   EXPECT_FALSE(
239       SupervisedUserURLFilter::HostMatchesPattern("www.google.blogspot.com",
240                                                   "www.google.*"));
241   EXPECT_FALSE(
242       SupervisedUserURLFilter::HostMatchesPattern("www.google",
243                                                   "www.google.*"));
244   EXPECT_FALSE(
245       SupervisedUserURLFilter::HostMatchesPattern("google.com",
246                                                   "www.google.*"));
247   EXPECT_FALSE(
248       SupervisedUserURLFilter::HostMatchesPattern("mail.google.com",
249                                                   "www.google.*"));
250   EXPECT_FALSE(
251       SupervisedUserURLFilter::HostMatchesPattern("www.googleplex.com",
252                                                   "www.google.*"));
253   EXPECT_FALSE(
254       SupervisedUserURLFilter::HostMatchesPattern("www.googleco.uk",
255                                                   "www.google.*"));
256
257
258   EXPECT_TRUE(
259       SupervisedUserURLFilter::HostMatchesPattern("www.google.com",
260                                                   "*.google.*"));
261   EXPECT_TRUE(
262       SupervisedUserURLFilter::HostMatchesPattern("google.com",
263                                                   "*.google.*"));
264   EXPECT_TRUE(
265       SupervisedUserURLFilter::HostMatchesPattern("accounts.google.com",
266                                                   "*.google.*"));
267   EXPECT_TRUE(
268       SupervisedUserURLFilter::HostMatchesPattern("mail.google.com",
269                                                   "*.google.*"));
270   EXPECT_TRUE(
271       SupervisedUserURLFilter::HostMatchesPattern("www.google.de",
272                                                   "*.google.*"));
273   EXPECT_TRUE(
274       SupervisedUserURLFilter::HostMatchesPattern("google.de",
275                                                   "*.google.*"));
276   EXPECT_FALSE(
277       SupervisedUserURLFilter::HostMatchesPattern("google.blogspot.com",
278                                                   "*.google.*"));
279   EXPECT_FALSE(
280       SupervisedUserURLFilter::HostMatchesPattern("google", "*.google.*"));
281   EXPECT_FALSE(
282       SupervisedUserURLFilter::HostMatchesPattern("notgoogle.com",
283                                                   "*.google.*"));
284   EXPECT_FALSE(
285       SupervisedUserURLFilter::HostMatchesPattern("www.googleplex.com",
286                                                   "*.google.*"));
287
288   // Now test a few invalid patterns. They should never match.
289   EXPECT_FALSE(
290       SupervisedUserURLFilter::HostMatchesPattern("www.google.com", ""));
291   EXPECT_FALSE(
292       SupervisedUserURLFilter::HostMatchesPattern("www.google.com", "."));
293   EXPECT_FALSE(
294       SupervisedUserURLFilter::HostMatchesPattern("www.google.com", "*"));
295   EXPECT_FALSE(
296       SupervisedUserURLFilter::HostMatchesPattern("www.google.com", ".*"));
297   EXPECT_FALSE(
298       SupervisedUserURLFilter::HostMatchesPattern("www.google.com", "*."));
299   EXPECT_FALSE(
300       SupervisedUserURLFilter::HostMatchesPattern("www.google.com", "*.*"));
301   EXPECT_FALSE(
302       SupervisedUserURLFilter::HostMatchesPattern("www.google..com", "*..*"));
303   EXPECT_FALSE(
304       SupervisedUserURLFilter::HostMatchesPattern("www.google.com", "*.*.com"));
305   EXPECT_FALSE(
306       SupervisedUserURLFilter::HostMatchesPattern("www.google.com", "www.*.*"));
307   EXPECT_FALSE(
308       SupervisedUserURLFilter::HostMatchesPattern("www.google.com",
309                                                   "*.goo.*le.*"));
310   EXPECT_FALSE(
311       SupervisedUserURLFilter::HostMatchesPattern("www.google.com",
312                                                   "*google*"));
313   EXPECT_FALSE(
314       SupervisedUserURLFilter::HostMatchesPattern("www.google.com",
315                                                   "www.*.google.com"));
316 }
317
318 TEST_F(SupervisedUserURLFilterTest, Patterns) {
319   std::map<std::string, bool> hosts;
320
321   // Initally, the second rule is ignored because has the same value as the
322   // default (block). When we change the default to allow, the first rule is
323   // ignored instead.
324   hosts["*.google.com"] = true;
325   hosts["www.google.*"] = false;
326
327   hosts["accounts.google.com"] = false;
328   hosts["mail.google.com"] = true;
329   filter_->SetManualHosts(&hosts);
330
331   // Initially, the default filtering behavior is BLOCK.
332   EXPECT_TRUE(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
337   filter_->SetDefaultFilteringBehavior(SupervisedUserURLFilter::ALLOW);
338   EXPECT_FALSE(IsURLWhitelisted("http://www.google.com/foo/"));
339   EXPECT_FALSE(IsURLWhitelisted("http://accounts.google.com/bar/"));
340   EXPECT_FALSE(IsURLWhitelisted("http://www.google.co.uk/blurp/"));
341   EXPECT_TRUE(IsURLWhitelisted("http://mail.google.com/moose/"));
342 }