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

Powered by Google App Engine
This is Rietveld 408576698