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

Side by Side Diff: chrome/browser/safe_browsing/safe_browsing_test.cc

Issue 1943993006: Create test fixture for SafeBrowsingService (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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
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 // This test uses the safebrowsing test server published at 5 // This test uses the safebrowsing test server published at
6 // http://code.google.com/p/google-safe-browsing/ to test the safebrowsing 6 // http://code.google.com/p/google-safe-browsing/ to test the safebrowsing
7 // protocol implemetation. Details of the safebrowsing testing flow is 7 // protocol implemetation. Details of the safebrowsing testing flow is
8 // documented at 8 // documented at
9 // http://code.google.com/p/google-safe-browsing/wiki/ProtocolTesting 9 // http://code.google.com/p/google-safe-browsing/wiki/ProtocolTesting
10 // 10 //
(...skipping 20 matching lines...) Expand all
31 #include "base/test/test_timeouts.h" 31 #include "base/test/test_timeouts.h"
32 #include "base/threading/platform_thread.h" 32 #include "base/threading/platform_thread.h"
33 #include "base/threading/thread.h" 33 #include "base/threading/thread.h"
34 #include "base/time/time.h" 34 #include "base/time/time.h"
35 #include "chrome/browser/browser_process.h" 35 #include "chrome/browser/browser_process.h"
36 #include "chrome/browser/chrome_notification_types.h" 36 #include "chrome/browser/chrome_notification_types.h"
37 #include "chrome/browser/profiles/profile.h" 37 #include "chrome/browser/profiles/profile.h"
38 #include "chrome/browser/safe_browsing/local_database_manager.h" 38 #include "chrome/browser/safe_browsing/local_database_manager.h"
39 #include "chrome/browser/safe_browsing/local_safebrowsing_test_server.h" 39 #include "chrome/browser/safe_browsing/local_safebrowsing_test_server.h"
40 #include "chrome/browser/safe_browsing/protocol_manager.h" 40 #include "chrome/browser/safe_browsing/protocol_manager.h"
41 #include "chrome/browser/safe_browsing/safe_browsing_service.h" 41 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h"
42 #include "chrome/browser/ui/browser.h" 42 #include "chrome/browser/ui/browser.h"
43 #include "chrome/common/chrome_switches.h" 43 #include "chrome/common/chrome_switches.h"
44 #include "chrome/common/url_constants.h" 44 #include "chrome/common/url_constants.h"
45 #include "chrome/test/base/in_process_browser_test.h" 45 #include "chrome/test/base/in_process_browser_test.h"
46 #include "components/safe_browsing_db/database_manager.h" 46 #include "components/safe_browsing_db/database_manager.h"
47 #include "content/public/browser/browser_context.h" 47 #include "content/public/browser/browser_context.h"
48 #include "content/public/test/test_browser_thread.h" 48 #include "content/public/test/test_browser_thread.h"
49 #include "content/public/test/test_utils.h" 49 #include "content/public/test/test_utils.h"
50 #include "net/base/load_flags.h" 50 #include "net/base/load_flags.h"
51 #include "net/dns/host_resolver.h" 51 #include "net/dns/host_resolver.h"
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 } else { 108 } else {
109 LOG(ERROR) << "Unrecognized expectation in " << url_str.as_string() 109 LOG(ERROR) << "Unrecognized expectation in " << url_str.as_string()
110 << ": " << record_parts[2]; 110 << ": " << record_parts[2];
111 return false; 111 return false;
112 } 112 }
113 phishing_urls->push_back(phishing_url); 113 phishing_urls->push_back(phishing_url);
114 } 114 }
115 return true; 115 return true;
116 } 116 }
117 117
118 class FakeSafeBrowsingService : public SafeBrowsingService {
119 public:
120 explicit FakeSafeBrowsingService(const std::string& url_prefix)
121 : url_prefix_(url_prefix) {}
122
123 SafeBrowsingProtocolConfig GetProtocolConfig() const override {
124 SafeBrowsingProtocolConfig config;
125 config.url_prefix = url_prefix_;
126 // Makes sure the auto update is not triggered. The tests will force the
127 // update when needed.
128 config.disable_auto_update = true;
129 config.client_name = "browser_tests";
130 return config;
131 }
132
133 private:
134 ~FakeSafeBrowsingService() override {}
135
136 std::string url_prefix_;
137
138 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingService);
139 };
140
141 // Factory that creates FakeSafeBrowsingService instances.
142 class TestSafeBrowsingServiceFactory : public SafeBrowsingServiceFactory {
143 public:
144 explicit TestSafeBrowsingServiceFactory(const std::string& url_prefix)
145 : url_prefix_(url_prefix) {}
146
147 SafeBrowsingService* CreateSafeBrowsingService() override {
148 return new FakeSafeBrowsingService(url_prefix_);
149 }
150
151 private:
152 std::string url_prefix_;
153 };
154
155 } // namespace 118 } // namespace
156 119
157 // This starts the browser and keeps status of states related to SafeBrowsing. 120 // This starts the browser and keeps status of states related to SafeBrowsing.
158 class SafeBrowsingServerTest : public InProcessBrowserTest { 121 class SafeBrowsingServerTest : public InProcessBrowserTest {
159 public: 122 public:
160 SafeBrowsingServerTest() 123 SafeBrowsingServerTest()
161 : safe_browsing_service_(NULL), 124 : is_database_ready_(true),
162 is_database_ready_(true),
163 is_update_scheduled_(false), 125 is_update_scheduled_(false),
164 is_checked_url_in_db_(false), 126 is_checked_url_in_db_(false),
165 is_checked_url_safe_(false) { 127 is_checked_url_safe_(false) {
166 } 128 }
167 129
168 ~SafeBrowsingServerTest() override {} 130 ~SafeBrowsingServerTest() override {}
169 131
170 void UpdateSafeBrowsingStatus() { 132 void UpdateSafeBrowsingStatus() {
171 ASSERT_TRUE(safe_browsing_service_); 133 ASSERT_TRUE(sb_factory_->test_safe_browsing_service());
172 base::AutoLock lock(update_status_mutex_); 134 base::AutoLock lock(update_status_mutex_);
173 last_update_ = safe_browsing_service_->protocol_manager_->last_update(); 135 last_update_ = sb_factory_->test_safe_browsing_service()
136 ->protocol_manager_->last_update();
174 is_update_scheduled_ = 137 is_update_scheduled_ =
175 safe_browsing_service_->protocol_manager_->update_timer_.IsRunning(); 138 sb_factory_->test_safe_browsing_service()->protocol_manager_
139 ->update_timer_.IsRunning();
176 } 140 }
177 141
178 void ForceUpdate() { 142 void ForceUpdate() {
179 content::WindowedNotificationObserver observer( 143 content::WindowedNotificationObserver observer(
180 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, 144 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE,
181 content::Source<SafeBrowsingDatabaseManager>(database_manager())); 145 content::Source<SafeBrowsingDatabaseManager>(database_manager()));
182 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 146 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
183 base::Bind(&SafeBrowsingServerTest::ForceUpdateOnIOThread, 147 base::Bind(&SafeBrowsingServerTest::ForceUpdateOnIOThread,
184 this)); 148 this));
185 observer.Wait(); 149 observer.Wait();
186 } 150 }
187 151
188 void ForceUpdateOnIOThread() { 152 void ForceUpdateOnIOThread() {
189 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); 153 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
190 ASSERT_TRUE(safe_browsing_service_); 154 ASSERT_TRUE(sb_factory_->test_safe_browsing_service());
191 safe_browsing_service_->protocol_manager_->ForceScheduleNextUpdate( 155 sb_factory_->test_safe_browsing_service()->protocol_manager_
192 base::TimeDelta::FromSeconds(0)); 156 ->ForceScheduleNextUpdate(base::TimeDelta::FromSeconds(0));
193 } 157 }
194 158
195 159
196 void CheckIsDatabaseReady() { 160 void CheckIsDatabaseReady() {
197 base::AutoLock lock(update_status_mutex_); 161 base::AutoLock lock(update_status_mutex_);
198 is_database_ready_ = 162 is_database_ready_ =
199 !local_database_manager()->database_update_in_progress_; 163 !local_database_manager()->database_update_in_progress_;
200 } 164 }
201 165
202 void CheckUrl(SafeBrowsingDatabaseManager::Client* helper, const GURL& url) { 166 void CheckUrl(SafeBrowsingDatabaseManager::Client* helper, const GURL& url) {
203 ASSERT_TRUE(safe_browsing_service_); 167 ASSERT_TRUE(sb_factory_->test_safe_browsing_service());
204 base::AutoLock lock(update_status_mutex_); 168 base::AutoLock lock(update_status_mutex_);
205 if (database_manager()->CheckBrowseUrl(url, helper)) { 169 if (database_manager()->CheckBrowseUrl(url, helper)) {
206 is_checked_url_in_db_ = false; 170 is_checked_url_in_db_ = false;
207 is_checked_url_safe_ = true; 171 is_checked_url_safe_ = true;
208 } else { 172 } else {
209 // In this case, Safebrowsing service will fetch the full hash 173 // In this case, Safebrowsing service will fetch the full hash
210 // from the server and examine that. Once it is done, 174 // from the server and examine that. Once it is done,
211 // set_is_checked_url_safe() will be called via callback. 175 // set_is_checked_url_safe() will be called via callback.
212 is_checked_url_in_db_ = true; 176 is_checked_url_in_db_ = true;
213 } 177 }
214 } 178 }
215 179
216 SafeBrowsingDatabaseManager* database_manager() { 180 SafeBrowsingDatabaseManager* database_manager() {
217 return safe_browsing_service_->database_manager().get(); 181 return sb_factory_->test_safe_browsing_service()->database_manager().get();
218 } 182 }
219 183
220 // TODO(nparker): Remove the need for this by wiring in our own 184 // TODO(nparker): Remove the need for this by wiring in our own
221 // SafeBrowsingDatabaseManager factory and keep a ptr to the subclass. 185 // SafeBrowsingDatabaseManager factory and keep a ptr to the subclass.
222 LocalSafeBrowsingDatabaseManager* local_database_manager() { 186 LocalSafeBrowsingDatabaseManager* local_database_manager() {
223 return static_cast<LocalSafeBrowsingDatabaseManager*>(database_manager()); 187 return static_cast<LocalSafeBrowsingDatabaseManager*>(database_manager());
224 } 188 }
225 189
226 190
227 bool is_checked_url_in_db() { 191 bool is_checked_url_in_db() {
(...skipping 28 matching lines...) Expand all
256 220
257 scoped_refptr<base::SequencedTaskRunner> SafeBrowsingTaskRunner() { 221 scoped_refptr<base::SequencedTaskRunner> SafeBrowsingTaskRunner() {
258 return local_database_manager()->safe_browsing_task_runner_; 222 return local_database_manager()->safe_browsing_task_runner_;
259 } 223 }
260 224
261 const net::SpawnedTestServer& spawned_test_server() const { 225 const net::SpawnedTestServer& spawned_test_server() const {
262 return *test_server_; 226 return *test_server_;
263 } 227 }
264 228
265 protected: 229 protected:
266 bool InitSafeBrowsingService() { 230 void SetProtocolConfigURLPrefix(const std::string& url_prefix) {
267 safe_browsing_service_ = g_browser_process->safe_browsing_service(); 231 SafeBrowsingProtocolConfig config;
268 return safe_browsing_service_ != NULL; 232 config.url_prefix = url_prefix;
233 // Makes sure the auto update is not triggered. The tests will force the
234 // update when needed.
235 config.disable_auto_update = true;
236 config.client_name = "browser_tests";
237 sb_factory_->SetTestProtocolConfig(config);
269 } 238 }
270 239
271 void SetUp() override { 240 void SetUp() override {
272 base::FilePath datafile_path; 241 base::FilePath datafile_path;
273 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &datafile_path)); 242 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &datafile_path));
274 243
275 datafile_path = datafile_path.Append(FILE_PATH_LITERAL("third_party")) 244 datafile_path = datafile_path.Append(FILE_PATH_LITERAL("third_party"))
276 .Append(FILE_PATH_LITERAL("safe_browsing")) 245 .Append(FILE_PATH_LITERAL("safe_browsing"))
277 .Append(FILE_PATH_LITERAL("testing")) 246 .Append(FILE_PATH_LITERAL("testing"))
278 .Append(kDataFile); 247 .Append(kDataFile);
279 test_server_.reset(new LocalSafeBrowsingTestServer(datafile_path)); 248 test_server_.reset(new LocalSafeBrowsingTestServer(datafile_path));
280 ASSERT_TRUE(test_server_->Start()); 249 ASSERT_TRUE(test_server_->Start());
281 LOG(INFO) << "server is " << test_server_->host_port_pair().ToString(); 250 LOG(INFO) << "server is " << test_server_->host_port_pair().ToString();
282 251
252 sb_factory_.reset(new TestSafeBrowsingServiceFactory());
283 // Point to the testing server for all SafeBrowsing requests. 253 // Point to the testing server for all SafeBrowsing requests.
284 std::string url_prefix = test_server_->GetURL("safebrowsing").spec(); 254 SetProtocolConfigURLPrefix(test_server_->GetURL("safebrowsing").spec());
285 sb_factory_.reset(new TestSafeBrowsingServiceFactory(url_prefix));
286 SafeBrowsingService::RegisterFactory(sb_factory_.get()); 255 SafeBrowsingService::RegisterFactory(sb_factory_.get());
287 256
288 InProcessBrowserTest::SetUp(); 257 InProcessBrowserTest::SetUp();
289 } 258 }
290 259
291 void TearDown() override { 260 void TearDown() override {
292 InProcessBrowserTest::TearDown(); 261 InProcessBrowserTest::TearDown();
293 262
294 SafeBrowsingService::RegisterFactory(NULL); 263 SafeBrowsingService::RegisterFactory(NULL);
295 } 264 }
296 265
297 void SetUpCommandLine(base::CommandLine* command_line) override { 266 void SetUpCommandLine(base::CommandLine* command_line) override {
298 // TODO(lzheng): The test server does not understand download related 267 // TODO(lzheng): The test server does not understand download related
299 // requests. We need to fix the server. 268 // requests. We need to fix the server.
300 command_line->AppendSwitch(switches::kSbDisableDownloadProtection); 269 command_line->AppendSwitch(switches::kSbDisableDownloadProtection);
301 270
302 // TODO(gcasto): Generate new testing data that includes the 271 // TODO(gcasto): Generate new testing data that includes the
303 // client-side phishing whitelist. 272 // client-side phishing whitelist.
304 command_line->AppendSwitch( 273 command_line->AppendSwitch(
305 switches::kDisableClientSidePhishingDetection); 274 switches::kDisableClientSidePhishingDetection);
306 275
307 // TODO(kalman): Generate new testing data that includes the extension 276 // TODO(kalman): Generate new testing data that includes the extension
308 // blacklist. 277 // blacklist.
309 command_line->AppendSwitch(switches::kSbDisableExtensionBlacklist); 278 command_line->AppendSwitch(switches::kSbDisableExtensionBlacklist);
310 } 279 }
311 280
312 void SetTestStep(int step) { 281 void SetTestStep(int step) {
313 std::string test_step = base::StringPrintf("test_step=%d", step); 282 std::string test_step = base::StringPrintf("test_step=%d", step);
314 safe_browsing_service_->protocol_manager_->set_additional_query(test_step); 283 sb_factory_->test_safe_browsing_service()->protocol_manager_
284 ->set_additional_query(test_step);
315 } 285 }
316 286
317 private:
318 std::unique_ptr<TestSafeBrowsingServiceFactory> sb_factory_; 287 std::unique_ptr<TestSafeBrowsingServiceFactory> sb_factory_;
319 288
320 SafeBrowsingService* safe_browsing_service_; 289 private:
321
322 std::unique_ptr<net::SpawnedTestServer> test_server_; 290 std::unique_ptr<net::SpawnedTestServer> test_server_;
323 291
324 // Protects all variables below since they are read on UI thread 292 // Protects all variables below since they are read on UI thread
325 // but updated on IO thread or safebrowsing thread. 293 // but updated on IO thread or safebrowsing thread.
326 base::Lock update_status_mutex_; 294 base::Lock update_status_mutex_;
327 295
328 // States associated with safebrowsing service updates. 296 // States associated with safebrowsing service updates.
329 bool is_database_ready_; 297 bool is_database_ready_;
330 base::Time last_update_; 298 base::Time last_update_;
331 bool is_update_scheduled_; 299 bool is_update_scheduled_;
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
494 std::unique_ptr<net::URLFetcher> url_fetcher_; 462 std::unique_ptr<net::URLFetcher> url_fetcher_;
495 std::string response_data_; 463 std::string response_data_;
496 net::URLRequestStatus::Status response_status_; 464 net::URLRequestStatus::Status response_status_;
497 net::URLRequestContextGetter* request_context_; 465 net::URLRequestContextGetter* request_context_;
498 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServerTestHelper); 466 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServerTestHelper);
499 }; 467 };
500 468
501 // TODO(shess): Disabled pending new data for third_party/safe_browsing/testing/ 469 // TODO(shess): Disabled pending new data for third_party/safe_browsing/testing/
502 IN_PROC_BROWSER_TEST_F(SafeBrowsingServerTest, 470 IN_PROC_BROWSER_TEST_F(SafeBrowsingServerTest,
503 DISABLED_SafeBrowsingServerTest) { 471 DISABLED_SafeBrowsingServerTest) {
504 LOG(INFO) << "Start test"; 472 ASSERT_TRUE(sb_factory_->test_safe_browsing_service() != NULL);
505 ASSERT_TRUE(InitSafeBrowsingService());
506 473
507 net::URLRequestContextGetter* request_context = 474 net::URLRequestContextGetter* request_context =
508 browser()->profile()->GetRequestContext(); 475 browser()->profile()->GetRequestContext();
509 scoped_refptr<SafeBrowsingServerTestHelper> safe_browsing_helper( 476 scoped_refptr<SafeBrowsingServerTestHelper> safe_browsing_helper(
510 new SafeBrowsingServerTestHelper(this, request_context)); 477 new SafeBrowsingServerTestHelper(this, request_context));
511 int last_step = 0; 478 int last_step = 0;
512 479
513 // Waits and makes sure safebrowsing update is not happening. 480 // Waits and makes sure safebrowsing update is not happening.
514 // The wait will stop once OnWaitForStatusUpdateDone in 481 // The wait will stop once OnWaitForStatusUpdateDone in
515 // safe_browsing_helper is called and status from safe_browsing_service_ 482 // safe_browsing_helper is called and status from test_safe_browsing_service
516 // is checked. 483 // is checked.
517 safe_browsing_helper->UpdateStatus(); 484 safe_browsing_helper->UpdateStatus();
518 EXPECT_TRUE(is_database_ready()); 485 EXPECT_TRUE(is_database_ready());
519 EXPECT_FALSE(is_update_scheduled()); 486 EXPECT_FALSE(is_update_scheduled());
520 EXPECT_TRUE(last_update().is_null()); 487 EXPECT_TRUE(last_update().is_null());
521 // Starts updates. After each update, the test will fetch a list of URLs with 488 // Starts updates. After each update, the test will fetch a list of URLs with
522 // expected results to verify with safebrowsing service. If there is no error, 489 // expected results to verify with safebrowsing service. If there is no error,
523 // the test moves on to the next step to get more update chunks. 490 // the test moves on to the next step to get more update chunks.
524 // This repeats till there is no update data. 491 // This repeats till there is no update data.
525 for (int step = 1;; step++) { 492 for (int step = 1;; step++) {
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 } 549 }
583 550
584 // Verifies with server if test is done and waits till server responses. 551 // Verifies with server if test is done and waits till server responses.
585 EXPECT_EQ(net::URLRequestStatus::SUCCESS, 552 EXPECT_EQ(net::URLRequestStatus::SUCCESS,
586 safe_browsing_helper->VerifyTestComplete(spawned_test_server(), 553 safe_browsing_helper->VerifyTestComplete(spawned_test_server(),
587 last_step)); 554 last_step));
588 EXPECT_EQ("yes", safe_browsing_helper->response_data()); 555 EXPECT_EQ("yes", safe_browsing_helper->response_data());
589 } 556 }
590 557
591 } // namespace safe_browsing 558 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698