OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/chromeos/login/google_authenticator.h" | 5 #include "chrome/browser/chromeos/login/google_authenticator.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
(...skipping 540 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
551 .WillOnce(Return(true)) | 551 .WillOnce(Return(true)) |
552 .RetiresOnSaturation(); | 552 .RetiresOnSaturation(); |
553 | 553 |
554 EXPECT_CALL(*mock_library_, GetSystemSalt()) | 554 EXPECT_CALL(*mock_library_, GetSystemSalt()) |
555 .WillOnce(Return(salt_v)) | 555 .WillOnce(Return(salt_v)) |
556 .RetiresOnSaturation(); | 556 .RetiresOnSaturation(); |
557 | 557 |
558 TestingProfile profile; | 558 TestingProfile profile; |
559 | 559 |
560 MockFactory<MockFetcher> factory; | 560 MockFactory<MockFetcher> factory; |
561 URLFetcher::set_factory(&factory); | |
562 | 561 |
563 scoped_refptr<GoogleAuthenticator> auth(new GoogleAuthenticator(&consumer)); | 562 scoped_refptr<GoogleAuthenticator> auth(new GoogleAuthenticator(&consumer)); |
564 EXPECT_CALL(*user_manager_.get(), IsKnownUser(username_)) | 563 EXPECT_CALL(*user_manager_.get(), IsKnownUser(username_)) |
565 .WillOnce(Return(true)) | 564 .WillOnce(Return(true)) |
566 .RetiresOnSaturation(); | 565 .RetiresOnSaturation(); |
567 auth->set_user_manager(user_manager_.get()); | 566 auth->set_user_manager(user_manager_.get()); |
568 auth->AuthenticateToLogin( | 567 auth->AuthenticateToLogin( |
569 &profile, username_, password_, std::string(), std::string()); | 568 &profile, username_, password_, std::string(), std::string()); |
570 | 569 |
571 URLFetcher::set_factory(NULL); | |
572 message_loop_ui_.RunAllPending(); | 570 message_loop_ui_.RunAllPending(); |
573 } | 571 } |
574 | 572 |
575 TEST_F(GoogleAuthenticatorTest, FullHostedLoginFailure) { | 573 TEST_F(GoogleAuthenticatorTest, FullHostedLoginFailure) { |
576 chromeos::CryptohomeBlob salt_v(fake_hash_, fake_hash_ + sizeof(fake_hash_)); | 574 chromeos::CryptohomeBlob salt_v(fake_hash_, fake_hash_ + sizeof(fake_hash_)); |
577 | 575 |
578 LoginFailure failure_details = | 576 LoginFailure failure_details = |
579 LoginFailure::FromNetworkAuthFailure( | 577 LoginFailure::FromNetworkAuthFailure( |
580 GoogleServiceAuthError( | 578 GoogleServiceAuthError( |
581 GoogleServiceAuthError::HOSTED_NOT_ALLOWED)); | 579 GoogleServiceAuthError::HOSTED_NOT_ALLOWED)); |
582 | 580 |
583 MockConsumer consumer; | 581 MockConsumer consumer; |
584 EXPECT_CALL(consumer, OnLoginFailure(failure_details)) | 582 EXPECT_CALL(consumer, OnLoginFailure(failure_details)) |
585 .WillOnce(Invoke(MockConsumer::OnFailQuit)) | 583 .WillOnce(Invoke(MockConsumer::OnFailQuit)) |
586 .RetiresOnSaturation(); | 584 .RetiresOnSaturation(); |
587 // A failure case, but we still want the test to finish gracefully. | 585 // A failure case, but we still want the test to finish gracefully. |
588 ON_CALL(consumer, OnLoginSuccess(username_, password_, _, _)) | 586 ON_CALL(consumer, OnLoginSuccess(username_, password_, _, _)) |
589 .WillByDefault(Invoke(MockConsumer::OnSuccessQuitAndFail)); | 587 .WillByDefault(Invoke(MockConsumer::OnSuccessQuitAndFail)); |
590 | 588 |
591 EXPECT_CALL(*mock_library_, GetSystemSalt()) | 589 EXPECT_CALL(*mock_library_, GetSystemSalt()) |
592 .WillOnce(Return(salt_v)) | 590 .WillOnce(Return(salt_v)) |
593 .RetiresOnSaturation(); | 591 .RetiresOnSaturation(); |
594 | 592 |
595 TestingProfile profile; | 593 TestingProfile profile; |
596 | 594 |
597 MockFactory<HostedFetcher> factory_invalid; | 595 MockFactory<HostedFetcher> factory_invalid; |
598 URLFetcher::set_factory(&factory_invalid); | |
599 | 596 |
600 scoped_refptr<GoogleAuthenticator> auth(new GoogleAuthenticator(&consumer)); | 597 scoped_refptr<GoogleAuthenticator> auth(new GoogleAuthenticator(&consumer)); |
601 auth->set_user_manager(user_manager_.get()); | 598 auth->set_user_manager(user_manager_.get()); |
602 EXPECT_CALL(*user_manager_.get(), IsKnownUser(username_)) | 599 EXPECT_CALL(*user_manager_.get(), IsKnownUser(username_)) |
603 .WillOnce(Return(false)) | 600 .WillOnce(Return(false)) |
604 .WillOnce(Return(false)) | 601 .WillOnce(Return(false)) |
605 .RetiresOnSaturation(); | 602 .RetiresOnSaturation(); |
606 auth->AuthenticateToLogin( | 603 auth->AuthenticateToLogin( |
607 &profile, username_, hash_ascii_, std::string(), std::string()); | 604 &profile, username_, hash_ascii_, std::string(), std::string()); |
608 | 605 |
609 // For when |auth| tries to load the localaccount file. | 606 // For when |auth| tries to load the localaccount file. |
610 BrowserThread file_thread(BrowserThread::FILE); | 607 BrowserThread file_thread(BrowserThread::FILE); |
611 file_thread.Start(); | 608 file_thread.Start(); |
612 | 609 |
613 // Run the UI thread until we exit it gracefully. | 610 // Run the UI thread until we exit it gracefully. |
614 message_loop_ui_.Run(); | 611 message_loop_ui_.Run(); |
615 URLFetcher::set_factory(NULL); | |
616 } | 612 } |
617 | 613 |
618 TEST_F(GoogleAuthenticatorTest, CancelLogin) { | 614 TEST_F(GoogleAuthenticatorTest, CancelLogin) { |
619 chromeos::CryptohomeBlob salt_v(fake_hash_, fake_hash_ + sizeof(fake_hash_)); | 615 chromeos::CryptohomeBlob salt_v(fake_hash_, fake_hash_ + sizeof(fake_hash_)); |
620 | 616 |
621 MockConsumer consumer; | 617 MockConsumer consumer; |
622 // The expected case. | 618 // The expected case. |
623 EXPECT_CALL(consumer, OnLoginFailure(_)) | 619 EXPECT_CALL(consumer, OnLoginFailure(_)) |
624 .WillOnce(Invoke(MockConsumer::OnFailQuit)) | 620 .WillOnce(Invoke(MockConsumer::OnFailQuit)) |
625 .RetiresOnSaturation(); | 621 .RetiresOnSaturation(); |
(...skipping 10 matching lines...) Expand all Loading... |
636 .WillOnce(Return(false)) | 632 .WillOnce(Return(false)) |
637 .RetiresOnSaturation(); | 633 .RetiresOnSaturation(); |
638 | 634 |
639 TestingProfile profile; | 635 TestingProfile profile; |
640 | 636 |
641 // This is how we inject fake URLFetcher objects, with a factory. | 637 // This is how we inject fake URLFetcher objects, with a factory. |
642 // This factory creates fake URLFetchers that Start() a fake fetch attempt | 638 // This factory creates fake URLFetchers that Start() a fake fetch attempt |
643 // and then come back on the UI thread after a small delay. They expect to | 639 // and then come back on the UI thread after a small delay. They expect to |
644 // be canceled before they come back, and the test will fail if they are not. | 640 // be canceled before they come back, and the test will fail if they are not. |
645 MockFactory<ExpectCanceledFetcher> factory; | 641 MockFactory<ExpectCanceledFetcher> factory; |
646 URLFetcher::set_factory(&factory); | |
647 | 642 |
648 scoped_refptr<GoogleAuthenticator> auth(new GoogleAuthenticator(&consumer)); | 643 scoped_refptr<GoogleAuthenticator> auth(new GoogleAuthenticator(&consumer)); |
649 // For when |auth| tries to load the localaccount file. | 644 // For when |auth| tries to load the localaccount file. |
650 BrowserThread file_thread(BrowserThread::FILE); | 645 BrowserThread file_thread(BrowserThread::FILE); |
651 file_thread.Start(); | 646 file_thread.Start(); |
652 | 647 |
653 // Start an authentication attempt, which will kick off a URL "fetch" that | 648 // Start an authentication attempt, which will kick off a URL "fetch" that |
654 // we expect to cancel before it completes. | 649 // we expect to cancel before it completes. |
655 auth->AuthenticateToLogin( | 650 auth->AuthenticateToLogin( |
656 &profile, username_, hash_ascii_, std::string(), std::string()); | 651 &profile, username_, hash_ascii_, std::string(), std::string()); |
657 | 652 |
658 // Post a task to cancel the login attempt. | 653 // Post a task to cancel the login attempt. |
659 CancelLogin(auth.get()); | 654 CancelLogin(auth.get()); |
660 | 655 |
661 URLFetcher::set_factory(NULL); | |
662 | |
663 // Run the UI thread until we exit it gracefully. | 656 // Run the UI thread until we exit it gracefully. |
664 message_loop_ui_.Run(); | 657 message_loop_ui_.Run(); |
665 } | 658 } |
666 | 659 |
667 TEST_F(GoogleAuthenticatorTest, CancelLoginAlreadyGotLocalaccount) { | 660 TEST_F(GoogleAuthenticatorTest, CancelLoginAlreadyGotLocalaccount) { |
668 chromeos::CryptohomeBlob salt_v(fake_hash_, fake_hash_ + sizeof(fake_hash_)); | 661 chromeos::CryptohomeBlob salt_v(fake_hash_, fake_hash_ + sizeof(fake_hash_)); |
669 | 662 |
670 MockConsumer consumer; | 663 MockConsumer consumer; |
671 // The expected case. | 664 // The expected case. |
672 EXPECT_CALL(consumer, OnLoginFailure(_)) | 665 EXPECT_CALL(consumer, OnLoginFailure(_)) |
(...skipping 12 matching lines...) Expand all Loading... |
685 .WillOnce(Return(false)) | 678 .WillOnce(Return(false)) |
686 .RetiresOnSaturation(); | 679 .RetiresOnSaturation(); |
687 | 680 |
688 TestingProfile profile; | 681 TestingProfile profile; |
689 | 682 |
690 // This is how we inject fake URLFetcher objects, with a factory. | 683 // This is how we inject fake URLFetcher objects, with a factory. |
691 // This factory creates fake URLFetchers that Start() a fake fetch attempt | 684 // This factory creates fake URLFetchers that Start() a fake fetch attempt |
692 // and then come back on the UI thread after a small delay. They expect to | 685 // and then come back on the UI thread after a small delay. They expect to |
693 // be canceled before they come back, and the test will fail if they are not. | 686 // be canceled before they come back, and the test will fail if they are not. |
694 MockFactory<ExpectCanceledFetcher> factory; | 687 MockFactory<ExpectCanceledFetcher> factory; |
695 URLFetcher::set_factory(&factory); | |
696 | 688 |
697 scoped_refptr<GoogleAuthenticator> auth(new GoogleAuthenticator(&consumer)); | 689 scoped_refptr<GoogleAuthenticator> auth(new GoogleAuthenticator(&consumer)); |
698 // This time, instead of allowing |auth| to go get the localaccount file | 690 // This time, instead of allowing |auth| to go get the localaccount file |
699 // itself, we simulate the case where the file is already loaded, which | 691 // itself, we simulate the case where the file is already loaded, which |
700 // happens when this isn't the first login since chrome started. | 692 // happens when this isn't the first login since chrome started. |
701 ReadLocalaccountFile(auth.get(), ""); | 693 ReadLocalaccountFile(auth.get(), ""); |
702 | 694 |
703 // Start an authentication attempt, which will kick off a URL "fetch" that | 695 // Start an authentication attempt, which will kick off a URL "fetch" that |
704 // we expect to cancel before it completes. | 696 // we expect to cancel before it completes. |
705 auth->AuthenticateToLogin( | 697 auth->AuthenticateToLogin( |
706 &profile, username_, hash_ascii_, std::string(), std::string()); | 698 &profile, username_, hash_ascii_, std::string(), std::string()); |
707 | 699 |
708 // Post a task to cancel the login attempt. | 700 // Post a task to cancel the login attempt. |
709 CancelLogin(auth.get()); | 701 CancelLogin(auth.get()); |
710 | 702 |
711 URLFetcher::set_factory(NULL); | |
712 | |
713 // Run the UI thread until we exit it gracefully. | 703 // Run the UI thread until we exit it gracefully. |
714 message_loop_ui_.Run(); | 704 message_loop_ui_.Run(); |
715 } | 705 } |
716 | 706 |
717 } // namespace chromeos | 707 } // namespace chromeos |
OLD | NEW |