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

Side by Side Diff: components/safe_browsing_db/v4_local_database_manager_unittest.cc

Issue 2616653002: Have a list of pending checks instead of pending clients (Closed)
Patch Set: Now with a unit test! Created 3 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
« no previous file with comments | « components/safe_browsing_db/v4_local_database_manager.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "base/files/scoped_temp_dir.h" 5 #include "base/files/scoped_temp_dir.h"
6 #include "base/memory/ptr_util.h" 6 #include "base/memory/ptr_util.h"
7 #include "base/memory/ref_counted.h" 7 #include "base/memory/ref_counted.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 #include "base/test/test_simple_task_runner.h" 10 #include "base/test/test_simple_task_runner.h"
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 const StoreAndHashPrefixes& store_and_hash_prefixes, 98 const StoreAndHashPrefixes& store_and_hash_prefixes,
99 bool stores_available) 99 bool stores_available)
100 : V4Database(db_task_runner, std::move(store_map)), 100 : V4Database(db_task_runner, std::move(store_map)),
101 store_and_hash_prefixes_(store_and_hash_prefixes), 101 store_and_hash_prefixes_(store_and_hash_prefixes),
102 stores_available_(stores_available) {} 102 stores_available_(stores_available) {}
103 103
104 const StoreAndHashPrefixes store_and_hash_prefixes_; 104 const StoreAndHashPrefixes store_and_hash_prefixes_;
105 const bool stores_available_; 105 const bool stores_available_;
106 }; 106 };
107 107
108 class FakeV4GetHashProtocolManager : public V4GetHashProtocolManager {
109 public:
110 FakeV4GetHashProtocolManager(
111 net::URLRequestContextGetter* request_context_getter,
112 const StoresToCheck& stores_to_check,
113 const V4ProtocolConfig& config,
114 const std::vector<FullHashInfo>& expected_response,
115 const scoped_refptr<base::TestSimpleTaskRunner>& task_runner)
116 : V4GetHashProtocolManager(request_context_getter,
117 stores_to_check,
118 config),
119 expected_response_(expected_response),
120 task_runner_(task_runner) {}
121
122 void WaitForTasksOnTaskRunner() {
Nathan Parker 2017/01/09 19:46:28 Make private, or better yet just inline it.
vakh (use Gerrit instead) 2017/01/10 01:54:53 Removed
123 // Wait for tasks on the task runner so we're sure that the
124 // V4LocalDatabaseManager has read the data from disk.
125 task_runner_->RunPendingTasks();
126 base::RunLoop().RunUntilIdle();
127 }
128
129 void GetFullHashes(const FullHashToStoreAndHashPrefixesMap&
130 full_hash_to_matching_hash_prefixes,
131 FullHashCallback callback) override {
132 WaitForTasksOnTaskRunner();
Nathan Parker 2017/01/09 19:46:28 I'm not clear on why this is necessary -- the one
vakh (use Gerrit instead) 2017/01/10 01:54:53 Removed
133 callback.Run(expected_response_);
134 }
135
136 private:
137 const std::vector<FullHashInfo> expected_response_;
138 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
139 };
140
108 class TestClient : public SafeBrowsingDatabaseManager::Client { 141 class TestClient : public SafeBrowsingDatabaseManager::Client {
109 public: 142 public:
110 TestClient(SBThreatType sb_threat_type, 143 TestClient(SBThreatType sb_threat_type,
111 const GURL& url, 144 const GURL& url,
112 V4LocalDatabaseManager* manager_to_cancel = nullptr) 145 V4LocalDatabaseManager* manager_to_cancel = nullptr)
113 : expected_sb_threat_type(sb_threat_type), 146 : expected_sb_threat_type(sb_threat_type),
114 expected_url(url), 147 expected_url(url),
115 result_received_(false), 148 result_received_(false),
116 manager_to_cancel_(manager_to_cancel) {} 149 manager_to_cancel_(manager_to_cancel) {}
117 150
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 240
208 NewDatabaseReadyCallback db_ready_callback = 241 NewDatabaseReadyCallback db_ready_callback =
209 base::Bind(&V4LocalDatabaseManager::DatabaseReadyForChecks, 242 base::Bind(&V4LocalDatabaseManager::DatabaseReadyForChecks,
210 base::Unretained(v4_local_database_manager_.get())); 243 base::Unretained(v4_local_database_manager_.get()));
211 FakeV4Database::Create(task_runner_, base::MakeUnique<StoreMap>(), 244 FakeV4Database::Create(task_runner_, base::MakeUnique<StoreMap>(),
212 store_and_hash_prefixes, db_ready_callback, 245 store_and_hash_prefixes, db_ready_callback,
213 stores_available); 246 stores_available);
214 WaitForTasksOnTaskRunner(); 247 WaitForTasksOnTaskRunner();
215 } 248 }
216 249
250 void ReplaceV4GetHashProtocolManager(const StoresToCheck& stores_to_check) {
251 v4_local_database_manager_->v4_get_hash_protocol_manager_ =
252 base::MakeUnique<FakeV4GetHashProtocolManager>(
253 nullptr, stores_to_check, GetTestV4ProtocolConfig(),
254 std::vector<FullHashInfo>{}, task_runner_);
255 }
256
217 void ResetV4Database() { 257 void ResetV4Database() {
218 V4Database::Destroy(std::move(v4_local_database_manager_->v4_database_)); 258 V4Database::Destroy(std::move(v4_local_database_manager_->v4_database_));
219 } 259 }
220 260
221 void SetTaskRunnerForTest() { 261 void SetTaskRunnerForTest() {
222 v4_local_database_manager_->SetTaskRunnerForTest(task_runner_); 262 v4_local_database_manager_->SetTaskRunnerForTest(task_runner_);
223 } 263 }
224 264
225 void StartLocalDatabaseManager() { 265 void StartLocalDatabaseManager() {
226 v4_local_database_manager_->StartOnIOThread(NULL, 266 v4_local_database_manager_->StartOnIOThread(NULL,
(...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after
609 EXPECT_FALSE( 649 EXPECT_FALSE(
610 v4_local_database_manager_->MatchModuleWhitelistString("badstuff.dll")); 650 v4_local_database_manager_->MatchModuleWhitelistString("badstuff.dll"));
611 // Whitelisted. 651 // Whitelisted.
612 EXPECT_TRUE( 652 EXPECT_TRUE(
613 v4_local_database_manager_->MatchModuleWhitelistString("chrome.dll")); 653 v4_local_database_manager_->MatchModuleWhitelistString("chrome.dll"));
614 654
615 EXPECT_FALSE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled( 655 EXPECT_FALSE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled(
616 v4_local_database_manager_)); 656 v4_local_database_manager_));
617 } 657 }
618 658
659 // See http://crbug.com/660293
Nathan Parker 2017/01/09 19:46:28 nit: "This verifies the fix for race in http://...
vakh (use Gerrit instead) 2017/01/10 01:54:53 Done.
660 TEST_F(V4LocalDatabaseManagerTest, TestCheckBrowseUrlWithSameClientAndCancel) {
661 WaitForTasksOnTaskRunner();
662 net::TestURLFetcherFactory factory;
663
664 StoreAndHashPrefixes store_and_hash_prefixes;
665 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(),
666 HashPrefix("sن\340\t\006_"));
667 ReplaceV4Database(store_and_hash_prefixes);
668 ReplaceV4GetHashProtocolManager(StoresToCheck({GetUrlMalwareId()}));
669
670 GURL first_url("http://example.com/a");
671 GURL second_url("http://example.com/");
672 TestClient client(SB_THREAT_TYPE_SAFE, first_url);
673 // The fake database returns a matched hash prefix.
674 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl(first_url, &client));
675
676 // That check gets queued. Now, let's cancel the check. After this, we should
677 // not receive a call for |OnCheckBrowseUrlResult| with |first_url|.
678 v4_local_database_manager_->CancelCheck(&client);
679
680 // Now, re-use that client but for |second_url|.
681 client.expected_url = second_url;
682 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl(second_url, &client));
683
684 // Wait for PerformFullHashCheck to complete.
685 WaitForTasksOnTaskRunner();
686 // |result_received_| is true only if OnCheckBrowseUrlResult gets called with
687 // the |url| equal to |expected_url|, which is |second_url| in this test.
688 EXPECT_TRUE(client.result_received_);
689 }
690
619 // TODO(nparker): Add tests for 691 // TODO(nparker): Add tests for
620 // CheckDownloadUrl() 692 // CheckDownloadUrl()
621 // CheckExtensionIDs() 693 // CheckExtensionIDs()
622 // CheckResourceUrl() 694 // CheckResourceUrl()
623 695
624 } // namespace safe_browsing 696 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « components/safe_browsing_db/v4_local_database_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698