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

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

Issue 110643005: Refactored the URLBlacklistManager to avoid chrome/ and content/ dependencies. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 years 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/message_loop.h" 10 #include "base/message_loop/message_loop.h"
11 #include "base/message_loop/message_loop_proxy.h"
11 #include "base/prefs/pref_registry_simple.h" 12 #include "base/prefs/pref_registry_simple.h"
12 #include "base/prefs/testing_pref_service.h" 13 #include "base/prefs/testing_pref_service.h"
13 #include "chrome/common/pref_names.h" 14 #include "chrome/browser/policy/policy_helpers.h"
14 #include "content/public/test/test_browser_thread.h" 15 #include "chrome/common/net/url_fixer_upper.h"
16 #include "components/policy/core/common/policy_pref_names.h"
15 #include "google_apis/gaia/gaia_urls.h" 17 #include "google_apis/gaia/gaia_urls.h"
16 #include "net/base/request_priority.h" 18 #include "net/base/request_priority.h"
17 #include "net/url_request/url_request.h" 19 #include "net/url_request/url_request.h"
18 #include "net/url_request/url_request_test_util.h" 20 #include "net/url_request/url_request_test_util.h"
19 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
20 #include "url/gurl.h" 22 #include "url/gurl.h"
21 23
22 namespace policy { 24 namespace policy {
23 25
24 namespace { 26 namespace {
25 27
26 using content::BrowserThread; 28 // Helper to get the disambiguated SegmentURL() function.
29 URLBlacklist::SegmentURLCallback GetSegmentURLCallback() {
30 return URLFixerUpper::SegmentURL;
31 }
27 32
28 class TestingURLBlacklistManager : public URLBlacklistManager { 33 class TestingURLBlacklistManager : public URLBlacklistManager {
29 public: 34 public:
30 explicit TestingURLBlacklistManager(PrefService* pref_service) 35 explicit TestingURLBlacklistManager(PrefService* pref_service)
31 : URLBlacklistManager(pref_service), 36 : URLBlacklistManager(pref_service,
37 base::MessageLoopProxy::current(),
38 GetSegmentURLCallback(),
39 SkipBlacklistForURL),
32 update_called_(0), 40 update_called_(0),
33 set_blacklist_called_(false) { 41 set_blacklist_called_(false) {}
34 }
35 42
36 virtual ~TestingURLBlacklistManager() { 43 virtual ~TestingURLBlacklistManager() {
37 } 44 }
38 45
39 // Make this method public for testing. 46 // Make this method public for testing.
40 using URLBlacklistManager::ScheduleUpdate; 47 using URLBlacklistManager::ScheduleUpdate;
41 48
42 // Makes a direct call to UpdateOnIO during tests. 49 // Makes a direct call to UpdateOnIO during tests.
43 void UpdateOnIOForTesting() { 50 void UpdateOnIOForTesting() {
44 scoped_ptr<base::ListValue> block(new base::ListValue); 51 scoped_ptr<base::ListValue> block(new base::ListValue);
(...skipping 18 matching lines...) Expand all
63 70
64 private: 71 private:
65 int update_called_; 72 int update_called_;
66 bool set_blacklist_called_; 73 bool set_blacklist_called_;
67 74
68 DISALLOW_COPY_AND_ASSIGN(TestingURLBlacklistManager); 75 DISALLOW_COPY_AND_ASSIGN(TestingURLBlacklistManager);
69 }; 76 };
70 77
71 class URLBlacklistManagerTest : public testing::Test { 78 class URLBlacklistManagerTest : public testing::Test {
72 protected: 79 protected:
73 URLBlacklistManagerTest() 80 URLBlacklistManagerTest() : loop_(base::MessageLoop::TYPE_IO) {}
74 : loop_(base::MessageLoop::TYPE_IO),
75 ui_thread_(BrowserThread::UI, &loop_),
76 file_thread_(BrowserThread::FILE, &loop_),
77 io_thread_(BrowserThread::IO, &loop_) {
78 }
79 81
80 virtual void SetUp() OVERRIDE { 82 virtual void SetUp() OVERRIDE {
81 pref_service_.registry()->RegisterListPref(prefs::kUrlBlacklist); 83 pref_service_.registry()->RegisterListPref(policy_prefs::kUrlBlacklist);
82 pref_service_.registry()->RegisterListPref(prefs::kUrlWhitelist); 84 pref_service_.registry()->RegisterListPref(policy_prefs::kUrlWhitelist);
83 blacklist_manager_.reset( 85 blacklist_manager_.reset(new TestingURLBlacklistManager(&pref_service_));
84 new TestingURLBlacklistManager(&pref_service_));
85 loop_.RunUntilIdle(); 86 loop_.RunUntilIdle();
86 } 87 }
87 88
88 virtual void TearDown() OVERRIDE { 89 virtual void TearDown() OVERRIDE {
89 if (blacklist_manager_.get()) 90 if (blacklist_manager_.get())
90 blacklist_manager_->ShutdownOnUIThread(); 91 blacklist_manager_->ShutdownOnUIThread();
91 loop_.RunUntilIdle(); 92 loop_.RunUntilIdle();
92 // Delete |blacklist_manager_| while |io_thread_| is mapping IO to 93 // Delete |blacklist_manager_| while |io_thread_| is mapping IO to
93 // |loop_|. 94 // |loop_|.
94 blacklist_manager_.reset(); 95 blacklist_manager_.reset();
95 } 96 }
96 97
97 base::MessageLoop loop_; 98 base::MessageLoop loop_;
98 TestingPrefServiceSimple pref_service_; 99 TestingPrefServiceSimple pref_service_;
99 scoped_ptr<TestingURLBlacklistManager> blacklist_manager_; 100 scoped_ptr<TestingURLBlacklistManager> blacklist_manager_;
100
101 private:
102 content::TestBrowserThread ui_thread_;
103 content::TestBrowserThread file_thread_;
104 content::TestBrowserThread io_thread_;
105
106 DISALLOW_COPY_AND_ASSIGN(URLBlacklistManagerTest);
107 }; 101 };
108 102
109 // Parameters for the FilterToComponents test. 103 // Parameters for the FilterToComponents test.
110 struct FilterTestParams { 104 struct FilterTestParams {
111 public: 105 public:
112 FilterTestParams(const std::string& filter, const std::string& scheme, 106 FilterTestParams(const std::string& filter, const std::string& scheme,
113 const std::string& host, bool match_subdomains, uint16 port, 107 const std::string& host, bool match_subdomains, uint16 port,
114 const std::string& path) 108 const std::string& path)
115 : filter_(filter), scheme_(scheme), host_(host), 109 : filter_(filter), scheme_(scheme), host_(host),
116 match_subdomains_(match_subdomains), port_(port), path_(path) {} 110 match_subdomains_(match_subdomains), port_(port), path_(path) {}
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 158
165 } // namespace 159 } // namespace
166 160
167 TEST_P(URLBlacklistFilterToComponentsTest, FilterToComponents) { 161 TEST_P(URLBlacklistFilterToComponentsTest, FilterToComponents) {
168 std::string scheme; 162 std::string scheme;
169 std::string host; 163 std::string host;
170 bool match_subdomains = true; 164 bool match_subdomains = true;
171 uint16 port = 42; 165 uint16 port = 42;
172 std::string path; 166 std::string path;
173 167
174 URLBlacklist::FilterToComponents(GetParam().filter(), &scheme, &host, 168 URLBlacklist::FilterToComponents(GetSegmentURLCallback(), GetParam().filter(),
175 &match_subdomains, &port, &path); 169 &scheme, &host, &match_subdomains, &port,
170 &path);
176 EXPECT_EQ(GetParam().scheme(), scheme); 171 EXPECT_EQ(GetParam().scheme(), scheme);
177 EXPECT_EQ(GetParam().host(), host); 172 EXPECT_EQ(GetParam().host(), host);
178 EXPECT_EQ(GetParam().match_subdomains(), match_subdomains); 173 EXPECT_EQ(GetParam().match_subdomains(), match_subdomains);
179 EXPECT_EQ(GetParam().port(), port); 174 EXPECT_EQ(GetParam().port(), port);
180 EXPECT_EQ(GetParam().path(), path); 175 EXPECT_EQ(GetParam().path(), path);
181 } 176 }
182 177
183 TEST_F(URLBlacklistManagerTest, SingleUpdateForTwoPrefChanges) { 178 TEST_F(URLBlacklistManagerTest, SingleUpdateForTwoPrefChanges) {
184 ListValue* blacklist = new ListValue; 179 ListValue* blacklist = new ListValue;
185 blacklist->Append(new StringValue("*.google.com")); 180 blacklist->Append(new StringValue("*.google.com"));
186 ListValue* whitelist = new ListValue; 181 ListValue* whitelist = new ListValue;
187 whitelist->Append(new StringValue("mail.google.com")); 182 whitelist->Append(new StringValue("mail.google.com"));
188 pref_service_.SetManagedPref(prefs::kUrlBlacklist, blacklist); 183 pref_service_.SetManagedPref(policy_prefs::kUrlBlacklist, blacklist);
189 pref_service_.SetManagedPref(prefs::kUrlBlacklist, whitelist); 184 pref_service_.SetManagedPref(policy_prefs::kUrlBlacklist, whitelist);
190 loop_.RunUntilIdle(); 185 loop_.RunUntilIdle();
191 186
192 EXPECT_EQ(1, blacklist_manager_->update_called()); 187 EXPECT_EQ(1, blacklist_manager_->update_called());
193 } 188 }
194 189
195 TEST_F(URLBlacklistManagerTest, ShutdownWithPendingTask0) { 190 TEST_F(URLBlacklistManagerTest, ShutdownWithPendingTask0) {
196 // Post an update task to the UI thread. 191 // Post an update task to the UI thread.
197 blacklist_manager_->ScheduleUpdate(); 192 blacklist_manager_->ScheduleUpdate();
198 // Shutdown comes before the task is executed. 193 // Shutdown comes before the task is executed.
199 blacklist_manager_->ShutdownOnUIThread(); 194 blacklist_manager_->ShutdownOnUIThread();
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 0u, 298 0u,
304 std::string()), 299 std::string()),
305 FilterTestParams("http://*/whatever", 300 FilterTestParams("http://*/whatever",
306 "http", 301 "http",
307 std::string(), 302 std::string(),
308 true, 303 true,
309 0u, 304 0u,
310 "/whatever"))); 305 "/whatever")));
311 306
312 TEST_F(URLBlacklistManagerTest, Filtering) { 307 TEST_F(URLBlacklistManagerTest, Filtering) {
313 URLBlacklist blacklist; 308 URLBlacklist blacklist(GetSegmentURLCallback());
314 309
315 // Block domain and all subdomains, for any filtered scheme. 310 // Block domain and all subdomains, for any filtered scheme.
316 scoped_ptr<base::ListValue> blocked(new base::ListValue); 311 scoped_ptr<base::ListValue> blocked(new base::ListValue);
317 blocked->Append(new base::StringValue("google.com")); 312 blocked->Append(new base::StringValue("google.com"));
318 blacklist.Block(blocked.get()); 313 blacklist.Block(blocked.get());
319 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com"))); 314 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com")));
320 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com/"))); 315 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com/")));
321 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com/whatever"))); 316 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com/whatever")));
322 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://google.com/"))); 317 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://google.com/")));
323 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("bogus://google.com/"))); 318 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("bogus://google.com/")));
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 blocked.reset(new base::ListValue); 451 blocked.reset(new base::ListValue);
457 blocked->Append(new base::StringValue("example.com")); 452 blocked->Append(new base::StringValue("example.com"));
458 blacklist.Block(blocked.get()); 453 blacklist.Block(blocked.get());
459 allowed.reset(new base::ListValue); 454 allowed.reset(new base::ListValue);
460 allowed->Append(new base::StringValue("example.com")); 455 allowed->Append(new base::StringValue("example.com"));
461 blacklist.Allow(allowed.get()); 456 blacklist.Allow(allowed.get());
462 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://example.com"))); 457 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://example.com")));
463 } 458 }
464 459
465 TEST_F(URLBlacklistManagerTest, BlockAllWithExceptions) { 460 TEST_F(URLBlacklistManagerTest, BlockAllWithExceptions) {
466 URLBlacklist blacklist; 461 URLBlacklist blacklist(GetSegmentURLCallback());
467 462
468 scoped_ptr<base::ListValue> blocked(new base::ListValue); 463 scoped_ptr<base::ListValue> blocked(new base::ListValue);
469 scoped_ptr<base::ListValue> allowed(new base::ListValue); 464 scoped_ptr<base::ListValue> allowed(new base::ListValue);
470 blocked->Append(new base::StringValue("*")); 465 blocked->Append(new base::StringValue("*"));
471 allowed->Append(new base::StringValue(".www.google.com")); 466 allowed->Append(new base::StringValue(".www.google.com"));
472 allowed->Append(new base::StringValue("plus.google.com")); 467 allowed->Append(new base::StringValue("plus.google.com"));
473 allowed->Append(new base::StringValue("https://mail.google.com")); 468 allowed->Append(new base::StringValue("https://mail.google.com"));
474 allowed->Append(new base::StringValue("https://very.safe/path")); 469 allowed->Append(new base::StringValue("https://very.safe/path"));
475 blacklist.Block(blocked.get()); 470 blacklist.Block(blocked.get());
476 blacklist.Allow(allowed.get()); 471 blacklist.Allow(allowed.get());
477 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://random.com"))); 472 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://random.com")));
478 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com"))); 473 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://google.com")));
479 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://s.www.google.com"))); 474 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://s.www.google.com")));
480 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://www.google.com"))); 475 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://www.google.com")));
481 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://plus.google.com"))); 476 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://plus.google.com")));
482 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://s.plus.google.com"))); 477 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("http://s.plus.google.com")));
483 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://mail.google.com"))); 478 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://mail.google.com")));
484 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://mail.google.com"))); 479 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://mail.google.com")));
485 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://s.mail.google.com"))); 480 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://s.mail.google.com")));
486 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://very.safe/"))); 481 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("https://very.safe/")));
487 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://very.safe/path"))); 482 EXPECT_TRUE(blacklist.IsURLBlocked(GURL("http://very.safe/path")));
488 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://very.safe/path"))); 483 EXPECT_FALSE(blacklist.IsURLBlocked(GURL("https://very.safe/path")));
489 } 484 }
490 485
491 TEST_F(URLBlacklistManagerTest, DontBlockResources) { 486 TEST_F(URLBlacklistManagerTest, DontBlockResources) {
492 scoped_ptr<URLBlacklist> blacklist(new URLBlacklist()); 487 scoped_ptr<URLBlacklist> blacklist(new URLBlacklist(GetSegmentURLCallback()));
493 scoped_ptr<base::ListValue> blocked(new base::ListValue); 488 scoped_ptr<base::ListValue> blocked(new base::ListValue);
494 blocked->Append(new base::StringValue("google.com")); 489 blocked->Append(new base::StringValue("google.com"));
495 blacklist->Block(blocked.get()); 490 blacklist->Block(blocked.get());
496 blacklist_manager_->SetBlacklist(blacklist.Pass()); 491 blacklist_manager_->SetBlacklist(blacklist.Pass());
497 EXPECT_TRUE(blacklist_manager_->IsURLBlocked(GURL("http://google.com"))); 492 EXPECT_TRUE(blacklist_manager_->IsURLBlocked(GURL("http://google.com")));
498 493
499 net::TestURLRequestContext context; 494 net::TestURLRequestContext context;
500 net::URLRequest request( 495 net::URLRequest request(
501 GURL("http://google.com"), net::DEFAULT_PRIORITY, NULL, &context); 496 GURL("http://google.com"), net::DEFAULT_PRIORITY, NULL, &context);
502 497
(...skipping 14 matching lines...) Expand all
517 512
518 GURL sync_url(GaiaUrls::GetInstance()->service_login_url().Resolve( 513 GURL sync_url(GaiaUrls::GetInstance()->service_login_url().Resolve(
519 "?service=chromiumsync")); 514 "?service=chromiumsync"));
520 net::URLRequest sync_request(sync_url, net::DEFAULT_PRIORITY, NULL, &context); 515 net::URLRequest sync_request(sync_url, net::DEFAULT_PRIORITY, NULL, &context);
521 sync_request.SetLoadFlags(net::LOAD_MAIN_FRAME); 516 sync_request.SetLoadFlags(net::LOAD_MAIN_FRAME);
522 EXPECT_EQ(block_signin_urls, 517 EXPECT_EQ(block_signin_urls,
523 blacklist_manager_->IsRequestBlocked(sync_request)); 518 blacklist_manager_->IsRequestBlocked(sync_request));
524 } 519 }
525 520
526 } // namespace policy 521 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/url_blacklist_manager.cc ('k') | chrome/browser/policy/url_blacklist_policy_handler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698