| 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 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 | 156 |
| 157 // Initial state. No errors. | 157 // Initial state. No errors. |
| 158 EXPECT_EQ(0ul, pm->update_error_count_); | 158 EXPECT_EQ(0ul, pm->update_error_count_); |
| 159 EXPECT_EQ(1ul, pm->update_back_off_mult_); | 159 EXPECT_EQ(1ul, pm->update_back_off_mult_); |
| 160 expect_callback_to_be_called_ = false; | 160 expect_callback_to_be_called_ = false; |
| 161 pm->store_state_map_ = std::move(store_state_map_); | 161 pm->store_state_map_ = std::move(store_state_map_); |
| 162 pm->IssueUpdateRequest(); | 162 pm->IssueUpdateRequest(); |
| 163 | 163 |
| 164 EXPECT_FALSE(pm->IsUpdateScheduled()); | 164 EXPECT_FALSE(pm->IsUpdateScheduled()); |
| 165 | 165 |
| 166 runner->RunPendingTasks(); | |
| 167 | |
| 168 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 166 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 169 DCHECK(fetcher); | 167 DCHECK(fetcher); |
| 170 // Failed request status should result in error. | 168 // Failed request status should result in error. |
| 171 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 169 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 172 net::ERR_CONNECTION_RESET)); | 170 net::ERR_CONNECTION_RESET)); |
| 173 fetcher->delegate()->OnURLFetchComplete(fetcher); | 171 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 174 | 172 |
| 175 // Should have recorded one error, but back off multiplier is unchanged. | 173 // Should have recorded one error, but back off multiplier is unchanged. |
| 176 EXPECT_EQ(1ul, pm->update_error_count_); | 174 EXPECT_EQ(1ul, pm->update_error_count_); |
| 177 EXPECT_EQ(1ul, pm->update_back_off_mult_); | 175 EXPECT_EQ(1ul, pm->update_back_off_mult_); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 190 | 188 |
| 191 // Initial state. No errors. | 189 // Initial state. No errors. |
| 192 EXPECT_EQ(0ul, pm->update_error_count_); | 190 EXPECT_EQ(0ul, pm->update_error_count_); |
| 193 EXPECT_EQ(1ul, pm->update_back_off_mult_); | 191 EXPECT_EQ(1ul, pm->update_back_off_mult_); |
| 194 expect_callback_to_be_called_ = false; | 192 expect_callback_to_be_called_ = false; |
| 195 pm->store_state_map_ = std::move(store_state_map_); | 193 pm->store_state_map_ = std::move(store_state_map_); |
| 196 pm->IssueUpdateRequest(); | 194 pm->IssueUpdateRequest(); |
| 197 | 195 |
| 198 EXPECT_FALSE(pm->IsUpdateScheduled()); | 196 EXPECT_FALSE(pm->IsUpdateScheduled()); |
| 199 | 197 |
| 200 runner->RunPendingTasks(); | |
| 201 | |
| 202 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 198 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 203 DCHECK(fetcher); | 199 DCHECK(fetcher); |
| 204 fetcher->set_status(net::URLRequestStatus()); | 200 fetcher->set_status(net::URLRequestStatus()); |
| 205 // Response code of anything other than 200 should result in error. | 201 // Response code of anything other than 200 should result in error. |
| 206 fetcher->set_response_code(net::HTTP_NO_CONTENT); | 202 fetcher->set_response_code(net::HTTP_NO_CONTENT); |
| 207 fetcher->SetResponseString(""); | 203 fetcher->SetResponseString(""); |
| 208 fetcher->delegate()->OnURLFetchComplete(fetcher); | 204 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 209 | 205 |
| 210 // Should have recorded one error, but back off multiplier is unchanged. | 206 // Should have recorded one error, but back off multiplier is unchanged. |
| 211 EXPECT_EQ(1ul, pm->update_error_count_); | 207 EXPECT_EQ(1ul, pm->update_error_count_); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 226 | 222 |
| 227 // Initial state. No errors. | 223 // Initial state. No errors. |
| 228 EXPECT_EQ(0ul, pm->update_error_count_); | 224 EXPECT_EQ(0ul, pm->update_error_count_); |
| 229 EXPECT_EQ(1ul, pm->update_back_off_mult_); | 225 EXPECT_EQ(1ul, pm->update_back_off_mult_); |
| 230 expect_callback_to_be_called_ = true; | 226 expect_callback_to_be_called_ = true; |
| 231 pm->store_state_map_ = std::move(store_state_map_); | 227 pm->store_state_map_ = std::move(store_state_map_); |
| 232 pm->IssueUpdateRequest(); | 228 pm->IssueUpdateRequest(); |
| 233 | 229 |
| 234 EXPECT_FALSE(pm->IsUpdateScheduled()); | 230 EXPECT_FALSE(pm->IsUpdateScheduled()); |
| 235 | 231 |
| 236 runner->RunPendingTasks(); | |
| 237 | |
| 238 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 232 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 239 DCHECK(fetcher); | 233 DCHECK(fetcher); |
| 240 fetcher->set_status(net::URLRequestStatus()); | 234 fetcher->set_status(net::URLRequestStatus()); |
| 241 fetcher->set_response_code(net::HTTP_OK); | 235 fetcher->set_response_code(net::HTTP_OK); |
| 242 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs)); | 236 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs)); |
| 243 fetcher->delegate()->OnURLFetchComplete(fetcher); | 237 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 244 | 238 |
| 245 // No error, back off multiplier is unchanged. | 239 // No error, back off multiplier is unchanged. |
| 246 EXPECT_EQ(0ul, pm->update_error_count_); | 240 EXPECT_EQ(0ul, pm->update_error_count_); |
| 247 EXPECT_EQ(1ul, pm->update_back_off_mult_); | 241 EXPECT_EQ(1ul, pm->update_back_off_mult_); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 261 | 255 |
| 262 // Initial state. No errors. | 256 // Initial state. No errors. |
| 263 EXPECT_EQ(0ul, pm->update_error_count_); | 257 EXPECT_EQ(0ul, pm->update_error_count_); |
| 264 EXPECT_EQ(1ul, pm->update_back_off_mult_); | 258 EXPECT_EQ(1ul, pm->update_back_off_mult_); |
| 265 expect_callback_to_be_called_ = false; | 259 expect_callback_to_be_called_ = false; |
| 266 pm->store_state_map_ = std::move(store_state_map_); | 260 pm->store_state_map_ = std::move(store_state_map_); |
| 267 pm->IssueUpdateRequest(); | 261 pm->IssueUpdateRequest(); |
| 268 | 262 |
| 269 EXPECT_FALSE(pm->IsUpdateScheduled()); | 263 EXPECT_FALSE(pm->IsUpdateScheduled()); |
| 270 | 264 |
| 271 runner->RunPendingTasks(); | |
| 272 | |
| 273 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 265 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 274 DCHECK(fetcher); | 266 DCHECK(fetcher); |
| 275 fetcher->set_status(net::URLRequestStatus()); | 267 fetcher->set_status(net::URLRequestStatus()); |
| 276 // Response code of anything other than 200 should result in error. | 268 // Response code of anything other than 200 should result in error. |
| 277 fetcher->set_response_code(net::HTTP_NO_CONTENT); | 269 fetcher->set_response_code(net::HTTP_NO_CONTENT); |
| 278 fetcher->SetResponseString(""); | 270 fetcher->SetResponseString(""); |
| 279 fetcher->delegate()->OnURLFetchComplete(fetcher); | 271 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 280 | 272 |
| 281 // Should have recorded one error, but back off multiplier is unchanged. | 273 // Should have recorded one error, but back off multiplier is unchanged. |
| 282 EXPECT_EQ(1ul, pm->update_error_count_); | 274 EXPECT_EQ(1ul, pm->update_error_count_); |
| 283 EXPECT_EQ(1ul, pm->update_back_off_mult_); | 275 EXPECT_EQ(1ul, pm->update_back_off_mult_); |
| 284 EXPECT_TRUE(pm->IsUpdateScheduled()); | 276 EXPECT_TRUE(pm->IsUpdateScheduled()); |
| 285 | 277 |
| 286 // Retry, now no backoff. | 278 // Retry, now no backoff. |
| 287 expect_callback_to_be_called_ = true; | 279 expect_callback_to_be_called_ = true; |
| 280 // Call RunPendingTasks to ensure that the request is sent after backoff. |
| 288 runner->RunPendingTasks(); | 281 runner->RunPendingTasks(); |
| 289 | 282 |
| 290 fetcher = factory.GetFetcherByID(1); | 283 fetcher = factory.GetFetcherByID(1); |
| 291 DCHECK(fetcher); | 284 DCHECK(fetcher); |
| 292 fetcher->set_status(net::URLRequestStatus()); | 285 fetcher->set_status(net::URLRequestStatus()); |
| 293 fetcher->set_response_code(net::HTTP_OK); | 286 fetcher->set_response_code(net::HTTP_OK); |
| 294 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs)); | 287 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs)); |
| 295 fetcher->delegate()->OnURLFetchComplete(fetcher); | 288 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 296 | 289 |
| 297 // No error, back off multiplier is unchanged. | 290 // No error, back off multiplier is unchanged. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 313 | 306 |
| 314 std::string encoded_request_with_minus = | 307 std::string encoded_request_with_minus = |
| 315 pm->GetBase64SerializedUpdateRequestProto(); | 308 pm->GetBase64SerializedUpdateRequestProto(); |
| 316 EXPECT_EQ("Cg8KCHVuaXR0ZXN0EgMxLjAaGAgBEAIaCmg4eGZZcVk-OlIiBCABIAIoAQ==", | 309 EXPECT_EQ("Cg8KCHVuaXR0ZXN0EgMxLjAaGAgBEAIaCmg4eGZZcVk-OlIiBCABIAIoAQ==", |
| 317 encoded_request_with_minus); | 310 encoded_request_with_minus); |
| 318 | 311 |
| 319 // TODO(vakh): Add a similar test for underscore for completeness, although | 312 // TODO(vakh): Add a similar test for underscore for completeness, although |
| 320 // the '-' case is sufficient to prove that we are using URL encoding. | 313 // the '-' case is sufficient to prove that we are using URL encoding. |
| 321 } | 314 } |
| 322 | 315 |
| 316 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesHasTimeout) { |
| 317 scoped_refptr<base::TestSimpleTaskRunner> runner( |
| 318 new base::TestSimpleTaskRunner()); |
| 319 base::ThreadTaskRunnerHandle runner_handler(runner); |
| 320 net::TestURLFetcherFactory factory; |
| 321 std::vector<ListUpdateResponse> expected_lurs; |
| 322 SetupExpectedListUpdateResponse(&expected_lurs); |
| 323 std::unique_ptr<V4UpdateProtocolManager> pm( |
| 324 CreateProtocolManager(expected_lurs)); |
| 325 runner->ClearPendingTasks(); |
| 326 |
| 327 // Initial state. No errors. |
| 328 EXPECT_EQ(0ul, pm->update_error_count_); |
| 329 EXPECT_EQ(1ul, pm->update_back_off_mult_); |
| 330 expect_callback_to_be_called_ = true; |
| 331 pm->store_state_map_ = std::move(store_state_map_); |
| 332 pm->IssueUpdateRequest(); |
| 333 |
| 334 net::TestURLFetcher* timeout_fetcher = factory.GetFetcherByID(0); |
| 335 DCHECK(timeout_fetcher); |
| 336 // Don't set anything on the fetcher. Let it time out. |
| 337 runner->RunPendingTasks(); |
| 338 |
| 339 // There should be another fetcher now. |
| 340 net::TestURLFetcher* fetcher = factory.GetFetcherByID(1); |
| 341 DCHECK(fetcher); |
| 342 fetcher->set_status(net::URLRequestStatus()); |
| 343 fetcher->set_response_code(net::HTTP_OK); |
| 344 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs)); |
| 345 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 346 |
| 347 // No error, back off multiplier is unchanged. |
| 348 EXPECT_EQ(0ul, pm->update_error_count_); |
| 349 EXPECT_EQ(1ul, pm->update_back_off_mult_); |
| 350 } |
| 351 |
| 323 } // namespace safe_browsing | 352 } // namespace safe_browsing |
| OLD | NEW |