Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: chrome/browser/policy/url_blacklist_manager_unittest.cc

Issue 10692158: Refactor URLBlacklistManager to use URLMatcher. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: joao Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/policy/url_blacklist_manager.h" 5 #include "chrome/browser/policy/url_blacklist_manager.h"
6 6
7 #include <ostream> 7 #include <ostream>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
11 #include "chrome/common/pref_names.h" 11 #include "chrome/common/pref_names.h"
12 #include "chrome/test/base/testing_pref_service.h" 12 #include "chrome/test/base/testing_pref_service.h"
13 #include "content/public/test/test_browser_thread.h" 13 #include "content/public/test/test_browser_thread.h"
14 #include "googleurl/src/gurl.h" 14 #include "googleurl/src/gurl.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
17 16
18 namespace policy { 17 namespace policy {
19 18
20 namespace { 19 namespace {
21 20
22 using ::testing::_;
23 using ::testing::Invoke;
24 using ::testing::Mock;
25 using content::BrowserThread; 21 using content::BrowserThread;
26 22
27 class TestingURLBlacklistManager : public URLBlacklistManager { 23 class TestingURLBlacklistManager : public URLBlacklistManager {
28 public: 24 public:
29 explicit TestingURLBlacklistManager(PrefService* pref_service) 25 explicit TestingURLBlacklistManager(PrefService* pref_service)
30 : URLBlacklistManager(pref_service) { 26 : URLBlacklistManager(pref_service),
27 update_called_(0),
28 set_blacklist_called_(false) {
31 } 29 }
32 30
33 virtual ~TestingURLBlacklistManager() { 31 virtual ~TestingURLBlacklistManager() {
34 } 32 }
35 33
36 // Make this method public for testing. 34 // Make this method public for testing.
37 using URLBlacklistManager::ScheduleUpdate; 35 using URLBlacklistManager::ScheduleUpdate;
38 36
39 // Makes a direct call to UpdateOnIO during tests. 37 // Makes a direct call to UpdateOnIO during tests.
40 void UpdateOnIO() { 38 void UpdateOnIOForTesting() {
41 StringVector* block = new StringVector; 39 scoped_ptr<base::ListValue> block(new base::ListValue);
42 block->push_back("example.com"); 40 block->Append(new base::StringValue("example.com"));
43 StringVector* allow = new StringVector; 41 scoped_ptr<base::ListValue> allow(new base::ListValue);
44 URLBlacklistManager::UpdateOnIO(block, allow); 42 URLBlacklistManager::UpdateOnIO(block.Pass(), allow.Pass());
45 } 43 }
46 44
47 void UpdateNotMocked() { 45 // URLBlacklistManager overrides:
46 virtual void SetBlacklist(scoped_ptr<URLBlacklist> blacklist) OVERRIDE {
47 set_blacklist_called_ = true;
48 URLBlacklistManager::SetBlacklist(blacklist.Pass());
49 }
50
51 virtual void Update() OVERRIDE {
52 update_called_++;
48 URLBlacklistManager::Update(); 53 URLBlacklistManager::Update();
49 } 54 }
50 55
51 MOCK_METHOD0(Update, void()); 56 int update_called() const { return update_called_; }
52 MOCK_METHOD1(SetBlacklist, void(URLBlacklist*)); 57 bool set_blacklist_called() const { return set_blacklist_called_; }
53 58
54 private: 59 private:
60 int update_called_;
61 bool set_blacklist_called_;
62
55 DISALLOW_COPY_AND_ASSIGN(TestingURLBlacklistManager); 63 DISALLOW_COPY_AND_ASSIGN(TestingURLBlacklistManager);
56 }; 64 };
57 65
58 class URLBlacklistManagerTest : public testing::Test { 66 class URLBlacklistManagerTest : public testing::Test {
59 protected: 67 protected:
60 URLBlacklistManagerTest() 68 URLBlacklistManagerTest()
61 : ui_thread_(BrowserThread::UI, &loop_), 69 : ui_thread_(BrowserThread::UI, &loop_),
62 file_thread_(BrowserThread::FILE, &loop_), 70 file_thread_(BrowserThread::FILE, &loop_),
63 io_thread_(BrowserThread::IO, &loop_) { 71 io_thread_(BrowserThread::IO, &loop_) {
64 } 72 }
65 73
66 virtual void SetUp() OVERRIDE { 74 virtual void SetUp() OVERRIDE {
67 pref_service_.RegisterListPref(prefs::kUrlBlacklist); 75 pref_service_.RegisterListPref(prefs::kUrlBlacklist);
68 pref_service_.RegisterListPref(prefs::kUrlWhitelist); 76 pref_service_.RegisterListPref(prefs::kUrlWhitelist);
69 blacklist_manager_.reset( 77 blacklist_manager_.reset(
70 new TestingURLBlacklistManager(&pref_service_)); 78 new TestingURLBlacklistManager(&pref_service_));
71 loop_.RunAllPending(); 79 loop_.RunAllPending();
72 } 80 }
73 81
74 virtual void TearDown() OVERRIDE { 82 virtual void TearDown() OVERRIDE {
75 if (blacklist_manager_.get()) 83 if (blacklist_manager_.get())
76 blacklist_manager_->ShutdownOnUIThread(); 84 blacklist_manager_->ShutdownOnUIThread();
77 loop_.RunAllPending(); 85 loop_.RunAllPending();
78 // Delete |blacklist_manager_| while |io_thread_| is mapping IO to 86 // Delete |blacklist_manager_| while |io_thread_| is mapping IO to
79 // |loop_|. 87 // |loop_|.
80 blacklist_manager_.reset(); 88 blacklist_manager_.reset();
81 } 89 }
82 90
83 void ExpectUpdate() {
84 EXPECT_CALL(*blacklist_manager_, Update())
85 .WillOnce(Invoke(blacklist_manager_.get(),
86 &TestingURLBlacklistManager::UpdateNotMocked));
87 }
88
89 MessageLoop loop_; 91 MessageLoop loop_;
90 TestingPrefService pref_service_; 92 TestingPrefService pref_service_;
91 scoped_ptr<TestingURLBlacklistManager> blacklist_manager_; 93 scoped_ptr<TestingURLBlacklistManager> blacklist_manager_;
92 94
93 private: 95 private:
94 content::TestBrowserThread ui_thread_; 96 content::TestBrowserThread ui_thread_;
95 content::TestBrowserThread file_thread_; 97 content::TestBrowserThread file_thread_;
96 content::TestBrowserThread io_thread_; 98 content::TestBrowserThread io_thread_;
97 99
98 DISALLOW_COPY_AND_ASSIGN(URLBlacklistManagerTest); 100 DISALLOW_COPY_AND_ASSIGN(URLBlacklistManagerTest);
99 }; 101 };
100 102
101 // Parameters for the FilterToComponents test. 103 // Parameters for the FilterToComponents test.
102 struct FilterTestParams { 104 struct FilterTestParams {
103 public: 105 public:
104 FilterTestParams(const std::string& filter, const std::string& scheme, 106 FilterTestParams(const std::string& filter, const std::string& scheme,
105 const std::string& host, uint16 port, 107 const std::string& host, bool match_subdomains, uint16 port,
106 const std::string& path) 108 const std::string& path)
107 : filter_(filter), scheme_(scheme), host_(host), port_(port), 109 : filter_(filter), scheme_(scheme), host_(host),
108 path_(path) {} 110 match_subdomains_(match_subdomains), port_(port), path_(path) {}
109 111
110 FilterTestParams(const FilterTestParams& params) 112 FilterTestParams(const FilterTestParams& params)
111 : filter_(params.filter_), scheme_(params.scheme_), host_(params.host_), 113 : filter_(params.filter_), scheme_(params.scheme_), host_(params.host_),
112 port_(params.port_), path_(params.path_) {} 114 match_subdomains_(params.match_subdomains_), port_(params.port_),
115 path_(params.path_) {}
113 116
114 const FilterTestParams& operator=(const FilterTestParams& params) { 117 const FilterTestParams& operator=(const FilterTestParams& params) {
115 filter_ = params.filter_; 118 filter_ = params.filter_;
116 scheme_ = params.scheme_; 119 scheme_ = params.scheme_;
117 host_ = params.host_; 120 host_ = params.host_;
121 match_subdomains_ = params.match_subdomains_;
118 port_ = params.port_; 122 port_ = params.port_;
119 path_ = params.path_; 123 path_ = params.path_;
120 return *this; 124 return *this;
121 } 125 }
122 126
123 const std::string& filter() const { return filter_; } 127 const std::string& filter() const { return filter_; }
124 const std::string& scheme() const { return scheme_; } 128 const std::string& scheme() const { return scheme_; }
125 const std::string& host() const { return host_; } 129 const std::string& host() const { return host_; }
130 bool match_subdomains() const { return match_subdomains_; }
126 uint16 port() const { return port_; } 131 uint16 port() const { return port_; }
127 const std::string& path() const { return path_; } 132 const std::string& path() const { return path_; }
128 133
129 private: 134 private:
130 std::string filter_; 135 std::string filter_;
131 std::string scheme_; 136 std::string scheme_;
132 std::string host_; 137 std::string host_;
138 bool match_subdomains_;
133 uint16 port_; 139 uint16 port_;
134 std::string path_; 140 std::string path_;
135 }; 141 };
136 142
137 // Make Valgrind happy. Without this function, a generic one will print the 143 // Make Valgrind happy. Without this function, a generic one will print the
138 // raw bytes in FilterTestParams, which due to some likely padding will access 144 // raw bytes in FilterTestParams, which due to some likely padding will access
139 // uninitialized memory. 145 // uninitialized memory.
140 void PrintTo(const FilterTestParams& params, std::ostream* os) { 146 void PrintTo(const FilterTestParams& params, std::ostream* os) {
141 *os << params.filter(); 147 *os << params.filter();
142 } 148 }
143 149
144 class URLBlacklistFilterToComponentsTest 150 class URLBlacklistFilterToComponentsTest
145 : public testing::TestWithParam<FilterTestParams> { 151 : public testing::TestWithParam<FilterTestParams> {
146 public: 152 public:
147 URLBlacklistFilterToComponentsTest() {} 153 URLBlacklistFilterToComponentsTest() {}
148 154
149 private: 155 private:
150 DISALLOW_COPY_AND_ASSIGN(URLBlacklistFilterToComponentsTest); 156 DISALLOW_COPY_AND_ASSIGN(URLBlacklistFilterToComponentsTest);
151 }; 157 };
152 158
159 } // namespace
160
153 TEST_P(URLBlacklistFilterToComponentsTest, FilterToComponents) { 161 TEST_P(URLBlacklistFilterToComponentsTest, FilterToComponents) {
154 std::string scheme; 162 std::string scheme;
155 std::string host; 163 std::string host;
156 uint16 port; 164 bool match_subdomains = true;
165 uint16 port = 42;
157 std::string path; 166 std::string path;
158 167
159 URLBlacklist::FilterToComponents(GetParam().filter(), &scheme, &host, &port, 168 URLBlacklist::FilterToComponents(GetParam().filter(), &scheme, &host,
160 &path); 169 &match_subdomains, &port, &path);
161 EXPECT_EQ(GetParam().scheme(), scheme); 170 EXPECT_EQ(GetParam().scheme(), scheme);
162 EXPECT_EQ(GetParam().host(), host); 171 EXPECT_EQ(GetParam().host(), host);
172 EXPECT_EQ(GetParam().match_subdomains(), match_subdomains);
163 EXPECT_EQ(GetParam().port(), port); 173 EXPECT_EQ(GetParam().port(), port);
164 EXPECT_EQ(GetParam().path(), path); 174 EXPECT_EQ(GetParam().path(), path);
165 } 175 }
166 176
167 TEST_F(URLBlacklistManagerTest, SingleUpdateForTwoPrefChanges) { 177 TEST_F(URLBlacklistManagerTest, SingleUpdateForTwoPrefChanges) {
168 ExpectUpdate();
169
170 ListValue* blacklist = new ListValue; 178 ListValue* blacklist = new ListValue;
171 blacklist->Append(new StringValue("*.google.com")); 179 blacklist->Append(new StringValue("*.google.com"));
172 ListValue* whitelist = new ListValue; 180 ListValue* whitelist = new ListValue;
173 whitelist->Append(new StringValue("mail.google.com")); 181 whitelist->Append(new StringValue("mail.google.com"));
174 pref_service_.SetManagedPref(prefs::kUrlBlacklist, blacklist); 182 pref_service_.SetManagedPref(prefs::kUrlBlacklist, blacklist);
175 pref_service_.SetManagedPref(prefs::kUrlBlacklist, whitelist); 183 pref_service_.SetManagedPref(prefs::kUrlBlacklist, whitelist);
176 loop_.RunAllPending(); 184 loop_.RunAllPending();
177 185
178 Mock::VerifyAndClearExpectations(blacklist_manager_.get()); 186 EXPECT_EQ(1, blacklist_manager_->update_called());
179 } 187 }
180 188
181 TEST_F(URLBlacklistManagerTest, ShutdownWithPendingTask0) { 189 TEST_F(URLBlacklistManagerTest, ShutdownWithPendingTask0) {
182 // Post an update task to the UI thread. 190 // Post an update task to the UI thread.
183 blacklist_manager_->ScheduleUpdate(); 191 blacklist_manager_->ScheduleUpdate();
184 // Shutdown comes before the task is executed. 192 // Shutdown comes before the task is executed.
185 blacklist_manager_->ShutdownOnUIThread(); 193 blacklist_manager_->ShutdownOnUIThread();
186 blacklist_manager_.reset(); 194 blacklist_manager_.reset();
187 // Run the task after shutdown and deletion. 195 // Run the task after shutdown and deletion.
188 loop_.RunAllPending(); 196 loop_.RunAllPending();
189 } 197 }
190 198
191 TEST_F(URLBlacklistManagerTest, ShutdownWithPendingTask1) { 199 TEST_F(URLBlacklistManagerTest, ShutdownWithPendingTask1) {
192 EXPECT_CALL(*blacklist_manager_, Update()).Times(0);
193 // Post an update task. 200 // Post an update task.
194 blacklist_manager_->ScheduleUpdate(); 201 blacklist_manager_->ScheduleUpdate();
195 // Shutdown comes before the task is executed. 202 // Shutdown comes before the task is executed.
196 blacklist_manager_->ShutdownOnUIThread(); 203 blacklist_manager_->ShutdownOnUIThread();
197 // Run the task after shutdown, but before deletion. 204 // Run the task after shutdown, but before deletion.
198 loop_.RunAllPending(); 205 loop_.RunAllPending();
199 Mock::VerifyAndClearExpectations(blacklist_manager_.get()); 206
207 EXPECT_EQ(0, blacklist_manager_->update_called());
200 blacklist_manager_.reset(); 208 blacklist_manager_.reset();
201 loop_.RunAllPending(); 209 loop_.RunAllPending();
202 } 210 }
203 211
204 TEST_F(URLBlacklistManagerTest, ShutdownWithPendingTask2) { 212 TEST_F(URLBlacklistManagerTest, ShutdownWithPendingTask2) {
205 EXPECT_CALL(*blacklist_manager_, SetBlacklist(_)).Times(0); 213 // This posts a task to the FILE thread.
206 // Update posts a BuildBlacklistTask to the FILE thread. 214 blacklist_manager_->UpdateOnIOForTesting();
207 blacklist_manager_->UpdateNotMocked(); 215 // But shutdown happens before it is done.
208 // Shutdown comes before the task is executed.
209 blacklist_manager_->ShutdownOnUIThread(); 216 blacklist_manager_->ShutdownOnUIThread();
210 // Run the task after shutdown, but before deletion. 217
211 loop_.RunAllPending(); 218 EXPECT_FALSE(blacklist_manager_->set_blacklist_called());
212 Mock::VerifyAndClearExpectations(blacklist_manager_.get());
213 blacklist_manager_.reset(); 219 blacklist_manager_.reset();
214 loop_.RunAllPending(); 220 loop_.RunAllPending();
215 } 221 }
216 222
217 TEST_F(URLBlacklistManagerTest, ShutdownWithPendingTask3) { 223 TEST_F(URLBlacklistManagerTest, HasStandardScheme) {
218 EXPECT_CALL(*blacklist_manager_, SetBlacklist(_)).Times(0); 224 EXPECT_TRUE(URLBlacklist::HasStandardScheme(GURL("http://example.com")));
219 225 EXPECT_TRUE(URLBlacklist::HasStandardScheme(GURL("https://example.com")));
220 // This posts a task to the FILE thread. 226 EXPECT_TRUE(URLBlacklist::HasStandardScheme(GURL("ftp://example.com")));
221 blacklist_manager_->UpdateOnIO(); 227 EXPECT_TRUE(URLBlacklist::HasStandardScheme(GURL("gopher://example.com")));
222 // But shutdown happens before it is done. 228 EXPECT_TRUE(URLBlacklist::HasStandardScheme(GURL("ws://example.com")));
223 blacklist_manager_->ShutdownOnUIThread(); 229 EXPECT_TRUE(URLBlacklist::HasStandardScheme(GURL("wss://example.com")));
224 blacklist_manager_.reset(); 230 EXPECT_FALSE(URLBlacklist::HasStandardScheme(GURL("wtf://example.com")));
225 loop_.RunAllPending();
226
227 Mock::VerifyAndClearExpectations(blacklist_manager_.get());
228 }
229
230 TEST_F(URLBlacklistManagerTest, ShutdownWithPendingTask4) {
231 EXPECT_CALL(*blacklist_manager_, SetBlacklist(_)).Times(0);
232
233 // This posts a task to the FILE thread.
234 blacklist_manager_->UpdateOnIO();
235 // But shutdown happens before it is done.
236 blacklist_manager_->ShutdownOnUIThread();
237 // This time, shutdown on UI is done but the object is still alive.
238 loop_.RunAllPending();
239 blacklist_manager_.reset();
240 loop_.RunAllPending();
241
242 Mock::VerifyAndClearExpectations(blacklist_manager_.get());
243 }
244
245 TEST_F(URLBlacklistManagerTest, SchemeToFlag) {
246 URLBlacklist::SchemeFlag flag;
247 EXPECT_TRUE(URLBlacklist::SchemeToFlag("http", &flag));
248 EXPECT_EQ(URLBlacklist::SCHEME_HTTP, flag);
249 EXPECT_TRUE(URLBlacklist::SchemeToFlag("https", &flag));
250 EXPECT_EQ(URLBlacklist::SCHEME_HTTPS, flag);
251 EXPECT_TRUE(URLBlacklist::SchemeToFlag("ftp", &flag));
252 EXPECT_EQ(URLBlacklist::SCHEME_FTP, flag);
253 EXPECT_TRUE(URLBlacklist::SchemeToFlag("ws", &flag));
254 EXPECT_EQ(URLBlacklist::SCHEME_WS, flag);
255 EXPECT_TRUE(URLBlacklist::SchemeToFlag("wss", &flag));
256 EXPECT_EQ(URLBlacklist::SCHEME_WSS, flag);
257 EXPECT_TRUE(URLBlacklist::SchemeToFlag("", &flag));
258 EXPECT_EQ(URLBlacklist::SCHEME_ALL, flag);
259 EXPECT_FALSE(URLBlacklist::SchemeToFlag("wtf", &flag));
260 } 231 }
261 232
262 INSTANTIATE_TEST_CASE_P( 233 INSTANTIATE_TEST_CASE_P(
263 URLBlacklistFilterToComponentsTestInstance, 234 URLBlacklistFilterToComponentsTestInstance,
264 URLBlacklistFilterToComponentsTest, 235 URLBlacklistFilterToComponentsTest,
265 testing::Values( 236 testing::Values(
266 FilterTestParams("google.com", 237 FilterTestParams("google.com",
267 "", "google.com", 0u, ""), 238 "", ".google.com", true, 0u, ""),
239 FilterTestParams(".google.com",
240 "", "google.com", false, 0u, ""),
268 FilterTestParams("http://google.com", 241 FilterTestParams("http://google.com",
269 "http", "google.com", 0u, ""), 242 "http", ".google.com", true, 0u, ""),
270 FilterTestParams("google.com/", 243 FilterTestParams("google.com/",
271 "", "google.com", 0u, "/"), 244 "", ".google.com", true, 0u, "/"),
272 FilterTestParams("http://google.com:8080/whatever", 245 FilterTestParams("http://google.com:8080/whatever",
273 "http", "google.com", 8080u, "/whatever"), 246 "http", ".google.com", true, 8080u, "/whatever"),
274 FilterTestParams("http://user:pass@google.com:8080/whatever", 247 FilterTestParams("http://user:pass@google.com:8080/whatever",
275 "http", "google.com", 8080u, "/whatever"), 248 "http", ".google.com", true, 8080u, "/whatever"),
276 FilterTestParams("123.123.123.123", 249 FilterTestParams("123.123.123.123",
277 "", "123.123.123.123", 0u, ""), 250 "", "123.123.123.123", false, 0u, ""),
278 FilterTestParams("https://123.123.123.123", 251 FilterTestParams("https://123.123.123.123",
279 "https", "123.123.123.123", 0u, ""), 252 "https", "123.123.123.123", false, 0u, ""),
280 FilterTestParams("123.123.123.123/", 253 FilterTestParams("123.123.123.123/",
281 "", "123.123.123.123", 0u, "/"), 254 "", "123.123.123.123", false, 0u, "/"),
282 FilterTestParams("http://123.123.123.123:123/whatever", 255 FilterTestParams("http://123.123.123.123:123/whatever",
283 "http", "123.123.123.123", 123u, "/whatever"), 256 "http", "123.123.123.123", false, 123u, "/whatever"),
284 FilterTestParams("*", 257 FilterTestParams("*",
285 "", "", 0u, ""), 258 "", "", true, 0u, ""),
286 FilterTestParams("ftp://*", 259 FilterTestParams("ftp://*",
287 "ftp", "", 0u, ""), 260 "ftp", "", true, 0u, ""),
288 FilterTestParams("http://*/whatever", 261 FilterTestParams("http://*/whatever",
289 "http", "", 0u, "/whatever"))); 262 "http", "", true, 0u, "/whatever")));
290 263
291 TEST_F(URLBlacklistManagerTest, Filtering) { 264 TEST_F(URLBlacklistManagerTest, Filtering) {
292 URLBlacklist blacklist; 265 URLBlacklist blacklist;
293 266
294 // Block domain and all subdomains, for any filtered scheme. 267 // Block domain and all subdomains, for any filtered scheme.
295 blacklist.Block("google.com"); 268 scoped_ptr<base::ListValue> blocked(new base::ListValue);
269 blocked->Append(new base::StringValue("google.com"));
270 blacklist.Block(blocked.get());
296 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com"))); 271 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com")));
297 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com/"))); 272 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com/")));
298 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com/whatever"))); 273 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com/whatever")));
299 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://google.com/"))); 274 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://google.com/")));
300 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("bogus://google.com/"))); 275 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("bogus://google.com/")));
276 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://notgoogle.com/")));
301 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://mail.google.com"))); 277 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://mail.google.com")));
302 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://x.mail.google.com"))); 278 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://x.mail.google.com")));
303 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://x.mail.google.com/"))); 279 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://x.mail.google.com/")));
304 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://x.y.google.com/a/b"))); 280 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://x.y.google.com/a/b")));
305 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://youtube.com/"))); 281 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://youtube.com/")));
306 282
307 // Filter only http, ftp and ws schemes. 283 // Filter only http, ftp and ws schemes.
308 blacklist.Block("http://secure.com"); 284 blocked.reset(new base::ListValue);
309 blacklist.Block("ftp://secure.com"); 285 blocked->Append(new base::StringValue("http://secure.com"));
310 blacklist.Block("ws://secure.com"); 286 blocked->Append(new base::StringValue("ftp://secure.com"));
287 blocked->Append(new base::StringValue("ws://secure.com"));
288 blacklist.Block(blocked.get());
311 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://secure.com"))); 289 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://secure.com")));
312 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://secure.com/whatever"))); 290 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://secure.com/whatever")));
313 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("ftp://secure.com/"))); 291 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("ftp://secure.com/")));
314 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("ws://secure.com"))); 292 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("ws://secure.com")));
315 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://secure.com/"))); 293 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://secure.com/")));
316 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("wss://secure.com"))); 294 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("wss://secure.com")));
317 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://www.secure.com"))); 295 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://www.secure.com")));
318 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://www.secure.com"))); 296 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://www.secure.com")));
319 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("wss://www.secure.com"))); 297 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("wss://www.secure.com")));
320 298
321 // Filter only a certain path prefix. 299 // Filter only a certain path prefix.
322 blacklist.Block("path.to/ruin"); 300 blocked.reset(new base::ListValue);
301 blocked->Append(new base::StringValue("path.to/ruin"));
302 blacklist.Block(blocked.get());
323 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://path.to/ruin"))); 303 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://path.to/ruin")));
324 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://path.to/ruin"))); 304 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://path.to/ruin")));
325 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://path.to/ruins"))); 305 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://path.to/ruins")));
326 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://path.to/ruin/signup"))); 306 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://path.to/ruin/signup")));
327 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://www.path.to/ruin"))); 307 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://www.path.to/ruin")));
328 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://path.to/fortune"))); 308 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://path.to/fortune")));
329 309
330 // Filter only a certain path prefix and scheme. 310 // Filter only a certain path prefix and scheme.
331 blacklist.Block("https://s.aaa.com/path"); 311 blocked.reset(new base::ListValue);
312 blocked->Append(new base::StringValue("https://s.aaa.com/path"));
313 blacklist.Block(blocked.get());
332 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://s.aaa.com/path"))); 314 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://s.aaa.com/path")));
333 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://s.aaa.com/path/bbb"))); 315 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://s.aaa.com/path/bbb")));
334 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://s.aaa.com/path"))); 316 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://s.aaa.com/path")));
335 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://aaa.com/path"))); 317 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://aaa.com/path")));
336 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://x.aaa.com/path"))); 318 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://x.aaa.com/path")));
337 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://s.aaa.com/bbb"))); 319 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://s.aaa.com/bbb")));
338 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://s.aaa.com/"))); 320 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://s.aaa.com/")));
339 321
340 // Filter only ws and wss schemes. 322 // Filter only ws and wss schemes.
341 blacklist.Block("ws://ws.aaa.com"); 323 blocked.reset(new base::ListValue);
342 blacklist.Block("wss://ws.aaa.com"); 324 blocked->Append(new base::StringValue("ws://ws.aaa.com"));
325 blocked->Append(new base::StringValue("wss://ws.aaa.com"));
326 blacklist.Block(blocked.get());
343 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("ws://ws.aaa.com"))); 327 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("ws://ws.aaa.com")));
344 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("wss://ws.aaa.com"))); 328 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("wss://ws.aaa.com")));
345 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://ws.aaa.com"))); 329 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://ws.aaa.com")));
346 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://ws.aaa.com"))); 330 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://ws.aaa.com")));
347 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("ftp://ws.aaa.com"))); 331 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("ftp://ws.aaa.com")));
348 332
349 // Test exceptions to path prefixes, and most specific matches. 333 // Test exceptions to path prefixes, and most specific matches.
350 blacklist.Block("s.xxx.com/a"); 334 blocked.reset(new base::ListValue);
351 blacklist.Allow("s.xxx.com/a/b"); 335 scoped_ptr<base::ListValue> allowed(new base::ListValue);
352 blacklist.Block("https://s.xxx.com/a/b/c"); 336 blocked->Append(new base::StringValue("s.xxx.com/a"));
353 blacklist.Allow("https://s.xxx.com/a/b/c/d"); 337 allowed->Append(new base::StringValue("s.xxx.com/a/b"));
338 blocked->Append(new base::StringValue("https://s.xxx.com/a/b/c"));
339 allowed->Append(new base::StringValue("https://s.xxx.com/a/b/c/d"));
340 blacklist.Block(blocked.get());
341 blacklist.Allow(allowed.get());
354 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://s.xxx.com/a"))); 342 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://s.xxx.com/a")));
355 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://s.xxx.com/a/x"))); 343 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://s.xxx.com/a/x")));
356 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://s.xxx.com/a/x"))); 344 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://s.xxx.com/a/x")));
357 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://s.xxx.com/a/b"))); 345 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://s.xxx.com/a/b")));
358 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://s.xxx.com/a/b"))); 346 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://s.xxx.com/a/b")));
359 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://s.xxx.com/a/b/x"))); 347 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://s.xxx.com/a/b/x")));
360 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://s.xxx.com/a/b/c"))); 348 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://s.xxx.com/a/b/c")));
361 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://s.xxx.com/a/b/c"))); 349 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://s.xxx.com/a/b/c")));
362 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://s.xxx.com/a/b/c/x"))); 350 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://s.xxx.com/a/b/c/x")));
363 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://s.xxx.com/a/b/c/d"))); 351 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://s.xxx.com/a/b/c/d")));
364 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://s.xxx.com/a/b/c/d"))); 352 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://s.xxx.com/a/b/c/d")));
365 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://s.xxx.com/a/b/c/d/x"))); 353 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://s.xxx.com/a/b/c/d/x")));
366 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://s.xxx.com/a/b/c/d/x"))); 354 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://s.xxx.com/a/b/c/d/x")));
367 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://xxx.com/a"))); 355 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://xxx.com/a")));
368 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://xxx.com/a/b"))); 356 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://xxx.com/a/b")));
369 357
370 // Block an ip address. 358 // Block an ip address.
371 blacklist.Block("123.123.123.123"); 359 blocked.reset(new base::ListValue);
360 blocked->Append(new base::StringValue("123.123.123.123"));
361 blacklist.Block(blocked.get());
372 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://123.123.123.123/"))); 362 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://123.123.123.123/")));
373 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://123.123.123.124/"))); 363 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://123.123.123.124/")));
374 364
375 // Open an exception. 365 // Open an exception.
376 blacklist.Allow("plus.google.com"); 366 allowed.reset(new base::ListValue);
367 allowed->Append(new base::StringValue("plus.google.com"));
368 blacklist.Allow(allowed.get());
377 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com/"))); 369 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com/")));
378 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://www.google.com/"))); 370 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://www.google.com/")));
379 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://plus.google.com/"))); 371 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://plus.google.com/")));
380 372
381 // Open an exception only when using https for mail. 373 // Open an exception only when using https for mail.
382 blacklist.Allow("https://mail.google.com"); 374 allowed.reset(new base::ListValue);
375 allowed->Append(new base::StringValue("https://mail.google.com"));
376 blacklist.Allow(allowed.get());
383 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com/"))); 377 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com/")));
384 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://mail.google.com/"))); 378 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://mail.google.com/")));
385 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://www.google.com/"))); 379 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://www.google.com/")));
386 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://www.google.com/"))); 380 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://www.google.com/")));
387 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://mail.google.com/"))); 381 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://mail.google.com/")));
388 382
389 // Match exactly "google.com", only for http. Subdomains without exceptions 383 // Match exactly "google.com", only for http. Subdomains without exceptions
390 // are still blocked. 384 // are still blocked.
391 blacklist.Allow("http://.google.com"); 385 allowed.reset(new base::ListValue);
386 allowed->Append(new base::StringValue("http://.google.com"));
387 blacklist.Allow(allowed.get());
392 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://google.com/"))); 388 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://google.com/")));
393 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://google.com/"))); 389 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://google.com/")));
394 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://www.google.com/"))); 390 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://www.google.com/")));
395 391
396 // A smaller path match in an exact host overrides a longer path for hosts 392 // A smaller path match in an exact host overrides a longer path for hosts
397 // that also match subdomains. 393 // that also match subdomains.
398 blacklist.Block("yyy.com/aaa"); 394 blocked.reset(new base::ListValue);
399 blacklist.Allow(".yyy.com/a"); 395 blocked->Append(new base::StringValue("yyy.com/aaa"));
396 blacklist.Block(blocked.get());
397 allowed.reset(new base::ListValue);
398 allowed->Append(new base::StringValue(".yyy.com/a"));
399 blacklist.Allow(allowed.get());
400 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://yyy.com"))); 400 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://yyy.com")));
401 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://yyy.com/aaa"))); 401 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://yyy.com/aaa")));
402 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://yyy.com/aaa2"))); 402 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://yyy.com/aaa2")));
403 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://www.yyy.com"))); 403 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://www.yyy.com")));
404 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://www.yyy.com/aaa"))); 404 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://www.yyy.com/aaa")));
405 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://www.yyy.com/aaa2"))); 405 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://www.yyy.com/aaa2")));
406
407 // If the exact entry is both allowed and blocked, allowing takes precedence.
408 blocked.reset(new base::ListValue);
409 blocked->Append(new base::StringValue("example.com"));
410 blacklist.Block(blocked.get());
411 allowed.reset(new base::ListValue);
412 allowed->Append(new base::StringValue("example.com"));
413 blacklist.Allow(allowed.get());
414 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://example.com")));
406 } 415 }
407 416
408 TEST_F(URLBlacklistManagerTest, BlockAllWithExceptions) { 417 TEST_F(URLBlacklistManagerTest, BlockAllWithExceptions) {
409 URLBlacklist blacklist; 418 URLBlacklist blacklist;
410 419
411 blacklist.Block("*"); 420 scoped_ptr<base::ListValue> blocked(new base::ListValue);
412 blacklist.Allow(".www.google.com"); 421 scoped_ptr<base::ListValue> allowed(new base::ListValue);
413 blacklist.Allow("plus.google.com"); 422 blocked->Append(new base::StringValue("*"));
414 blacklist.Allow("https://mail.google.com"); 423 allowed->Append(new base::StringValue(".www.google.com"));
415 blacklist.Allow("https://very.safe/path"); 424 allowed->Append(new base::StringValue("plus.google.com"));
425 allowed->Append(new base::StringValue("https://mail.google.com"));
426 allowed->Append(new base::StringValue("https://very.safe/path"));
427 blacklist.Block(blocked.get());
428 blacklist.Allow(allowed.get());
416 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://random.com"))); 429 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://random.com")));
417 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com"))); 430 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com")));
418 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://s.www.google.com"))); 431 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://s.www.google.com")));
419 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://www.google.com"))); 432 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://www.google.com")));
420 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://plus.google.com"))); 433 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://plus.google.com")));
421 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://s.plus.google.com"))); 434 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://s.plus.google.com")));
422 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://mail.google.com"))); 435 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://mail.google.com")));
423 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://mail.google.com"))); 436 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://mail.google.com")));
424 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://s.mail.google.com"))); 437 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://s.mail.google.com")));
425 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://very.safe/"))); 438 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://very.safe/")));
426 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://very.safe/path"))); 439 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://very.safe/path")));
427 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://very.safe/path"))); 440 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://very.safe/path")));
428 } 441 }
429 442
430 } // namespace
431
432 } // namespace policy 443 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/url_blacklist_manager.cc ('k') | chrome/common/extensions/matcher/url_matcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698