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

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: cleanup Created 8 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 // 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 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 TestProtocolManager* pm = pm_factory_.GetProtocolManager(); 389 TestProtocolManager* pm = pm_factory_.GetProtocolManager();
391 pm->SetGetFullHashResponse(hash_result); 390 pm->SetGetFullHashResponse(hash_result);
392 } 391 }
393 392
394 bool ShowingInterstitialPage() { 393 bool ShowingInterstitialPage() {
395 WebContents* contents = chrome::GetActiveWebContents(browser()); 394 WebContents* contents = chrome::GetActiveWebContents(browser());
396 InterstitialPage* interstitial_page = contents->GetInterstitialPage(); 395 InterstitialPage* interstitial_page = contents->GetInterstitialPage();
397 return interstitial_page != NULL; 396 return interstitial_page != NULL;
398 } 397 }
399 398
400 void IntroduceGetHashDelay(int64 ms) { 399 void IntroduceGetHashDelay(const base::TimeDelta& delay) {
401 pm_factory_.GetProtocolManager()->IntroduceDelay(ms); 400 pm_factory_.GetProtocolManager()->IntroduceDelay(delay);
402 } 401 }
403 402
404 int64 DownloadUrlCheckTimeout(SafeBrowsingService* sb_service) { 403 base::TimeDelta DownloadUrlCheckTimeout(SafeBrowsingService* sb_service) {
405 return sb_service->database_manager()->download_urlcheck_timeout_ms_; 404 return sb_service->database_manager()->download_url_check_timeout_;
406 } 405 }
407 406
408 int64 DownloadHashCheckTimeout(SafeBrowsingService* sb_service) { 407 base::TimeDelta DownloadHashCheckTimeout(SafeBrowsingService* sb_service) {
409 return sb_service->database_manager()->download_hashcheck_timeout_ms_; 408 return sb_service->database_manager()->download_hash_check_timeout_;
410 } 409 }
411 410
412 void SetDownloadUrlCheckTimeout(SafeBrowsingService* sb_service, int64 ms) { 411 void SetDownloadUrlCheckTimeout(SafeBrowsingService* sb_service,
413 sb_service->database_manager()->download_urlcheck_timeout_ms_ = ms; 412 const base::TimeDelta& delay) {
413 sb_service->database_manager()->download_url_check_timeout_ = delay;
414 } 414 }
415 415
416 void SetDownloadHashCheckTimeout(SafeBrowsingService* sb_service, int64 ms) { 416 void SetDownloadHashCheckTimeout(SafeBrowsingService* sb_service,
417 sb_service->database_manager()->download_hashcheck_timeout_ms_ = ms; 417 const base::TimeDelta& delay) {
418 sb_service->database_manager()->download_hash_check_timeout_ = delay;
418 } 419 }
419 420
420 void CreateCSDService() { 421 void CreateCSDService() {
421 safe_browsing::ClientSideDetectionService* csd_service = 422 safe_browsing::ClientSideDetectionService* csd_service =
422 safe_browsing::ClientSideDetectionService::Create(NULL); 423 safe_browsing::ClientSideDetectionService::Create(NULL);
423 SafeBrowsingService* sb_service = 424 SafeBrowsingService* sb_service =
424 g_browser_process->safe_browsing_service(); 425 g_browser_process->safe_browsing_service();
425 sb_service->csd_service_.reset(csd_service); 426 sb_service->csd_service_.reset(csd_service);
426 sb_service->RefreshState(); 427 sb_service->RefreshState();
427 } 428 }
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
687 SetupResponseForUrl(badbin_url, full_hash_result); 688 SetupResponseForUrl(badbin_url, full_hash_result);
688 client->CheckDownloadUrl(badbin_urls); 689 client->CheckDownloadUrl(badbin_urls);
689 690
690 // badbin_url is not safe since it is added to download database. 691 // badbin_url is not safe since it is added to download database.
691 EXPECT_EQ(SB_THREAT_TYPE_BINARY_MALWARE_URL, client->GetThreatType()); 692 EXPECT_EQ(SB_THREAT_TYPE_BINARY_MALWARE_URL, client->GetThreatType());
692 693
693 // 694 //
694 // Now introducing delays and we should hit timeout. 695 // Now introducing delays and we should hit timeout.
695 // 696 //
696 SafeBrowsingService* sb_service = g_browser_process->safe_browsing_service(); 697 SafeBrowsingService* sb_service = g_browser_process->safe_browsing_service();
697 const int64 kOneSec = 1000; 698 base::TimeDelta default_urlcheck_timeout =
698 const int64 kOneMs = 1; 699 DownloadUrlCheckTimeout(sb_service);
699 int64 default_urlcheck_timeout = DownloadUrlCheckTimeout(sb_service); 700 IntroduceGetHashDelay(base::TimeDelta::FromSeconds(1));
700 IntroduceGetHashDelay(kOneSec); 701 SetDownloadUrlCheckTimeout(sb_service, base::TimeDelta::FromMilliseconds(1));
701 SetDownloadUrlCheckTimeout(sb_service, kOneMs);
702 client->CheckDownloadUrl(badbin_urls); 702 client->CheckDownloadUrl(badbin_urls);
703 703
704 // There should be a timeout and the hash would be considered as safe. 704 // There should be a timeout and the hash would be considered as safe.
705 EXPECT_EQ(SB_THREAT_TYPE_SAFE, client->GetThreatType()); 705 EXPECT_EQ(SB_THREAT_TYPE_SAFE, client->GetThreatType());
706 706
707 // Need to set the timeout back to the default value. 707 // Need to set the timeout back to the default value.
708 SetDownloadHashCheckTimeout(sb_service, default_urlcheck_timeout); 708 SetDownloadHashCheckTimeout(sb_service, default_urlcheck_timeout);
709 } 709 }
710 710
711 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, CheckDownloadHashTimedOut) { 711 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, CheckDownloadHashTimedOut) {
712 const std::string full_hash = "12345678902234567890323456789012"; 712 const std::string full_hash = "12345678902234567890323456789012";
713 713
714 scoped_refptr<TestSBClient> client(new TestSBClient); 714 scoped_refptr<TestSBClient> client(new TestSBClient);
715 SBFullHashResult full_hash_result; 715 SBFullHashResult full_hash_result;
716 int chunk_id = 0; 716 int chunk_id = 0;
717 GenDigestFullhashResult(full_hash, safe_browsing_util::kBinHashList, 717 GenDigestFullhashResult(full_hash, safe_browsing_util::kBinHashList,
718 chunk_id, &full_hash_result); 718 chunk_id, &full_hash_result);
719 SetupResponseForDigest(full_hash, full_hash_result); 719 SetupResponseForDigest(full_hash, full_hash_result);
720 client->CheckDownloadHash(full_hash); 720 client->CheckDownloadHash(full_hash);
721 721
722 // The badbin_url is not safe since it is added to download database. 722 // The badbin_url is not safe since it is added to download database.
723 EXPECT_EQ(SB_THREAT_TYPE_BINARY_MALWARE_HASH, client->GetThreatType()); 723 EXPECT_EQ(SB_THREAT_TYPE_BINARY_MALWARE_HASH, client->GetThreatType());
724 724
725 // 725 //
726 // Now introducing delays and we should hit timeout. 726 // Now introducing delays and we should hit timeout.
727 // 727 //
728 SafeBrowsingService* sb_service = g_browser_process->safe_browsing_service(); 728 SafeBrowsingService* sb_service = g_browser_process->safe_browsing_service();
729 const int64 kOneSec = 1000; 729 base::TimeDelta default_hashcheck_timeout =
730 const int64 kOneMs = 1; 730 DownloadHashCheckTimeout(sb_service);
731 int64 default_hashcheck_timeout = DownloadHashCheckTimeout(sb_service); 731 IntroduceGetHashDelay(base::TimeDelta::FromSeconds(1));
732 IntroduceGetHashDelay(kOneSec); 732 SetDownloadHashCheckTimeout(sb_service, base::TimeDelta::FromMilliseconds(1));
733 SetDownloadHashCheckTimeout(sb_service, kOneMs);
734 client->CheckDownloadHash(full_hash); 733 client->CheckDownloadHash(full_hash);
735 734
736 // There should be a timeout and the hash would be considered as safe. 735 // There should be a timeout and the hash would be considered as safe.
737 EXPECT_EQ(SB_THREAT_TYPE_SAFE, client->GetThreatType()); 736 EXPECT_EQ(SB_THREAT_TYPE_SAFE, client->GetThreatType());
738 737
739 // Need to set the timeout back to the default value. 738 // Need to set the timeout back to the default value.
740 SetDownloadHashCheckTimeout(sb_service, default_hashcheck_timeout); 739 SetDownloadHashCheckTimeout(sb_service, default_hashcheck_timeout);
741 } 740 }
742 741
743 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, StartAndStop) { 742 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, StartAndStop) {
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
986 TestSBUpdateCookies) { 985 TestSBUpdateCookies) {
987 content::WindowedNotificationObserver observer( 986 content::WindowedNotificationObserver observer(
988 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, 987 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE,
989 content::Source<SafeBrowsingDatabaseManager>( 988 content::Source<SafeBrowsingDatabaseManager>(
990 sb_service_->database_manager())); 989 sb_service_->database_manager()));
991 BrowserThread::PostTask( 990 BrowserThread::PostTask(
992 BrowserThread::IO, FROM_HERE, 991 BrowserThread::IO, FROM_HERE,
993 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, this)); 992 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, this));
994 observer.Wait(); 993 observer.Wait();
995 } 994 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698