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

Side by Side Diff: components/safe_browsing_db/v4_update_protocol_manager_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: Nit: Use base::SStringPrintf instead of string concat 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 "components/safe_browsing_db/v4_update_protocol_manager.h" 5 #include "components/safe_browsing_db/v4_update_protocol_manager.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/base64.h" 10 #include "base/base64.h"
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 EXPECT_EQ(expected.response_type(), actual.response_type()); 51 EXPECT_EQ(expected.response_type(), actual.response_type());
52 EXPECT_EQ(expected.threat_entry_type(), actual.threat_entry_type()); 52 EXPECT_EQ(expected.threat_entry_type(), actual.threat_entry_type());
53 EXPECT_EQ(expected.threat_type(), actual.threat_type()); 53 EXPECT_EQ(expected.threat_type(), actual.threat_type());
54 EXPECT_EQ(expected.new_client_state(), actual.new_client_state()); 54 EXPECT_EQ(expected.new_client_state(), actual.new_client_state());
55 55
56 // TODO(vakh): Test more fields from the proto. 56 // TODO(vakh): Test more fields from the proto.
57 } 57 }
58 } 58 }
59 59
60 std::unique_ptr<V4UpdateProtocolManager> CreateProtocolManager( 60 std::unique_ptr<V4UpdateProtocolManager> CreateProtocolManager(
61 const base::hash_map<UpdateListIdentifier, std::string>
62 current_list_states,
63 const std::vector<ListUpdateResponse>& expected_lurs) { 61 const std::vector<ListUpdateResponse>& expected_lurs) {
64 V4ProtocolConfig config; 62 V4ProtocolConfig config;
65 config.client_name = kClient; 63 config.client_name = kClient;
66 config.version = kAppVer; 64 config.version = kAppVer;
67 config.key_param = kKeyParam; 65 config.key_param = kKeyParam;
68 config.disable_auto_update = false; 66 config.disable_auto_update = false;
69 return V4UpdateProtocolManager::Create( 67 return V4UpdateProtocolManager::Create(
70 NULL, config, current_list_states, 68 NULL, config,
71 base::Bind(&V4UpdateProtocolManagerTest::ValidateGetUpdatesResults, 69 base::Bind(&V4UpdateProtocolManagerTest::ValidateGetUpdatesResults,
72 base::Unretained(this), expected_lurs)); 70 base::Unretained(this), expected_lurs));
73 } 71 }
74 72
75 void SetupCurrentListStates( 73 void SetupStoreStates(StoreStateMap* store_state_map) {
76 base::hash_map<UpdateListIdentifier, std::string>* current_list_states) {
77 UpdateListIdentifier list_identifier; 74 UpdateListIdentifier list_identifier;
78 list_identifier.platform_type = WINDOWS_PLATFORM; 75 list_identifier.platform_type = WINDOWS_PLATFORM;
79 list_identifier.threat_entry_type = URL; 76 list_identifier.threat_entry_type = URL;
80 list_identifier.threat_type = MALWARE_THREAT; 77 list_identifier.threat_type = MALWARE_THREAT;
81 current_list_states->insert({list_identifier, "initial_state_1"}); 78 store_state_map->insert({list_identifier, "initial_state_1"});
82 79
83 list_identifier.platform_type = WINDOWS_PLATFORM; 80 list_identifier.platform_type = WINDOWS_PLATFORM;
84 list_identifier.threat_entry_type = URL; 81 list_identifier.threat_entry_type = URL;
85 list_identifier.threat_type = UNWANTED_SOFTWARE; 82 list_identifier.threat_type = UNWANTED_SOFTWARE;
86 current_list_states->insert({list_identifier, "initial_state_2"}); 83 store_state_map->insert({list_identifier, "initial_state_2"});
87 84
88 list_identifier.platform_type = WINDOWS_PLATFORM; 85 list_identifier.platform_type = WINDOWS_PLATFORM;
89 list_identifier.threat_entry_type = EXECUTABLE; 86 list_identifier.threat_entry_type = EXECUTABLE;
90 list_identifier.threat_type = MALWARE_THREAT; 87 list_identifier.threat_type = MALWARE_THREAT;
91 current_list_states->insert({list_identifier, "initial_state_3"}); 88 store_state_map->insert({list_identifier, "initial_state_3"});
92 } 89 }
93 90
94 void SetupExpectedListUpdateResponse( 91 void SetupExpectedListUpdateResponse(
95 std::vector<ListUpdateResponse>* expected_lurs) { 92 std::vector<ListUpdateResponse>* expected_lurs) {
96 ListUpdateResponse lur; 93 ListUpdateResponse lur;
97 lur.set_platform_type(WINDOWS_PLATFORM); 94 lur.set_platform_type(WINDOWS_PLATFORM);
98 lur.set_response_type(ListUpdateResponse::PARTIAL_UPDATE); 95 lur.set_response_type(ListUpdateResponse::PARTIAL_UPDATE);
99 lur.set_threat_entry_type(URL); 96 lur.set_threat_entry_type(URL);
100 lur.set_threat_type(MALWARE_THREAT); 97 lur.set_threat_type(MALWARE_THREAT);
101 lur.set_new_client_state("new_state_1"); 98 lur.set_new_client_state("new_state_1");
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 135
139 bool expect_callback_to_be_called_; 136 bool expect_callback_to_be_called_;
140 }; 137 };
141 138
142 // TODO(vakh): Add many more tests. 139 // TODO(vakh): Add many more tests.
143 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingNetwork) { 140 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingNetwork) {
144 scoped_refptr<base::TestSimpleTaskRunner> runner( 141 scoped_refptr<base::TestSimpleTaskRunner> runner(
145 new base::TestSimpleTaskRunner()); 142 new base::TestSimpleTaskRunner());
146 base::ThreadTaskRunnerHandle runner_handler(runner); 143 base::ThreadTaskRunnerHandle runner_handler(runner);
147 net::TestURLFetcherFactory factory; 144 net::TestURLFetcherFactory factory;
148 const base::hash_map<UpdateListIdentifier, std::string> current_list_states;
149 const std::vector<ListUpdateResponse> expected_lurs; 145 const std::vector<ListUpdateResponse> expected_lurs;
150 std::unique_ptr<V4UpdateProtocolManager> pm( 146 std::unique_ptr<V4UpdateProtocolManager> pm(
151 CreateProtocolManager(current_list_states, expected_lurs)); 147 CreateProtocolManager(expected_lurs));
152 runner->ClearPendingTasks(); 148 runner->ClearPendingTasks();
153 149
154 // Initial state. No errors. 150 // Initial state. No errors.
155 EXPECT_EQ(0ul, pm->update_error_count_); 151 EXPECT_EQ(0ul, pm->update_error_count_);
156 EXPECT_EQ(1ul, pm->update_back_off_mult_); 152 EXPECT_EQ(1ul, pm->update_back_off_mult_);
157 expect_callback_to_be_called_ = false; 153 expect_callback_to_be_called_ = false;
154 StoreStateMap store_state_map;
155 pm->set_store_state_map(&store_state_map);
158 pm->IssueUpdateRequest(); 156 pm->IssueUpdateRequest();
159 157
160 EXPECT_FALSE(pm->IsUpdateScheduled()); 158 EXPECT_FALSE(pm->IsUpdateScheduled());
161 159
162 runner->RunPendingTasks(); 160 runner->RunPendingTasks();
163 161
164 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 162 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
165 DCHECK(fetcher); 163 DCHECK(fetcher);
166 // Failed request status should result in error. 164 // Failed request status should result in error.
167 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, 165 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
168 net::ERR_CONNECTION_RESET)); 166 net::ERR_CONNECTION_RESET));
169 fetcher->delegate()->OnURLFetchComplete(fetcher); 167 fetcher->delegate()->OnURLFetchComplete(fetcher);
170 168
171 // Should have recorded one error, but back off multiplier is unchanged. 169 // Should have recorded one error, but back off multiplier is unchanged.
172 EXPECT_EQ(1ul, pm->update_error_count_); 170 EXPECT_EQ(1ul, pm->update_error_count_);
173 EXPECT_EQ(1ul, pm->update_back_off_mult_); 171 EXPECT_EQ(1ul, pm->update_back_off_mult_);
174 EXPECT_TRUE(pm->IsUpdateScheduled()); 172 EXPECT_TRUE(pm->IsUpdateScheduled());
175 } 173 }
176 174
177 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingResponseCode) { 175 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingResponseCode) {
178 scoped_refptr<base::TestSimpleTaskRunner> runner( 176 scoped_refptr<base::TestSimpleTaskRunner> runner(
179 new base::TestSimpleTaskRunner()); 177 new base::TestSimpleTaskRunner());
180 base::ThreadTaskRunnerHandle runner_handler(runner); 178 base::ThreadTaskRunnerHandle runner_handler(runner);
181 net::TestURLFetcherFactory factory; 179 net::TestURLFetcherFactory factory;
182 const std::vector<ListUpdateResponse> expected_lurs; 180 const std::vector<ListUpdateResponse> expected_lurs;
183 const base::hash_map<UpdateListIdentifier, std::string> current_list_states;
184 std::unique_ptr<V4UpdateProtocolManager> pm( 181 std::unique_ptr<V4UpdateProtocolManager> pm(
185 CreateProtocolManager(current_list_states, expected_lurs)); 182 CreateProtocolManager(expected_lurs));
186 runner->ClearPendingTasks(); 183 runner->ClearPendingTasks();
187 184
188 // Initial state. No errors. 185 // Initial state. No errors.
189 EXPECT_EQ(0ul, pm->update_error_count_); 186 EXPECT_EQ(0ul, pm->update_error_count_);
190 EXPECT_EQ(1ul, pm->update_back_off_mult_); 187 EXPECT_EQ(1ul, pm->update_back_off_mult_);
191 expect_callback_to_be_called_ = false; 188 expect_callback_to_be_called_ = false;
189 StoreStateMap store_state_map;
190 pm->set_store_state_map(&store_state_map);
192 pm->IssueUpdateRequest(); 191 pm->IssueUpdateRequest();
193 192
194 EXPECT_FALSE(pm->IsUpdateScheduled()); 193 EXPECT_FALSE(pm->IsUpdateScheduled());
195 194
196 runner->RunPendingTasks(); 195 runner->RunPendingTasks();
197 196
198 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 197 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
199 DCHECK(fetcher); 198 DCHECK(fetcher);
200 fetcher->set_status(net::URLRequestStatus()); 199 fetcher->set_status(net::URLRequestStatus());
201 // Response code of anything other than 200 should result in error. 200 // Response code of anything other than 200 should result in error.
202 fetcher->set_response_code(net::HTTP_NO_CONTENT); 201 fetcher->set_response_code(net::HTTP_NO_CONTENT);
203 fetcher->SetResponseString(""); 202 fetcher->SetResponseString("");
204 fetcher->delegate()->OnURLFetchComplete(fetcher); 203 fetcher->delegate()->OnURLFetchComplete(fetcher);
205 204
206 // Should have recorded one error, but back off multiplier is unchanged. 205 // Should have recorded one error, but back off multiplier is unchanged.
207 EXPECT_EQ(1ul, pm->update_error_count_); 206 EXPECT_EQ(1ul, pm->update_error_count_);
208 EXPECT_EQ(1ul, pm->update_back_off_mult_); 207 EXPECT_EQ(1ul, pm->update_back_off_mult_);
209 EXPECT_TRUE(pm->IsUpdateScheduled()); 208 EXPECT_TRUE(pm->IsUpdateScheduled());
210 } 209 }
211 210
212 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesNoError) { 211 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesNoError) {
213 scoped_refptr<base::TestSimpleTaskRunner> runner( 212 scoped_refptr<base::TestSimpleTaskRunner> runner(
214 new base::TestSimpleTaskRunner()); 213 new base::TestSimpleTaskRunner());
215 base::ThreadTaskRunnerHandle runner_handler(runner); 214 base::ThreadTaskRunnerHandle runner_handler(runner);
216 net::TestURLFetcherFactory factory; 215 net::TestURLFetcherFactory factory;
217 std::vector<ListUpdateResponse> expected_lurs; 216 std::vector<ListUpdateResponse> expected_lurs;
218 SetupExpectedListUpdateResponse(&expected_lurs); 217 SetupExpectedListUpdateResponse(&expected_lurs);
219 base::hash_map<UpdateListIdentifier, std::string> current_list_states;
220 SetupCurrentListStates(&current_list_states);
221 std::unique_ptr<V4UpdateProtocolManager> pm( 218 std::unique_ptr<V4UpdateProtocolManager> pm(
222 CreateProtocolManager(current_list_states, expected_lurs)); 219 CreateProtocolManager(expected_lurs));
223 runner->ClearPendingTasks(); 220 runner->ClearPendingTasks();
224 221
225 // Initial state. No errors. 222 // Initial state. No errors.
226 EXPECT_EQ(0ul, pm->update_error_count_); 223 EXPECT_EQ(0ul, pm->update_error_count_);
227 EXPECT_EQ(1ul, pm->update_back_off_mult_); 224 EXPECT_EQ(1ul, pm->update_back_off_mult_);
228 expect_callback_to_be_called_ = true; 225 expect_callback_to_be_called_ = true;
226 StoreStateMap store_state_map;
227 SetupStoreStates(&store_state_map);
228 pm->set_store_state_map(&store_state_map);
229 pm->IssueUpdateRequest(); 229 pm->IssueUpdateRequest();
230 230
231 EXPECT_FALSE(pm->IsUpdateScheduled()); 231 EXPECT_FALSE(pm->IsUpdateScheduled());
232 232
233 runner->RunPendingTasks(); 233 runner->RunPendingTasks();
234 234
235 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 235 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
236 DCHECK(fetcher); 236 DCHECK(fetcher);
237 fetcher->set_status(net::URLRequestStatus()); 237 fetcher->set_status(net::URLRequestStatus());
238 fetcher->set_response_code(net::HTTP_OK); 238 fetcher->set_response_code(net::HTTP_OK);
239 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs)); 239 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs));
240 fetcher->delegate()->OnURLFetchComplete(fetcher); 240 fetcher->delegate()->OnURLFetchComplete(fetcher);
241 241
242 // No error, back off multiplier is unchanged. 242 // No error, back off multiplier is unchanged.
243 EXPECT_EQ(0ul, pm->update_error_count_); 243 EXPECT_EQ(0ul, pm->update_error_count_);
244 EXPECT_EQ(1ul, pm->update_back_off_mult_); 244 EXPECT_EQ(1ul, pm->update_back_off_mult_);
245 EXPECT_FALSE(pm->IsUpdateScheduled()); 245 EXPECT_FALSE(pm->IsUpdateScheduled());
246 } 246 }
247 247
248 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesWithOneBackoff) { 248 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesWithOneBackoff) {
249 scoped_refptr<base::TestSimpleTaskRunner> runner( 249 scoped_refptr<base::TestSimpleTaskRunner> runner(
250 new base::TestSimpleTaskRunner()); 250 new base::TestSimpleTaskRunner());
251 base::ThreadTaskRunnerHandle runner_handler(runner); 251 base::ThreadTaskRunnerHandle runner_handler(runner);
252 net::TestURLFetcherFactory factory; 252 net::TestURLFetcherFactory factory;
253 std::vector<ListUpdateResponse> expected_lurs; 253 std::vector<ListUpdateResponse> expected_lurs;
254 SetupExpectedListUpdateResponse(&expected_lurs); 254 SetupExpectedListUpdateResponse(&expected_lurs);
255 base::hash_map<UpdateListIdentifier, std::string> current_list_states;
256 SetupCurrentListStates(&current_list_states);
257 std::unique_ptr<V4UpdateProtocolManager> pm( 255 std::unique_ptr<V4UpdateProtocolManager> pm(
258 CreateProtocolManager(current_list_states, expected_lurs)); 256 CreateProtocolManager(expected_lurs));
259 runner->ClearPendingTasks(); 257 runner->ClearPendingTasks();
260 258
261 // Initial state. No errors. 259 // Initial state. No errors.
262 EXPECT_EQ(0ul, pm->update_error_count_); 260 EXPECT_EQ(0ul, pm->update_error_count_);
263 EXPECT_EQ(1ul, pm->update_back_off_mult_); 261 EXPECT_EQ(1ul, pm->update_back_off_mult_);
264 expect_callback_to_be_called_ = false; 262 expect_callback_to_be_called_ = false;
263 StoreStateMap store_state_map;
264 SetupStoreStates(&store_state_map);
265 pm->set_store_state_map(&store_state_map);
265 pm->IssueUpdateRequest(); 266 pm->IssueUpdateRequest();
266 267
267 EXPECT_FALSE(pm->IsUpdateScheduled()); 268 EXPECT_FALSE(pm->IsUpdateScheduled());
268 269
269 runner->RunPendingTasks(); 270 runner->RunPendingTasks();
270 271
271 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 272 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
272 DCHECK(fetcher); 273 DCHECK(fetcher);
273 fetcher->set_status(net::URLRequestStatus()); 274 fetcher->set_status(net::URLRequestStatus());
274 // Response code of anything other than 200 should result in error. 275 // Response code of anything other than 200 should result in error.
(...skipping 17 matching lines...) Expand all
292 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs)); 293 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs));
293 fetcher->delegate()->OnURLFetchComplete(fetcher); 294 fetcher->delegate()->OnURLFetchComplete(fetcher);
294 295
295 // No error, back off multiplier is unchanged. 296 // No error, back off multiplier is unchanged.
296 EXPECT_EQ(0ul, pm->update_error_count_); 297 EXPECT_EQ(0ul, pm->update_error_count_);
297 EXPECT_EQ(1ul, pm->update_back_off_mult_); 298 EXPECT_EQ(1ul, pm->update_back_off_mult_);
298 EXPECT_FALSE(pm->IsUpdateScheduled()); 299 EXPECT_FALSE(pm->IsUpdateScheduled());
299 } 300 }
300 301
301 } // namespace safe_browsing 302 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698