OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <string> | 5 #include <string> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
44 MockConsumer() {} | 44 MockConsumer() {} |
45 ~MockConsumer() {} | 45 ~MockConsumer() {} |
46 MOCK_METHOD1(OnLoginFailure, void(const std::string& error)); | 46 MOCK_METHOD1(OnLoginFailure, void(const std::string& error)); |
47 MOCK_METHOD2(OnLoginSuccess, void(const std::string& username, | 47 MOCK_METHOD2(OnLoginSuccess, void(const std::string& username, |
48 const GaiaAuthConsumer::ClientLoginResult& result)); | 48 const GaiaAuthConsumer::ClientLoginResult& result)); |
49 MOCK_METHOD0(OnOffTheRecordLoginSuccess, void(void)); | 49 MOCK_METHOD0(OnOffTheRecordLoginSuccess, void(void)); |
50 MOCK_METHOD1(OnPasswordChangeDetected, | 50 MOCK_METHOD1(OnPasswordChangeDetected, |
51 void(const GaiaAuthConsumer::ClientLoginResult& result)); | 51 void(const GaiaAuthConsumer::ClientLoginResult& result)); |
52 }; | 52 }; |
53 | 53 |
54 // Responds as though ClientLogin returned from the server. | |
55 class TimedMockFetcher : public URLFetcher { | |
56 public: | |
57 TimedMockFetcher(bool success, | |
58 const GURL& url, | |
DaveMoore
2010/07/28 04:44:33
nit: indenting
Chris Masone
2010/07/28 05:55:52
Done.
| |
59 URLFetcher::RequestType request_type, | |
60 URLFetcher::Delegate* d) | |
61 : URLFetcher(url, request_type, d), | |
62 success_(success), | |
63 url_(url), | |
64 sleeper_(new Sleeper) { | |
65 } | |
DaveMoore
2010/07/28 04:44:33
nit: nl
Chris Masone
2010/07/28 05:55:52
Done.
| |
66 ~TimedMockFetcher() { | |
oshima
2010/07/28 08:00:10
virtual (just for style)
| |
67 sleeper_->task_->Cancel(); | |
68 } | |
69 | |
70 void Start() { | |
71 LOG(INFO) << "Delaying fetch completion in mock"; | |
72 sleeper_->DelayedCompleteFetch(success_, url_, delegate(), 100); | |
73 } | |
74 | |
75 private: | |
76 class Sleeper : public base::RefCountedThreadSafe<Sleeper> { | |
77 public: | |
78 Sleeper() {} | |
79 virtual ~Sleeper() {} | |
80 | |
81 void DelayedCompleteFetch(bool success, | |
82 const GURL& url, | |
83 URLFetcher::Delegate* d, | |
84 int delay_ms) { | |
85 task_ = NewRunnableMethod(this, | |
86 &TimedMockFetcher::Sleeper::CompleteFetch, | |
87 success, | |
88 url, | |
89 d); | |
90 ChromeThread::PostDelayedTask(ChromeThread::UI, | |
91 FROM_HERE, | |
92 task_, | |
93 delay_ms); | |
94 } | |
95 | |
96 void CompleteFetch(bool success, | |
97 const GURL& url, | |
98 URLFetcher::Delegate* d) { | |
99 ADD_FAILURE() << "Should not have completed fetch in TimedMockFetcher"; | |
100 MessageLoop::current()->Quit(); // Allow exiting even if we mess up. | |
101 } | |
DaveMoore
2010/07/28 04:44:33
nit: nl
Chris Masone
2010/07/28 05:55:52
Done.
| |
102 CancelableTask* task_; | |
oshima
2010/07/28 08:00:10
DISALLOW_COPY_AND_ASSIGN
| |
103 }; | |
104 bool success_; | |
105 GURL url_; | |
106 | |
107 scoped_refptr<Sleeper> sleeper_; | |
108 | |
109 DISALLOW_COPY_AND_ASSIGN(TimedMockFetcher); | |
110 }; | |
111 | |
54 class GoogleAuthenticatorTest : public ::testing::Test { | 112 class GoogleAuthenticatorTest : public ::testing::Test { |
55 public: | 113 public: |
56 GoogleAuthenticatorTest() | 114 GoogleAuthenticatorTest() |
57 : username_("me@nowhere.org"), | 115 : username_("me@nowhere.org"), |
58 bytes_as_ascii_("ffff") { | 116 bytes_as_ascii_("ffff") { |
59 memset(fake_hash_, 0, sizeof(fake_hash_)); | 117 memset(fake_hash_, 0, sizeof(fake_hash_)); |
60 fake_hash_[0] = 10; | 118 fake_hash_[0] = 10; |
61 fake_hash_[1] = 1; | 119 fake_hash_[1] = 1; |
62 fake_hash_[7] = 10 << 4; | 120 fake_hash_[7] = 10 << 4; |
63 hash_ascii_.assign("0a010000000000a0"); | 121 hash_ascii_.assign("0a010000000000a0"); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 filename)); | 185 filename)); |
128 } | 186 } |
129 | 187 |
130 void PrepForLogin(GoogleAuthenticator* auth) { | 188 void PrepForLogin(GoogleAuthenticator* auth) { |
131 auth->set_password_hash(hash_ascii_); | 189 auth->set_password_hash(hash_ascii_); |
132 auth->set_username(username_); | 190 auth->set_username(username_); |
133 auth->SetLocalaccount(""); | 191 auth->SetLocalaccount(""); |
134 } | 192 } |
135 | 193 |
136 void CancelLogin(GoogleAuthenticator* auth) { | 194 void CancelLogin(GoogleAuthenticator* auth) { |
137 ChromeThread::PostDelayedTask( | 195 ChromeThread::PostTask( |
138 ChromeThread::UI, | 196 ChromeThread::UI, |
139 FROM_HERE, | 197 FROM_HERE, |
140 NewRunnableMethod(auth, | 198 NewRunnableMethod(auth, |
141 &GoogleAuthenticator::CancelClientLogin), | 199 &GoogleAuthenticator::CancelClientLogin)); |
142 50); | |
143 } | 200 } |
144 | 201 |
145 unsigned char fake_hash_[32]; | 202 unsigned char fake_hash_[32]; |
146 std::string hash_ascii_; | 203 std::string hash_ascii_; |
147 std::string username_; | 204 std::string username_; |
148 GaiaAuthConsumer::ClientLoginResult result_; | 205 GaiaAuthConsumer::ClientLoginResult result_; |
149 // Mocks, destroyed by CrosLibrary class. | 206 // Mocks, destroyed by CrosLibrary class. |
150 MockCryptohomeLibrary* mock_library_; | 207 MockCryptohomeLibrary* mock_library_; |
151 MockLibraryLoader* loader_; | 208 MockLibraryLoader* loader_; |
152 char raw_bytes_[2]; | 209 char raw_bytes_[2]; |
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
479 .RetiresOnSaturation(); | 536 .RetiresOnSaturation(); |
480 | 537 |
481 scoped_refptr<GoogleAuthenticator> auth(new GoogleAuthenticator(&consumer)); | 538 scoped_refptr<GoogleAuthenticator> auth(new GoogleAuthenticator(&consumer)); |
482 PrepForLogin(auth.get()); | 539 PrepForLogin(auth.get()); |
483 auth->SetLocalaccount(username_); | 540 auth->SetLocalaccount(username_); |
484 | 541 |
485 auth->CheckLocalaccount(std::string()); | 542 auth->CheckLocalaccount(std::string()); |
486 } | 543 } |
487 | 544 |
488 // Compatible with LoginStatusConsumer::OnLoginSuccess() | 545 // Compatible with LoginStatusConsumer::OnLoginSuccess() |
489 static void Quit(const std::string& username, | 546 static void OnSuccessQuit( |
490 const GaiaAuthConsumer::ClientLoginResult& credentials) { | 547 const std::string& username, |
548 const GaiaAuthConsumer::ClientLoginResult& credentials) { | |
549 MessageLoop::current()->Quit(); | |
550 } | |
DaveMoore
2010/07/28 04:44:33
nit: nl
Chris Masone
2010/07/28 05:55:52
Done.
| |
551 static void OnSuccessQuitAndFail( | |
552 const std::string& username, | |
553 const GaiaAuthConsumer::ClientLoginResult& credentials) { | |
554 ADD_FAILURE() << "Login should NOT have succeeded!"; | |
491 MessageLoop::current()->Quit(); | 555 MessageLoop::current()->Quit(); |
492 } | 556 } |
DaveMoore
2010/07/28 04:44:33
nit: nl
Chris Masone
2010/07/28 05:55:52
Done.
| |
493 // Compatible with LoginStatusConsumer::OnLoginFailure() | 557 // Compatible with LoginStatusConsumer::OnLoginFailure() |
494 static void QuitAndFail(const std::string& error) { | 558 static void OnFailQuit(const std::string& error) { |
559 MessageLoop::current()->Quit(); | |
560 } | |
DaveMoore
2010/07/28 04:44:33
nit: nl
Chris Masone
2010/07/28 05:55:52
Done.
| |
561 static void OnFailQuitAndFail(const std::string& error) { | |
495 ADD_FAILURE() << "Login should have succeeded!"; | 562 ADD_FAILURE() << "Login should have succeeded!"; |
496 MessageLoop::current()->Quit(); | 563 MessageLoop::current()->Quit(); |
497 } | 564 } |
oshima
2010/07/28 08:00:10
better to move these static functions to anonymous
| |
498 | 565 |
499 TEST_F(GoogleAuthenticatorTest, LocalaccountLogin) { | 566 TEST_F(GoogleAuthenticatorTest, LocalaccountLogin) { |
500 // This test checks the logic that governs asynchronously reading the | 567 // This test checks the logic that governs asynchronously reading the |
501 // localaccount name off disk and trying to authenticate against it | 568 // localaccount name off disk and trying to authenticate against it |
502 // simultaneously. | 569 // simultaneously. |
503 MessageLoop message_loop(MessageLoop::TYPE_UI); | 570 MessageLoop message_loop(MessageLoop::TYPE_UI); |
504 ChromeThread ui_thread(ChromeThread::UI, &message_loop); | 571 ChromeThread ui_thread(ChromeThread::UI, &message_loop); |
505 | 572 |
506 MockConsumer consumer; | 573 MockConsumer consumer; |
507 ON_CALL(consumer, OnLoginSuccess(username_, _)) | |
508 .WillByDefault(Invoke(Quit)); | |
509 EXPECT_CALL(consumer, OnLoginSuccess(username_, _)) | 574 EXPECT_CALL(consumer, OnLoginSuccess(username_, _)) |
510 .Times(1) | 575 .WillOnce(Invoke(OnSuccessQuit)) |
511 .RetiresOnSaturation(); | 576 .RetiresOnSaturation(); |
512 EXPECT_CALL(*mock_library_, MountForBwsi(_)) | 577 EXPECT_CALL(*mock_library_, MountForBwsi(_)) |
513 .WillOnce(Return(true)) | 578 .WillOnce(Return(true)) |
514 .RetiresOnSaturation(); | 579 .RetiresOnSaturation(); |
515 // Enable the test to terminate (and fail), even if the login fails. | 580 // Enable the test to terminate (and fail), even if the login fails. |
516 ON_CALL(consumer, OnLoginFailure(_)) | 581 ON_CALL(consumer, OnLoginFailure(_)) |
517 .WillByDefault(Invoke(QuitAndFail)); | 582 .WillByDefault(Invoke(OnFailQuitAndFail)); |
518 | 583 |
519 // Manually prep for login, so that localaccount isn't set for us. | 584 // Manually prep for login, so that localaccount isn't set for us. |
520 scoped_refptr<GoogleAuthenticator> auth(new GoogleAuthenticator(&consumer)); | 585 scoped_refptr<GoogleAuthenticator> auth(new GoogleAuthenticator(&consumer)); |
521 auth->set_password_hash(hash_ascii_); | 586 auth->set_password_hash(hash_ascii_); |
522 auth->set_username(username_); | 587 auth->set_username(username_); |
523 | 588 |
524 // First, force a check of username_ against the localaccount -- which we | 589 // First, force a check of username_ against the localaccount -- which we |
525 // haven't yet gotten off disk. | 590 // haven't yet gotten off disk. |
526 ChromeThread::PostTask( | 591 ChromeThread::PostTask( |
527 ChromeThread::UI, FROM_HERE, | 592 ChromeThread::UI, FROM_HERE, |
(...skipping 27 matching lines...) Expand all Loading... | |
555 EXPECT_CALL(*mock_library_, Mount(username_, _, _)) | 620 EXPECT_CALL(*mock_library_, Mount(username_, _, _)) |
556 .WillOnce(Return(true)) | 621 .WillOnce(Return(true)) |
557 .RetiresOnSaturation(); | 622 .RetiresOnSaturation(); |
558 | 623 |
559 EXPECT_CALL(*mock_library_, GetSystemSalt()) | 624 EXPECT_CALL(*mock_library_, GetSystemSalt()) |
560 .WillOnce(Return(salt_v)) | 625 .WillOnce(Return(salt_v)) |
561 .RetiresOnSaturation(); | 626 .RetiresOnSaturation(); |
562 | 627 |
563 TestingProfile profile; | 628 TestingProfile profile; |
564 | 629 |
565 MockFactory factory; | 630 MockFactory<MockFetcher> factory; |
566 URLFetcher::set_factory(&factory); | 631 URLFetcher::set_factory(&factory); |
567 | 632 |
568 scoped_refptr<GoogleAuthenticator> auth(new GoogleAuthenticator(&consumer)); | 633 scoped_refptr<GoogleAuthenticator> auth(new GoogleAuthenticator(&consumer)); |
569 auth->AuthenticateToLogin( | 634 auth->AuthenticateToLogin( |
570 &profile, username_, hash_ascii_, std::string(), std::string()); | 635 &profile, username_, hash_ascii_, std::string(), std::string()); |
571 | 636 |
572 URLFetcher::set_factory(NULL); | 637 URLFetcher::set_factory(NULL); |
573 message_loop.RunAllPending(); | 638 message_loop.RunAllPending(); |
574 } | 639 } |
575 | 640 |
576 TEST_F(GoogleAuthenticatorTest, CancelLogin) { | 641 TEST_F(GoogleAuthenticatorTest, CancelLogin) { |
577 MessageLoopForUI message_loop; | 642 MessageLoop message_loop(MessageLoop::TYPE_UI); |
oshima
2010/07/28 08:00:10
just for my understanding. what's the difference?
Chris Masone
2010/07/28 15:25:30
You can't call Run() on a MessageLoopForUI, only R
| |
578 ChromeThread ui_thread(ChromeThread::UI, &message_loop); | 643 ChromeThread ui_thread(ChromeThread::UI, &message_loop); |
579 chromeos::CryptohomeBlob salt_v(fake_hash_, fake_hash_ + sizeof(fake_hash_)); | 644 chromeos::CryptohomeBlob salt_v(fake_hash_, fake_hash_ + sizeof(fake_hash_)); |
580 | 645 |
581 MockConsumer consumer; | 646 MockConsumer consumer; |
582 EXPECT_CALL(consumer, OnLoginFailure(_)) | 647 EXPECT_CALL(consumer, OnLoginFailure(_)) |
583 .Times(1) | 648 .WillOnce(Invoke(OnFailQuit)) |
584 .RetiresOnSaturation(); | 649 .RetiresOnSaturation(); |
650 ON_CALL(consumer, OnLoginSuccess(username_, _)) | |
651 .WillByDefault(Invoke(OnSuccessQuitAndFail)); | |
585 | 652 |
586 EXPECT_CALL(*mock_library_, GetSystemSalt()) | 653 EXPECT_CALL(*mock_library_, GetSystemSalt()) |
587 .WillOnce(Return(salt_v)) | 654 .WillOnce(Return(salt_v)) |
588 .RetiresOnSaturation(); | 655 .RetiresOnSaturation(); |
589 EXPECT_CALL(*mock_library_, CheckKey(username_, _)) | 656 EXPECT_CALL(*mock_library_, CheckKey(username_, _)) |
590 .WillOnce(Return(false)) | 657 .WillOnce(Return(false)) |
591 .RetiresOnSaturation(); | 658 .RetiresOnSaturation(); |
592 | 659 |
593 TestingProfile profile; | 660 TestingProfile profile; |
594 | 661 |
595 MockFactory factory; | 662 MockFactory<TimedMockFetcher> factory; |
596 factory.set_success(false); | |
597 URLFetcher::set_factory(&factory); | 663 URLFetcher::set_factory(&factory); |
598 | 664 |
599 scoped_refptr<GoogleAuthenticator> auth(new GoogleAuthenticator(&consumer)); | 665 scoped_refptr<GoogleAuthenticator> auth(new GoogleAuthenticator(&consumer)); |
600 ReadLocalaccountFile(auth.get(), ""); // No localaccount file. | 666 |
667 // For when |auth| tries to load the localaccount file. | |
668 ChromeThread file_thread(ChromeThread::FILE); | |
669 file_thread.Start(); | |
670 | |
601 auth->AuthenticateToLogin( | 671 auth->AuthenticateToLogin( |
602 &profile, username_, hash_ascii_, std::string(), std::string()); | 672 &profile, username_, hash_ascii_, std::string(), std::string()); |
603 CancelLogin(auth.get()); | 673 CancelLogin(auth.get()); |
674 | |
604 URLFetcher::set_factory(NULL); | 675 URLFetcher::set_factory(NULL); |
605 message_loop.RunAllPending(); | 676 message_loop.Run(); |
677 } | |
678 | |
679 TEST_F(GoogleAuthenticatorTest, CancelLoginAlreadyGotLocalaccount) { | |
680 MessageLoop message_loop(MessageLoop::TYPE_UI); | |
681 ChromeThread ui_thread(ChromeThread::UI, &message_loop); | |
682 chromeos::CryptohomeBlob salt_v(fake_hash_, fake_hash_ + sizeof(fake_hash_)); | |
683 | |
684 MockConsumer consumer; | |
685 EXPECT_CALL(consumer, OnLoginFailure(_)) | |
686 .WillOnce(Invoke(OnFailQuit)) | |
687 .RetiresOnSaturation(); | |
688 ON_CALL(consumer, OnLoginSuccess(username_, _)) | |
689 .WillByDefault(Invoke(OnSuccessQuitAndFail)); | |
690 | |
691 EXPECT_CALL(*mock_library_, GetSystemSalt()) | |
692 .WillOnce(Return(salt_v)) | |
693 .RetiresOnSaturation(); | |
694 EXPECT_CALL(*mock_library_, CheckKey(username_, _)) | |
695 .WillOnce(Return(false)) | |
696 .RetiresOnSaturation(); | |
697 | |
698 TestingProfile profile; | |
699 | |
700 MockFactory<TimedMockFetcher> factory; | |
701 URLFetcher::set_factory(&factory); | |
702 | |
703 scoped_refptr<GoogleAuthenticator> auth(new GoogleAuthenticator(&consumer)); | |
704 ReadLocalaccountFile(auth.get(), ""); | |
705 | |
706 auth->AuthenticateToLogin( | |
707 &profile, username_, hash_ascii_, std::string(), std::string()); | |
708 CancelLogin(auth.get()); | |
709 | |
710 URLFetcher::set_factory(NULL); | |
711 message_loop.Run(); | |
606 } | 712 } |
607 | 713 |
608 } // namespace chromeos | 714 } // namespace chromeos |
OLD | NEW |