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/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" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 void SetUp() override { | 58 void SetUp() override { |
59 PlatformTest::SetUp(); | 59 PlatformTest::SetUp(); |
60 | 60 |
61 // Setup a database in a temporary directory. | 61 // Setup a database in a temporary directory. |
62 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 62 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
63 database_dirname_ = | 63 database_dirname_ = |
64 temp_dir_.path().AppendASCII("SafeBrowsingV4DatabaseTest"); | 64 temp_dir_.path().AppendASCII("SafeBrowsingV4DatabaseTest"); |
65 | 65 |
66 created_but_not_called_back_ = false; | 66 created_but_not_called_back_ = false; |
67 created_and_called_back_ = false; | 67 created_and_called_back_ = false; |
| 68 |
| 69 callback_db_updated_ = base::Bind( |
| 70 &SafeBrowsingV4DatabaseTest::DatabaseUpdated, base::Unretained(this)); |
| 71 |
| 72 callback_db_ready_ = |
| 73 base::Bind(&SafeBrowsingV4DatabaseTest:: |
| 74 NewDatabaseReadyWithExpectedStorePathsAndIds, |
| 75 base::Unretained(this)); |
| 76 |
| 77 SetupInfoMapAndExpectedState(); |
| 78 } |
| 79 |
| 80 void TearDown() override { |
| 81 V4Database::RegisterStoreFactoryForTest(nullptr); |
| 82 PlatformTest::TearDown(); |
| 83 } |
| 84 |
| 85 void RegisterFactory(bool fails_first_reset) { |
| 86 factory_.reset(new FakeV4StoreFactory(fails_first_reset)); |
| 87 V4Database::RegisterStoreFactoryForTest(factory_.get()); |
68 } | 88 } |
69 | 89 |
70 void SetupInfoMapAndExpectedState() { | 90 void SetupInfoMapAndExpectedState() { |
71 UpdateListIdentifier update_list_identifier; | 91 UpdateListIdentifier win_malware_id(WINDOWS_PLATFORM, URL, MALWARE_THREAT); |
72 | 92 store_file_name_map_[win_malware_id] = "win_url_malware"; |
73 update_list_identifier.platform_type = WINDOWS_PLATFORM; | 93 expected_identifiers_.push_back(win_malware_id); |
74 update_list_identifier.threat_entry_type = URL; | |
75 update_list_identifier.threat_type = MALWARE_THREAT; | |
76 store_file_name_map_[update_list_identifier] = "win_url_malware"; | |
77 expected_identifiers_.push_back(update_list_identifier); | |
78 expected_store_paths_.push_back( | 94 expected_store_paths_.push_back( |
79 database_dirname_.AppendASCII("win_url_malware.fake")); | 95 database_dirname_.AppendASCII("win_url_malware.fake")); |
80 | 96 |
81 update_list_identifier.platform_type = LINUX_PLATFORM; | 97 UpdateListIdentifier linux_malware_id(LINUX_PLATFORM, URL, MALWARE_THREAT); |
82 update_list_identifier.threat_entry_type = URL; | 98 store_file_name_map_[linux_malware_id] = "linux_url_malware"; |
83 update_list_identifier.threat_type = MALWARE_THREAT; | 99 expected_identifiers_.push_back(linux_malware_id); |
84 store_file_name_map_[update_list_identifier] = "linux_url_malware"; | |
85 expected_identifiers_.push_back(update_list_identifier); | |
86 expected_store_paths_.push_back( | 100 expected_store_paths_.push_back( |
87 database_dirname_.AppendASCII("linux_url_malware.fake")); | 101 database_dirname_.AppendASCII("linux_url_malware.fake")); |
88 } | 102 } |
89 | 103 |
| 104 void DatabaseUpdated() {} |
90 void NewDatabaseReadyWithExpectedStorePathsAndIds( | 105 void NewDatabaseReadyWithExpectedStorePathsAndIds( |
91 std::vector<base::FilePath> expected_store_paths, | |
92 std::vector<UpdateListIdentifier> expected_identifiers, | |
93 bool expected_resets_successfully, | |
94 std::unique_ptr<V4Database> v4_database) { | 106 std::unique_ptr<V4Database> v4_database) { |
95 ASSERT_TRUE(v4_database); | 107 ASSERT_TRUE(v4_database); |
96 ASSERT_TRUE(v4_database->store_map_); | 108 ASSERT_TRUE(v4_database->store_map_); |
97 | 109 |
98 // The following check ensures that the callback was called asynchronously. | 110 // The following check ensures that the callback was called asynchronously. |
99 EXPECT_TRUE(created_but_not_called_back_); | 111 EXPECT_TRUE(created_but_not_called_back_); |
100 | 112 |
101 ASSERT_EQ(expected_store_paths.size(), v4_database->store_map_->size()); | 113 ASSERT_EQ(expected_store_paths_.size(), v4_database->store_map_->size()); |
102 ASSERT_EQ(expected_identifiers.size(), v4_database->store_map_->size()); | 114 ASSERT_EQ(expected_identifiers_.size(), v4_database->store_map_->size()); |
103 for (size_t i = 0; i < expected_identifiers.size(); i++) { | 115 for (size_t i = 0; i < expected_identifiers_.size(); i++) { |
104 const auto& expected_identifier = expected_identifiers[i]; | 116 const auto& expected_identifier = expected_identifiers_[i]; |
105 const auto& store = (*v4_database->store_map_)[expected_identifier]; | 117 const auto& store = (*v4_database->store_map_)[expected_identifier]; |
106 ASSERT_TRUE(store); | 118 ASSERT_TRUE(store); |
107 const auto& expected_store_path = expected_store_paths[i]; | 119 const auto& expected_store_path = expected_store_paths_[i]; |
108 EXPECT_EQ(expected_store_path, store->store_path()); | 120 EXPECT_EQ(expected_store_path, store->store_path()); |
109 } | 121 } |
110 | 122 |
111 EXPECT_EQ(expected_resets_successfully, v4_database->ResetDatabase()); | 123 EXPECT_EQ(expected_resets_successfully_, v4_database->ResetDatabase()); |
112 | 124 |
113 EXPECT_FALSE(created_and_called_back_); | 125 EXPECT_FALSE(created_and_called_back_); |
114 created_and_called_back_ = true; | 126 created_and_called_back_ = true; |
| 127 |
| 128 v4_database_ = std::move(v4_database); |
| 129 } |
| 130 |
| 131 void PopulateFakeUpdateResponse(StoreStateMap store_state_map, |
| 132 std::vector<ListUpdateResponse>* responses) { |
| 133 for (const auto& store_state_iter : store_state_map) { |
| 134 UpdateListIdentifier identifier = store_state_iter.first; |
| 135 ListUpdateResponse list_update_response; |
| 136 list_update_response.set_platform_type(identifier.platform_type); |
| 137 list_update_response.set_threat_entry_type(identifier.threat_entry_type); |
| 138 list_update_response.set_threat_type(identifier.threat_type); |
| 139 list_update_response.set_new_client_state(store_state_iter.second); |
| 140 responses->push_back(list_update_response); |
| 141 } |
| 142 } |
| 143 |
| 144 void VerifyExpectedStoresState(bool expect_new_stores) { |
| 145 const StoreMap* new_store_map = v4_database_->store_map_.get(); |
| 146 std::unique_ptr<StoreStateMap> new_store_state_map = |
| 147 v4_database_->GetStoreStateMap(); |
| 148 EXPECT_EQ(expected_store_state_map_.size(), new_store_map->size()); |
| 149 EXPECT_EQ(expected_store_state_map_.size(), new_store_state_map->size()); |
| 150 for (const auto& expected_iter : expected_store_state_map_) { |
| 151 const UpdateListIdentifier& identifier = expected_iter.first; |
| 152 const std::string& state = expected_iter.second; |
| 153 ASSERT_EQ(1u, new_store_map->count(identifier)); |
| 154 ASSERT_EQ(1u, new_store_state_map->count(identifier)); |
| 155 |
| 156 // Verify the expected state in the store map and the state map. |
| 157 EXPECT_EQ(state, new_store_map->at(identifier)->state()); |
| 158 EXPECT_EQ(state, new_store_state_map->at(identifier)); |
| 159 |
| 160 if (expect_new_stores) { |
| 161 // Verify that a new store was created. |
| 162 EXPECT_NE(old_stores_map_.at(identifier), |
| 163 new_store_map->at(identifier).get()); |
| 164 } else { |
| 165 // Verify that NO new store was created. |
| 166 EXPECT_EQ(old_stores_map_.at(identifier), |
| 167 new_store_map->at(identifier).get()); |
| 168 } |
| 169 } |
115 } | 170 } |
116 | 171 |
117 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 172 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
118 std::unique_ptr<V4Database> v4_database_; | 173 std::unique_ptr<V4Database> v4_database_; |
119 base::FilePath database_dirname_; | 174 base::FilePath database_dirname_; |
120 base::ScopedTempDir temp_dir_; | 175 base::ScopedTempDir temp_dir_; |
121 content::TestBrowserThreadBundle thread_bundle_; | 176 content::TestBrowserThreadBundle thread_bundle_; |
122 bool created_but_not_called_back_; | 177 bool created_but_not_called_back_; |
123 bool created_and_called_back_; | 178 bool created_and_called_back_; |
124 StoreFileNameMap store_file_name_map_; | 179 StoreFileNameMap store_file_name_map_; |
125 std::vector<UpdateListIdentifier> expected_identifiers_; | 180 std::vector<UpdateListIdentifier> expected_identifiers_; |
126 std::vector<base::FilePath> expected_store_paths_; | 181 std::vector<base::FilePath> expected_store_paths_; |
| 182 bool expected_resets_successfully_; |
| 183 std::unique_ptr<FakeV4StoreFactory> factory_; |
| 184 DatabaseUpdatedCallback callback_db_updated_; |
| 185 NewDatabaseReadyCallback callback_db_ready_; |
| 186 StoreStateMap expected_store_state_map_; |
| 187 base::hash_map<UpdateListIdentifier, V4Store*> old_stores_map_; |
127 }; | 188 }; |
128 | 189 |
129 // Test to set up the database with no stores. | 190 // Test to set up the database with fake stores. |
130 TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithNoStores) { | 191 TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithFakeStores) { |
131 NewDatabaseReadyCallback callback_db_ready = base::Bind( | 192 expected_resets_successfully_ = true; |
132 &SafeBrowsingV4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds, | 193 RegisterFactory(!expected_resets_successfully_); |
133 base::Unretained(this), expected_store_paths_, expected_identifiers_, | 194 |
134 true); | |
135 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, | 195 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
136 callback_db_ready); | 196 callback_db_ready_); |
137 created_but_not_called_back_ = true; | 197 created_but_not_called_back_ = true; |
138 task_runner_->RunPendingTasks(); | 198 task_runner_->RunPendingTasks(); |
139 | 199 |
140 base::RunLoop().RunUntilIdle(); | 200 base::RunLoop().RunUntilIdle(); |
141 EXPECT_EQ(true, created_and_called_back_); | 201 EXPECT_EQ(true, created_and_called_back_); |
142 } | 202 } |
143 | 203 |
144 // Test to set up the database with fake stores. | 204 // Test to set up the database with fake stores that fail to reset. |
145 TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithFakeStores) { | 205 TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithFakeStoresFailsReset) { |
146 SetupInfoMapAndExpectedState(); | 206 expected_resets_successfully_ = false; |
| 207 RegisterFactory(!expected_resets_successfully_); |
147 | 208 |
148 NewDatabaseReadyCallback callback_db_ready = base::Bind( | |
149 &SafeBrowsingV4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds, | |
150 base::Unretained(this), expected_store_paths_, expected_identifiers_, | |
151 true); | |
152 | |
153 FakeV4StoreFactory* factory = new FakeV4StoreFactory(false); | |
154 ANNOTATE_LEAKING_OBJECT_PTR(factory); | |
155 V4Database::RegisterStoreFactoryForTest(factory); | |
156 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, | 209 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
157 callback_db_ready); | 210 callback_db_ready_); |
158 created_but_not_called_back_ = true; | 211 created_but_not_called_back_ = true; |
159 task_runner_->RunPendingTasks(); | 212 task_runner_->RunPendingTasks(); |
160 | 213 |
161 base::RunLoop().RunUntilIdle(); | 214 base::RunLoop().RunUntilIdle(); |
162 EXPECT_EQ(true, created_and_called_back_); | 215 EXPECT_EQ(true, created_and_called_back_); |
163 } | 216 } |
164 | 217 |
165 // Test to set up the database with fake stores that fail to reset. | 218 // Test to check database updates as expected. |
166 TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithFakeStoresFailsReset) { | 219 TEST_F(SafeBrowsingV4DatabaseTest, TestApplyUpdateWithNewStates) { |
167 SetupInfoMapAndExpectedState(); | 220 expected_resets_successfully_ = true; |
| 221 RegisterFactory(!expected_resets_successfully_); |
168 | 222 |
169 NewDatabaseReadyCallback callback_db_ready = base::Bind( | |
170 &SafeBrowsingV4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds, | |
171 base::Unretained(this), expected_store_paths_, expected_identifiers_, | |
172 false); | |
173 | |
174 FakeV4StoreFactory* factory = new FakeV4StoreFactory(true); | |
175 ANNOTATE_LEAKING_OBJECT_PTR(factory); | |
176 V4Database::RegisterStoreFactoryForTest(factory); | |
177 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, | 223 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
178 callback_db_ready); | 224 callback_db_ready_); |
179 created_but_not_called_back_ = true; | 225 created_but_not_called_back_ = true; |
180 task_runner_->RunPendingTasks(); | 226 task_runner_->RunPendingTasks(); |
| 227 base::RunLoop().RunUntilIdle(); |
181 | 228 |
| 229 // The database has now been created. Time to try to update it. |
| 230 EXPECT_TRUE(v4_database_); |
| 231 const StoreMap* db_stores = v4_database_->store_map_.get(); |
| 232 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); |
| 233 for (const auto& store_iter : *db_stores) { |
| 234 V4Store* store = store_iter.second.get(); |
| 235 expected_store_state_map_[store_iter.first] = store->state() + "_fake"; |
| 236 old_stores_map_[store_iter.first] = store; |
| 237 } |
| 238 |
| 239 std::vector<ListUpdateResponse> update_response; |
| 240 PopulateFakeUpdateResponse(expected_store_state_map_, &update_response); |
| 241 v4_database_->ApplyUpdate(update_response, callback_db_updated_); |
| 242 |
| 243 task_runner_->RunPendingTasks(); |
182 base::RunLoop().RunUntilIdle(); | 244 base::RunLoop().RunUntilIdle(); |
183 EXPECT_EQ(true, created_and_called_back_); | 245 |
| 246 VerifyExpectedStoresState(true); |
| 247 } |
| 248 |
| 249 // Test to ensure no state updates leads to no store updates. |
| 250 TEST_F(SafeBrowsingV4DatabaseTest, TestApplyUpdateWithNoNewState) { |
| 251 expected_resets_successfully_ = true; |
| 252 RegisterFactory(!expected_resets_successfully_); |
| 253 |
| 254 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
| 255 callback_db_ready_); |
| 256 created_but_not_called_back_ = true; |
| 257 task_runner_->RunPendingTasks(); |
| 258 base::RunLoop().RunUntilIdle(); |
| 259 |
| 260 // The database has now been created. Time to try to update it. |
| 261 EXPECT_TRUE(v4_database_); |
| 262 const StoreMap* db_stores = v4_database_->store_map_.get(); |
| 263 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); |
| 264 for (const auto& store_iter : *db_stores) { |
| 265 V4Store* store = store_iter.second.get(); |
| 266 expected_store_state_map_[store_iter.first] = store->state(); |
| 267 old_stores_map_[store_iter.first] = store; |
| 268 } |
| 269 |
| 270 std::vector<ListUpdateResponse> update_response; |
| 271 PopulateFakeUpdateResponse(expected_store_state_map_, &update_response); |
| 272 v4_database_->ApplyUpdate(update_response, callback_db_updated_); |
| 273 |
| 274 task_runner_->RunPendingTasks(); |
| 275 base::RunLoop().RunUntilIdle(); |
| 276 |
| 277 VerifyExpectedStoresState(false); |
| 278 } |
| 279 |
| 280 // Test to ensure no updates leads to no store updates. |
| 281 TEST_F(SafeBrowsingV4DatabaseTest, TestApplyUpdateWithEmptyUpdate) { |
| 282 expected_resets_successfully_ = true; |
| 283 RegisterFactory(!expected_resets_successfully_); |
| 284 |
| 285 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, |
| 286 callback_db_ready_); |
| 287 created_but_not_called_back_ = true; |
| 288 task_runner_->RunPendingTasks(); |
| 289 base::RunLoop().RunUntilIdle(); |
| 290 |
| 291 // The database has now been created. Time to try to update it. |
| 292 EXPECT_TRUE(v4_database_); |
| 293 const StoreMap* db_stores = v4_database_->store_map_.get(); |
| 294 EXPECT_EQ(expected_store_paths_.size(), db_stores->size()); |
| 295 for (const auto& store_iter : *db_stores) { |
| 296 V4Store* store = store_iter.second.get(); |
| 297 expected_store_state_map_[store_iter.first] = store->state(); |
| 298 old_stores_map_[store_iter.first] = store; |
| 299 } |
| 300 |
| 301 std::vector<ListUpdateResponse> update_response; |
| 302 v4_database_->ApplyUpdate(update_response, callback_db_updated_); |
| 303 |
| 304 task_runner_->RunPendingTasks(); |
| 305 base::RunLoop().RunUntilIdle(); |
| 306 |
| 307 VerifyExpectedStoresState(false); |
184 } | 308 } |
185 | 309 |
186 } // namespace safe_browsing | 310 } // namespace safe_browsing |
OLD | NEW |