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

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

Issue 2384893002: PVer4: Test checksum on startup outside the hotpath of DB load (Closed)
Patch Set: shess@ feedback 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/bind.h" 5 #include "base/bind.h"
6 #include "base/debug/leak_annotations.h" 6 #include "base/debug/leak_annotations.h"
7 #include "base/files/scoped_temp_dir.h" 7 #include "base/files/scoped_temp_dir.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/test/test_simple_task_runner.h" 10 #include "base/test/test_simple_task_runner.h"
11 #include "components/safe_browsing_db/v4_database.h" 11 #include "components/safe_browsing_db/v4_database.h"
12 #include "components/safe_browsing_db/v4_store.h" 12 #include "components/safe_browsing_db/v4_store.h"
13 #include "content/public/test/test_browser_thread_bundle.h" 13 #include "content/public/test/test_browser_thread_bundle.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 FakeV4Store : public V4Store { 18 class FakeV4Store : public V4Store {
19 public: 19 public:
20 FakeV4Store(const scoped_refptr<base::SequencedTaskRunner>& task_runner, 20 FakeV4Store(const scoped_refptr<base::SequencedTaskRunner>& task_runner,
21 const base::FilePath& store_path, 21 const base::FilePath& store_path,
22 const bool reset_succeeds,
23 const bool hash_prefix_matches) 22 const bool hash_prefix_matches)
24 : V4Store( 23 : V4Store(
25 task_runner, 24 task_runner,
26 base::FilePath(store_path.value() + FILE_PATH_LITERAL(".fake"))), 25 base::FilePath(store_path.value() + FILE_PATH_LITERAL(".fake"))),
27 hash_prefix_should_match_(hash_prefix_matches), 26 hash_prefix_should_match_(hash_prefix_matches) {}
28 reset_succeeds_(reset_succeeds) {}
29
30 bool Reset() override { return reset_succeeds_; }
31 27
32 HashPrefix GetMatchingHashPrefix(const FullHash& full_hash) override { 28 HashPrefix GetMatchingHashPrefix(const FullHash& full_hash) override {
33 return hash_prefix_should_match_ ? full_hash : HashPrefix(); 29 return hash_prefix_should_match_ ? full_hash : HashPrefix();
34 } 30 }
35 31
36 void set_hash_prefix_matches(bool hash_prefix_matches) { 32 void set_hash_prefix_matches(bool hash_prefix_matches) {
37 hash_prefix_should_match_ = hash_prefix_matches; 33 hash_prefix_should_match_ = hash_prefix_matches;
38 } 34 }
39 35
40 private: 36 private:
41 bool hash_prefix_should_match_; 37 bool hash_prefix_should_match_;
42 bool reset_succeeds_;
43 }; 38 };
44 39
45 // This factory creates a "fake" store. It allows the caller to specify that the 40 // This factory creates a "fake" store. It allows the caller to specify whether
46 // first store should fail on Reset() i.e. return false. All subsequent stores 41 // the store has a hash prefix matching a full hash. This is used to test the
47 // always return true. This is used to test the Reset() method in V4Database. 42 // |GetStoresMatchingFullHash()| method in |V4Database|.
48 class FakeV4StoreFactory : public V4StoreFactory { 43 class FakeV4StoreFactory : public V4StoreFactory {
49 public: 44 public:
50 FakeV4StoreFactory(bool next_store_reset_fails, bool hash_prefix_matches) 45 FakeV4StoreFactory(bool hash_prefix_matches)
51 : hash_prefix_should_match_(hash_prefix_matches), 46 : hash_prefix_should_match_(hash_prefix_matches) {}
52 next_store_reset_fails_(next_store_reset_fails) {}
53 47
54 V4Store* CreateV4Store( 48 V4Store* CreateV4Store(
55 const scoped_refptr<base::SequencedTaskRunner>& task_runner, 49 const scoped_refptr<base::SequencedTaskRunner>& task_runner,
56 const base::FilePath& store_path) override { 50 const base::FilePath& store_path) override {
57 bool reset_succeeds = !next_store_reset_fails_; 51 return new FakeV4Store(task_runner, store_path, hash_prefix_should_match_);
58 next_store_reset_fails_ = false;
59 return new FakeV4Store(task_runner, store_path, reset_succeeds,
60 hash_prefix_should_match_);
61 } 52 }
62 53
63 private: 54 private:
64 bool hash_prefix_should_match_; 55 bool hash_prefix_should_match_;
65 bool next_store_reset_fails_;
66 }; 56 };
67 57
68 class V4DatabaseTest : public PlatformTest { 58 class V4DatabaseTest : public PlatformTest {
69 public: 59 public:
70 V4DatabaseTest() 60 V4DatabaseTest()
71 : task_runner_(new base::TestSimpleTaskRunner), 61 : task_runner_(new base::TestSimpleTaskRunner),
72 linux_malware_id_(LINUX_PLATFORM, URL, MALWARE_THREAT), 62 linux_malware_id_(LINUX_PLATFORM, URL, MALWARE_THREAT),
73 win_malware_id_(WINDOWS_PLATFORM, URL, MALWARE_THREAT) {} 63 win_malware_id_(WINDOWS_PLATFORM, URL, MALWARE_THREAT) {}
74 64
75 void SetUp() override { 65 void SetUp() override {
(...skipping 14 matching lines...) Expand all
90 base::Unretained(this)); 80 base::Unretained(this));
91 81
92 SetupInfoMapAndExpectedState(); 82 SetupInfoMapAndExpectedState();
93 } 83 }
94 84
95 void TearDown() override { 85 void TearDown() override {
96 V4Database::RegisterStoreFactoryForTest(nullptr); 86 V4Database::RegisterStoreFactoryForTest(nullptr);
97 PlatformTest::TearDown(); 87 PlatformTest::TearDown();
98 } 88 }
99 89
100 void RegisterFactory(bool fails_first_reset, 90 void RegisterFactory(bool hash_prefix_matches = true) {
101 bool hash_prefix_matches = true) { 91 factory_.reset(new FakeV4StoreFactory(hash_prefix_matches));
102 factory_.reset(
103 new FakeV4StoreFactory(fails_first_reset, hash_prefix_matches));
104 V4Database::RegisterStoreFactoryForTest(factory_.get()); 92 V4Database::RegisterStoreFactoryForTest(factory_.get());
105 } 93 }
106 94
107 void SetupInfoMapAndExpectedState() { 95 void SetupInfoMapAndExpectedState() {
108 list_infos_.emplace_back(true, "win_url_malware", win_malware_id_, 96 list_infos_.emplace_back(true, "win_url_malware", win_malware_id_,
109 SB_THREAT_TYPE_URL_MALWARE); 97 SB_THREAT_TYPE_URL_MALWARE);
110 expected_identifiers_.push_back(win_malware_id_); 98 expected_identifiers_.push_back(win_malware_id_);
111 expected_store_paths_.push_back( 99 expected_store_paths_.push_back(
112 database_dirname_.AppendASCII("win_url_malware.fake")); 100 database_dirname_.AppendASCII("win_url_malware.fake"));
113 101
(...skipping 16 matching lines...) Expand all
130 ASSERT_EQ(expected_store_paths_.size(), v4_database->store_map_->size()); 118 ASSERT_EQ(expected_store_paths_.size(), v4_database->store_map_->size());
131 ASSERT_EQ(expected_identifiers_.size(), v4_database->store_map_->size()); 119 ASSERT_EQ(expected_identifiers_.size(), v4_database->store_map_->size());
132 for (size_t i = 0; i < expected_identifiers_.size(); i++) { 120 for (size_t i = 0; i < expected_identifiers_.size(); i++) {
133 const auto& expected_identifier = expected_identifiers_[i]; 121 const auto& expected_identifier = expected_identifiers_[i];
134 const auto& store = (*v4_database->store_map_)[expected_identifier]; 122 const auto& store = (*v4_database->store_map_)[expected_identifier];
135 ASSERT_TRUE(store); 123 ASSERT_TRUE(store);
136 const auto& expected_store_path = expected_store_paths_[i]; 124 const auto& expected_store_path = expected_store_paths_[i];
137 EXPECT_EQ(expected_store_path, store->store_path()); 125 EXPECT_EQ(expected_store_path, store->store_path());
138 } 126 }
139 127
140 EXPECT_EQ(expected_resets_successfully_, v4_database->ResetDatabase());
141
142 EXPECT_FALSE(created_and_called_back_); 128 EXPECT_FALSE(created_and_called_back_);
143 created_and_called_back_ = true; 129 created_and_called_back_ = true;
144 130
145 v4_database_ = std::move(v4_database); 131 v4_database_ = std::move(v4_database);
146 } 132 }
147 133
134 void DatabaseReadyForUpdates() {}
Nathan Parker 2016/10/07 23:24:28 This isn't used. Should it be?
vakh (use Gerrit instead) 2016/10/10 17:42:33 Nope, not needed now. Removed.
135
148 std::unique_ptr<ParsedServerResponse> CreateFakeServerResponse( 136 std::unique_ptr<ParsedServerResponse> CreateFakeServerResponse(
149 StoreStateMap store_state_map, 137 StoreStateMap store_state_map,
150 bool use_valid_response_type) { 138 bool use_valid_response_type) {
151 std::unique_ptr<ParsedServerResponse> parsed_server_response( 139 std::unique_ptr<ParsedServerResponse> parsed_server_response(
152 new ParsedServerResponse); 140 new ParsedServerResponse);
153 for (const auto& store_state_iter : store_state_map) { 141 for (const auto& store_state_iter : store_state_map) {
154 ListIdentifier identifier = store_state_iter.first; 142 ListIdentifier identifier = store_state_iter.first;
155 ListUpdateResponse* lur = new ListUpdateResponse; 143 ListUpdateResponse* lur = new ListUpdateResponse;
156 lur->set_platform_type(identifier.platform_type()); 144 lur->set_platform_type(identifier.platform_type());
157 lur->set_threat_entry_type(identifier.threat_entry_type()); 145 lur->set_threat_entry_type(identifier.threat_entry_type());
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 186 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
199 std::unique_ptr<V4Database> v4_database_; 187 std::unique_ptr<V4Database> v4_database_;
200 base::FilePath database_dirname_; 188 base::FilePath database_dirname_;
201 base::ScopedTempDir temp_dir_; 189 base::ScopedTempDir temp_dir_;
202 content::TestBrowserThreadBundle thread_bundle_; 190 content::TestBrowserThreadBundle thread_bundle_;
203 bool created_but_not_called_back_; 191 bool created_but_not_called_back_;
204 bool created_and_called_back_; 192 bool created_and_called_back_;
205 ListInfos list_infos_; 193 ListInfos list_infos_;
206 std::vector<ListIdentifier> expected_identifiers_; 194 std::vector<ListIdentifier> expected_identifiers_;
207 std::vector<base::FilePath> expected_store_paths_; 195 std::vector<base::FilePath> expected_store_paths_;
208 bool expected_resets_successfully_;
209 std::unique_ptr<FakeV4StoreFactory> factory_; 196 std::unique_ptr<FakeV4StoreFactory> factory_;
210 DatabaseUpdatedCallback callback_db_updated_; 197 DatabaseUpdatedCallback callback_db_updated_;
211 NewDatabaseReadyCallback callback_db_ready_; 198 NewDatabaseReadyCallback callback_db_ready_;
212 StoreStateMap expected_store_state_map_; 199 StoreStateMap expected_store_state_map_;
213 base::hash_map<ListIdentifier, V4Store*> old_stores_map_; 200 base::hash_map<ListIdentifier, V4Store*> old_stores_map_;
214 const ListIdentifier linux_malware_id_, win_malware_id_; 201 const ListIdentifier linux_malware_id_, win_malware_id_;
215 }; 202 };
216 203
217 // Test to set up the database with fake stores. 204 // Test to set up the database with fake stores.
218 TEST_F(V4DatabaseTest, TestSetupDatabaseWithFakeStores) { 205 TEST_F(V4DatabaseTest, TestSetupDatabaseWithFakeStores) {
219 expected_resets_successfully_ = true; 206 RegisterFactory();
220 RegisterFactory(!expected_resets_successfully_);
221 207
222 V4Database::Create(task_runner_, database_dirname_, list_infos_, 208 V4Database::Create(task_runner_, database_dirname_, list_infos_,
223 callback_db_ready_); 209 callback_db_ready_);
224 created_but_not_called_back_ = true;
225 task_runner_->RunPendingTasks();
226
227 base::RunLoop().RunUntilIdle();
228 EXPECT_EQ(true, created_and_called_back_);
229 }
230
231 // Test to set up the database with fake stores that fail to reset.
232 TEST_F(V4DatabaseTest, TestSetupDatabaseWithFakeStoresFailsReset) {
233 expected_resets_successfully_ = false;
234 RegisterFactory(!expected_resets_successfully_);
235
236 V4Database::Create(task_runner_, database_dirname_, list_infos_,
237 callback_db_ready_);
238 created_but_not_called_back_ = true; 210 created_but_not_called_back_ = true;
239 task_runner_->RunPendingTasks(); 211 task_runner_->RunPendingTasks();
240 212
241 base::RunLoop().RunUntilIdle(); 213 base::RunLoop().RunUntilIdle();
242 EXPECT_EQ(true, created_and_called_back_); 214 EXPECT_EQ(true, created_and_called_back_);
243 } 215 }
244 216
245 // Test to check database updates as expected. 217 // Test to check database updates as expected.
246 TEST_F(V4DatabaseTest, TestApplyUpdateWithNewStates) { 218 TEST_F(V4DatabaseTest, TestApplyUpdateWithNewStates) {
247 expected_resets_successfully_ = true; 219 RegisterFactory();
248 RegisterFactory(!expected_resets_successfully_);
249 220
250 V4Database::Create(task_runner_, database_dirname_, list_infos_, 221 V4Database::Create(task_runner_, database_dirname_, list_infos_,
251 callback_db_ready_); 222 callback_db_ready_);
252 created_but_not_called_back_ = true; 223 created_but_not_called_back_ = true;
253 task_runner_->RunPendingTasks(); 224 task_runner_->RunPendingTasks();
254 base::RunLoop().RunUntilIdle(); 225 base::RunLoop().RunUntilIdle();
255 226
256 // The database has now been created. Time to try to update it. 227 // The database has now been created. Time to try to update it.
257 EXPECT_TRUE(v4_database_); 228 EXPECT_TRUE(v4_database_);
258 const StoreMap* db_stores = v4_database_->store_map_.get(); 229 const StoreMap* db_stores = v4_database_->store_map_.get();
259 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); 230 EXPECT_EQ(expected_store_paths_.size(), db_stores->size());
260 for (const auto& store_iter : *db_stores) { 231 for (const auto& store_iter : *db_stores) {
261 V4Store* store = store_iter.second.get(); 232 V4Store* store = store_iter.second.get();
262 expected_store_state_map_[store_iter.first] = store->state() + "_fake"; 233 expected_store_state_map_[store_iter.first] = store->state() + "_fake";
263 old_stores_map_[store_iter.first] = store; 234 old_stores_map_[store_iter.first] = store;
264 } 235 }
265 236
266 v4_database_->ApplyUpdate( 237 v4_database_->ApplyUpdate(
267 CreateFakeServerResponse(expected_store_state_map_, true), 238 CreateFakeServerResponse(expected_store_state_map_, true),
268 callback_db_updated_); 239 callback_db_updated_);
269 240
270 task_runner_->RunPendingTasks(); 241 task_runner_->RunPendingTasks();
271 base::RunLoop().RunUntilIdle(); 242 base::RunLoop().RunUntilIdle();
272 243
273 VerifyExpectedStoresState(true); 244 VerifyExpectedStoresState(true);
274 } 245 }
275 246
276 // Test to ensure no state updates leads to no store updates. 247 // Test to ensure no state updates leads to no store updates.
277 TEST_F(V4DatabaseTest, TestApplyUpdateWithNoNewState) { 248 TEST_F(V4DatabaseTest, TestApplyUpdateWithNoNewState) {
278 expected_resets_successfully_ = true; 249 RegisterFactory();
279 RegisterFactory(!expected_resets_successfully_);
280 250
281 V4Database::Create(task_runner_, database_dirname_, list_infos_, 251 V4Database::Create(task_runner_, database_dirname_, list_infos_,
282 callback_db_ready_); 252 callback_db_ready_);
283 created_but_not_called_back_ = true; 253 created_but_not_called_back_ = true;
284 task_runner_->RunPendingTasks(); 254 task_runner_->RunPendingTasks();
285 base::RunLoop().RunUntilIdle(); 255 base::RunLoop().RunUntilIdle();
286 256
287 // The database has now been created. Time to try to update it. 257 // The database has now been created. Time to try to update it.
288 EXPECT_TRUE(v4_database_); 258 EXPECT_TRUE(v4_database_);
289 const StoreMap* db_stores = v4_database_->store_map_.get(); 259 const StoreMap* db_stores = v4_database_->store_map_.get();
290 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); 260 EXPECT_EQ(expected_store_paths_.size(), db_stores->size());
291 for (const auto& store_iter : *db_stores) { 261 for (const auto& store_iter : *db_stores) {
292 V4Store* store = store_iter.second.get(); 262 V4Store* store = store_iter.second.get();
293 expected_store_state_map_[store_iter.first] = store->state(); 263 expected_store_state_map_[store_iter.first] = store->state();
294 old_stores_map_[store_iter.first] = store; 264 old_stores_map_[store_iter.first] = store;
295 } 265 }
296 266
297 v4_database_->ApplyUpdate( 267 v4_database_->ApplyUpdate(
298 CreateFakeServerResponse(expected_store_state_map_, true), 268 CreateFakeServerResponse(expected_store_state_map_, true),
299 callback_db_updated_); 269 callback_db_updated_);
300 270
301 task_runner_->RunPendingTasks(); 271 task_runner_->RunPendingTasks();
302 base::RunLoop().RunUntilIdle(); 272 base::RunLoop().RunUntilIdle();
303 273
304 VerifyExpectedStoresState(false); 274 VerifyExpectedStoresState(false);
305 } 275 }
306 276
307 // Test to ensure no updates leads to no store updates. 277 // Test to ensure no updates leads to no store updates.
308 TEST_F(V4DatabaseTest, TestApplyUpdateWithEmptyUpdate) { 278 TEST_F(V4DatabaseTest, TestApplyUpdateWithEmptyUpdate) {
309 expected_resets_successfully_ = true; 279 RegisterFactory();
310 RegisterFactory(!expected_resets_successfully_);
311 280
312 V4Database::Create(task_runner_, database_dirname_, list_infos_, 281 V4Database::Create(task_runner_, database_dirname_, list_infos_,
313 callback_db_ready_); 282 callback_db_ready_);
314 created_but_not_called_back_ = true; 283 created_but_not_called_back_ = true;
315 task_runner_->RunPendingTasks(); 284 task_runner_->RunPendingTasks();
316 base::RunLoop().RunUntilIdle(); 285 base::RunLoop().RunUntilIdle();
317 286
318 // The database has now been created. Time to try to update it. 287 // The database has now been created. Time to try to update it.
319 EXPECT_TRUE(v4_database_); 288 EXPECT_TRUE(v4_database_);
320 const StoreMap* db_stores = v4_database_->store_map_.get(); 289 const StoreMap* db_stores = v4_database_->store_map_.get();
(...skipping 10 matching lines...) Expand all
331 callback_db_updated_); 300 callback_db_updated_);
332 301
333 task_runner_->RunPendingTasks(); 302 task_runner_->RunPendingTasks();
334 base::RunLoop().RunUntilIdle(); 303 base::RunLoop().RunUntilIdle();
335 304
336 VerifyExpectedStoresState(false); 305 VerifyExpectedStoresState(false);
337 } 306 }
338 307
339 // Test to ensure invalid update leads to no store changes. 308 // Test to ensure invalid update leads to no store changes.
340 TEST_F(V4DatabaseTest, TestApplyUpdateWithInvalidUpdate) { 309 TEST_F(V4DatabaseTest, TestApplyUpdateWithInvalidUpdate) {
341 expected_resets_successfully_ = true; 310 RegisterFactory();
342 RegisterFactory(!expected_resets_successfully_);
343 311
344 V4Database::Create(task_runner_, database_dirname_, list_infos_, 312 V4Database::Create(task_runner_, database_dirname_, list_infos_,
345 callback_db_ready_); 313 callback_db_ready_);
346 created_but_not_called_back_ = true; 314 created_but_not_called_back_ = true;
347 task_runner_->RunPendingTasks(); 315 task_runner_->RunPendingTasks();
348 base::RunLoop().RunUntilIdle(); 316 base::RunLoop().RunUntilIdle();
349 317
350 // The database has now been created. Time to try to update it. 318 // The database has now been created. Time to try to update it.
351 EXPECT_TRUE(v4_database_); 319 EXPECT_TRUE(v4_database_);
352 const StoreMap* db_stores = v4_database_->store_map_.get(); 320 const StoreMap* db_stores = v4_database_->store_map_.get();
353 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); 321 EXPECT_EQ(expected_store_paths_.size(), db_stores->size());
354 for (const auto& store_iter : *db_stores) { 322 for (const auto& store_iter : *db_stores) {
355 V4Store* store = store_iter.second.get(); 323 V4Store* store = store_iter.second.get();
356 expected_store_state_map_[store_iter.first] = store->state(); 324 expected_store_state_map_[store_iter.first] = store->state();
357 old_stores_map_[store_iter.first] = store; 325 old_stores_map_[store_iter.first] = store;
358 } 326 }
359 327
360 v4_database_->ApplyUpdate( 328 v4_database_->ApplyUpdate(
361 CreateFakeServerResponse(expected_store_state_map_, false), 329 CreateFakeServerResponse(expected_store_state_map_, false),
362 callback_db_updated_); 330 callback_db_updated_);
363 task_runner_->RunPendingTasks(); 331 task_runner_->RunPendingTasks();
364 base::RunLoop().RunUntilIdle(); 332 base::RunLoop().RunUntilIdle();
365 333
366 VerifyExpectedStoresState(false); 334 VerifyExpectedStoresState(false);
367 } 335 }
368 336
369 // Test to ensure the case that all stores match a given full hash. 337 // Test to ensure the case that all stores match a given full hash.
370 TEST_F(V4DatabaseTest, TestAllStoresMatchFullHash) { 338 TEST_F(V4DatabaseTest, TestAllStoresMatchFullHash) {
371 bool hash_prefix_matches = true; 339 bool hash_prefix_matches = true;
372 expected_resets_successfully_ = true; 340 RegisterFactory(hash_prefix_matches);
373 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches);
374 341
375 V4Database::Create(task_runner_, database_dirname_, list_infos_, 342 V4Database::Create(task_runner_, database_dirname_, list_infos_,
376 callback_db_ready_); 343 callback_db_ready_);
377 created_but_not_called_back_ = true; 344 created_but_not_called_back_ = true;
378 task_runner_->RunPendingTasks(); 345 task_runner_->RunPendingTasks();
379 346
380 base::RunLoop().RunUntilIdle(); 347 base::RunLoop().RunUntilIdle();
381 EXPECT_EQ(true, created_and_called_back_); 348 EXPECT_EQ(true, created_and_called_back_);
382 349
383 StoresToCheck stores_to_check({linux_malware_id_, win_malware_id_}); 350 StoresToCheck stores_to_check({linux_malware_id_, win_malware_id_});
384 StoreAndHashPrefixes store_and_hash_prefixes; 351 StoreAndHashPrefixes store_and_hash_prefixes;
385 v4_database_->GetStoresMatchingFullHash("anything", stores_to_check, 352 v4_database_->GetStoresMatchingFullHash("anything", stores_to_check,
386 &store_and_hash_prefixes); 353 &store_and_hash_prefixes);
387 EXPECT_EQ(2u, store_and_hash_prefixes.size()); 354 EXPECT_EQ(2u, store_and_hash_prefixes.size());
388 StoresToCheck stores_found; 355 StoresToCheck stores_found;
389 for (const auto& it : store_and_hash_prefixes) { 356 for (const auto& it : store_and_hash_prefixes) {
390 stores_found.insert(it.list_id); 357 stores_found.insert(it.list_id);
391 } 358 }
392 EXPECT_EQ(stores_to_check, stores_found); 359 EXPECT_EQ(stores_to_check, stores_found);
393 } 360 }
394 361
395 // Test to ensure the case that no stores match a given full hash. 362 // Test to ensure the case that no stores match a given full hash.
396 TEST_F(V4DatabaseTest, TestNoStoreMatchesFullHash) { 363 TEST_F(V4DatabaseTest, TestNoStoreMatchesFullHash) {
397 bool hash_prefix_matches = false; 364 bool hash_prefix_matches = false;
398 expected_resets_successfully_ = true; 365 RegisterFactory(hash_prefix_matches);
399 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches);
400 366
401 V4Database::Create(task_runner_, database_dirname_, list_infos_, 367 V4Database::Create(task_runner_, database_dirname_, list_infos_,
402 callback_db_ready_); 368 callback_db_ready_);
403 created_but_not_called_back_ = true; 369 created_but_not_called_back_ = true;
404 task_runner_->RunPendingTasks(); 370 task_runner_->RunPendingTasks();
405 371
406 base::RunLoop().RunUntilIdle(); 372 base::RunLoop().RunUntilIdle();
407 EXPECT_EQ(true, created_and_called_back_); 373 EXPECT_EQ(true, created_and_called_back_);
408 374
409 StoreAndHashPrefixes store_and_hash_prefixes; 375 StoreAndHashPrefixes store_and_hash_prefixes;
410 v4_database_->GetStoresMatchingFullHash( 376 v4_database_->GetStoresMatchingFullHash(
411 "anything", StoresToCheck({linux_malware_id_, win_malware_id_}), 377 "anything", StoresToCheck({linux_malware_id_, win_malware_id_}),
412 &store_and_hash_prefixes); 378 &store_and_hash_prefixes);
413 EXPECT_TRUE(store_and_hash_prefixes.empty()); 379 EXPECT_TRUE(store_and_hash_prefixes.empty());
414 } 380 }
415 381
416 // Test to ensure the case that some stores match a given full hash. 382 // Test to ensure the case that some stores match a given full hash.
417 TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHash) { 383 TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHash) {
418 // Setup stores to not match the full hash. 384 // Setup stores to not match the full hash.
419 bool hash_prefix_matches = false; 385 bool hash_prefix_matches = false;
420 expected_resets_successfully_ = true; 386 RegisterFactory(hash_prefix_matches);
421 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches);
422 387
423 V4Database::Create(task_runner_, database_dirname_, list_infos_, 388 V4Database::Create(task_runner_, database_dirname_, list_infos_,
424 callback_db_ready_); 389 callback_db_ready_);
425 created_but_not_called_back_ = true; 390 created_but_not_called_back_ = true;
426 task_runner_->RunPendingTasks(); 391 task_runner_->RunPendingTasks();
427 392
428 base::RunLoop().RunUntilIdle(); 393 base::RunLoop().RunUntilIdle();
429 EXPECT_EQ(true, created_and_called_back_); 394 EXPECT_EQ(true, created_and_called_back_);
430 395
431 // Set the store corresponding to linux_malware_id_ to match the full hash. 396 // Set the store corresponding to linux_malware_id_ to match the full hash.
432 FakeV4Store* store = static_cast<FakeV4Store*>( 397 FakeV4Store* store = static_cast<FakeV4Store*>(
433 v4_database_->store_map_->at(win_malware_id_).get()); 398 v4_database_->store_map_->at(win_malware_id_).get());
434 store->set_hash_prefix_matches(true); 399 store->set_hash_prefix_matches(true);
435 400
436 StoreAndHashPrefixes store_and_hash_prefixes; 401 StoreAndHashPrefixes store_and_hash_prefixes;
437 v4_database_->GetStoresMatchingFullHash( 402 v4_database_->GetStoresMatchingFullHash(
438 "anything", StoresToCheck({linux_malware_id_, win_malware_id_}), 403 "anything", StoresToCheck({linux_malware_id_, win_malware_id_}),
439 &store_and_hash_prefixes); 404 &store_and_hash_prefixes);
440 EXPECT_EQ(1u, store_and_hash_prefixes.size()); 405 EXPECT_EQ(1u, store_and_hash_prefixes.size());
441 EXPECT_EQ(store_and_hash_prefixes.begin()->list_id, win_malware_id_); 406 EXPECT_EQ(store_and_hash_prefixes.begin()->list_id, win_malware_id_);
442 EXPECT_FALSE(store_and_hash_prefixes.begin()->hash_prefix.empty()); 407 EXPECT_FALSE(store_and_hash_prefixes.begin()->hash_prefix.empty());
443 } 408 }
444 409
445 // Test to ensure the case that only some stores are reported to match a given 410 // Test to ensure the case that only some stores are reported to match a given
446 // full hash because of StoresToCheck. 411 // full hash because of StoresToCheck.
447 TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHashBecauseOfStoresToMatch) { 412 TEST_F(V4DatabaseTest, TestSomeStoresMatchFullHashBecauseOfStoresToMatch) {
448 // Setup all stores to match the full hash. 413 // Setup all stores to match the full hash.
449 bool hash_prefix_matches = true; 414 bool hash_prefix_matches = true;
450 expected_resets_successfully_ = true; 415 RegisterFactory(hash_prefix_matches);
451 RegisterFactory(!expected_resets_successfully_, hash_prefix_matches);
452 416
453 V4Database::Create(task_runner_, database_dirname_, list_infos_, 417 V4Database::Create(task_runner_, database_dirname_, list_infos_,
454 callback_db_ready_); 418 callback_db_ready_);
455 created_but_not_called_back_ = true; 419 created_but_not_called_back_ = true;
456 task_runner_->RunPendingTasks(); 420 task_runner_->RunPendingTasks();
457 421
458 base::RunLoop().RunUntilIdle(); 422 base::RunLoop().RunUntilIdle();
459 EXPECT_EQ(true, created_and_called_back_); 423 EXPECT_EQ(true, created_and_called_back_);
460 424
461 // Don't add win_malware_id_ to the StoresToCheck. 425 // Don't add win_malware_id_ to the StoresToCheck.
462 StoreAndHashPrefixes store_and_hash_prefixes; 426 StoreAndHashPrefixes store_and_hash_prefixes;
463 v4_database_->GetStoresMatchingFullHash( 427 v4_database_->GetStoresMatchingFullHash(
464 "anything", StoresToCheck({linux_malware_id_}), &store_and_hash_prefixes); 428 "anything", StoresToCheck({linux_malware_id_}), &store_and_hash_prefixes);
465 EXPECT_EQ(1u, store_and_hash_prefixes.size()); 429 EXPECT_EQ(1u, store_and_hash_prefixes.size());
466 EXPECT_EQ(store_and_hash_prefixes.begin()->list_id, linux_malware_id_); 430 EXPECT_EQ(store_and_hash_prefixes.begin()->list_id, linux_malware_id_);
467 EXPECT_FALSE(store_and_hash_prefixes.begin()->hash_prefix.empty()); 431 EXPECT_FALSE(store_and_hash_prefixes.begin()->hash_prefix.empty());
468 } 432 }
469 433
470 } // namespace safe_browsing 434 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698