| 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 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 } | 142 } |
| 143 | 143 |
| 144 TEST_F(V4StoreTest, TestAddUnlumpedHashesWithInvalidAddition) { |
| 145 HashPrefixMap prefix_map; |
| 146 MergeUpdateResult result = V4Store::AddUnlumpedHashes(5, "a", &prefix_map); |
| 147 EXPECT_EQ(ADDITIONS_SIZE_UNEXPECTED_FAILURE, result); |
| 148 EXPECT_TRUE(prefix_map.empty()); |
| 149 } |
| 150 |
| 151 TEST_F(V4StoreTest, TestAddUnlumpedHashesWithEmptyString) { |
| 152 HashPrefixMap prefix_map; |
| 153 MergeUpdateResult result = V4Store::AddUnlumpedHashes(5, "", &prefix_map); |
| 154 EXPECT_EQ(MERGE_SUCCESS, result); |
| 155 EXPECT_TRUE(prefix_map.empty()); |
| 156 } |
| 157 |
| 158 TEST_F(V4StoreTest, TestAddUnlumpedHashes) { |
| 159 HashPrefixMap prefix_map; |
| 160 EXPECT_EQ(MERGE_SUCCESS, |
| 161 V4Store::AddUnlumpedHashes(5, "abcde5432100000-----", &prefix_map)); |
| 162 EXPECT_EQ(1u, prefix_map.size()); |
| 163 HashPrefixes& hash_prefixes = prefix_map[5]; |
| 164 EXPECT_EQ(4u, hash_prefixes.size()); |
| 165 |
| 166 EXPECT_EQ("abcde", std::string(hash_prefixes[0].get())); |
| 167 EXPECT_EQ("54321", std::string(hash_prefixes[1].get())); |
| 168 EXPECT_EQ("00000", std::string(hash_prefixes[2].get())); |
| 169 EXPECT_EQ("-----", std::string(hash_prefixes[3].get())); |
| 170 |
| 171 EXPECT_EQ(MERGE_SUCCESS, |
| 172 V4Store::AddUnlumpedHashes(4, "abcde5432100000-----", &prefix_map)); |
| 173 EXPECT_EQ(2u, prefix_map.size()); |
| 174 HashPrefixes& hash_prefixes_of_length_4 = prefix_map[4]; |
| 175 EXPECT_EQ(5u, hash_prefixes_of_length_4.size()); |
| 176 EXPECT_EQ("abcd", std::string(hash_prefixes_of_length_4[0].get())); |
| 177 EXPECT_EQ("e543", std::string(hash_prefixes_of_length_4[1].get())); |
| 178 EXPECT_EQ("2100", std::string(hash_prefixes_of_length_4[2].get())); |
| 179 EXPECT_EQ("000-", std::string(hash_prefixes_of_length_4[3].get())); |
| 180 EXPECT_EQ("----", std::string(hash_prefixes_of_length_4[4].get())); |
| 181 } |
| 182 |
| 183 TEST_F(V4StoreTest, TestGetNextSmallestPrefixSizeWithEmptyPrefixMap) { |
| 184 HashPrefixMap prefix_map; |
| 185 CounterMap counter_map = V4Store::GetInitializedCounterMap(prefix_map); |
| 186 |
| 187 PrefixSize prefix_size; |
| 188 EXPECT_FALSE(V4Store::GetNextSmallestPrefixSize(prefix_map, counter_map, |
| 189 &prefix_size)); |
| 190 } |
| 191 |
| 192 TEST_F(V4StoreTest, TestGetNextSmallestPrefixSize) { |
| 193 HashPrefixMap prefix_map; |
| 194 V4Store::AddUnlumpedHashes(5, "abcde5432100000-----", &prefix_map); |
| 195 V4Store::AddUnlumpedHashes(4, "abcde5432100000-----", &prefix_map); |
| 196 CounterMap counter_map = V4Store::GetInitializedCounterMap(prefix_map); |
| 197 |
| 198 PrefixSize prefix_size; |
| 199 EXPECT_TRUE(V4Store::GetNextSmallestPrefixSize(prefix_map, counter_map, |
| 200 &prefix_size)); |
| 201 EXPECT_EQ(4u, prefix_size); |
| 202 } |
| 203 |
| 204 TEST_F(V4StoreTest, TestGetNextUnmergedPrefix) { |
| 205 HashPrefixMap prefix_map; |
| 206 V4Store::AddUnlumpedHashes(5, "abcde5432100000-----", &prefix_map); |
| 207 V4Store::AddUnlumpedHashes(4, "abcde5432100000-----", &prefix_map); |
| 208 CounterMap counter_map = V4Store::GetInitializedCounterMap(prefix_map); |
| 209 |
| 210 PrefixSize prefix_size; |
| 211 EXPECT_TRUE(V4Store::GetNextSmallestPrefixSize(prefix_map, counter_map, |
| 212 &prefix_size)); |
| 213 const HashPrefix& prefix = V4Store::GetNextUnmergedPrefixForSize( |
| 214 prefix_size, prefix_map, counter_map); |
| 215 std::string prefix_str = std::string(prefix.get(), prefix_size); |
| 216 EXPECT_EQ("abcd", prefix_str); |
| 217 } |
| 218 |
| 219 TEST_F(V4StoreTest, TestMergeUpdates) { |
| 220 HashPrefixMap prefix_map_old; |
| 221 V4Store::AddUnlumpedHashes(4, "abcdefgh", &prefix_map_old); |
| 222 V4Store::AddUnlumpedHashes(5, "54321abcde", &prefix_map_old); |
| 223 HashPrefixMap prefix_map_additions; |
| 224 V4Store::AddUnlumpedHashes(4, "----1111bbbb", &prefix_map_additions); |
| 225 V4Store::AddUnlumpedHashes(5, "22222bcdef", &prefix_map_additions); |
| 226 |
| 227 std::unique_ptr<V4Store> store(new V4Store(task_runner_, store_path_)); |
| 228 store->MergeUpdate(prefix_map_old, prefix_map_additions); |
| 229 const HashPrefixMap& prefix_map = store->hash_prefix_map_; |
| 230 EXPECT_EQ(2u, prefix_map.size()); |
| 231 const HashPrefixes& hash_prefixes_of_length_4 = prefix_map.at(4); |
| 232 EXPECT_EQ(5u, hash_prefixes_of_length_4.size()); |
| 233 EXPECT_EQ("----", std::string(hash_prefixes_of_length_4[0].get())); |
| 234 EXPECT_EQ("1111", std::string(hash_prefixes_of_length_4[1].get())); |
| 235 EXPECT_EQ("abcd", std::string(hash_prefixes_of_length_4[2].get())); |
| 236 EXPECT_EQ("bbbb", std::string(hash_prefixes_of_length_4[3].get())); |
| 237 EXPECT_EQ("efgh", std::string(hash_prefixes_of_length_4[4].get())); |
| 238 |
| 239 const HashPrefixes& hash_prefixes_of_length_5 = prefix_map.at(5); |
| 240 EXPECT_EQ(4u, hash_prefixes_of_length_5.size()); |
| 241 EXPECT_EQ("22222", std::string(hash_prefixes_of_length_5[0].get())); |
| 242 EXPECT_EQ("54321", std::string(hash_prefixes_of_length_5[1].get())); |
| 243 EXPECT_EQ("abcde", std::string(hash_prefixes_of_length_5[2].get())); |
| 244 EXPECT_EQ("bcdef", std::string(hash_prefixes_of_length_5[3].get())); |
| 245 } |
| 246 |
| 144 } // namespace safe_browsing | 247 } // namespace safe_browsing |
| OLD | NEW |