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

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: Nits: Added some comments in BUILD.gn. Using #else for platform_type until I resolve the android bu… 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() {
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 28 matching lines...) Expand all
130 } 127 }
131 128
132 // Serialize. 129 // Serialize.
133 std::string res_data; 130 std::string res_data;
134 response.SerializeToString(&res_data); 131 response.SerializeToString(&res_data);
135 132
136 return res_data; 133 return res_data;
137 } 134 }
138 135
139 bool expect_callback_to_be_called_; 136 bool expect_callback_to_be_called_;
137 StoreStateMap store_state_map_;
138
140 }; 139 };
141 140
142 // TODO(vakh): Add many more tests. 141 // TODO(vakh): Add many more tests.
143 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingNetwork) { 142 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingNetwork) {
144 scoped_refptr<base::TestSimpleTaskRunner> runner( 143 scoped_refptr<base::TestSimpleTaskRunner> runner(
145 new base::TestSimpleTaskRunner()); 144 new base::TestSimpleTaskRunner());
146 base::ThreadTaskRunnerHandle runner_handler(runner); 145 base::ThreadTaskRunnerHandle runner_handler(runner);
147 net::TestURLFetcherFactory factory; 146 net::TestURLFetcherFactory factory;
148 const base::hash_map<UpdateListIdentifier, std::string> current_list_states;
149 const std::vector<ListUpdateResponse> expected_lurs; 147 const std::vector<ListUpdateResponse> expected_lurs;
150 std::unique_ptr<V4UpdateProtocolManager> pm( 148 std::unique_ptr<V4UpdateProtocolManager> pm(
151 CreateProtocolManager(current_list_states, expected_lurs)); 149 CreateProtocolManager(expected_lurs));
152 runner->ClearPendingTasks(); 150 runner->ClearPendingTasks();
153 151
154 // Initial state. No errors. 152 // Initial state. No errors.
155 EXPECT_EQ(0ul, pm->update_error_count_); 153 EXPECT_EQ(0ul, pm->update_error_count_);
156 EXPECT_EQ(1ul, pm->update_back_off_mult_); 154 EXPECT_EQ(1ul, pm->update_back_off_mult_);
157 expect_callback_to_be_called_ = false; 155 expect_callback_to_be_called_ = false;
156 pm->SetStoreStateMap(&store_state_map_);
158 pm->IssueUpdateRequest(); 157 pm->IssueUpdateRequest();
159 158
160 EXPECT_FALSE(pm->IsUpdateScheduled()); 159 EXPECT_FALSE(pm->IsUpdateScheduled());
161 160
162 runner->RunPendingTasks(); 161 runner->RunPendingTasks();
163 162
164 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 163 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
165 DCHECK(fetcher); 164 DCHECK(fetcher);
166 // Failed request status should result in error. 165 // Failed request status should result in error.
167 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, 166 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
168 net::ERR_CONNECTION_RESET)); 167 net::ERR_CONNECTION_RESET));
169 fetcher->delegate()->OnURLFetchComplete(fetcher); 168 fetcher->delegate()->OnURLFetchComplete(fetcher);
170 169
171 // Should have recorded one error, but back off multiplier is unchanged. 170 // Should have recorded one error, but back off multiplier is unchanged.
172 EXPECT_EQ(1ul, pm->update_error_count_); 171 EXPECT_EQ(1ul, pm->update_error_count_);
173 EXPECT_EQ(1ul, pm->update_back_off_mult_); 172 EXPECT_EQ(1ul, pm->update_back_off_mult_);
174 EXPECT_TRUE(pm->IsUpdateScheduled()); 173 EXPECT_TRUE(pm->IsUpdateScheduled());
175 } 174 }
176 175
177 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingResponseCode) { 176 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingResponseCode) {
178 scoped_refptr<base::TestSimpleTaskRunner> runner( 177 scoped_refptr<base::TestSimpleTaskRunner> runner(
179 new base::TestSimpleTaskRunner()); 178 new base::TestSimpleTaskRunner());
180 base::ThreadTaskRunnerHandle runner_handler(runner); 179 base::ThreadTaskRunnerHandle runner_handler(runner);
181 net::TestURLFetcherFactory factory; 180 net::TestURLFetcherFactory factory;
182 const std::vector<ListUpdateResponse> expected_lurs; 181 const std::vector<ListUpdateResponse> expected_lurs;
183 const base::hash_map<UpdateListIdentifier, std::string> current_list_states;
184 std::unique_ptr<V4UpdateProtocolManager> pm( 182 std::unique_ptr<V4UpdateProtocolManager> pm(
185 CreateProtocolManager(current_list_states, expected_lurs)); 183 CreateProtocolManager(expected_lurs));
186 runner->ClearPendingTasks(); 184 runner->ClearPendingTasks();
187 185
188 // Initial state. No errors. 186 // Initial state. No errors.
189 EXPECT_EQ(0ul, pm->update_error_count_); 187 EXPECT_EQ(0ul, pm->update_error_count_);
190 EXPECT_EQ(1ul, pm->update_back_off_mult_); 188 EXPECT_EQ(1ul, pm->update_back_off_mult_);
191 expect_callback_to_be_called_ = false; 189 expect_callback_to_be_called_ = false;
190 pm->SetStoreStateMap(&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 SetupStoreStates();
227 pm->SetStoreStateMap(&store_state_map_);
229 pm->IssueUpdateRequest(); 228 pm->IssueUpdateRequest();
230 229
231 EXPECT_FALSE(pm->IsUpdateScheduled()); 230 EXPECT_FALSE(pm->IsUpdateScheduled());
232 231
233 runner->RunPendingTasks(); 232 runner->RunPendingTasks();
234 233
235 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 234 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
236 DCHECK(fetcher); 235 DCHECK(fetcher);
237 fetcher->set_status(net::URLRequestStatus()); 236 fetcher->set_status(net::URLRequestStatus());
238 fetcher->set_response_code(net::HTTP_OK); 237 fetcher->set_response_code(net::HTTP_OK);
239 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs)); 238 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs));
240 fetcher->delegate()->OnURLFetchComplete(fetcher); 239 fetcher->delegate()->OnURLFetchComplete(fetcher);
241 240
242 // No error, back off multiplier is unchanged. 241 // No error, back off multiplier is unchanged.
243 EXPECT_EQ(0ul, pm->update_error_count_); 242 EXPECT_EQ(0ul, pm->update_error_count_);
244 EXPECT_EQ(1ul, pm->update_back_off_mult_); 243 EXPECT_EQ(1ul, pm->update_back_off_mult_);
245 EXPECT_FALSE(pm->IsUpdateScheduled()); 244 EXPECT_FALSE(pm->IsUpdateScheduled());
246 } 245 }
247 246
248 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesWithOneBackoff) { 247 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesWithOneBackoff) {
249 scoped_refptr<base::TestSimpleTaskRunner> runner( 248 scoped_refptr<base::TestSimpleTaskRunner> runner(
250 new base::TestSimpleTaskRunner()); 249 new base::TestSimpleTaskRunner());
251 base::ThreadTaskRunnerHandle runner_handler(runner); 250 base::ThreadTaskRunnerHandle runner_handler(runner);
252 net::TestURLFetcherFactory factory; 251 net::TestURLFetcherFactory factory;
253 std::vector<ListUpdateResponse> expected_lurs; 252 std::vector<ListUpdateResponse> expected_lurs;
254 SetupExpectedListUpdateResponse(&expected_lurs); 253 SetupExpectedListUpdateResponse(&expected_lurs);
255 base::hash_map<UpdateListIdentifier, std::string> current_list_states;
256 SetupCurrentListStates(&current_list_states);
257 std::unique_ptr<V4UpdateProtocolManager> pm( 254 std::unique_ptr<V4UpdateProtocolManager> pm(
258 CreateProtocolManager(current_list_states, expected_lurs)); 255 CreateProtocolManager(expected_lurs));
259 runner->ClearPendingTasks(); 256 runner->ClearPendingTasks();
260 257
261 // Initial state. No errors. 258 // Initial state. No errors.
262 EXPECT_EQ(0ul, pm->update_error_count_); 259 EXPECT_EQ(0ul, pm->update_error_count_);
263 EXPECT_EQ(1ul, pm->update_back_off_mult_); 260 EXPECT_EQ(1ul, pm->update_back_off_mult_);
264 expect_callback_to_be_called_ = false; 261 expect_callback_to_be_called_ = false;
262 SetupStoreStates();
263 pm->SetStoreStateMap(&store_state_map_);
265 pm->IssueUpdateRequest(); 264 pm->IssueUpdateRequest();
266 265
267 EXPECT_FALSE(pm->IsUpdateScheduled()); 266 EXPECT_FALSE(pm->IsUpdateScheduled());
268 267
269 runner->RunPendingTasks(); 268 runner->RunPendingTasks();
270 269
271 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 270 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
272 DCHECK(fetcher); 271 DCHECK(fetcher);
273 fetcher->set_status(net::URLRequestStatus()); 272 fetcher->set_status(net::URLRequestStatus());
274 // Response code of anything other than 200 should result in error. 273 // Response code of anything other than 200 should result in error.
(...skipping 17 matching lines...) Expand all
292 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs)); 291 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs));
293 fetcher->delegate()->OnURLFetchComplete(fetcher); 292 fetcher->delegate()->OnURLFetchComplete(fetcher);
294 293
295 // No error, back off multiplier is unchanged. 294 // No error, back off multiplier is unchanged.
296 EXPECT_EQ(0ul, pm->update_error_count_); 295 EXPECT_EQ(0ul, pm->update_error_count_);
297 EXPECT_EQ(1ul, pm->update_back_off_mult_); 296 EXPECT_EQ(1ul, pm->update_back_off_mult_);
298 EXPECT_FALSE(pm->IsUpdateScheduled()); 297 EXPECT_FALSE(pm->IsUpdateScheduled());
299 } 298 }
300 299
301 } // namespace safe_browsing 300 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698