OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "chrome/browser/policy/url_blacklist_manager.h" |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "chrome/common/pref_names.h" |
| 10 #include "chrome/test/base/testing_pref_service.h" |
| 11 #include "chrome/test/base/testing_profile.h" |
| 12 #include "content/browser/browser_thread.h" |
| 13 #include "content/common/notification_service.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace policy { |
| 18 |
| 19 namespace { |
| 20 |
| 21 using ::testing::_; |
| 22 using ::testing::Invoke; |
| 23 using ::testing::Mock; |
| 24 |
| 25 class TestingURLBlacklistManager : public URLBlacklistManager { |
| 26 public: |
| 27 explicit TestingURLBlacklistManager(Profile* profile) |
| 28 : URLBlacklistManager(profile) { |
| 29 } |
| 30 |
| 31 virtual ~TestingURLBlacklistManager() { |
| 32 } |
| 33 |
| 34 // Make this method public for testing. |
| 35 using URLBlacklistManager::ScheduleUpdate; |
| 36 |
| 37 virtual void PostUpdateTask(Task* task) OVERRIDE { |
| 38 // Post tasks without a delay during tests. |
| 39 MessageLoop::current()->PostTask(FROM_HERE, task); |
| 40 } |
| 41 |
| 42 MOCK_METHOD0(Update, void()); |
| 43 MOCK_METHOD1(SetBlacklist, void(Blacklist*)); |
| 44 |
| 45 void UpdateNotMocked() { |
| 46 URLBlacklistManager::Update(); |
| 47 } |
| 48 |
| 49 private: |
| 50 DISALLOW_COPY_AND_ASSIGN(TestingURLBlacklistManager); |
| 51 }; |
| 52 |
| 53 class URLBlacklistManagerTest : public testing::Test { |
| 54 protected: |
| 55 URLBlacklistManagerTest() |
| 56 : ui_thread_(BrowserThread::UI, &loop_), |
| 57 file_thread_(BrowserThread::FILE, &loop_), |
| 58 io_thread_(BrowserThread::IO, &loop_) { |
| 59 } |
| 60 |
| 61 virtual void SetUp() OVERRIDE { |
| 62 pref_service_ = profile_.GetTestingPrefService(); |
| 63 url_blacklist_manager_.reset(new TestingURLBlacklistManager(&profile_)); |
| 64 url_blacklist_manager_->InitializeOnIOThread(); |
| 65 loop_.RunAllPending(); |
| 66 } |
| 67 |
| 68 virtual void TearDown() OVERRIDE { |
| 69 if (url_blacklist_manager_.get()) |
| 70 url_blacklist_manager_->ShutdownOnUIThread(); |
| 71 loop_.RunAllPending(); |
| 72 } |
| 73 |
| 74 void ExpectUpdate() { |
| 75 EXPECT_CALL(*url_blacklist_manager_, Update()) |
| 76 .WillOnce(Invoke(url_blacklist_manager_.get(), |
| 77 &TestingURLBlacklistManager::UpdateNotMocked)); |
| 78 } |
| 79 |
| 80 MessageLoop loop_; |
| 81 TestingProfile profile_; |
| 82 TestingPrefService* pref_service_; |
| 83 scoped_ptr<TestingURLBlacklistManager> url_blacklist_manager_; |
| 84 |
| 85 private: |
| 86 BrowserThread ui_thread_; |
| 87 BrowserThread file_thread_; |
| 88 BrowserThread io_thread_; |
| 89 |
| 90 DISALLOW_COPY_AND_ASSIGN(URLBlacklistManagerTest); |
| 91 }; |
| 92 |
| 93 TEST_F(URLBlacklistManagerTest, SingleUpdateForTwoPrefChanges) { |
| 94 ExpectUpdate(); |
| 95 |
| 96 ListValue* blacklist = new ListValue; |
| 97 blacklist->Append(new StringValue("*.google.com")); |
| 98 ListValue* whitelist = new ListValue; |
| 99 whitelist->Append(new StringValue("mail.google.com")); |
| 100 pref_service_->SetManagedPref(prefs::kUrlBlacklist, blacklist); |
| 101 pref_service_->SetManagedPref(prefs::kUrlBlacklist, whitelist); |
| 102 loop_.RunAllPending(); |
| 103 |
| 104 Mock::VerifyAndClearExpectations(url_blacklist_manager_.get()); |
| 105 } |
| 106 |
| 107 TEST_F(URLBlacklistManagerTest, ShutdownWithPendingTask0) { |
| 108 // Post an update task to the UI thread. |
| 109 url_blacklist_manager_->ScheduleUpdate(); |
| 110 // Shutdown comes before the task is executed. |
| 111 url_blacklist_manager_->ShutdownOnUIThread(); |
| 112 url_blacklist_manager_.reset(); |
| 113 // Run the task after shutdown and deletion. |
| 114 loop_.RunAllPending(); |
| 115 } |
| 116 |
| 117 TEST_F(URLBlacklistManagerTest, ShutdownWithPendingTask1) { |
| 118 EXPECT_CALL(*url_blacklist_manager_, Update()).Times(0); |
| 119 // Post an update task. |
| 120 url_blacklist_manager_->ScheduleUpdate(); |
| 121 // Shutdown comes before the task is executed. |
| 122 url_blacklist_manager_->ShutdownOnUIThread(); |
| 123 // Run the task after shutdown, but before deletion. |
| 124 loop_.RunAllPending(); |
| 125 Mock::VerifyAndClearExpectations(url_blacklist_manager_.get()); |
| 126 url_blacklist_manager_.reset(); |
| 127 loop_.RunAllPending(); |
| 128 } |
| 129 |
| 130 TEST_F(URLBlacklistManagerTest, ShutdownWithPendingTask2) { |
| 131 // Update posts a BuildBlacklistTask to the FILE thread. |
| 132 url_blacklist_manager_->UpdateNotMocked(); |
| 133 // Shutdown comes before the task is executed. |
| 134 url_blacklist_manager_->ShutdownOnUIThread(); |
| 135 url_blacklist_manager_.reset(); |
| 136 // Run the task after shutdown and deletion. |
| 137 loop_.RunAllPending(); |
| 138 } |
| 139 |
| 140 TEST_F(URLBlacklistManagerTest, ShutdownWithPendingTask3) { |
| 141 EXPECT_CALL(*url_blacklist_manager_, SetBlacklist(_)).Times(0); |
| 142 // Update posts a BuildBlacklistTask to the FILE thread. |
| 143 url_blacklist_manager_->UpdateNotMocked(); |
| 144 // Shutdown comes before the task is executed. |
| 145 url_blacklist_manager_->ShutdownOnUIThread(); |
| 146 // Run the task after shutdown, but before deletion. |
| 147 loop_.RunAllPending(); |
| 148 Mock::VerifyAndClearExpectations(url_blacklist_manager_.get()); |
| 149 url_blacklist_manager_.reset(); |
| 150 loop_.RunAllPending(); |
| 151 } |
| 152 |
| 153 TEST_F(URLBlacklistManagerTest, SchemeToFlag) { |
| 154 Blacklist::SchemeFlag flag; |
| 155 EXPECT_TRUE(Blacklist::SchemeToFlag("http", &flag)); |
| 156 EXPECT_EQ(Blacklist::SCHEME_HTTP, flag); |
| 157 EXPECT_TRUE(Blacklist::SchemeToFlag("https", &flag)); |
| 158 EXPECT_EQ(Blacklist::SCHEME_HTTPS, flag); |
| 159 EXPECT_TRUE(Blacklist::SchemeToFlag("ftp", &flag)); |
| 160 EXPECT_EQ(Blacklist::SCHEME_FTP, flag); |
| 161 EXPECT_TRUE(Blacklist::SchemeToFlag("", &flag)); |
| 162 EXPECT_EQ(Blacklist::SCHEME_ALL, flag); |
| 163 EXPECT_FALSE(Blacklist::SchemeToFlag("wtf", &flag)); |
| 164 } |
| 165 |
| 166 TEST_F(URLBlacklistManagerTest, FilterToComponents) { |
| 167 std::string scheme; |
| 168 std::string host; |
| 169 uint16 port; |
| 170 std::string path; |
| 171 |
| 172 Blacklist::FilterToComponents("google.com", &scheme, &host, &port, &path); |
| 173 EXPECT_EQ("", scheme); |
| 174 EXPECT_EQ("google.com", host); |
| 175 EXPECT_EQ(0u, port); |
| 176 EXPECT_EQ("", path); |
| 177 |
| 178 Blacklist::FilterToComponents( |
| 179 "http://google.com", &scheme, &host, &port, &path); |
| 180 EXPECT_EQ("http", scheme); |
| 181 EXPECT_EQ("google.com", host); |
| 182 EXPECT_EQ(0u, port); |
| 183 EXPECT_EQ("", path); |
| 184 |
| 185 Blacklist::FilterToComponents("google.com/", &scheme, &host, &port, &path); |
| 186 EXPECT_EQ("", scheme); |
| 187 EXPECT_EQ("google.com", host); |
| 188 EXPECT_EQ(0u, port); |
| 189 EXPECT_EQ("/", path); |
| 190 |
| 191 Blacklist::FilterToComponents( |
| 192 "http://google.com:8080/whatever", &scheme, &host, &port, &path); |
| 193 EXPECT_EQ("http", scheme); |
| 194 EXPECT_EQ("google.com", host); |
| 195 EXPECT_EQ(8080u, port); |
| 196 EXPECT_EQ("/whatever", path); |
| 197 |
| 198 Blacklist::FilterToComponents( |
| 199 "http://user:pass@google.com:8080/whatever", |
| 200 &scheme, &host, &port, &path); |
| 201 EXPECT_EQ("http", scheme); |
| 202 EXPECT_EQ("google.com", host); |
| 203 EXPECT_EQ(8080u, port); |
| 204 EXPECT_EQ("/whatever", path); |
| 205 |
| 206 Blacklist::FilterToComponents( |
| 207 "123.123.123.123", &scheme, &host, &port, &path); |
| 208 EXPECT_EQ("", scheme); |
| 209 EXPECT_EQ("123.123.123.123", host); |
| 210 EXPECT_EQ(0u, port); |
| 211 EXPECT_EQ("", path); |
| 212 |
| 213 Blacklist::FilterToComponents( |
| 214 "https://123.123.123.123", &scheme, &host, &port, &path); |
| 215 EXPECT_EQ("https", scheme); |
| 216 EXPECT_EQ("123.123.123.123", host); |
| 217 EXPECT_EQ(0u, port); |
| 218 EXPECT_EQ("", path); |
| 219 |
| 220 Blacklist::FilterToComponents( |
| 221 "123.123.123.123/", &scheme, &host, &port, &path); |
| 222 EXPECT_EQ("", scheme); |
| 223 EXPECT_EQ("123.123.123.123", host); |
| 224 EXPECT_EQ(0u, port); |
| 225 EXPECT_EQ("/", path); |
| 226 |
| 227 Blacklist::FilterToComponents( |
| 228 "http://123.123.123.123:123/whatever", &scheme, &host, &port, &path); |
| 229 EXPECT_EQ("http", scheme); |
| 230 EXPECT_EQ("123.123.123.123", host); |
| 231 EXPECT_EQ(123u, port); |
| 232 EXPECT_EQ("/whatever", path); |
| 233 |
| 234 Blacklist::FilterToComponents("*", &scheme, &host, &port, &path); |
| 235 EXPECT_EQ("", scheme); |
| 236 EXPECT_EQ("", host); |
| 237 EXPECT_EQ(0u, port); |
| 238 EXPECT_EQ("", path); |
| 239 |
| 240 Blacklist::FilterToComponents("ftp://*", &scheme, &host, &port, &path); |
| 241 EXPECT_EQ("ftp", scheme); |
| 242 EXPECT_EQ("", host); |
| 243 EXPECT_EQ(0u, port); |
| 244 EXPECT_EQ("", path); |
| 245 |
| 246 Blacklist::FilterToComponents( |
| 247 "http://*/whatever", &scheme, &host, &port, &path); |
| 248 EXPECT_EQ("http", scheme); |
| 249 EXPECT_EQ("", host); |
| 250 EXPECT_EQ(0u, port); |
| 251 EXPECT_EQ("/whatever", path); |
| 252 } |
| 253 |
| 254 TEST_F(URLBlacklistManagerTest, Filtering) { |
| 255 Blacklist blacklist; |
| 256 |
| 257 // Block domain and all subdomains, for any filtered scheme. |
| 258 blacklist.Block("google.com"); |
| 259 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com"))); |
| 260 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com/"))); |
| 261 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com/whatever"))); |
| 262 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://google.com/"))); |
| 263 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("bogus://google.com/"))); |
| 264 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://mail.google.com"))); |
| 265 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://x.mail.google.com"))); |
| 266 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://x.mail.google.com/"))); |
| 267 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://x.y.google.com/a/b"))); |
| 268 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://youtube.com/"))); |
| 269 |
| 270 // Filter only http and ftp schemes. |
| 271 blacklist.Block("http://secure.com"); |
| 272 blacklist.Block("ftp://secure.com"); |
| 273 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://secure.com"))); |
| 274 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://secure.com/whatever"))); |
| 275 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("ftp://secure.com/"))); |
| 276 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://secure.com/"))); |
| 277 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://www.secure.com"))); |
| 278 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://www.secure.com"))); |
| 279 |
| 280 // Filter only a certain path prefix. |
| 281 blacklist.Block("path.to/ruin"); |
| 282 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://path.to/ruin"))); |
| 283 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://path.to/ruin"))); |
| 284 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://path.to/ruins"))); |
| 285 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://path.to/ruin/signup"))); |
| 286 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://www.path.to/ruin"))); |
| 287 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://path.to/fortune"))); |
| 288 |
| 289 // Filter only a certain path prefix and scheme. |
| 290 blacklist.Block("https://s.aaa.com/path"); |
| 291 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://s.aaa.com/path"))); |
| 292 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://s.aaa.com/path/bbb"))); |
| 293 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://s.aaa.com/path"))); |
| 294 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://aaa.com/path"))); |
| 295 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://x.aaa.com/path"))); |
| 296 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://s.aaa.com/bbb"))); |
| 297 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://s.aaa.com/"))); |
| 298 |
| 299 // Test exceptions to path prefixes, and most specific matches. |
| 300 blacklist.Block("s.xxx.com/a"); |
| 301 blacklist.Allow("s.xxx.com/a/b"); |
| 302 blacklist.Block("https://s.xxx.com/a/b/c"); |
| 303 blacklist.Allow("https://s.xxx.com/a/b/c/d"); |
| 304 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://s.xxx.com/a"))); |
| 305 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://s.xxx.com/a/x"))); |
| 306 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://s.xxx.com/a/x"))); |
| 307 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://s.xxx.com/a/b"))); |
| 308 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://s.xxx.com/a/b"))); |
| 309 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://s.xxx.com/a/b/x"))); |
| 310 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://s.xxx.com/a/b/c"))); |
| 311 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://s.xxx.com/a/b/c"))); |
| 312 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://s.xxx.com/a/b/c/x"))); |
| 313 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://s.xxx.com/a/b/c/d"))); |
| 314 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://s.xxx.com/a/b/c/d"))); |
| 315 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://s.xxx.com/a/b/c/d/x"))); |
| 316 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://s.xxx.com/a/b/c/d/x"))); |
| 317 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://xxx.com/a"))); |
| 318 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://xxx.com/a/b"))); |
| 319 |
| 320 // Block an ip address. |
| 321 blacklist.Block("123.123.123.123"); |
| 322 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://123.123.123.123/"))); |
| 323 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://123.123.123.124/"))); |
| 324 |
| 325 // Open an exception. |
| 326 blacklist.Allow("plus.google.com"); |
| 327 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com/"))); |
| 328 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://www.google.com/"))); |
| 329 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://plus.google.com/"))); |
| 330 |
| 331 // Open an exception only when using https for mail. |
| 332 blacklist.Allow("https://mail.google.com"); |
| 333 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com/"))); |
| 334 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://mail.google.com/"))); |
| 335 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://www.google.com/"))); |
| 336 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://www.google.com/"))); |
| 337 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://mail.google.com/"))); |
| 338 |
| 339 // Match exactly "google.com", only for http. Subdomains without exceptions |
| 340 // are still blocked. |
| 341 blacklist.Allow("http://.google.com"); |
| 342 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://google.com/"))); |
| 343 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://google.com/"))); |
| 344 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://www.google.com/"))); |
| 345 |
| 346 // A smaller path match in an exact host overrides a longer path for hosts |
| 347 // that also match subdomains. |
| 348 blacklist.Block("yyy.com/aaa"); |
| 349 blacklist.Allow(".yyy.com/a"); |
| 350 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://yyy.com"))); |
| 351 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://yyy.com/aaa"))); |
| 352 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://yyy.com/aaa2"))); |
| 353 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://www.yyy.com"))); |
| 354 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://www.yyy.com/aaa"))); |
| 355 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://www.yyy.com/aaa2"))); |
| 356 } |
| 357 |
| 358 TEST_F(URLBlacklistManagerTest, BlockAllWithExceptions) { |
| 359 Blacklist blacklist; |
| 360 |
| 361 blacklist.Block("*"); |
| 362 blacklist.Allow(".www.google.com"); |
| 363 blacklist.Allow("plus.google.com"); |
| 364 blacklist.Allow("https://mail.google.com"); |
| 365 blacklist.Allow("https://very.safe/path"); |
| 366 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://random.com"))); |
| 367 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com"))); |
| 368 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://s.www.google.com"))); |
| 369 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://www.google.com"))); |
| 370 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://plus.google.com"))); |
| 371 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://s.plus.google.com"))); |
| 372 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://mail.google.com"))); |
| 373 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://mail.google.com"))); |
| 374 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://s.mail.google.com"))); |
| 375 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://very.safe/"))); |
| 376 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://very.safe/path"))); |
| 377 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://very.safe/path"))); |
| 378 } |
| 379 |
| 380 } // namespace |
| 381 |
| 382 } // namespace policy |
OLD | NEW |