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

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

Issue 2384893002: PVer4: Test checksum on startup outside the hotpath of DB load (Closed)
Patch Set: Verify that the checksum check happens async Created 4 years, 2 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 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/test/test_simple_task_runner.h" 9 #include "base/test/test_simple_task_runner.h"
10 #include "components/safe_browsing_db/v4_database.h" 10 #include "components/safe_browsing_db/v4_database.h"
11 #include "components/safe_browsing_db/v4_local_database_manager.h" 11 #include "components/safe_browsing_db/v4_local_database_manager.h"
12 #include "content/public/test/test_browser_thread_bundle.h" 12 #include "content/public/test/test_browser_thread_bundle.h"
13 #include "net/url_request/test_url_fetcher_factory.h" 13 #include "net/url_request/test_url_fetcher_factory.h"
14 #include "testing/platform_test.h" 14 #include "testing/platform_test.h"
15 15
16 namespace safe_browsing { 16 namespace safe_browsing {
17 17
18 class FakeV4Database : public V4Database { 18 class FakeV4Database : public V4Database {
19 public: 19 public:
20 FakeV4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, 20 static void Create(
21 std::unique_ptr<StoreMap> store_map, 21 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
22 const StoreAndHashPrefixes& store_and_hash_prefixes) 22 std::unique_ptr<StoreMap> store_map,
23 : V4Database(db_task_runner, std::move(store_map)), 23 const StoreAndHashPrefixes& store_and_hash_prefixes,
24 store_and_hash_prefixes_(store_and_hash_prefixes) {} 24 NewDatabaseReadyCallback new_db_callback) {
25 // Mimics V4Database::Create
26 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner =
27 base::MessageLoop::current()->task_runner();
28 db_task_runner->PostTask(
29 FROM_HERE,
30 base::Bind(&FakeV4Database::CreateOnTaskRunner, db_task_runner,
31 base::Passed(&store_map), store_and_hash_prefixes,
32 callback_task_runner, new_db_callback));
33 }
25 34
26 void GetStoresMatchingFullHash( 35 void GetStoresMatchingFullHash(
27 const FullHash& full_hash, 36 const FullHash& full_hash,
28 const StoresToCheck& stores_to_check, 37 const StoresToCheck& stores_to_check,
29 StoreAndHashPrefixes* store_and_hash_prefixes) override { 38 StoreAndHashPrefixes* store_and_hash_prefixes) override {
30 *store_and_hash_prefixes = store_and_hash_prefixes_; 39 *store_and_hash_prefixes = store_and_hash_prefixes_;
31 } 40 }
32 41
33 private: 42 private:
34 const StoreAndHashPrefixes& store_and_hash_prefixes_; 43 static void CreateOnTaskRunner(
44 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
45 std::unique_ptr<StoreMap> store_map,
46 const StoreAndHashPrefixes& store_and_hash_prefixes,
47 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner,
48 NewDatabaseReadyCallback new_db_callback) {
49 // Mimics the semantics of V4Database::CreateOnTaskRunner
50 std::unique_ptr<FakeV4Database> fake_v4_database(new FakeV4Database(
51 db_task_runner, std::move(store_map), store_and_hash_prefixes));
52 callback_task_runner->PostTask(
53 FROM_HERE,
54 base::Bind(new_db_callback, base::Passed(&fake_v4_database)));
55 }
56
57 FakeV4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
58 std::unique_ptr<StoreMap> store_map,
59 const StoreAndHashPrefixes& store_and_hash_prefixes)
60 : V4Database(db_task_runner, std::move(store_map)),
61 store_and_hash_prefixes_(store_and_hash_prefixes) {}
62
63 const StoreAndHashPrefixes store_and_hash_prefixes_;
35 }; 64 };
36 65
37 class TestClient : public SafeBrowsingDatabaseManager::Client { 66 class TestClient : public SafeBrowsingDatabaseManager::Client {
38 public: 67 public:
39 TestClient(SBThreatType sb_threat_type, const GURL& url) 68 TestClient(SBThreatType sb_threat_type, const GURL& url)
40 : expected_sb_threat_type(sb_threat_type), expected_url(url) {} 69 : expected_sb_threat_type(sb_threat_type), expected_url(url) {}
41 70
42 void OnCheckBrowseUrlResult(const GURL& url, 71 void OnCheckBrowseUrlResult(const GURL& url,
43 SBThreatType threat_type, 72 SBThreatType threat_type,
44 const ThreatMetadata& metadata) override { 73 const ThreatMetadata& metadata) override {
(...skipping 25 matching lines...) Expand all
70 void TearDown() override { 99 void TearDown() override {
71 StopLocalDatabaseManager(); 100 StopLocalDatabaseManager();
72 101
73 PlatformTest::TearDown(); 102 PlatformTest::TearDown();
74 } 103 }
75 104
76 void ForceDisableLocalDatabaseManager() { 105 void ForceDisableLocalDatabaseManager() {
77 v4_local_database_manager_->enabled_ = false; 106 v4_local_database_manager_->enabled_ = false;
78 } 107 }
79 108
109 void ForceEnableLocalDatabaseManager() {
110 v4_local_database_manager_->enabled_ = true;
111 }
112
80 const V4LocalDatabaseManager::QueuedChecks& GetQueuedChecks() { 113 const V4LocalDatabaseManager::QueuedChecks& GetQueuedChecks() {
81 return v4_local_database_manager_->queued_checks_; 114 return v4_local_database_manager_->queued_checks_;
82 } 115 }
83 116
84 void ReplaceV4Database(const StoreAndHashPrefixes& store_and_hash_prefixes) { 117 void ReplaceV4Database(const StoreAndHashPrefixes& store_and_hash_prefixes) {
85 v4_local_database_manager_->v4_database_.reset(new FakeV4Database( 118 // Disable the V4LocalDatabaseManager first so that if the callback to
86 task_runner_, base::MakeUnique<StoreMap>(), store_and_hash_prefixes)); 119 // verify checksum has been scheduled, then it doesn't do anything when it
120 // is called back.
121 ForceDisableLocalDatabaseManager();
122 // Wait to make sure that the callback gets executed if it has already been
123 // scheduled.
124 WaitForTasksOnTaskRunner();
125 // Re-enable the V4LocalDatabaseManager otherwise the checks won't work and
126 // the fake database won't be set either.
127 ForceEnableLocalDatabaseManager();
128
129 NewDatabaseReadyCallback db_ready_callback =
130 base::Bind(&V4LocalDatabaseManager::DatabaseReadyForChecks,
131 base::Unretained(v4_local_database_manager_.get()));
132 FakeV4Database::Create(task_runner_, base::MakeUnique<StoreMap>(),
133 store_and_hash_prefixes, db_ready_callback);
134 WaitForTasksOnTaskRunner();
87 } 135 }
88 136
89 void ResetV4Database() { v4_local_database_manager_->v4_database_.reset(); } 137 void ResetV4Database() {
138 V4Database::Destroy(std::move(v4_local_database_manager_->v4_database_));
139 }
90 140
91 void StartLocalDatabaseManager() { 141 void StartLocalDatabaseManager() {
92 v4_local_database_manager_->StartOnIOThread(NULL, V4ProtocolConfig()); 142 v4_local_database_manager_->StartOnIOThread(NULL, V4ProtocolConfig());
93
94 task_runner_->RunPendingTasks();
95 base::RunLoop().RunUntilIdle();
96 } 143 }
97 144
98 void StopLocalDatabaseManager() { 145 void StopLocalDatabaseManager() {
99 v4_local_database_manager_->StopOnIOThread(true); 146 v4_local_database_manager_->StopOnIOThread(true);
100 147
101 // Force destruction of the database. 148 // Force destruction of the database.
102 task_runner_->RunPendingTasks(); 149 task_runner_->RunPendingTasks();
103 base::RunLoop().RunUntilIdle(); 150 base::RunLoop().RunUntilIdle();
104 } 151 }
105 152
153 void WaitForTasksOnTaskRunner() {
154 // Wait for tasks on the task runner so we're sure that the
155 // V4LocalDatabaseManager has read the data from disk.
156 task_runner_->RunPendingTasks();
157 base::RunLoop().RunUntilIdle();
158 }
159
106 base::ScopedTempDir base_dir_; 160 base::ScopedTempDir base_dir_;
107 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 161 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
108 content::TestBrowserThreadBundle thread_bundle_; 162 content::TestBrowserThreadBundle thread_bundle_;
109 scoped_refptr<V4LocalDatabaseManager> v4_local_database_manager_; 163 scoped_refptr<V4LocalDatabaseManager> v4_local_database_manager_;
110 }; 164 };
111 165
112 TEST_F(V4LocalDatabaseManagerTest, TestGetThreatSource) { 166 TEST_F(V4LocalDatabaseManagerTest, TestGetThreatSource) {
167 WaitForTasksOnTaskRunner();
113 EXPECT_EQ(ThreatSource::LOCAL_PVER4, 168 EXPECT_EQ(ThreatSource::LOCAL_PVER4,
114 v4_local_database_manager_->GetThreatSource()); 169 v4_local_database_manager_->GetThreatSource());
115 } 170 }
116 171
117 TEST_F(V4LocalDatabaseManagerTest, TestIsSupported) { 172 TEST_F(V4LocalDatabaseManagerTest, TestIsSupported) {
173 WaitForTasksOnTaskRunner();
118 EXPECT_TRUE(v4_local_database_manager_->IsSupported()); 174 EXPECT_TRUE(v4_local_database_manager_->IsSupported());
119 } 175 }
120 176
121 TEST_F(V4LocalDatabaseManagerTest, TestCanCheckUrl) { 177 TEST_F(V4LocalDatabaseManagerTest, TestCanCheckUrl) {
178 WaitForTasksOnTaskRunner();
122 EXPECT_TRUE( 179 EXPECT_TRUE(
123 v4_local_database_manager_->CanCheckUrl(GURL("http://example.com/a/"))); 180 v4_local_database_manager_->CanCheckUrl(GURL("http://example.com/a/")));
124 EXPECT_TRUE( 181 EXPECT_TRUE(
125 v4_local_database_manager_->CanCheckUrl(GURL("https://example.com/a/"))); 182 v4_local_database_manager_->CanCheckUrl(GURL("https://example.com/a/")));
126 EXPECT_TRUE( 183 EXPECT_TRUE(
127 v4_local_database_manager_->CanCheckUrl(GURL("ftp://example.com/a/"))); 184 v4_local_database_manager_->CanCheckUrl(GURL("ftp://example.com/a/")));
128 EXPECT_FALSE( 185 EXPECT_FALSE(
129 v4_local_database_manager_->CanCheckUrl(GURL("adp://example.com/a/"))); 186 v4_local_database_manager_->CanCheckUrl(GURL("adp://example.com/a/")));
130 } 187 }
131 188
132 TEST_F(V4LocalDatabaseManagerTest, 189 TEST_F(V4LocalDatabaseManagerTest,
133 TestCheckBrowseUrlWithEmptyStoresReturnsNoMatch) { 190 TestCheckBrowseUrlWithEmptyStoresReturnsNoMatch) {
191 WaitForTasksOnTaskRunner();
134 // Both the stores are empty right now so CheckBrowseUrl should return true. 192 // Both the stores are empty right now so CheckBrowseUrl should return true.
135 EXPECT_TRUE(v4_local_database_manager_->CheckBrowseUrl( 193 EXPECT_TRUE(v4_local_database_manager_->CheckBrowseUrl(
136 GURL("http://example.com/a/"), nullptr)); 194 GURL("http://example.com/a/"), nullptr));
137 } 195 }
138 196
139 TEST_F(V4LocalDatabaseManagerTest, TestCheckBrowseUrlWithFakeDbReturnsMatch) { 197 TEST_F(V4LocalDatabaseManagerTest, TestCheckBrowseUrlWithFakeDbReturnsMatch) {
198 WaitForTasksOnTaskRunner();
140 net::TestURLFetcherFactory factory; 199 net::TestURLFetcherFactory factory;
141 200
142 StoreAndHashPrefixes store_and_hash_prefixes; 201 StoreAndHashPrefixes store_and_hash_prefixes;
143 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), HashPrefix("aaaa")); 202 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), HashPrefix("aaaa"));
144 ReplaceV4Database(store_and_hash_prefixes); 203 ReplaceV4Database(store_and_hash_prefixes);
145 204
146 // The fake database returns a matched hash prefix. 205 // The fake database returns a matched hash prefix.
147 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl( 206 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl(
148 GURL("http://example.com/a/"), nullptr)); 207 GURL("http://example.com/a/"), nullptr));
149 } 208 }
150 209
151 TEST_F(V4LocalDatabaseManagerTest, 210 TEST_F(V4LocalDatabaseManagerTest,
152 TestCheckBrowseUrlReturnsNoMatchWhenDisabled) { 211 TestCheckBrowseUrlReturnsNoMatchWhenDisabled) {
153 StoreAndHashPrefixes store_and_hash_prefixes; 212 WaitForTasksOnTaskRunner();
154 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), HashPrefix("aaaa"));
155 ReplaceV4Database(store_and_hash_prefixes);
156 213
157 // The same URL returns |false| in the previous test because 214 // The same URL returns |false| in the previous test because
158 // v4_local_database_manager_ is enabled. 215 // v4_local_database_manager_ is enabled.
159 ForceDisableLocalDatabaseManager(); 216 ForceDisableLocalDatabaseManager();
160 217
161 EXPECT_TRUE(v4_local_database_manager_->CheckBrowseUrl( 218 EXPECT_TRUE(v4_local_database_manager_->CheckBrowseUrl(
162 GURL("http://example.com/a/"), nullptr)); 219 GURL("http://example.com/a/"), nullptr));
163 } 220 }
164 221
165 TEST_F(V4LocalDatabaseManagerTest, TestGetSeverestThreatTypeAndMetadata) { 222 TEST_F(V4LocalDatabaseManagerTest, TestGetSeverestThreatTypeAndMetadata) {
223 WaitForTasksOnTaskRunner();
224
166 FullHashInfo fhi_malware(FullHash("Malware"), GetUrlMalwareId(), 225 FullHashInfo fhi_malware(FullHash("Malware"), GetUrlMalwareId(),
167 base::Time::Now()); 226 base::Time::Now());
168 fhi_malware.metadata.population_id = "malware_popid"; 227 fhi_malware.metadata.population_id = "malware_popid";
169 228
170 FullHashInfo fhi_api(FullHash("api"), GetChromeUrlApiId(), base::Time::Now()); 229 FullHashInfo fhi_api(FullHash("api"), GetChromeUrlApiId(), base::Time::Now());
171 fhi_api.metadata.population_id = "api_popid"; 230 fhi_api.metadata.population_id = "api_popid";
172 231
173 std::vector<FullHashInfo> fhis({fhi_malware, fhi_api}); 232 std::vector<FullHashInfo> fhis({fhi_malware, fhi_api});
174 233
175 SBThreatType result_threat_type; 234 SBThreatType result_threat_type;
(...skipping 10 matching lines...) Expand all
186 &result_threat_type, &metadata, fhis); 245 &result_threat_type, &metadata, fhis);
187 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, result_threat_type); 246 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, result_threat_type);
188 EXPECT_EQ("malware_popid", metadata.population_id); 247 EXPECT_EQ("malware_popid", metadata.population_id);
189 } 248 }
190 249
191 TEST_F(V4LocalDatabaseManagerTest, TestChecksAreQueued) { 250 TEST_F(V4LocalDatabaseManagerTest, TestChecksAreQueued) {
192 const GURL url("https://www.example.com/"); 251 const GURL url("https://www.example.com/");
193 TestClient client(SB_THREAT_TYPE_SAFE, url); 252 TestClient client(SB_THREAT_TYPE_SAFE, url);
194 EXPECT_TRUE(GetQueuedChecks().empty()); 253 EXPECT_TRUE(GetQueuedChecks().empty());
195 v4_local_database_manager_->CheckBrowseUrl(url, &client); 254 v4_local_database_manager_->CheckBrowseUrl(url, &client);
196 // The database is available so the check shouldn't get queued.
197 EXPECT_TRUE(GetQueuedChecks().empty());
198
199 ResetV4Database();
200 v4_local_database_manager_->CheckBrowseUrl(url, &client);
201 // The database is unavailable so the check should get queued. 255 // The database is unavailable so the check should get queued.
202 EXPECT_EQ(1ul, GetQueuedChecks().size()); 256 EXPECT_EQ(1ul, GetQueuedChecks().size());
203 257
204 // The following function calls StartOnIOThread which should load the 258 // The following function waits for the DB to load.
205 // database from disk and cause the queued check to be performed. 259 WaitForTasksOnTaskRunner();
206 StartLocalDatabaseManager();
207 EXPECT_TRUE(GetQueuedChecks().empty()); 260 EXPECT_TRUE(GetQueuedChecks().empty());
208 261
209 ResetV4Database(); 262 ResetV4Database();
210 v4_local_database_manager_->CheckBrowseUrl(url, &client); 263 v4_local_database_manager_->CheckBrowseUrl(url, &client);
211 // The database is unavailable so the check should get queued. 264 // The database is unavailable so the check should get queued.
212 EXPECT_EQ(1ul, GetQueuedChecks().size()); 265 EXPECT_EQ(1ul, GetQueuedChecks().size());
213 266
214 StopLocalDatabaseManager(); 267 StopLocalDatabaseManager();
215 EXPECT_TRUE(GetQueuedChecks().empty()); 268 EXPECT_TRUE(GetQueuedChecks().empty());
216 } 269 }
217 270
218 } // namespace safe_browsing 271 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « components/safe_browsing_db/v4_local_database_manager.cc ('k') | components/safe_browsing_db/v4_store.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698