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

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

Issue 2062013002: Fetch incremental updates. Store new state in V4Store. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: git fetch && git pull Created 4 years, 6 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"
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
73 void RegisterFactory(bool fails_first_reset) {
74 factory_.reset(new FakeV4StoreFactory(fails_first_reset));
75 V4Database::RegisterStoreFactoryForTest(factory_.get());
68 } 76 }
69 77
70 void SetupInfoMapAndExpectedState() { 78 void SetupInfoMapAndExpectedState() {
71 UpdateListIdentifier update_list_identifier; 79 UpdateListIdentifier win_malware_id(WINDOWS_PLATFORM, URL, MALWARE_THREAT);
72 80 store_file_name_map_[win_malware_id] = "win_url_malware";
73 update_list_identifier.platform_type = WINDOWS_PLATFORM; 81 expected_identifiers_.push_back(win_malware_id);
Scott Hess - ex-Googler 2016/06/21 22:23:25 I guess that worked out well!
vakh (use Gerrit instead) 2016/06/23 06:23:50 Acknowledged.
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( 82 expected_store_paths_.push_back(
79 database_dirname_.AppendASCII("win_url_malware.fake")); 83 database_dirname_.AppendASCII("win_url_malware.fake"));
80 84
81 update_list_identifier.platform_type = LINUX_PLATFORM; 85 UpdateListIdentifier linux_malware_id(LINUX_PLATFORM, URL, MALWARE_THREAT);
82 update_list_identifier.threat_entry_type = URL; 86 store_file_name_map_[linux_malware_id] = "linux_url_malware";
83 update_list_identifier.threat_type = MALWARE_THREAT; 87 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( 88 expected_store_paths_.push_back(
87 database_dirname_.AppendASCII("linux_url_malware.fake")); 89 database_dirname_.AppendASCII("linux_url_malware.fake"));
88 } 90 }
89 91
92 void DatabaseUpdated() {}
90 void NewDatabaseReadyWithExpectedStorePathsAndIds( 93 void NewDatabaseReadyWithExpectedStorePathsAndIds(
91 std::vector<base::FilePath> expected_store_paths, 94 std::vector<base::FilePath> expected_store_paths,
92 std::vector<UpdateListIdentifier> expected_identifiers, 95 std::vector<UpdateListIdentifier> expected_identifiers,
93 bool expected_resets_successfully, 96 bool expected_resets_successfully,
94 std::unique_ptr<V4Database> v4_database) { 97 std::unique_ptr<V4Database> v4_database) {
95 ASSERT_TRUE(v4_database); 98 ASSERT_TRUE(v4_database);
96 ASSERT_TRUE(v4_database->store_map_); 99 ASSERT_TRUE(v4_database->store_map_);
97 100
98 // The following check ensures that the callback was called asynchronously. 101 // The following check ensures that the callback was called asynchronously.
99 EXPECT_TRUE(created_but_not_called_back_); 102 EXPECT_TRUE(created_but_not_called_back_);
100 103
101 ASSERT_EQ(expected_store_paths.size(), v4_database->store_map_->size()); 104 ASSERT_EQ(expected_store_paths.size(), v4_database->store_map_->size());
102 ASSERT_EQ(expected_identifiers.size(), v4_database->store_map_->size()); 105 ASSERT_EQ(expected_identifiers.size(), v4_database->store_map_->size());
103 for (size_t i = 0; i < expected_identifiers.size(); i++) { 106 for (size_t i = 0; i < expected_identifiers.size(); i++) {
104 const auto& expected_identifier = expected_identifiers[i]; 107 const auto& expected_identifier = expected_identifiers[i];
105 const auto& store = (*v4_database->store_map_)[expected_identifier]; 108 const auto& store = (*v4_database->store_map_)[expected_identifier];
106 ASSERT_TRUE(store); 109 ASSERT_TRUE(store);
107 const auto& expected_store_path = expected_store_paths[i]; 110 const auto& expected_store_path = expected_store_paths[i];
108 EXPECT_EQ(expected_store_path, store->store_path()); 111 EXPECT_EQ(expected_store_path, store->store_path());
109 } 112 }
110 113
111 EXPECT_EQ(expected_resets_successfully, v4_database->ResetDatabase()); 114 EXPECT_EQ(expected_resets_successfully, v4_database->ResetDatabase());
112 115
113 EXPECT_FALSE(created_and_called_back_); 116 EXPECT_FALSE(created_and_called_back_);
114 created_and_called_back_ = true; 117 created_and_called_back_ = true;
118
119 v4_database_ = std::move(v4_database);
120 }
121
122 void PopulateFakeUpdateResponse(StoreStateMap store_state_map,
123 std::vector<ListUpdateResponse>* responses) {
124 for (const auto& store_state_iter : store_state_map) {
125 UpdateListIdentifier identifier = store_state_iter.first;
126 ListUpdateResponse list_update_response;
127 list_update_response.set_platform_type(identifier.platform_type);
128 list_update_response.set_threat_entry_type(identifier.threat_entry_type);
129 list_update_response.set_threat_type(identifier.threat_type);
130 list_update_response.set_new_client_state(store_state_iter.second);
131 responses->push_back(list_update_response);
132 }
133 }
134
135 void SetupNewDatabaseReadyCallback(bool expected_resets_successfully) {
136 callback_db_ready_ =
137 base::Bind(&SafeBrowsingV4DatabaseTest::
138 NewDatabaseReadyWithExpectedStorePathsAndIds,
139 base::Unretained(this), expected_store_paths_,
140 expected_identifiers_, expected_resets_successfully);
141 }
Scott Hess - ex-Googler 2016/06/21 22:23:25 This feels odd to me. It's setting a member varia
vakh (use Gerrit instead) 2016/06/23 06:23:50 Makes good sense. Done.
142
143 void VerifyExpectedStoresState(bool expect_new_stores) {
144 const StoreMap* new_store_map = v4_database_->store_map_.get();
145 std::unique_ptr<StoreStateMap> new_store_state_map =
146 v4_database_->GetStoreStateMap();
147 EXPECT_EQ(expected_store_state_map_.size(), new_store_map->size());
148 EXPECT_EQ(expected_store_state_map_.size(), new_store_state_map->size());
149 for (const auto& expected_iter : expected_store_state_map_) {
150 const UpdateListIdentifier& identifier = expected_iter.first;
151 const std::string& state = expected_iter.second;
152 EXPECT_EQ(1ul, new_store_map->count(identifier));
153 EXPECT_EQ(1ul, new_store_state_map->count(identifier));
Scott Hess - ex-Googler 2016/06/21 22:23:25 The new_store_map case needs to be an ASSERT, beca
vakh (use Gerrit instead) 2016/06/23 06:23:50 Done
154
155 // Verify the expected state in the store map and the state map.
156 EXPECT_EQ(state, new_store_map->at(identifier)->state());
157 EXPECT_EQ(state, new_store_state_map->at(identifier));
158
159 if (expect_new_stores) {
160 // Verify that a new store was created.
161 EXPECT_NE(old_stores_map_[identifier],
162 new_store_map->at(identifier).get());
Scott Hess - ex-Googler 2016/06/21 22:23:25 I'd say either consistently use [] or at(), just t
vakh (use Gerrit instead) 2016/06/23 06:23:50 I prefer to use [], but: 1. I really dislike: (*ne
163 } else {
164 // Verify that NO new store was created.
165 EXPECT_EQ(old_stores_map_[identifier],
166 new_store_map->at(identifier).get());
167 }
168 }
115 } 169 }
116 170
117 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 171 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
118 std::unique_ptr<V4Database> v4_database_; 172 std::unique_ptr<V4Database> v4_database_;
119 base::FilePath database_dirname_; 173 base::FilePath database_dirname_;
120 base::ScopedTempDir temp_dir_; 174 base::ScopedTempDir temp_dir_;
121 content::TestBrowserThreadBundle thread_bundle_; 175 content::TestBrowserThreadBundle thread_bundle_;
122 bool created_but_not_called_back_; 176 bool created_but_not_called_back_;
123 bool created_and_called_back_; 177 bool created_and_called_back_;
124 StoreFileNameMap store_file_name_map_; 178 StoreFileNameMap store_file_name_map_;
125 std::vector<UpdateListIdentifier> expected_identifiers_; 179 std::vector<UpdateListIdentifier> expected_identifiers_;
126 std::vector<base::FilePath> expected_store_paths_; 180 std::vector<base::FilePath> expected_store_paths_;
181 std::unique_ptr<FakeV4StoreFactory> factory_;
182 DatabaseUpdatedCallback callback_db_updated_;
183 NewDatabaseReadyCallback callback_db_ready_;
184 StoreStateMap expected_store_state_map_;
185 base::hash_map<UpdateListIdentifier, V4Store*> old_stores_map_;
127 }; 186 };
128 187
129 // Test to set up the database with no stores. 188 // Test to set up the database with fake stores.
130 TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithNoStores) { 189 TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithFakeStores) {
131 NewDatabaseReadyCallback callback_db_ready = base::Bind( 190 RegisterFactory(false);
132 &SafeBrowsingV4DatabaseTest::NewDatabaseReadyWithExpectedStorePathsAndIds, 191 SetupInfoMapAndExpectedState();
133 base::Unretained(this), expected_store_paths_, expected_identifiers_, 192 SetupNewDatabaseReadyCallback(true);
Scott Hess - ex-Googler 2016/06/21 22:23:25 My earlier suggestion would push the two Setup*()
vakh (use Gerrit instead) 2016/06/23 06:23:50 I've moved the two Setup* methods to SetUp as you
134 true); 193
135 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_, 194 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_,
136 callback_db_ready); 195 callback_db_updated_, callback_db_ready_);
137 created_but_not_called_back_ = true; 196 created_but_not_called_back_ = true;
138 task_runner_->RunPendingTasks(); 197 task_runner_->RunPendingTasks();
139 198
140 base::RunLoop().RunUntilIdle(); 199 base::RunLoop().RunUntilIdle();
141 EXPECT_EQ(true, created_and_called_back_); 200 EXPECT_EQ(true, created_and_called_back_);
142 } 201 }
143 202
144 // Test to set up the database with fake stores. 203 // Test to set up the database with fake stores that fail to reset.
145 TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithFakeStores) { 204 TEST_F(SafeBrowsingV4DatabaseTest, TestSetupDatabaseWithFakeStoresFailsReset) {
205 RegisterFactory(true);
146 SetupInfoMapAndExpectedState(); 206 SetupInfoMapAndExpectedState();
207 SetupNewDatabaseReadyCallback(false);
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_updated_, 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) {
220 RegisterFactory(false);
167 SetupInfoMapAndExpectedState(); 221 SetupInfoMapAndExpectedState();
222 SetupNewDatabaseReadyCallback(true);
168 223
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_, 224 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_,
178 callback_db_ready); 225 callback_db_updated_, callback_db_ready_);
179 created_but_not_called_back_ = true; 226 created_but_not_called_back_ = true;
180 task_runner_->RunPendingTasks(); 227 task_runner_->RunPendingTasks();
228 base::RunLoop().RunUntilIdle();
181 229
230 // The database has now been created. Time to try to update it.
231 EXPECT_TRUE(v4_database_);
232 const StoreMap* db_stores = v4_database_->store_map_.get();
233 EXPECT_EQ(expected_store_paths_.size(), db_stores->size());
234 for (const auto& store_iter : *db_stores) {
235 V4Store* store = store_iter.second.get();
236 expected_store_state_map_[store_iter.first] = store->state() + "_fake";
237 old_stores_map_[store_iter.first] = store;
238 }
239
240 std::vector<ListUpdateResponse> update_response;
241 PopulateFakeUpdateResponse(expected_store_state_map_, &update_response);
242 v4_database_->ApplyUpdate(update_response);
243
244 task_runner_->RunPendingTasks();
182 base::RunLoop().RunUntilIdle(); 245 base::RunLoop().RunUntilIdle();
183 EXPECT_EQ(true, created_and_called_back_); 246
247 VerifyExpectedStoresState(true);
248 }
249
250 // Test to ensure no state updates leads to no store updates.
251 TEST_F(SafeBrowsingV4DatabaseTest, TestApplyUpdateWithNoNewState) {
252 RegisterFactory(false);
253 SetupInfoMapAndExpectedState();
254 SetupNewDatabaseReadyCallback(true);
255
256 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_,
257 callback_db_updated_, callback_db_ready_);
258 created_but_not_called_back_ = true;
259 task_runner_->RunPendingTasks();
260 base::RunLoop().RunUntilIdle();
261
262 // The database has now been created. Time to try to update it.
263 EXPECT_TRUE(v4_database_);
264 const StoreMap* db_stores = v4_database_->store_map_.get();
265 EXPECT_EQ(expected_store_paths_.size(), db_stores->size());
266 for (const auto& store_iter : *db_stores) {
267 V4Store* store = store_iter.second.get();
268 expected_store_state_map_[store_iter.first] = store->state();
269 old_stores_map_[store_iter.first] = store;
270 }
271
272 std::vector<ListUpdateResponse> update_response;
273 PopulateFakeUpdateResponse(expected_store_state_map_, &update_response);
274 v4_database_->ApplyUpdate(update_response);
275
276 task_runner_->RunPendingTasks();
277 base::RunLoop().RunUntilIdle();
278
279 VerifyExpectedStoresState(false);
280 }
281
282 // Test to ensure no updates leads to no store updates.
283 TEST_F(SafeBrowsingV4DatabaseTest, TestApplyUpdateWithEmptyUpdate) {
284 RegisterFactory(false);
285 SetupInfoMapAndExpectedState();
286 SetupNewDatabaseReadyCallback(true);
287
288 V4Database::Create(task_runner_, database_dirname_, store_file_name_map_,
289 callback_db_updated_, callback_db_ready_);
290 created_but_not_called_back_ = true;
291 task_runner_->RunPendingTasks();
292 base::RunLoop().RunUntilIdle();
293
294 // The database has now been created. Time to try to update it.
295 EXPECT_TRUE(v4_database_);
296 const StoreMap* db_stores = v4_database_->store_map_.get();
297 EXPECT_EQ(expected_store_paths_.size(), db_stores->size());
298 for (const auto& store_iter : *db_stores) {
299 V4Store* store = store_iter.second.get();
300 expected_store_state_map_[store_iter.first] = store->state();
301 old_stores_map_[store_iter.first] = store;
302 }
303
304 std::vector<ListUpdateResponse> update_response;
305 v4_database_->ApplyUpdate(update_response);
306
307 task_runner_->RunPendingTasks();
308 base::RunLoop().RunUntilIdle();
309
310 VerifyExpectedStoresState(false);
184 } 311 }
185 312
186 } // namespace safe_browsing 313 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698