OLD | NEW |
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/file_util.h" | 5 #include "base/files/file_util.h" |
6 #include "base/files/scoped_temp_dir.h" | 6 #include "base/files/scoped_temp_dir.h" |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.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 "base/time/time.h" | 10 #include "base/time/time.h" |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 std::unique_ptr<ListUpdateResponse> list_update_response( | 132 std::unique_ptr<ListUpdateResponse> list_update_response( |
133 new ListUpdateResponse); | 133 new ListUpdateResponse); |
134 list_update_response->set_response_type(ListUpdateResponse::FULL_UPDATE); | 134 list_update_response->set_response_type(ListUpdateResponse::FULL_UPDATE); |
135 list_update_response->set_new_client_state("test_client_state"); | 135 list_update_response->set_new_client_state("test_client_state"); |
136 EXPECT_EQ(WRITE_SUCCESS, V4Store(task_runner_, store_path_) | 136 EXPECT_EQ(WRITE_SUCCESS, V4Store(task_runner_, store_path_) |
137 .WriteToDisk(std::move(list_update_response))); | 137 .WriteToDisk(std::move(list_update_response))); |
138 | 138 |
139 std::unique_ptr<V4Store> read_store(new V4Store(task_runner_, store_path_)); | 139 std::unique_ptr<V4Store> read_store(new V4Store(task_runner_, store_path_)); |
140 EXPECT_EQ(READ_SUCCESS, read_store->ReadFromDisk()); | 140 EXPECT_EQ(READ_SUCCESS, read_store->ReadFromDisk()); |
141 EXPECT_EQ("test_client_state", read_store->state_); | 141 EXPECT_EQ("test_client_state", read_store->state_); |
| 142 EXPECT_TRUE(read_store->hash_prefix_map_.empty()); |
142 } | 143 } |
143 | 144 |
144 TEST_F(V4StoreTest, TestAddUnlumpedHashesWithInvalidAddition) { | 145 TEST_F(V4StoreTest, TestAddUnlumpedHashesWithInvalidAddition) { |
145 HashPrefixMap prefix_map; | 146 HashPrefixMap prefix_map; |
146 EXPECT_EQ(ADDITIONS_SIZE_UNEXPECTED_FAILURE, | 147 EXPECT_EQ(ADDITIONS_SIZE_UNEXPECTED_FAILURE, |
147 V4Store::AddUnlumpedHashes(5, "a", &prefix_map)); | 148 V4Store::AddUnlumpedHashes(5, "a", &prefix_map)); |
148 EXPECT_TRUE(prefix_map.empty()); | 149 EXPECT_TRUE(prefix_map.empty()); |
149 } | 150 } |
150 | 151 |
151 TEST_F(V4StoreTest, TestAddUnlumpedHashesWithEmptyString) { | 152 TEST_F(V4StoreTest, TestAddUnlumpedHashesWithEmptyString) { |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 EXPECT_EQ(1u, prefix_map.size()); | 318 EXPECT_EQ(1u, prefix_map.size()); |
318 | 319 |
319 PrefixSize prefix_size = 4; | 320 PrefixSize prefix_size = 4; |
320 HashPrefixes hash_prefixes = prefix_map.at(prefix_size); | 321 HashPrefixes hash_prefixes = prefix_map.at(prefix_size); |
321 EXPECT_EQ(3 * prefix_size, hash_prefixes.size()); | 322 EXPECT_EQ(3 * prefix_size, hash_prefixes.size()); |
322 EXPECT_EQ("0000", hash_prefixes.substr(0 * prefix_size, prefix_size)); | 323 EXPECT_EQ("0000", hash_prefixes.substr(0 * prefix_size, prefix_size)); |
323 EXPECT_EQ("1111", hash_prefixes.substr(1 * prefix_size, prefix_size)); | 324 EXPECT_EQ("1111", hash_prefixes.substr(1 * prefix_size, prefix_size)); |
324 EXPECT_EQ("2222", hash_prefixes.substr(2 * prefix_size, prefix_size)); | 325 EXPECT_EQ("2222", hash_prefixes.substr(2 * prefix_size, prefix_size)); |
325 } | 326 } |
326 | 327 |
327 TEST_F(V4StoreTest, TestMergeUpdatesFailsForRepeatedhashPrefix) { | 328 TEST_F(V4StoreTest, TestMergeUpdatesFailsForRepeatedHashPrefix) { |
328 HashPrefixMap prefix_map_old; | 329 HashPrefixMap prefix_map_old; |
329 EXPECT_EQ(APPLY_UPDATE_SUCCESS, | 330 EXPECT_EQ(APPLY_UPDATE_SUCCESS, |
330 V4Store::AddUnlumpedHashes(4, "2222", &prefix_map_old)); | 331 V4Store::AddUnlumpedHashes(4, "2222", &prefix_map_old)); |
331 HashPrefixMap prefix_map_additions; | 332 HashPrefixMap prefix_map_additions; |
332 EXPECT_EQ(APPLY_UPDATE_SUCCESS, | 333 EXPECT_EQ(APPLY_UPDATE_SUCCESS, |
333 V4Store::AddUnlumpedHashes(4, "2222", &prefix_map_additions)); | 334 V4Store::AddUnlumpedHashes(4, "2222", &prefix_map_additions)); |
334 | 335 |
335 std::unique_ptr<V4Store> store(new V4Store(task_runner_, store_path_)); | 336 std::unique_ptr<V4Store> store(new V4Store(task_runner_, store_path_)); |
336 EXPECT_EQ(ADDITIONS_HAS_EXISTING_PREFIX_FAILURE, | 337 EXPECT_EQ(ADDITIONS_HAS_EXISTING_PREFIX_FAILURE, |
337 store->MergeUpdate(prefix_map_old, prefix_map_additions)); | 338 store->MergeUpdate(prefix_map_old, prefix_map_additions)); |
338 } | 339 } |
339 | 340 |
| 341 TEST_F(V4StoreTest, TestReadFullResponseWithValidHashPrefixMap) { |
| 342 std::unique_ptr<ListUpdateResponse> lur(new ListUpdateResponse); |
| 343 lur->set_response_type(ListUpdateResponse::FULL_UPDATE); |
| 344 lur->set_new_client_state("test_client_state"); |
| 345 lur->set_platform_type(WINDOWS_PLATFORM); |
| 346 lur->set_threat_entry_type(URL); |
| 347 lur->set_threat_type(MALWARE_THREAT); |
| 348 ThreatEntrySet* additions = lur->add_additions(); |
| 349 additions->set_compression_type(RAW); |
| 350 additions->mutable_raw_hashes()->set_prefix_size(5); |
| 351 additions->mutable_raw_hashes()->set_raw_hashes("00000abcde"); |
| 352 additions = lur->add_additions(); |
| 353 additions->set_compression_type(RAW); |
| 354 additions->mutable_raw_hashes()->set_prefix_size(4); |
| 355 additions->mutable_raw_hashes()->set_raw_hashes("00000abc"); |
| 356 EXPECT_EQ(WRITE_SUCCESS, |
| 357 V4Store(task_runner_, store_path_).WriteToDisk(std::move(lur))); |
| 358 |
| 359 V4Store read_store (task_runner_, store_path_); |
| 360 EXPECT_EQ(READ_SUCCESS, read_store.ReadFromDisk()); |
| 361 EXPECT_EQ("test_client_state", read_store.state_); |
| 362 ASSERT_EQ(2u, read_store.hash_prefix_map_.size()); |
| 363 EXPECT_EQ("00000abc", read_store.hash_prefix_map_[4]); |
| 364 EXPECT_EQ("00000abcde", read_store.hash_prefix_map_[5]); |
| 365 } |
| 366 |
| 367 // This tests fails to read the prefix map from the disk because the file on |
| 368 // disk is invalid. The hash prefixes string is 6 bytes long, but the prefix |
| 369 // size is 5 so the parser isn't able to split the hash prefixes list |
| 370 // completely. |
| 371 TEST_F(V4StoreTest, TestReadFullResponseWithInvalidHashPrefixMap) { |
| 372 std::unique_ptr<ListUpdateResponse> lur(new ListUpdateResponse); |
| 373 lur->set_response_type(ListUpdateResponse::FULL_UPDATE); |
| 374 lur->set_new_client_state("test_client_state"); |
| 375 lur->set_platform_type(WINDOWS_PLATFORM); |
| 376 lur->set_threat_entry_type(URL); |
| 377 lur->set_threat_type(MALWARE_THREAT); |
| 378 ThreatEntrySet* additions = lur->add_additions(); |
| 379 additions->set_compression_type(RAW); |
| 380 additions->mutable_raw_hashes()->set_prefix_size(5); |
| 381 additions->mutable_raw_hashes()->set_raw_hashes("abcdef"); |
| 382 EXPECT_EQ(WRITE_SUCCESS, |
| 383 V4Store(task_runner_, store_path_).WriteToDisk(std::move(lur))); |
| 384 |
| 385 V4Store read_store(task_runner_, store_path_); |
| 386 EXPECT_EQ(HASH_PREFIX_MAP_GENERATION_FAILURE, read_store.ReadFromDisk()); |
| 387 EXPECT_TRUE(read_store.state_.empty()); |
| 388 EXPECT_TRUE(read_store.hash_prefix_map_.empty()); |
| 389 } |
| 390 |
340 } // namespace safe_browsing | 391 } // namespace safe_browsing |
OLD | NEW |