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

Side by Side Diff: chrome/browser/safe_browsing/protocol_manager_unittest.cc

Issue 11419041: Add tests for redirect responses from SafeBrowsingProtocolManager. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Free chunks in test Created 8 years 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/safe_browsing/protocol_manager.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 5
6 #include "base/logging.h" 6 #include "base/logging.h"
7 #include "base/single_thread_task_runner.h" 7 #include "base/single_thread_task_runner.h"
8 #include "base/stringprintf.h" 8 #include "base/stringprintf.h"
9 #include "base/thread_task_runner_handle.h" 9 #include "base/thread_task_runner_handle.h"
10 #include "base/time.h" 10 #include "base/time.h"
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 246
247 class MockProtocolDelegate : public SafeBrowsingProtocolManagerDelegate { 247 class MockProtocolDelegate : public SafeBrowsingProtocolManagerDelegate {
248 public: 248 public:
249 MockProtocolDelegate() {} 249 MockProtocolDelegate() {}
250 virtual ~MockProtocolDelegate() {} 250 virtual ~MockProtocolDelegate() {}
251 251
252 MOCK_METHOD0(UpdateStarted, void()); 252 MOCK_METHOD0(UpdateStarted, void());
253 MOCK_METHOD1(UpdateFinished, void(bool)); 253 MOCK_METHOD1(UpdateFinished, void(bool));
254 MOCK_METHOD0(ResetDatabase, void()); 254 MOCK_METHOD0(ResetDatabase, void());
255 MOCK_METHOD1(GetChunks, void(GetChunksCallback)); 255 MOCK_METHOD1(GetChunks, void(GetChunksCallback));
256 MOCK_METHOD2(AddChunks, void(const std::string&, SBChunkList*)); 256 MOCK_METHOD3(AddChunks, void(const std::string&, SBChunkList*,
257 AddChunksCallback));
257 MOCK_METHOD1(DeleteChunks, void(std::vector<SBChunkDelete>*)); 258 MOCK_METHOD1(DeleteChunks, void(std::vector<SBChunkDelete>*));
258 }; 259 };
259 260
260 // ImmediateSingleThreadTaskRunner will ignore delayed times for tasks. 261 // ImmediateSingleThreadTaskRunner will ignore delayed times for tasks.
261 // This is primarily used to run the timer tasks immediately, and prevent 262 // This is primarily used to run the timer tasks immediately, and prevent
262 // the need for constructing a MessageLoop. 263 // the need for constructing a MessageLoop.
263 class ImmediateSingleThreadTaskRunner : public base::SingleThreadTaskRunner { 264 class ImmediateSingleThreadTaskRunner : public base::SingleThreadTaskRunner {
264 public: 265 public:
265 virtual bool RunsTasksOnCurrentThread() const OVERRIDE { 266 virtual bool RunsTasksOnCurrentThread() const OVERRIDE {
266 return true; 267 return true;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 // |InvokeGetChunksCallback| is required because GMock's InvokeArgument action 306 // |InvokeGetChunksCallback| is required because GMock's InvokeArgument action
306 // expects to use operator(), and a Callback only provides Run(). 307 // expects to use operator(), and a Callback only provides Run().
307 // TODO(cbentzel): Use ACTION or ACTION_TEMPLATE instead? 308 // TODO(cbentzel): Use ACTION or ACTION_TEMPLATE instead?
308 void InvokeGetChunksCallback( 309 void InvokeGetChunksCallback(
309 const std::vector<SBListChunkRanges>& ranges, 310 const std::vector<SBListChunkRanges>& ranges,
310 bool database_error, 311 bool database_error,
311 SafeBrowsingProtocolManagerDelegate::GetChunksCallback callback) { 312 SafeBrowsingProtocolManagerDelegate::GetChunksCallback callback) {
312 callback.Run(ranges, database_error); 313 callback.Run(ranges, database_error);
313 } 314 }
314 315
316 // |HandleAddChunks| deletes the chunks and asynchronously invokes
317 // |callback| since SafeBrowsingProtocolManager is not re-entrant at the time
318 // this is called. This guarantee is part of the
319 // SafeBrowsingProtocolManagerDelegate contract.
320 void HandleAddChunks(
321 const std::string& unused_list,
322 SBChunkList* chunks,
323 SafeBrowsingProtocolManagerDelegate::AddChunksCallback callback) {
324 delete chunks;
325 scoped_refptr<base::SingleThreadTaskRunner> task_runner(
326 base::ThreadTaskRunnerHandle::Get());
327 if (!task_runner)
328 return;
329 task_runner->PostTask(FROM_HERE, callback);
330 }
331
315 } // namespace 332 } // namespace
316 333
317 // Tests that the Update protocol will be skipped if there are problems 334 // Tests that the Update protocol will be skipped if there are problems
318 // accessing the database. 335 // accessing the database.
319 TEST_F(SafeBrowsingProtocolManagerTest, ProblemAccessingDatabase) { 336 TEST_F(SafeBrowsingProtocolManagerTest, ProblemAccessingDatabase) {
320 scoped_refptr<ImmediateSingleThreadTaskRunner> runner( 337 scoped_refptr<ImmediateSingleThreadTaskRunner> runner(
321 new ImmediateSingleThreadTaskRunner()); 338 new ImmediateSingleThreadTaskRunner());
322 base::ThreadTaskRunnerHandle runner_handler(runner); 339 base::ThreadTaskRunnerHandle runner_handler(runner);
323 340
324 testing::StrictMock<MockProtocolDelegate> test_delegate; 341 testing::StrictMock<MockProtocolDelegate> test_delegate;
325 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); 342 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1);
326 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( 343 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce(
327 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, 344 Invoke(testing::CreateFunctor(InvokeGetChunksCallback,
328 std::vector<SBListChunkRanges>(), 345 std::vector<SBListChunkRanges>(),
329 true))); 346 true)));
330 EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1); 347 EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1);
331 348
332 scoped_ptr<SafeBrowsingProtocolManager> pm( 349 scoped_ptr<SafeBrowsingProtocolManager> pm(
333 CreateProtocolManager(&test_delegate)); 350 CreateProtocolManager(&test_delegate));
334 351
335 pm->ForceScheduleNextUpdate(base::TimeDelta()); 352 pm->ForceScheduleNextUpdate(base::TimeDelta());
336 runner->RunTasks(); 353 runner->RunTasks();
354
355 EXPECT_TRUE(pm->IsUpdateScheduled());
mattm 2012/12/06 00:51:40 Could inserting some EXPECT_FALSE(pm->IsUpdateSche
cbentzel 2012/12/17 11:24:10 I put it in a few locations (such as the multiple
337 } 356 }
338 357
339 // Tests the contents of the POST body when the local database is empty. 358 // Tests what happens when there is a response with no chunks.
340 TEST_F(SafeBrowsingProtocolManagerTest, EmptyDatabase) { 359 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseEmptyBody) {
341 scoped_refptr<ImmediateSingleThreadTaskRunner> runner( 360 scoped_refptr<ImmediateSingleThreadTaskRunner> runner(
342 new ImmediateSingleThreadTaskRunner()); 361 new ImmediateSingleThreadTaskRunner());
343 base::ThreadTaskRunnerHandle runner_handler(runner); 362 base::ThreadTaskRunnerHandle runner_handler(runner);
344 net::TestURLFetcherFactory url_fetcher_factory; 363 net::TestURLFetcherFactory url_fetcher_factory;
345 364
346 testing::StrictMock<MockProtocolDelegate> test_delegate; 365 testing::StrictMock<MockProtocolDelegate> test_delegate;
347 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); 366 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1);
348 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( 367 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce(
349 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, 368 Invoke(testing::CreateFunctor(InvokeGetChunksCallback,
350 std::vector<SBListChunkRanges>(), 369 std::vector<SBListChunkRanges>(),
351 false))); 370 false)));
371 EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1);
352 372
353 scoped_ptr<SafeBrowsingProtocolManager> pm( 373 scoped_ptr<SafeBrowsingProtocolManager> pm(
354 CreateProtocolManager(&test_delegate)); 374 CreateProtocolManager(&test_delegate));
355 375
356 // Kick off initialization. This returns chunks from the DB synchronously. 376 // Kick off initialization. This returns chunks from the DB synchronously.
357 pm->ForceScheduleNextUpdate(base::TimeDelta()); 377 pm->ForceScheduleNextUpdate(base::TimeDelta());
358 runner->RunTasks(); 378 runner->RunTasks();
359 379
360 // We should have an URLFetcher at this point in time. 380 // We should have an URLFetcher at this point in time.
361 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); 381 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
362 ValidateUpdateFetcherRequest(url_fetcher); 382 ValidateUpdateFetcherRequest(url_fetcher);
383
384 // The update response is successful, but an empty body.
385 url_fetcher->set_status(net::URLRequestStatus());
386 url_fetcher->set_response_code(200);
387 url_fetcher->SetResponseString("");
388 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
389
390 EXPECT_TRUE(pm->IsUpdateScheduled());
363 } 391 }
364 392
365 // Tests the contents of the POST body when there are contents in the 393 // Tests the contents of the POST body when there are contents in the
366 // local database. This is not exhaustive, as the actual list formatting 394 // local database. This is not exhaustive, as the actual list formatting
367 // is covered by SafeBrowsingProtocolManagerTest.TestChunkStrings. 395 // is covered by SafeBrowsingProtocolManagerTest.TestChunkStrings.
368 TEST_F(SafeBrowsingProtocolManagerTest, ExistingDatabase) { 396 TEST_F(SafeBrowsingProtocolManagerTest, ExistingDatabase) {
369 scoped_refptr<ImmediateSingleThreadTaskRunner> runner( 397 scoped_refptr<ImmediateSingleThreadTaskRunner> runner(
370 new ImmediateSingleThreadTaskRunner()); 398 new ImmediateSingleThreadTaskRunner());
371 base::ThreadTaskRunnerHandle runner_handler(runner); 399 base::ThreadTaskRunnerHandle runner_handler(runner);
372 net::TestURLFetcherFactory url_fetcher_factory; 400 net::TestURLFetcherFactory url_fetcher_factory;
373 401
374 std::vector<SBListChunkRanges> ranges; 402 std::vector<SBListChunkRanges> ranges;
375 SBListChunkRanges range_phish(safe_browsing_util::kPhishingList); 403 SBListChunkRanges range_phish(safe_browsing_util::kPhishingList);
376 range_phish.adds = "adds_phish"; 404 range_phish.adds = "adds_phish";
377 range_phish.subs = "subs_phish"; 405 range_phish.subs = "subs_phish";
378 ranges.push_back(range_phish); 406 ranges.push_back(range_phish);
379 407
380 SBListChunkRanges range_unknown("unknown_list"); 408 SBListChunkRanges range_unknown("unknown_list");
381 range_unknown.adds = "adds_unknown"; 409 range_unknown.adds = "adds_unknown";
382 range_unknown.subs = "subs_unknown"; 410 range_unknown.subs = "subs_unknown";
383 ranges.push_back(range_unknown); 411 ranges.push_back(range_unknown);
384 412
385 testing::StrictMock<MockProtocolDelegate> test_delegate; 413 testing::StrictMock<MockProtocolDelegate> test_delegate;
386 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); 414 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1);
387 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( 415 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce(
388 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, 416 Invoke(testing::CreateFunctor(InvokeGetChunksCallback,
389 ranges, 417 ranges,
390 false))); 418 false)));
419 EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1);
391 420
392 scoped_ptr<SafeBrowsingProtocolManager> pm( 421 scoped_ptr<SafeBrowsingProtocolManager> pm(
393 CreateProtocolManager(&test_delegate)); 422 CreateProtocolManager(&test_delegate));
394 423
395 // Kick off initialization. This returns chunks from the DB synchronously. 424 // Kick off initialization. This returns chunks from the DB synchronously.
396 pm->ForceScheduleNextUpdate(base::TimeDelta()); 425 pm->ForceScheduleNextUpdate(base::TimeDelta());
397 runner->RunTasks(); 426 runner->RunTasks();
398 427
399 // We should have an URLFetcher at this point in time. 428 // We should have an URLFetcher at this point in time.
400 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); 429 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
401 ASSERT_TRUE(url_fetcher); 430 ASSERT_TRUE(url_fetcher);
402 EXPECT_EQ(net::LOAD_DISABLE_CACHE, url_fetcher->GetLoadFlags()); 431 EXPECT_EQ(net::LOAD_DISABLE_CACHE, url_fetcher->GetLoadFlags());
403 EXPECT_EQ("goog-phish-shavar;a:adds_phish:s:subs_phish\n" 432 EXPECT_EQ("goog-phish-shavar;a:adds_phish:s:subs_phish\n"
404 "unknown_list;a:adds_unknown:s:subs_unknown\n" 433 "unknown_list;a:adds_unknown:s:subs_unknown\n"
405 "goog-malware-shavar;\n", 434 "goog-malware-shavar;\n",
406 url_fetcher->upload_data()); 435 url_fetcher->upload_data());
407 EXPECT_EQ(GURL("https://prefix.com/foo/downloads?client=unittest&appver=1.0" 436 EXPECT_EQ(GURL("https://prefix.com/foo/downloads?client=unittest&appver=1.0"
408 "&pver=2.2" + key_param_), 437 "&pver=2.2" + key_param_),
409 url_fetcher->GetOriginalURL()); 438 url_fetcher->GetOriginalURL());
439 url_fetcher->set_status(net::URLRequestStatus());
440 url_fetcher->set_response_code(200);
441 url_fetcher->SetResponseString("");
442 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
443
444 EXPECT_TRUE(pm->IsUpdateScheduled());
410 } 445 }
411 446
412 // Tests what happens when there is a response with no chunks. 447
mattm 2012/12/05 23:58:10 extra line
cbentzel 2012/12/17 11:24:10 Done.
413 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseEmptyBody) { 448 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseBadyBody) {
mattm 2012/12/05 23:58:10 s/Bady/Bad/
cbentzel 2012/12/17 11:24:10 Done.
414 scoped_refptr<ImmediateSingleThreadTaskRunner> runner( 449 scoped_refptr<ImmediateSingleThreadTaskRunner> runner(
415 new ImmediateSingleThreadTaskRunner()); 450 new ImmediateSingleThreadTaskRunner());
416 base::ThreadTaskRunnerHandle runner_handler(runner); 451 base::ThreadTaskRunnerHandle runner_handler(runner);
417 net::TestURLFetcherFactory url_fetcher_factory; 452 net::TestURLFetcherFactory url_fetcher_factory;
418 453
419 testing::StrictMock<MockProtocolDelegate> test_delegate; 454 testing::StrictMock<MockProtocolDelegate> test_delegate;
420 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); 455 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1);
421 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( 456 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce(
422 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, 457 Invoke(testing::CreateFunctor(InvokeGetChunksCallback,
423 std::vector<SBListChunkRanges>(), 458 std::vector<SBListChunkRanges>(),
424 false))); 459 false)));
425 EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1); 460 EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1);
426 461
427 scoped_ptr<SafeBrowsingProtocolManager> pm( 462 scoped_ptr<SafeBrowsingProtocolManager> pm(
428 CreateProtocolManager(&test_delegate)); 463 CreateProtocolManager(&test_delegate));
429 464
430 // Kick off initialization. This returns chunks from the DB synchronously. 465 // Kick off initialization. This returns chunks from the DB synchronously.
431 pm->ForceScheduleNextUpdate(base::TimeDelta()); 466 pm->ForceScheduleNextUpdate(base::TimeDelta());
432 runner->RunTasks(); 467 runner->RunTasks();
433 468
434 // We should have an URLFetcher at this point in time. 469 // We should have an URLFetcher at this point in time.
435 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); 470 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
436 ValidateUpdateFetcherRequest(url_fetcher); 471 ValidateUpdateFetcherRequest(url_fetcher);
437 472
438 // The update response is successful, but an empty body. 473 // The update response is successful, but an invalid body.
439 url_fetcher->set_status(net::URLRequestStatus()); 474 url_fetcher->set_status(net::URLRequestStatus());
440 url_fetcher->set_response_code(200); 475 url_fetcher->set_response_code(200);
441 url_fetcher->SetResponseString(""); 476 url_fetcher->SetResponseString("THIS_IS_A_BAD_RESPONSE");
442 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); 477 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
478
479 EXPECT_TRUE(pm->IsUpdateScheduled());
443 } 480 }
444 481
445 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseBadBody) { 482 // Tests what happens when there is an error in the update response.
483 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseHttpError) {
446 scoped_refptr<ImmediateSingleThreadTaskRunner> runner( 484 scoped_refptr<ImmediateSingleThreadTaskRunner> runner(
447 new ImmediateSingleThreadTaskRunner()); 485 new ImmediateSingleThreadTaskRunner());
448 base::ThreadTaskRunnerHandle runner_handler(runner); 486 base::ThreadTaskRunnerHandle runner_handler(runner);
449 net::TestURLFetcherFactory url_fetcher_factory; 487 net::TestURLFetcherFactory url_fetcher_factory;
450 488
451 testing::StrictMock<MockProtocolDelegate> test_delegate; 489 testing::StrictMock<MockProtocolDelegate> test_delegate;
452 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); 490 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1);
453 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( 491 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce(
454 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, 492 Invoke(testing::CreateFunctor(InvokeGetChunksCallback,
455 std::vector<SBListChunkRanges>(), 493 std::vector<SBListChunkRanges>(),
456 false))); 494 false)));
457 EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1); 495 EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1);
458 496
459 scoped_ptr<SafeBrowsingProtocolManager> pm( 497 scoped_ptr<SafeBrowsingProtocolManager> pm(
460 CreateProtocolManager(&test_delegate)); 498 CreateProtocolManager(&test_delegate));
461 499
462 // Kick off initialization. This returns chunks from the DB synchronously. 500 // Kick off initialization. This returns chunks from the DB synchronously.
463 pm->ForceScheduleNextUpdate(base::TimeDelta()); 501 pm->ForceScheduleNextUpdate(base::TimeDelta());
464 runner->RunTasks(); 502 runner->RunTasks();
465 503
466 // We should have an URLFetcher at this point in time. 504 // We should have an URLFetcher at this point in time.
467 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); 505 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
468 ValidateUpdateFetcherRequest(url_fetcher); 506 ValidateUpdateFetcherRequest(url_fetcher);
469 507
470 // The update response is successful, but an invalid body. 508 // Go ahead and respond to it.
471 url_fetcher->set_status(net::URLRequestStatus()); 509 url_fetcher->set_status(net::URLRequestStatus());
472 url_fetcher->set_response_code(200); 510 url_fetcher->set_response_code(404);
473 url_fetcher->SetResponseString("THIS_IS_A_BAD_RESPONSE"); 511 url_fetcher->SetResponseString("");
474 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); 512 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
513
514 EXPECT_TRUE(pm->IsUpdateScheduled());
475 } 515 }
476 516
477 // Tests what happens when there is an error in the update response. 517 // Tests what happens when there is an error with the connection.
478 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseHttpError) { 518 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseConnectionError) {
479 scoped_refptr<ImmediateSingleThreadTaskRunner> runner( 519 scoped_refptr<ImmediateSingleThreadTaskRunner> runner(
480 new ImmediateSingleThreadTaskRunner()); 520 new ImmediateSingleThreadTaskRunner());
481 base::ThreadTaskRunnerHandle runner_handler(runner); 521 base::ThreadTaskRunnerHandle runner_handler(runner);
482 net::TestURLFetcherFactory url_fetcher_factory; 522 net::TestURLFetcherFactory url_fetcher_factory;
483 523
484 testing::StrictMock<MockProtocolDelegate> test_delegate; 524 testing::StrictMock<MockProtocolDelegate> test_delegate;
485 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); 525 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1);
486 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( 526 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce(
487 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, 527 Invoke(testing::CreateFunctor(InvokeGetChunksCallback,
488 std::vector<SBListChunkRanges>(), 528 std::vector<SBListChunkRanges>(),
489 false))); 529 false)));
490 EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1); 530 EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1);
491 531
492 scoped_ptr<SafeBrowsingProtocolManager> pm( 532 scoped_ptr<SafeBrowsingProtocolManager> pm(
493 CreateProtocolManager(&test_delegate)); 533 CreateProtocolManager(&test_delegate));
494 534
495 // Kick off initialization. This returns chunks from the DB synchronously. 535 // Kick off initialization. This returns chunks from the DB synchronously.
496 pm->ForceScheduleNextUpdate(base::TimeDelta()); 536 pm->ForceScheduleNextUpdate(base::TimeDelta());
497 runner->RunTasks(); 537 runner->RunTasks();
498 538
499 // We should have an URLFetcher at this point in time. 539 // We should have an URLFetcher at this point in time.
500 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); 540 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
501 ValidateUpdateFetcherRequest(url_fetcher); 541 ValidateUpdateFetcherRequest(url_fetcher);
502 542
503 // Go ahead and respond to it. 543 // Go ahead and respond to it.
504 url_fetcher->set_status(net::URLRequestStatus()); 544 url_fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
505 url_fetcher->set_response_code(404); 545 net::ERR_CONNECTION_RESET));
506 url_fetcher->SetResponseString("");
507 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); 546 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
547
548 EXPECT_TRUE(pm->IsUpdateScheduled());
508 } 549 }
509 550
510 // Tests what happens when there is an error with the connection. 551 // Tests what happens when there is a timeout before an update response.
511 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseConnectionError) { 552 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseTimeout) {
512 scoped_refptr<ImmediateSingleThreadTaskRunner> runner( 553 scoped_refptr<ImmediateSingleThreadTaskRunner> runner(
513 new ImmediateSingleThreadTaskRunner()); 554 new ImmediateSingleThreadTaskRunner());
514 base::ThreadTaskRunnerHandle runner_handler(runner); 555 base::ThreadTaskRunnerHandle runner_handler(runner);
515 net::TestURLFetcherFactory url_fetcher_factory; 556 net::TestURLFetcherFactory url_fetcher_factory;
516 557
517 testing::StrictMock<MockProtocolDelegate> test_delegate; 558 testing::StrictMock<MockProtocolDelegate> test_delegate;
518 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); 559 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1);
519 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( 560 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce(
520 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, 561 Invoke(testing::CreateFunctor(InvokeGetChunksCallback,
521 std::vector<SBListChunkRanges>(), 562 std::vector<SBListChunkRanges>(),
522 false))); 563 false)));
523 EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1); 564 EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1);
524 565
525 scoped_ptr<SafeBrowsingProtocolManager> pm( 566 scoped_ptr<SafeBrowsingProtocolManager> pm(
526 CreateProtocolManager(&test_delegate)); 567 CreateProtocolManager(&test_delegate));
527 568
528 // Kick off initialization. This returns chunks from the DB synchronously. 569 // Kick off initialization. This returns chunks from the DB synchronously.
529 pm->ForceScheduleNextUpdate(base::TimeDelta()); 570 pm->ForceScheduleNextUpdate(base::TimeDelta());
530 runner->RunTasks(); 571 runner->RunTasks();
531 572
532 // We should have an URLFetcher at this point in time. 573 // We should have an URLFetcher at this point in time.
533 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); 574 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
534 ValidateUpdateFetcherRequest(url_fetcher); 575 ValidateUpdateFetcherRequest(url_fetcher);
535 576
536 // Go ahead and respond to it. 577 // The first time RunTasks is called above, the update timeout timer is not
537 url_fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, 578 // handled. This call of RunTasks will handle the update.
538 net::ERR_CONNECTION_RESET)); 579 runner->RunTasks();
539 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); 580
581 EXPECT_TRUE(pm->IsUpdateScheduled());
540 } 582 }
541 583
542 // Tests what happens when there is a timeout before an update response. 584 // Tests what happens when there is a reset command in the response.
543 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseTimeout) { 585 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseReset) {
544 scoped_refptr<ImmediateSingleThreadTaskRunner> runner( 586 scoped_refptr<ImmediateSingleThreadTaskRunner> runner(
545 new ImmediateSingleThreadTaskRunner()); 587 new ImmediateSingleThreadTaskRunner());
546 base::ThreadTaskRunnerHandle runner_handler(runner); 588 base::ThreadTaskRunnerHandle runner_handler(runner);
589 net::TestURLFetcherFactory url_fetcher_factory;
590
591 testing::StrictMock<MockProtocolDelegate> test_delegate;
592 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1);
593 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce(
594 Invoke(testing::CreateFunctor(InvokeGetChunksCallback,
595 std::vector<SBListChunkRanges>(),
596 false)));
597 EXPECT_CALL(test_delegate, ResetDatabase()).Times(1);
598 EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1);
599
600 scoped_ptr<SafeBrowsingProtocolManager> pm(
601 CreateProtocolManager(&test_delegate));
602
603 // Kick off initialization. This returns chunks from the DB synchronously.
604 pm->ForceScheduleNextUpdate(base::TimeDelta());
605 runner->RunTasks();
606
607 // We should have an URLFetcher at this point in time.
608 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
609 ValidateUpdateFetcherRequest(url_fetcher);
610
611 // The update response is successful, and has a reset command.
612 url_fetcher->set_status(net::URLRequestStatus());
613 url_fetcher->set_response_code(200);
614 url_fetcher->SetResponseString("r:pleasereset\n");
615 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
616
617 EXPECT_TRUE(pm->IsUpdateScheduled());
618 }
619
620 // Tests a single valid update response, followed by a single redirect response
621 // that has an valid, but empty body.
622 TEST_F(SafeBrowsingProtocolManagerTest, EmptyRedirectResponse) {
623 scoped_refptr<ImmediateSingleThreadTaskRunner> runner(
624 new ImmediateSingleThreadTaskRunner());
625 base::ThreadTaskRunnerHandle runner_handler(runner);
626 net::TestURLFetcherFactory url_fetcher_factory;
627
628 testing::StrictMock<MockProtocolDelegate> test_delegate;
629 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1);
630 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce(
631 Invoke(testing::CreateFunctor(InvokeGetChunksCallback,
632 std::vector<SBListChunkRanges>(),
633 false)));
634 EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1);
635
636 scoped_ptr<SafeBrowsingProtocolManager> pm(
637 CreateProtocolManager(&test_delegate));
638
639 // Kick off initialization. This returns chunks from the DB synchronously.
640 pm->ForceScheduleNextUpdate(base::TimeDelta());
641 runner->RunTasks();
642
643 // We should have an URLFetcher at this point in time.
644 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
645 ValidateUpdateFetcherRequest(url_fetcher);
646
647 // The update response is successful, and has a reset command.
mattm 2012/12/06 00:51:40 s/reset/redirect/ (in following tests too)
cbentzel 2012/12/17 11:24:10 Done.
648 url_fetcher->set_status(net::URLRequestStatus());
649 url_fetcher->set_response_code(200);
650 url_fetcher->SetResponseString(
651 "i:goog-phish-shavar\n"
652 "u:redirect-server.example.com/path\n");
653 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
654
655 // We should have another request.
656 net::TestURLFetcher* chunk_url_fetcher =
657 url_fetcher_factory.GetFetcherByID(1);
658 ASSERT_TRUE(chunk_url_fetcher != NULL);
mattm 2012/12/06 00:51:40 Should test the fetched URL value as well, at leas
cbentzel 2012/12/17 11:24:10 Done.
659 chunk_url_fetcher->set_status(net::URLRequestStatus());
660 chunk_url_fetcher->set_response_code(200);
661 chunk_url_fetcher->SetResponseString("");
662 chunk_url_fetcher->delegate()->OnURLFetchComplete(chunk_url_fetcher);
663
664 EXPECT_TRUE(pm->IsUpdateScheduled());
665 }
666
667 // Tests a single valid update response, followed by a single redirect response
668 // that has an invalid body.
669 TEST_F(SafeBrowsingProtocolManagerTest, InvalidRedirectResponse) {
670 scoped_refptr<ImmediateSingleThreadTaskRunner> runner(
671 new ImmediateSingleThreadTaskRunner());
672 base::ThreadTaskRunnerHandle runner_handler(runner);
547 net::TestURLFetcherFactory url_fetcher_factory; 673 net::TestURLFetcherFactory url_fetcher_factory;
548 674
549 testing::StrictMock<MockProtocolDelegate> test_delegate; 675 testing::StrictMock<MockProtocolDelegate> test_delegate;
550 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); 676 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1);
551 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( 677 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce(
552 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, 678 Invoke(testing::CreateFunctor(InvokeGetChunksCallback,
553 std::vector<SBListChunkRanges>(), 679 std::vector<SBListChunkRanges>(),
554 false))); 680 false)));
555 EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1); 681 EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1);
556 682
557 scoped_ptr<SafeBrowsingProtocolManager> pm( 683 scoped_ptr<SafeBrowsingProtocolManager> pm(
558 CreateProtocolManager(&test_delegate)); 684 CreateProtocolManager(&test_delegate));
559 685
560 // Kick off initialization. This returns chunks from the DB synchronously. 686 // Kick off initialization. This returns chunks from the DB synchronously.
561 pm->ForceScheduleNextUpdate(base::TimeDelta()); 687 pm->ForceScheduleNextUpdate(base::TimeDelta());
562 runner->RunTasks(); 688 runner->RunTasks();
563 689
564 // We should have an URLFetcher at this point in time. 690 // We should have an URLFetcher at this point in time.
565 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); 691 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
566 ValidateUpdateFetcherRequest(url_fetcher); 692 ValidateUpdateFetcherRequest(url_fetcher);
567 693
568 // The first time RunTasks is called above, the update timeout timer is not 694 // The update response is successful, and has a reset command.
569 // handled. This call of RunTasks will handle the update. 695 url_fetcher->set_status(net::URLRequestStatus());
570 runner->RunTasks(); 696 url_fetcher->set_response_code(200);
697 url_fetcher->SetResponseString(
698 "i:goog-phish-shavar\n"
699 "u:redirect-server.example.com/path\n");
700 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
701
702 // We should have another request.
703 net::TestURLFetcher* chunk_url_fetcher =
704 url_fetcher_factory.GetFetcherByID(1);
705 ASSERT_TRUE(chunk_url_fetcher != NULL);
706 chunk_url_fetcher->set_status(net::URLRequestStatus());
707 chunk_url_fetcher->set_response_code(200);
708 chunk_url_fetcher->SetResponseString("THIS IS AN INVALID RESPONSE");
709 chunk_url_fetcher->delegate()->OnURLFetchComplete(chunk_url_fetcher);
710
711 EXPECT_TRUE(pm->IsUpdateScheduled());
571 } 712 }
572 713
573 // Tests what happens when there is a reset command in the response. 714 // Tests a single valid update response, followed by a single redirect response
574 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseReset) { 715 // containing chunks.
716 TEST_F(SafeBrowsingProtocolManagerTest, SingleRedirectResponseWithChunks) {
575 scoped_refptr<ImmediateSingleThreadTaskRunner> runner( 717 scoped_refptr<ImmediateSingleThreadTaskRunner> runner(
576 new ImmediateSingleThreadTaskRunner()); 718 new ImmediateSingleThreadTaskRunner());
577 base::ThreadTaskRunnerHandle runner_handler(runner); 719 base::ThreadTaskRunnerHandle runner_handler(runner);
720 net::TestURLFetcherFactory url_fetcher_factory;
721
722 testing::StrictMock<MockProtocolDelegate> test_delegate;
723 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1);
724 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce(
725 Invoke(testing::CreateFunctor(InvokeGetChunksCallback,
726 std::vector<SBListChunkRanges>(),
727 false)));
728 EXPECT_CALL(test_delegate, AddChunks("goog-phish-shavar", _, _)).WillOnce(
729 Invoke(HandleAddChunks));
730 EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1);
731
732 scoped_ptr<SafeBrowsingProtocolManager> pm(
733 CreateProtocolManager(&test_delegate));
734
735 // Kick off initialization. This returns chunks from the DB synchronously.
736 pm->ForceScheduleNextUpdate(base::TimeDelta());
737 runner->RunTasks();
738
739 // We should have an URLFetcher at this point in time.
740 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
741 ValidateUpdateFetcherRequest(url_fetcher);
742
743 // The update response is successful, and has a reset command.
744 url_fetcher->set_status(net::URLRequestStatus());
745 url_fetcher->set_response_code(200);
746 url_fetcher->SetResponseString(
747 "i:goog-phish-shavar\n"
748 "u:redirect-server.example.com/path\n");
749 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
750
751 // We should have another request.
752 net::TestURLFetcher* chunk_url_fetcher =
753 url_fetcher_factory.GetFetcherByID(1);
754 ASSERT_TRUE(chunk_url_fetcher != NULL);
755 chunk_url_fetcher->set_status(net::URLRequestStatus());
756 chunk_url_fetcher->set_response_code(200);
757 chunk_url_fetcher->SetResponseString(
758 "a:4:4:9\n"
759 "host\1fdaf");
760 chunk_url_fetcher->delegate()->OnURLFetchComplete(chunk_url_fetcher);
761
762 // The AddChunksCallback needs to be invoked
763 runner->RunTasks();
764
765 EXPECT_TRUE(pm->IsUpdateScheduled());
766 }
767
768 // Tests a single valid update response, followed by multiple redirect responses
769 // containing chunks.
770 TEST_F(SafeBrowsingProtocolManagerTest, MultipleRedirectResponsesWithChunks) {
771 scoped_refptr<ImmediateSingleThreadTaskRunner> runner(
772 new ImmediateSingleThreadTaskRunner());
773 base::ThreadTaskRunnerHandle runner_handler(runner);
578 net::TestURLFetcherFactory url_fetcher_factory; 774 net::TestURLFetcherFactory url_fetcher_factory;
579 775
580 testing::StrictMock<MockProtocolDelegate> test_delegate; 776 testing::StrictMock<MockProtocolDelegate> test_delegate;
581 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); 777 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1);
582 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( 778 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce(
583 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, 779 Invoke(testing::CreateFunctor(InvokeGetChunksCallback,
584 std::vector<SBListChunkRanges>(), 780 std::vector<SBListChunkRanges>(),
585 false))); 781 false)));
586 EXPECT_CALL(test_delegate, ResetDatabase()).Times(1); 782 EXPECT_CALL(test_delegate, AddChunks("goog-phish-shavar", _, _)).
783 WillRepeatedly(Invoke(HandleAddChunks));
587 EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1); 784 EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1);
588 785
589 scoped_ptr<SafeBrowsingProtocolManager> pm( 786 scoped_ptr<SafeBrowsingProtocolManager> pm(
590 CreateProtocolManager(&test_delegate)); 787 CreateProtocolManager(&test_delegate));
591 788
592 // Kick off initialization. This returns chunks from the DB synchronously. 789 // Kick off initialization. This returns chunks from the DB synchronously.
593 pm->ForceScheduleNextUpdate(base::TimeDelta()); 790 pm->ForceScheduleNextUpdate(base::TimeDelta());
594 runner->RunTasks(); 791 runner->RunTasks();
595 792
596 // We should have an URLFetcher at this point in time. 793 // We should have an URLFetcher at this point in time.
597 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); 794 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0);
598 ValidateUpdateFetcherRequest(url_fetcher); 795 ValidateUpdateFetcherRequest(url_fetcher);
599 796
600 // The update response is successful, and has a reset command. 797 // The update response is successful, and has a reset command.
601 url_fetcher->set_status(net::URLRequestStatus()); 798 url_fetcher->set_status(net::URLRequestStatus());
602 url_fetcher->set_response_code(200); 799 url_fetcher->set_response_code(200);
603 url_fetcher->SetResponseString("r:pleasereset\n"); 800 url_fetcher->SetResponseString(
801 "i:goog-phish-shavar\n"
802 "u:redirect-server.example.com/one\n"
803 "u:redirect-server.example.com/two\n");
604 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); 804 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
805
806 // First redirect request.
807 net::TestURLFetcher* first_chunk_url_fetcher =
mattm 2012/12/06 00:51:40 should probably check the URLs on these requests t
cbentzel 2012/12/17 11:24:10 Done.
808 url_fetcher_factory.GetFetcherByID(1);
809 ASSERT_TRUE(first_chunk_url_fetcher != NULL);
810 first_chunk_url_fetcher->set_status(net::URLRequestStatus());
811 first_chunk_url_fetcher->set_response_code(200);
812 first_chunk_url_fetcher->SetResponseString(
813 "a:4:4:9\n"
814 "host\1aaaa");
815 first_chunk_url_fetcher->delegate()->OnURLFetchComplete(
816 first_chunk_url_fetcher);
817
818 // Invoke the AddChunksCallback
819 runner->RunTasks();
820
821 // Second redirect request.
822 net::TestURLFetcher* second_chunk_url_fetcher =
823 url_fetcher_factory.GetFetcherByID(2);
824 ASSERT_TRUE(second_chunk_url_fetcher != NULL);
825 second_chunk_url_fetcher->set_status(net::URLRequestStatus());
826 second_chunk_url_fetcher->set_response_code(200);
827 second_chunk_url_fetcher->SetResponseString(
828 "a:5:4:9\n"
829 "host\1bbbb");
830 second_chunk_url_fetcher->delegate()->OnURLFetchComplete(
831 second_chunk_url_fetcher);
832
833 // The AddChunksCallback needs to be invoked
834 runner->RunTasks();
835
836 EXPECT_TRUE(pm->IsUpdateScheduled());
605 } 837 }
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/protocol_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698