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

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

Issue 11615011: Small modifications to safebrowsing code to make it simpler to add the extension (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: seriously there is a macro called check() in AssertMacros.h wow Created 7 years, 11 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 // This test creates a safebrowsing service using test safebrowsing database 5 // This test creates a safebrowsing service using test safebrowsing database
6 // and a test protocol manager. It is used to test logics in safebrowsing 6 // and a test protocol manager. It is used to test logics in safebrowsing
7 // service. 7 // service.
8 8
9 #include <algorithm> 9 #include <algorithm>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/file_path.h" 13 #include "base/file_path.h"
14 #include "base/files/scoped_temp_dir.h" 14 #include "base/files/scoped_temp_dir.h"
15 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.h"
16 #include "base/path_service.h" 16 #include "base/path_service.h"
17 #include "base/string_split.h" 17 #include "base/string_split.h"
18 #include "base/test/thread_test_helper.h" 18 #include "base/test/thread_test_helper.h"
19 #include "base/time.h"
19 #include "chrome/browser/browser_process.h" 20 #include "chrome/browser/browser_process.h"
20 #include "chrome/browser/prefs/pref_service.h" 21 #include "chrome/browser/prefs/pref_service.h"
21 #include "chrome/browser/prerender/prerender_manager.h" 22 #include "chrome/browser/prerender/prerender_manager.h"
22 #include "chrome/browser/profiles/profile.h" 23 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/profiles/profile_manager.h" 24 #include "chrome/browser/profiles/profile_manager.h"
24 #include "chrome/browser/safe_browsing/client_side_detection_service.h" 25 #include "chrome/browser/safe_browsing/client_side_detection_service.h"
25 #include "chrome/browser/safe_browsing/database_manager.h" 26 #include "chrome/browser/safe_browsing/database_manager.h"
26 #include "chrome/browser/safe_browsing/protocol_manager.h" 27 #include "chrome/browser/safe_browsing/protocol_manager.h"
27 #include "chrome/browser/safe_browsing/safe_browsing_database.h" 28 #include "chrome/browser/safe_browsing/safe_browsing_database.h"
28 #include "chrome/browser/safe_browsing/safe_browsing_service.h" 29 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 } 124 }
124 virtual void DeleteChunks(const std::vector<SBChunkDelete>& chunk_deletes) { 125 virtual void DeleteChunks(const std::vector<SBChunkDelete>& chunk_deletes) {
125 ADD_FAILURE() << "Not implemented."; 126 ADD_FAILURE() << "Not implemented.";
126 } 127 }
127 virtual void UpdateFinished(bool update_succeeded) { 128 virtual void UpdateFinished(bool update_succeeded) {
128 ADD_FAILURE() << "Not implemented."; 129 ADD_FAILURE() << "Not implemented.";
129 } 130 }
130 virtual void CacheHashResults(const std::vector<SBPrefix>& prefixes, 131 virtual void CacheHashResults(const std::vector<SBPrefix>& prefixes,
131 const std::vector<SBFullHashResult>& full_hits) { 132 const std::vector<SBFullHashResult>& full_hits) {
132 // Do nothing for the cache. 133 // Do nothing for the cache.
133 return;
134 } 134 }
135 135
136 // Fill up the database with test URL. 136 // Fill up the database with test URL.
137 void AddUrl(const GURL& url, 137 void AddUrl(const GURL& url,
138 const std::string& list_name, 138 const std::string& list_name,
139 const std::vector<SBPrefix>& prefix_hits, 139 const std::vector<SBPrefix>& prefix_hits,
140 const std::vector<SBFullHashResult>& full_hits) { 140 const std::vector<SBFullHashResult>& full_hits) {
141 badurls_[url.spec()].list_name = list_name; 141 badurls_[url.spec()].list_name = list_name;
142 badurls_[url.spec()].prefix_hits = prefix_hits; 142 badurls_[url.spec()].prefix_hits = prefix_hits;
143 badurls_[url.spec()].full_hits = full_hits; 143 badurls_[url.spec()].full_hits = full_hits;
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 TestSafeBrowsingDatabase* db_; 209 TestSafeBrowsingDatabase* db_;
210 }; 210 };
211 211
212 // A TestProtocolManager that could return fixed responses from 212 // A TestProtocolManager that could return fixed responses from
213 // safebrowsing server for testing purpose. 213 // safebrowsing server for testing purpose.
214 class TestProtocolManager : public SafeBrowsingProtocolManager { 214 class TestProtocolManager : public SafeBrowsingProtocolManager {
215 public: 215 public:
216 TestProtocolManager(SafeBrowsingProtocolManagerDelegate* delegate, 216 TestProtocolManager(SafeBrowsingProtocolManagerDelegate* delegate,
217 net::URLRequestContextGetter* request_context_getter, 217 net::URLRequestContextGetter* request_context_getter,
218 const SafeBrowsingProtocolConfig& config) 218 const SafeBrowsingProtocolConfig& config)
219 : SafeBrowsingProtocolManager(delegate, request_context_getter, config), 219 : SafeBrowsingProtocolManager(delegate, request_context_getter, config) {
220 delay_ms_(0) {
221 create_count_++; 220 create_count_++;
222 } 221 }
223 222
224 ~TestProtocolManager() { 223 ~TestProtocolManager() {
225 delete_count_++; 224 delete_count_++;
226 } 225 }
227 226
228 // This function is called when there is a prefix hit in local safebrowsing 227 // This function is called when there is a prefix hit in local safebrowsing
229 // database and safebrowsing service issues a get hash request to backends. 228 // database and safebrowsing service issues a get hash request to backends.
230 // We return a result from the prefilled full_hashes_ hash_map to simulate 229 // We return a result from the prefilled full_hashes_ hash_map to simulate
231 // server's response. At the same time, latency is added to simulate real 230 // server's response. At the same time, latency is added to simulate real
232 // life network issues. 231 // life network issues.
233 virtual void GetFullHash( 232 virtual void GetFullHash(
234 const std::vector<SBPrefix>& prefixes, 233 const std::vector<SBPrefix>& prefixes,
235 SafeBrowsingProtocolManager::FullHashCallback callback, 234 SafeBrowsingProtocolManager::FullHashCallback callback,
236 bool is_download) OVERRIDE { 235 bool is_download) OVERRIDE {
237 BrowserThread::PostDelayedTask( 236 BrowserThread::PostDelayedTask(
238 BrowserThread::IO, FROM_HERE, 237 BrowserThread::IO, FROM_HERE,
239 base::Bind(InvokeFullHashCallback, callback, full_hashes_), 238 base::Bind(InvokeFullHashCallback, callback, full_hashes_),
240 base::TimeDelta::FromMilliseconds(delay_ms_)); 239 delay_);
241 } 240 }
242 241
243 // Prepare the GetFullHash results for the next request. 242 // Prepare the GetFullHash results for the next request.
244 void SetGetFullHashResponse(const SBFullHashResult& full_hash_result) { 243 void SetGetFullHashResponse(const SBFullHashResult& full_hash_result) {
245 full_hashes_.clear(); 244 full_hashes_.clear();
246 full_hashes_.push_back(full_hash_result); 245 full_hashes_.push_back(full_hash_result);
247 } 246 }
248 247
249 void IntroduceDelay(int64 ms) { 248 void IntroduceDelay(const base::TimeDelta& delay) {
250 delay_ms_ = ms; 249 delay_ = delay;
251 } 250 }
252 251
253 static int create_count() { 252 static int create_count() {
254 return create_count_; 253 return create_count_;
255 } 254 }
256 255
257 static int delete_count() { 256 static int delete_count() {
258 return delete_count_; 257 return delete_count_;
259 } 258 }
260 259
261 private: 260 private:
262 std::vector<SBFullHashResult> full_hashes_; 261 std::vector<SBFullHashResult> full_hashes_;
263 int64 delay_ms_; 262 base::TimeDelta delay_;
264 static int create_count_; 263 static int create_count_;
265 static int delete_count_; 264 static int delete_count_;
266 }; 265 };
267 266
268 // static 267 // static
269 int TestProtocolManager::create_count_ = 0; 268 int TestProtocolManager::create_count_ = 0;
270 // static 269 // static
271 int TestProtocolManager::delete_count_ = 0; 270 int TestProtocolManager::delete_count_ = 0;
272 271
273 // Factory that creates TestProtocolManager instances. 272 // Factory that creates TestProtocolManager instances.
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 crypto::SHA256HashString(host + path, &full_hash->hash, 321 crypto::SHA256HashString(host + path, &full_hash->hash,
323 sizeof(SBFullHash)); 322 sizeof(SBFullHash));
324 full_hash->list_name = list_name; 323 full_hash->list_name = list_name;
325 full_hash->add_chunk_id = add_chunk_id; 324 full_hash->add_chunk_id = add_chunk_id;
326 } 325 }
327 326
328 static void GenDigestFullhashResult(const std::string& full_digest, 327 static void GenDigestFullhashResult(const std::string& full_digest,
329 const std::string& list_name, 328 const std::string& list_name,
330 int add_chunk_id, 329 int add_chunk_id,
331 SBFullHashResult* full_hash) { 330 SBFullHashResult* full_hash) {
332 safe_browsing_util::StringToSBFullHash(full_digest, &full_hash->hash); 331 full_hash->hash = safe_browsing_util::StringToSBFullHash(full_digest);
333 full_hash->list_name = list_name; 332 full_hash->list_name = list_name;
334 full_hash->add_chunk_id = add_chunk_id; 333 full_hash->add_chunk_id = add_chunk_id;
335 } 334 }
336 335
337 virtual void SetUp() { 336 virtual void SetUp() {
338 // InProcessBrowserTest::SetUp() instantiates SafebrowsingService and 337 // InProcessBrowserTest::SetUp() instantiates SafebrowsingService and
339 // RegisterFactory has to be called before SafeBrowsingService is created. 338 // RegisterFactory has to be called before SafeBrowsingService is created.
340 SafeBrowsingDatabase::RegisterFactory(&db_factory_); 339 SafeBrowsingDatabase::RegisterFactory(&db_factory_);
341 SafeBrowsingProtocolManager::RegisterFactory(&pm_factory_); 340 SafeBrowsingProtocolManager::RegisterFactory(&pm_factory_);
342 InProcessBrowserTest::SetUp(); 341 InProcessBrowserTest::SetUp();
(...skipping 29 matching lines...) Expand all
372 // full hash is hit in database's local cache. 371 // full hash is hit in database's local cache.
373 std::vector<SBFullHashResult> empty_full_hits; 372 std::vector<SBFullHashResult> empty_full_hits;
374 TestSafeBrowsingDatabase* db = db_factory_.GetDb(); 373 TestSafeBrowsingDatabase* db = db_factory_.GetDb();
375 db->AddUrl(url, full_hash.list_name, prefix_hits, empty_full_hits); 374 db->AddUrl(url, full_hash.list_name, prefix_hits, empty_full_hits);
376 375
377 TestProtocolManager* pm = pm_factory_.GetProtocolManager(); 376 TestProtocolManager* pm = pm_factory_.GetProtocolManager();
378 pm->SetGetFullHashResponse(full_hash); 377 pm->SetGetFullHashResponse(full_hash);
379 } 378 }
380 379
381 // This will setup the binary digest prefix in database and prepare protocol 380 // This will setup the binary digest prefix in database and prepare protocol
382 // manager to response with |full_hash| for get full hash request. 381 // manager to respond with the result hash.
383 void SetupResponseForDigest(const std::string& digest, 382 void SetupResponseForDigest(const std::string& digest,
384 const SBFullHashResult& hash_result) { 383 const SBFullHashResult& hash_result) {
385 TestSafeBrowsingDatabase* db = db_factory_.GetDb(); 384 TestSafeBrowsingDatabase* db = db_factory_.GetDb();
386 SBFullHash full_hash; 385 db->AddDownloadPrefix(
387 safe_browsing_util::StringToSBFullHash(digest, &full_hash); 386 safe_browsing_util::StringToSBFullHash(digest).prefix);
388 db->AddDownloadPrefix(full_hash.prefix);
389 387
390 TestProtocolManager* pm = pm_factory_.GetProtocolManager(); 388 TestProtocolManager* pm = pm_factory_.GetProtocolManager();
391 pm->SetGetFullHashResponse(hash_result); 389 pm->SetGetFullHashResponse(hash_result);
392 } 390 }
393 391
394 bool ShowingInterstitialPage() { 392 bool ShowingInterstitialPage() {
395 WebContents* contents = chrome::GetActiveWebContents(browser()); 393 WebContents* contents = chrome::GetActiveWebContents(browser());
396 InterstitialPage* interstitial_page = contents->GetInterstitialPage(); 394 InterstitialPage* interstitial_page = contents->GetInterstitialPage();
397 return interstitial_page != NULL; 395 return interstitial_page != NULL;
398 } 396 }
399 397
400 void IntroduceGetHashDelay(int64 ms) { 398 void IntroduceGetHashDelay(const base::TimeDelta& delay) {
401 pm_factory_.GetProtocolManager()->IntroduceDelay(ms); 399 pm_factory_.GetProtocolManager()->IntroduceDelay(delay);
402 } 400 }
403 401
404 int64 DownloadUrlCheckTimeout(SafeBrowsingService* sb_service) { 402 base::TimeDelta GetCheckTimeout(SafeBrowsingService* sb_service) {
405 return sb_service->database_manager()->download_urlcheck_timeout_ms_; 403 return sb_service->database_manager()->check_timeout_;
406 } 404 }
407 405
408 int64 DownloadHashCheckTimeout(SafeBrowsingService* sb_service) { 406 void SetCheckTimeout(SafeBrowsingService* sb_service,
409 return sb_service->database_manager()->download_hashcheck_timeout_ms_; 407 const base::TimeDelta& delay) {
410 } 408 sb_service->database_manager()->check_timeout_ = delay;
411
412 void SetDownloadUrlCheckTimeout(SafeBrowsingService* sb_service, int64 ms) {
413 sb_service->database_manager()->download_urlcheck_timeout_ms_ = ms;
414 }
415
416 void SetDownloadHashCheckTimeout(SafeBrowsingService* sb_service, int64 ms) {
417 sb_service->database_manager()->download_hashcheck_timeout_ms_ = ms;
418 } 409 }
419 410
420 void CreateCSDService() { 411 void CreateCSDService() {
421 safe_browsing::ClientSideDetectionService* csd_service = 412 safe_browsing::ClientSideDetectionService* csd_service =
422 safe_browsing::ClientSideDetectionService::Create(NULL); 413 safe_browsing::ClientSideDetectionService::Create(NULL);
423 SafeBrowsingService* sb_service = 414 SafeBrowsingService* sb_service =
424 g_browser_process->safe_browsing_service(); 415 g_browser_process->safe_browsing_service();
425 sb_service->csd_service_.reset(csd_service); 416 sb_service->csd_service_.reset(csd_service);
426 sb_service->RefreshState(); 417 sb_service->RefreshState();
427 } 418 }
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
687 SetupResponseForUrl(badbin_url, full_hash_result); 678 SetupResponseForUrl(badbin_url, full_hash_result);
688 client->CheckDownloadUrl(badbin_urls); 679 client->CheckDownloadUrl(badbin_urls);
689 680
690 // badbin_url is not safe since it is added to download database. 681 // badbin_url is not safe since it is added to download database.
691 EXPECT_EQ(SB_THREAT_TYPE_BINARY_MALWARE_URL, client->GetThreatType()); 682 EXPECT_EQ(SB_THREAT_TYPE_BINARY_MALWARE_URL, client->GetThreatType());
692 683
693 // 684 //
694 // Now introducing delays and we should hit timeout. 685 // Now introducing delays and we should hit timeout.
695 // 686 //
696 SafeBrowsingService* sb_service = g_browser_process->safe_browsing_service(); 687 SafeBrowsingService* sb_service = g_browser_process->safe_browsing_service();
697 const int64 kOneSec = 1000; 688 base::TimeDelta default_urlcheck_timeout =
698 const int64 kOneMs = 1; 689 GetCheckTimeout(sb_service);
699 int64 default_urlcheck_timeout = DownloadUrlCheckTimeout(sb_service); 690 IntroduceGetHashDelay(base::TimeDelta::FromSeconds(1));
700 IntroduceGetHashDelay(kOneSec); 691 SetCheckTimeout(sb_service, base::TimeDelta::FromMilliseconds(1));
701 SetDownloadUrlCheckTimeout(sb_service, kOneMs);
702 client->CheckDownloadUrl(badbin_urls); 692 client->CheckDownloadUrl(badbin_urls);
703 693
704 // There should be a timeout and the hash would be considered as safe. 694 // There should be a timeout and the hash would be considered as safe.
705 EXPECT_EQ(SB_THREAT_TYPE_SAFE, client->GetThreatType()); 695 EXPECT_EQ(SB_THREAT_TYPE_SAFE, client->GetThreatType());
706 696
707 // Need to set the timeout back to the default value. 697 // Need to set the timeout back to the default value.
708 SetDownloadHashCheckTimeout(sb_service, default_urlcheck_timeout); 698 SetCheckTimeout(sb_service, default_urlcheck_timeout);
709 } 699 }
710 700
711 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, CheckDownloadHashTimedOut) { 701 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, CheckDownloadHashTimedOut) {
712 const std::string full_hash = "12345678902234567890323456789012"; 702 const std::string full_hash = "12345678902234567890323456789012";
713 703
714 scoped_refptr<TestSBClient> client(new TestSBClient); 704 scoped_refptr<TestSBClient> client(new TestSBClient);
715 SBFullHashResult full_hash_result; 705 SBFullHashResult full_hash_result;
716 int chunk_id = 0; 706 int chunk_id = 0;
717 GenDigestFullhashResult(full_hash, safe_browsing_util::kBinHashList, 707 GenDigestFullhashResult(full_hash, safe_browsing_util::kBinHashList,
718 chunk_id, &full_hash_result); 708 chunk_id, &full_hash_result);
719 SetupResponseForDigest(full_hash, full_hash_result); 709 SetupResponseForDigest(full_hash, full_hash_result);
720 client->CheckDownloadHash(full_hash); 710 client->CheckDownloadHash(full_hash);
721 711
722 // The badbin_url is not safe since it is added to download database. 712 // The badbin_url is not safe since it is added to download database.
723 EXPECT_EQ(SB_THREAT_TYPE_BINARY_MALWARE_HASH, client->GetThreatType()); 713 EXPECT_EQ(SB_THREAT_TYPE_BINARY_MALWARE_HASH, client->GetThreatType());
724 714
725 // 715 //
726 // Now introducing delays and we should hit timeout. 716 // Now introducing delays and we should hit timeout.
727 // 717 //
728 SafeBrowsingService* sb_service = g_browser_process->safe_browsing_service(); 718 SafeBrowsingService* sb_service = g_browser_process->safe_browsing_service();
729 const int64 kOneSec = 1000; 719 base::TimeDelta default_hashcheck_timeout =
730 const int64 kOneMs = 1; 720 GetCheckTimeout(sb_service);
731 int64 default_hashcheck_timeout = DownloadHashCheckTimeout(sb_service); 721 IntroduceGetHashDelay(base::TimeDelta::FromSeconds(1));
732 IntroduceGetHashDelay(kOneSec); 722 SetCheckTimeout(sb_service, base::TimeDelta::FromMilliseconds(1));
733 SetDownloadHashCheckTimeout(sb_service, kOneMs);
734 client->CheckDownloadHash(full_hash); 723 client->CheckDownloadHash(full_hash);
735 724
736 // There should be a timeout and the hash would be considered as safe. 725 // There should be a timeout and the hash would be considered as safe.
737 EXPECT_EQ(SB_THREAT_TYPE_SAFE, client->GetThreatType()); 726 EXPECT_EQ(SB_THREAT_TYPE_SAFE, client->GetThreatType());
738 727
739 // Need to set the timeout back to the default value. 728 // Need to set the timeout back to the default value.
740 SetDownloadHashCheckTimeout(sb_service, default_hashcheck_timeout); 729 SetCheckTimeout(sb_service, default_hashcheck_timeout);
741 } 730 }
742 731
743 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, StartAndStop) { 732 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, StartAndStop) {
744 CreateCSDService(); 733 CreateCSDService();
745 SafeBrowsingService* sb_service = g_browser_process->safe_browsing_service(); 734 SafeBrowsingService* sb_service = g_browser_process->safe_browsing_service();
746 safe_browsing::ClientSideDetectionService* csd_service = 735 safe_browsing::ClientSideDetectionService* csd_service =
747 sb_service->safe_browsing_detection_service(); 736 sb_service->safe_browsing_detection_service();
748 PrefService* pref_service = browser()->profile()->GetPrefs(); 737 PrefService* pref_service = browser()->profile()->GetPrefs();
749 738
750 ASSERT_TRUE(sb_service != NULL); 739 ASSERT_TRUE(sb_service != NULL);
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
980 TestSBUpdateCookies) { 969 TestSBUpdateCookies) {
981 content::WindowedNotificationObserver observer( 970 content::WindowedNotificationObserver observer(
982 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, 971 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE,
983 content::Source<SafeBrowsingDatabaseManager>( 972 content::Source<SafeBrowsingDatabaseManager>(
984 sb_service_->database_manager())); 973 sb_service_->database_manager()));
985 BrowserThread::PostTask( 974 BrowserThread::PostTask(
986 BrowserThread::IO, FROM_HERE, 975 BrowserThread::IO, FROM_HERE,
987 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, this)); 976 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, this));
988 observer.Wait(); 977 observer.Wait();
989 } 978 }
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/safe_browsing_database.cc ('k') | chrome/browser/safe_browsing/safe_browsing_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698