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 "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 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 | 141 |
142 // Initial state. No errors. | 142 // Initial state. No errors. |
143 EXPECT_EQ(0ul, pm->update_error_count_); | 143 EXPECT_EQ(0ul, pm->update_error_count_); |
144 EXPECT_EQ(1ul, pm->update_back_off_mult_); | 144 EXPECT_EQ(1ul, pm->update_back_off_mult_); |
145 expect_callback_to_be_called_ = false; | 145 expect_callback_to_be_called_ = false; |
146 pm->store_state_map_ = std::move(store_state_map_); | 146 pm->store_state_map_ = std::move(store_state_map_); |
147 pm->IssueUpdateRequest(); | 147 pm->IssueUpdateRequest(); |
148 | 148 |
149 EXPECT_FALSE(pm->IsUpdateScheduled()); | 149 EXPECT_FALSE(pm->IsUpdateScheduled()); |
150 | 150 |
151 runner->RunPendingTasks(); | |
152 | |
153 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 151 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
154 DCHECK(fetcher); | 152 DCHECK(fetcher); |
155 // Failed request status should result in error. | 153 // Failed request status should result in error. |
156 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 154 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
157 net::ERR_CONNECTION_RESET)); | 155 net::ERR_CONNECTION_RESET)); |
158 fetcher->delegate()->OnURLFetchComplete(fetcher); | 156 fetcher->delegate()->OnURLFetchComplete(fetcher); |
159 | 157 |
160 // Should have recorded one error, but back off multiplier is unchanged. | 158 // Should have recorded one error, but back off multiplier is unchanged. |
161 EXPECT_EQ(1ul, pm->update_error_count_); | 159 EXPECT_EQ(1ul, pm->update_error_count_); |
162 EXPECT_EQ(1ul, pm->update_back_off_mult_); | 160 EXPECT_EQ(1ul, pm->update_back_off_mult_); |
(...skipping 12 matching lines...) Expand all Loading... |
175 | 173 |
176 // Initial state. No errors. | 174 // Initial state. No errors. |
177 EXPECT_EQ(0ul, pm->update_error_count_); | 175 EXPECT_EQ(0ul, pm->update_error_count_); |
178 EXPECT_EQ(1ul, pm->update_back_off_mult_); | 176 EXPECT_EQ(1ul, pm->update_back_off_mult_); |
179 expect_callback_to_be_called_ = false; | 177 expect_callback_to_be_called_ = false; |
180 pm->store_state_map_ = std::move(store_state_map_); | 178 pm->store_state_map_ = std::move(store_state_map_); |
181 pm->IssueUpdateRequest(); | 179 pm->IssueUpdateRequest(); |
182 | 180 |
183 EXPECT_FALSE(pm->IsUpdateScheduled()); | 181 EXPECT_FALSE(pm->IsUpdateScheduled()); |
184 | 182 |
185 runner->RunPendingTasks(); | |
186 | |
187 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 183 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
188 DCHECK(fetcher); | 184 DCHECK(fetcher); |
189 fetcher->set_status(net::URLRequestStatus()); | 185 fetcher->set_status(net::URLRequestStatus()); |
190 // Response code of anything other than 200 should result in error. | 186 // Response code of anything other than 200 should result in error. |
191 fetcher->set_response_code(net::HTTP_NO_CONTENT); | 187 fetcher->set_response_code(net::HTTP_NO_CONTENT); |
192 fetcher->SetResponseString(""); | 188 fetcher->SetResponseString(""); |
193 fetcher->delegate()->OnURLFetchComplete(fetcher); | 189 fetcher->delegate()->OnURLFetchComplete(fetcher); |
194 | 190 |
195 // Should have recorded one error, but back off multiplier is unchanged. | 191 // Should have recorded one error, but back off multiplier is unchanged. |
196 EXPECT_EQ(1ul, pm->update_error_count_); | 192 EXPECT_EQ(1ul, pm->update_error_count_); |
(...skipping 14 matching lines...) Expand all Loading... |
211 | 207 |
212 // Initial state. No errors. | 208 // Initial state. No errors. |
213 EXPECT_EQ(0ul, pm->update_error_count_); | 209 EXPECT_EQ(0ul, pm->update_error_count_); |
214 EXPECT_EQ(1ul, pm->update_back_off_mult_); | 210 EXPECT_EQ(1ul, pm->update_back_off_mult_); |
215 expect_callback_to_be_called_ = true; | 211 expect_callback_to_be_called_ = true; |
216 pm->store_state_map_ = std::move(store_state_map_); | 212 pm->store_state_map_ = std::move(store_state_map_); |
217 pm->IssueUpdateRequest(); | 213 pm->IssueUpdateRequest(); |
218 | 214 |
219 EXPECT_FALSE(pm->IsUpdateScheduled()); | 215 EXPECT_FALSE(pm->IsUpdateScheduled()); |
220 | 216 |
221 runner->RunPendingTasks(); | |
222 | |
223 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 217 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
224 DCHECK(fetcher); | 218 DCHECK(fetcher); |
225 fetcher->set_status(net::URLRequestStatus()); | 219 fetcher->set_status(net::URLRequestStatus()); |
226 fetcher->set_response_code(net::HTTP_OK); | 220 fetcher->set_response_code(net::HTTP_OK); |
227 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs)); | 221 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs)); |
228 fetcher->delegate()->OnURLFetchComplete(fetcher); | 222 fetcher->delegate()->OnURLFetchComplete(fetcher); |
229 | 223 |
230 // No error, back off multiplier is unchanged. | 224 // No error, back off multiplier is unchanged. |
231 EXPECT_EQ(0ul, pm->update_error_count_); | 225 EXPECT_EQ(0ul, pm->update_error_count_); |
232 EXPECT_EQ(1ul, pm->update_back_off_mult_); | 226 EXPECT_EQ(1ul, pm->update_back_off_mult_); |
(...skipping 13 matching lines...) Expand all Loading... |
246 | 240 |
247 // Initial state. No errors. | 241 // Initial state. No errors. |
248 EXPECT_EQ(0ul, pm->update_error_count_); | 242 EXPECT_EQ(0ul, pm->update_error_count_); |
249 EXPECT_EQ(1ul, pm->update_back_off_mult_); | 243 EXPECT_EQ(1ul, pm->update_back_off_mult_); |
250 expect_callback_to_be_called_ = false; | 244 expect_callback_to_be_called_ = false; |
251 pm->store_state_map_ = std::move(store_state_map_); | 245 pm->store_state_map_ = std::move(store_state_map_); |
252 pm->IssueUpdateRequest(); | 246 pm->IssueUpdateRequest(); |
253 | 247 |
254 EXPECT_FALSE(pm->IsUpdateScheduled()); | 248 EXPECT_FALSE(pm->IsUpdateScheduled()); |
255 | 249 |
256 runner->RunPendingTasks(); | |
257 | |
258 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 250 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
259 DCHECK(fetcher); | 251 DCHECK(fetcher); |
260 fetcher->set_status(net::URLRequestStatus()); | 252 fetcher->set_status(net::URLRequestStatus()); |
261 // Response code of anything other than 200 should result in error. | 253 // Response code of anything other than 200 should result in error. |
262 fetcher->set_response_code(net::HTTP_NO_CONTENT); | 254 fetcher->set_response_code(net::HTTP_NO_CONTENT); |
263 fetcher->SetResponseString(""); | 255 fetcher->SetResponseString(""); |
264 fetcher->delegate()->OnURLFetchComplete(fetcher); | 256 fetcher->delegate()->OnURLFetchComplete(fetcher); |
265 | 257 |
266 // Should have recorded one error, but back off multiplier is unchanged. | 258 // Should have recorded one error, but back off multiplier is unchanged. |
267 EXPECT_EQ(1ul, pm->update_error_count_); | 259 EXPECT_EQ(1ul, pm->update_error_count_); |
268 EXPECT_EQ(1ul, pm->update_back_off_mult_); | 260 EXPECT_EQ(1ul, pm->update_back_off_mult_); |
269 EXPECT_TRUE(pm->IsUpdateScheduled()); | 261 EXPECT_TRUE(pm->IsUpdateScheduled()); |
270 | 262 |
271 // Retry, now no backoff. | 263 // Retry, now no backoff. |
272 expect_callback_to_be_called_ = true; | 264 expect_callback_to_be_called_ = true; |
| 265 // Call RunPendingTasks to ensure that the request is sent after backoff. |
273 runner->RunPendingTasks(); | 266 runner->RunPendingTasks(); |
274 | 267 |
275 fetcher = factory.GetFetcherByID(1); | 268 fetcher = factory.GetFetcherByID(1); |
276 DCHECK(fetcher); | 269 DCHECK(fetcher); |
277 fetcher->set_status(net::URLRequestStatus()); | 270 fetcher->set_status(net::URLRequestStatus()); |
278 fetcher->set_response_code(net::HTTP_OK); | 271 fetcher->set_response_code(net::HTTP_OK); |
279 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs)); | 272 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs)); |
280 fetcher->delegate()->OnURLFetchComplete(fetcher); | 273 fetcher->delegate()->OnURLFetchComplete(fetcher); |
281 | 274 |
282 // No error, back off multiplier is unchanged. | 275 // No error, back off multiplier is unchanged. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 pm->ScheduleNextUpdate(std::move(store_state_map_)); | 310 pm->ScheduleNextUpdate(std::move(store_state_map_)); |
318 EXPECT_FALSE(pm->IsUpdateScheduled()); | 311 EXPECT_FALSE(pm->IsUpdateScheduled()); |
319 | 312 |
320 runner->RunPendingTasks(); | 313 runner->RunPendingTasks(); |
321 EXPECT_FALSE(pm->IsUpdateScheduled()); | 314 EXPECT_FALSE(pm->IsUpdateScheduled()); |
322 | 315 |
323 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 316 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
324 DCHECK(!fetcher); | 317 DCHECK(!fetcher); |
325 } | 318 } |
326 | 319 |
| 320 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesHasTimeout) { |
| 321 scoped_refptr<base::TestSimpleTaskRunner> runner( |
| 322 new base::TestSimpleTaskRunner()); |
| 323 base::ThreadTaskRunnerHandle runner_handler(runner); |
| 324 net::TestURLFetcherFactory factory; |
| 325 std::vector<ListUpdateResponse> expected_lurs; |
| 326 SetupExpectedListUpdateResponse(&expected_lurs); |
| 327 std::unique_ptr<V4UpdateProtocolManager> pm( |
| 328 CreateProtocolManager(expected_lurs)); |
| 329 runner->ClearPendingTasks(); |
| 330 |
| 331 // Initial state. No errors. |
| 332 EXPECT_EQ(0ul, pm->update_error_count_); |
| 333 EXPECT_EQ(1ul, pm->update_back_off_mult_); |
| 334 expect_callback_to_be_called_ = true; |
| 335 pm->store_state_map_ = std::move(store_state_map_); |
| 336 pm->IssueUpdateRequest(); |
| 337 |
| 338 net::TestURLFetcher* timeout_fetcher = factory.GetFetcherByID(0); |
| 339 DCHECK(timeout_fetcher); |
| 340 // Don't set anything on the fetcher. Let it time out. |
| 341 runner->RunPendingTasks(); |
| 342 |
| 343 net::TestURLFetcher* fetcher = factory.GetFetcherByID(1); |
| 344 DCHECK(!fetcher); |
| 345 // Now wait for the next request to be scheduled. |
| 346 runner->RunPendingTasks(); |
| 347 |
| 348 // There should be another fetcher now. |
| 349 fetcher = factory.GetFetcherByID(1); |
| 350 DCHECK(fetcher); |
| 351 fetcher->set_status(net::URLRequestStatus()); |
| 352 fetcher->set_response_code(net::HTTP_OK); |
| 353 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs)); |
| 354 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 355 |
| 356 // No error, back off multiplier is unchanged. |
| 357 EXPECT_EQ(0ul, pm->update_error_count_); |
| 358 EXPECT_EQ(1ul, pm->update_back_off_mult_); |
| 359 } |
| 360 |
327 } // namespace safe_browsing | 361 } // namespace safe_browsing |
OLD | NEW |