| 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 // A complete set of unit tests for GaiaAuthFetcher. | 5 // A complete set of unit tests for GaiaAuthFetcher. |
| 6 // Originally ported from GoogleAuthenticator tests. | 6 // Originally ported from GoogleAuthenticator tests. |
| 7 | 7 |
| 8 #include "chrome/common/net/gaia/gaia_auth_fetcher_unittest.h" | 8 #include "chrome/common/net/gaia/gaia_auth_fetcher_unittest.h" |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "content/test/test_url_fetcher_factory.h" | 21 #include "content/test/test_url_fetcher_factory.h" |
| 22 #include "googleurl/src/gurl.h" | 22 #include "googleurl/src/gurl.h" |
| 23 #include "net/base/load_flags.h" | 23 #include "net/base/load_flags.h" |
| 24 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
| 25 #include "net/url_request/url_request_status.h" | 25 #include "net/url_request/url_request_status.h" |
| 26 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 28 | 28 |
| 29 using ::testing::_; | 29 using ::testing::_; |
| 30 | 30 |
| 31 namespace { | |
| 32 static const char kGetAuthCodeValidCookie[] = | |
| 33 "oauth_code=test-code; Path=/test; Secure; HttpOnly"; | |
| 34 static const char kGetAuthCodeCookieNoSecure[] = | |
| 35 "oauth_code=test-code; Path=/test; HttpOnly"; | |
| 36 static const char kGetAuthCodeCookieNoHttpOnly[] = | |
| 37 "oauth_code=test-code; Path=/test; Secure"; | |
| 38 static const char kGetAuthCodeCookieNoOAuthCode[] = | |
| 39 "Path=/test; Secure; HttpOnly"; | |
| 40 static const char kGetTokenPairValidResponse[] = | |
| 41 "{" | |
| 42 " \"refresh_token\": \"rt1\"," | |
| 43 " \"access_token\": \"at1\"," | |
| 44 " \"expires_in\": 3600," | |
| 45 " \"token_type\": \"Bearer\"" | |
| 46 "}"; | |
| 47 static const char kGetTokenPairResponseNoRefreshToken[] = | |
| 48 "{" | |
| 49 " \"access_token\": \"at1\"," | |
| 50 " \"expires_in\": 3600," | |
| 51 " \"token_type\": \"Bearer\"" | |
| 52 "}"; | |
| 53 static const char kGetTokenPairResponseNoAccessToken[] = | |
| 54 "{" | |
| 55 " \"refresh_token\": \"rt1\"," | |
| 56 " \"expires_in\": 3600," | |
| 57 " \"token_type\": \"Bearer\"" | |
| 58 "}"; | |
| 59 static const char kGetTokenPairResponseNoExpiresIn[] = | |
| 60 "{" | |
| 61 " \"refresh_token\": \"rt1\"," | |
| 62 " \"access_token\": \"at1\"," | |
| 63 " \"token_type\": \"Bearer\"" | |
| 64 "}"; | |
| 65 } // namespace | |
| 66 | |
| 67 MockFetcher::MockFetcher(bool success, | 31 MockFetcher::MockFetcher(bool success, |
| 68 const GURL& url, | 32 const GURL& url, |
| 69 const std::string& results, | 33 const std::string& results, |
| 70 content::URLFetcher::RequestType request_type, | 34 content::URLFetcher::RequestType request_type, |
| 71 content::URLFetcherDelegate* d) | 35 content::URLFetcherDelegate* d) |
| 72 : TestURLFetcher(0, url, d) { | 36 : TestURLFetcher(0, url, d) { |
| 73 set_url(url); | 37 set_url(url); |
| 74 net::URLRequestStatus::Status code; | 38 net::URLRequestStatus::Status code; |
| 75 | 39 |
| 76 if (success) { | 40 if (success) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 105 void MockFetcher::Start() { | 69 void MockFetcher::Start() { |
| 106 delegate()->OnURLFetchComplete(this); | 70 delegate()->OnURLFetchComplete(this); |
| 107 } | 71 } |
| 108 | 72 |
| 109 class GaiaAuthFetcherTest : public testing::Test { | 73 class GaiaAuthFetcherTest : public testing::Test { |
| 110 public: | 74 public: |
| 111 GaiaAuthFetcherTest() | 75 GaiaAuthFetcherTest() |
| 112 : client_login_source_(GaiaUrls::GetInstance()->client_login_url()), | 76 : client_login_source_(GaiaUrls::GetInstance()->client_login_url()), |
| 113 issue_auth_token_source_( | 77 issue_auth_token_source_( |
| 114 GaiaUrls::GetInstance()->issue_auth_token_url()), | 78 GaiaUrls::GetInstance()->issue_auth_token_url()), |
| 115 client_login_to_oauth2_source_( | |
| 116 GaiaUrls::GetInstance()->client_login_to_oauth2_url()), | |
| 117 oauth2_token_source_(GaiaUrls::GetInstance()->oauth2_token_url()), | |
| 118 token_auth_source_(GaiaUrls::GetInstance()->token_auth_url()), | 79 token_auth_source_(GaiaUrls::GetInstance()->token_auth_url()), |
| 119 merge_session_source_(GaiaUrls::GetInstance()->merge_session_url()) {} | 80 merge_session_source_(GaiaUrls::GetInstance()->merge_session_url()) {} |
| 120 | 81 |
| 121 void RunParsingTest(const std::string& data, | 82 void RunParsingTest(const std::string& data, |
| 122 const std::string& sid, | 83 const std::string& sid, |
| 123 const std::string& lsid, | 84 const std::string& lsid, |
| 124 const std::string& token) { | 85 const std::string& token) { |
| 125 std::string out_sid; | 86 std::string out_sid; |
| 126 std::string out_lsid; | 87 std::string out_lsid; |
| 127 std::string out_token; | 88 std::string out_token; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 152 &out_captcha_token); | 113 &out_captcha_token); |
| 153 EXPECT_EQ(error, out_error); | 114 EXPECT_EQ(error, out_error); |
| 154 EXPECT_EQ(error_url, out_error_url); | 115 EXPECT_EQ(error_url, out_error_url); |
| 155 EXPECT_EQ(captcha_url, out_captcha_url); | 116 EXPECT_EQ(captcha_url, out_captcha_url); |
| 156 EXPECT_EQ(captcha_token, out_captcha_token); | 117 EXPECT_EQ(captcha_token, out_captcha_token); |
| 157 } | 118 } |
| 158 | 119 |
| 159 net::ResponseCookies cookies_; | 120 net::ResponseCookies cookies_; |
| 160 GURL client_login_source_; | 121 GURL client_login_source_; |
| 161 GURL issue_auth_token_source_; | 122 GURL issue_auth_token_source_; |
| 162 GURL client_login_to_oauth2_source_; | |
| 163 GURL oauth2_token_source_; | |
| 164 GURL token_auth_source_; | 123 GURL token_auth_source_; |
| 165 GURL merge_session_source_; | 124 GURL merge_session_source_; |
| 166 TestingProfile profile_; | 125 TestingProfile profile_; |
| 167 protected: | 126 protected: |
| 168 MessageLoop message_loop_; | 127 MessageLoop message_loop_; |
| 169 }; | 128 }; |
| 170 | 129 |
| 171 class MockGaiaConsumer : public GaiaAuthConsumer { | 130 class MockGaiaConsumer : public GaiaAuthConsumer { |
| 172 public: | 131 public: |
| 173 MockGaiaConsumer() {} | 132 MockGaiaConsumer() {} |
| 174 ~MockGaiaConsumer() {} | 133 ~MockGaiaConsumer() {} |
| 175 | 134 |
| 176 MOCK_METHOD1(OnClientLoginSuccess, void(const ClientLoginResult& result)); | 135 MOCK_METHOD1(OnClientLoginSuccess, void(const ClientLoginResult& result)); |
| 177 MOCK_METHOD2(OnIssueAuthTokenSuccess, void(const std::string& service, | 136 MOCK_METHOD2(OnIssueAuthTokenSuccess, void(const std::string& service, |
| 178 const std::string& token)); | 137 const std::string& token)); |
| 179 MOCK_METHOD3(OnOAuthLoginTokenSuccess, | |
| 180 void(const std::string& refresh_token, | |
| 181 const std::string& access_token, | |
| 182 int expires_in_secs)); | |
| 183 MOCK_METHOD1(OnTokenAuthSuccess, void(const std::string& data)); | 138 MOCK_METHOD1(OnTokenAuthSuccess, void(const std::string& data)); |
| 184 MOCK_METHOD1(OnMergeSessionSuccess, void(const std::string& data)); | 139 MOCK_METHOD1(OnMergeSessionSuccess, void(const std::string& data)); |
| 185 MOCK_METHOD1(OnClientLoginFailure, | 140 MOCK_METHOD1(OnClientLoginFailure, |
| 186 void(const GoogleServiceAuthError& error)); | 141 void(const GoogleServiceAuthError& error)); |
| 187 MOCK_METHOD2(OnIssueAuthTokenFailure, void(const std::string& service, | 142 MOCK_METHOD2(OnIssueAuthTokenFailure, void(const std::string& service, |
| 188 const GoogleServiceAuthError& error)); | 143 const GoogleServiceAuthError& error)); |
| 189 MOCK_METHOD1(OnOAuthLoginTokenFailure, | |
| 190 void(const GoogleServiceAuthError& error)); | |
| 191 MOCK_METHOD1(OnTokenAuthFailure, void(const GoogleServiceAuthError& error)); | 144 MOCK_METHOD1(OnTokenAuthFailure, void(const GoogleServiceAuthError& error)); |
| 192 MOCK_METHOD1(OnMergeSessionFailure, void( | 145 MOCK_METHOD1(OnMergeSessionFailure, void( |
| 193 const GoogleServiceAuthError& error)); | 146 const GoogleServiceAuthError& error)); |
| 194 }; | 147 }; |
| 195 | 148 |
| 196 TEST_F(GaiaAuthFetcherTest, ErrorComparator) { | 149 TEST_F(GaiaAuthFetcherTest, ErrorComparator) { |
| 197 GoogleServiceAuthError expected_error = | 150 GoogleServiceAuthError expected_error = |
| 198 GoogleServiceAuthError::FromConnectionError(-101); | 151 GoogleServiceAuthError::FromConnectionError(-101); |
| 199 | 152 |
| 200 GoogleServiceAuthError matching_error = | 153 GoogleServiceAuthError matching_error = |
| (...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 563 | 516 |
| 564 EXPECT_TRUE(auth.HasPendingFetch()); | 517 EXPECT_TRUE(auth.HasPendingFetch()); |
| 565 MockFetcher mock_fetcher( | 518 MockFetcher mock_fetcher( |
| 566 issue_auth_token_source_, | 519 issue_auth_token_source_, |
| 567 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), | 520 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), |
| 568 RC_FORBIDDEN, cookies_, "", content::URLFetcher::GET, &auth); | 521 RC_FORBIDDEN, cookies_, "", content::URLFetcher::GET, &auth); |
| 569 auth.OnURLFetchComplete(&mock_fetcher); | 522 auth.OnURLFetchComplete(&mock_fetcher); |
| 570 EXPECT_FALSE(auth.HasPendingFetch()); | 523 EXPECT_FALSE(auth.HasPendingFetch()); |
| 571 } | 524 } |
| 572 | 525 |
| 573 TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenSuccess) { | |
| 574 MockGaiaConsumer consumer; | |
| 575 EXPECT_CALL(consumer, OnOAuthLoginTokenSuccess("rt1", "at1", 3600)) | |
| 576 .Times(1); | |
| 577 | |
| 578 TestingProfile profile; | |
| 579 | |
| 580 TestURLFetcherFactory factory; | |
| 581 GaiaAuthFetcher auth(&consumer, std::string(), | |
| 582 profile_.GetRequestContext()); | |
| 583 auth.StartOAuthLoginTokenFetch("lso_token"); | |
| 584 | |
| 585 net::ResponseCookies cookies; | |
| 586 cookies.push_back(kGetAuthCodeValidCookie); | |
| 587 EXPECT_TRUE(auth.HasPendingFetch()); | |
| 588 MockFetcher mock_fetcher1( | |
| 589 client_login_to_oauth2_source_, | |
| 590 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), | |
| 591 RC_REQUEST_OK, cookies, "", | |
| 592 content::URLFetcher::POST, &auth); | |
| 593 auth.OnURLFetchComplete(&mock_fetcher1); | |
| 594 EXPECT_TRUE(auth.HasPendingFetch()); | |
| 595 MockFetcher mock_fetcher2( | |
| 596 oauth2_token_source_, | |
| 597 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), | |
| 598 RC_REQUEST_OK, cookies_, kGetTokenPairValidResponse, | |
| 599 content::URLFetcher::POST, &auth); | |
| 600 auth.OnURLFetchComplete(&mock_fetcher2); | |
| 601 EXPECT_FALSE(auth.HasPendingFetch()); | |
| 602 } | |
| 603 | |
| 604 TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenClientLoginToOAuth2Failure) { | |
| 605 MockGaiaConsumer consumer; | |
| 606 EXPECT_CALL(consumer, OnOAuthLoginTokenFailure(_)) | |
| 607 .Times(1); | |
| 608 | |
| 609 TestingProfile profile; | |
| 610 | |
| 611 TestURLFetcherFactory factory; | |
| 612 GaiaAuthFetcher auth(&consumer, std::string(), | |
| 613 profile_.GetRequestContext()); | |
| 614 auth.StartOAuthLoginTokenFetch("lso_token"); | |
| 615 | |
| 616 net::ResponseCookies cookies; | |
| 617 cookies.push_back(kGetAuthCodeCookieNoSecure); | |
| 618 EXPECT_TRUE(auth.HasPendingFetch()); | |
| 619 MockFetcher mock_fetcher( | |
| 620 client_login_to_oauth2_source_, | |
| 621 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), | |
| 622 RC_REQUEST_OK, cookies, "", | |
| 623 content::URLFetcher::POST, &auth); | |
| 624 auth.OnURLFetchComplete(&mock_fetcher); | |
| 625 EXPECT_FALSE(auth.HasPendingFetch()); | |
| 626 } | |
| 627 | |
| 628 TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenOAuth2TokenPairFailure) { | |
| 629 MockGaiaConsumer consumer; | |
| 630 EXPECT_CALL(consumer, OnOAuthLoginTokenFailure(_)) | |
| 631 .Times(1); | |
| 632 | |
| 633 TestingProfile profile; | |
| 634 | |
| 635 TestURLFetcherFactory factory; | |
| 636 GaiaAuthFetcher auth(&consumer, std::string(), | |
| 637 profile_.GetRequestContext()); | |
| 638 auth.StartOAuthLoginTokenFetch("lso_token"); | |
| 639 | |
| 640 net::ResponseCookies cookies; | |
| 641 cookies.push_back(kGetAuthCodeValidCookie); | |
| 642 EXPECT_TRUE(auth.HasPendingFetch()); | |
| 643 MockFetcher mock_fetcher1( | |
| 644 client_login_to_oauth2_source_, | |
| 645 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), | |
| 646 RC_REQUEST_OK, cookies, "", | |
| 647 content::URLFetcher::POST, &auth); | |
| 648 auth.OnURLFetchComplete(&mock_fetcher1); | |
| 649 EXPECT_TRUE(auth.HasPendingFetch()); | |
| 650 MockFetcher mock_fetcher2( | |
| 651 oauth2_token_source_, | |
| 652 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), | |
| 653 RC_REQUEST_OK, cookies_, kGetTokenPairResponseNoRefreshToken, | |
| 654 content::URLFetcher::POST, &auth); | |
| 655 auth.OnURLFetchComplete(&mock_fetcher2); | |
| 656 EXPECT_FALSE(auth.HasPendingFetch()); | |
| 657 } | |
| 658 | |
| 659 TEST_F(GaiaAuthFetcherTest, TokenAuthSuccess) { | 526 TEST_F(GaiaAuthFetcherTest, TokenAuthSuccess) { |
| 660 MockGaiaConsumer consumer; | 527 MockGaiaConsumer consumer; |
| 661 EXPECT_CALL(consumer, OnTokenAuthSuccess("<html></html>")) | 528 EXPECT_CALL(consumer, OnTokenAuthSuccess("<html></html>")) |
| 662 .Times(1); | 529 .Times(1); |
| 663 | 530 |
| 664 TestingProfile profile; | 531 TestingProfile profile; |
| 665 TestURLFetcherFactory factory; | 532 TestURLFetcherFactory factory; |
| 666 | 533 |
| 667 GaiaAuthFetcher auth(&consumer, std::string(), | 534 GaiaAuthFetcher auth(&consumer, std::string(), |
| 668 profile_.GetRequestContext()); | 535 profile_.GetRequestContext()); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 765 test_fetcher->set_url(final_url); | 632 test_fetcher->set_url(final_url); |
| 766 test_fetcher->set_status( | 633 test_fetcher->set_status( |
| 767 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); | 634 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); |
| 768 test_fetcher->set_response_code(RC_REQUEST_OK); | 635 test_fetcher->set_response_code(RC_REQUEST_OK); |
| 769 test_fetcher->set_cookies(cookies_); | 636 test_fetcher->set_cookies(cookies_); |
| 770 test_fetcher->SetResponseString("<html></html>"); | 637 test_fetcher->SetResponseString("<html></html>"); |
| 771 | 638 |
| 772 auth.OnURLFetchComplete(test_fetcher); | 639 auth.OnURLFetchComplete(test_fetcher); |
| 773 EXPECT_FALSE(auth.HasPendingFetch()); | 640 EXPECT_FALSE(auth.HasPendingFetch()); |
| 774 } | 641 } |
| 775 | |
| 776 TEST_F(GaiaAuthFetcherTest, ParseClientLoginToOAuth2Response) { | |
| 777 { // No cookies. | |
| 778 std::string auth_code; | |
| 779 net::ResponseCookies cookies; | |
| 780 EXPECT_FALSE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response( | |
| 781 cookies, &auth_code)); | |
| 782 EXPECT_EQ("", auth_code); | |
| 783 } | |
| 784 { // Few cookies, nothing appropriate. | |
| 785 std::string auth_code; | |
| 786 net::ResponseCookies cookies; | |
| 787 cookies.push_back(kGetAuthCodeCookieNoSecure); | |
| 788 cookies.push_back(kGetAuthCodeCookieNoHttpOnly); | |
| 789 cookies.push_back(kGetAuthCodeCookieNoOAuthCode); | |
| 790 EXPECT_FALSE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response( | |
| 791 cookies, &auth_code)); | |
| 792 EXPECT_EQ("", auth_code); | |
| 793 } | |
| 794 { // Few cookies, one of them is valid. | |
| 795 std::string auth_code; | |
| 796 net::ResponseCookies cookies; | |
| 797 cookies.push_back(kGetAuthCodeCookieNoSecure); | |
| 798 cookies.push_back(kGetAuthCodeCookieNoHttpOnly); | |
| 799 cookies.push_back(kGetAuthCodeCookieNoOAuthCode); | |
| 800 cookies.push_back(kGetAuthCodeValidCookie); | |
| 801 EXPECT_TRUE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response( | |
| 802 cookies, &auth_code)); | |
| 803 EXPECT_EQ("test-code", auth_code); | |
| 804 } | |
| 805 { // Single valid cookie (like in real responses). | |
| 806 std::string auth_code; | |
| 807 net::ResponseCookies cookies; | |
| 808 cookies.push_back(kGetAuthCodeValidCookie); | |
| 809 EXPECT_TRUE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response( | |
| 810 cookies, &auth_code)); | |
| 811 EXPECT_EQ("test-code", auth_code); | |
| 812 } | |
| 813 } | |
| 814 | |
| 815 TEST_F(GaiaAuthFetcherTest, ParseOAuth2TokenPairResponse) { | |
| 816 { // No data. | |
| 817 std::string rt; | |
| 818 std::string at; | |
| 819 int exp = -1; | |
| 820 EXPECT_FALSE(GaiaAuthFetcher::ParseOAuth2TokenPairResponse( | |
| 821 "", &rt, &at, &exp)); | |
| 822 EXPECT_EQ("", rt); | |
| 823 EXPECT_EQ("", at); | |
| 824 EXPECT_EQ(-1, exp); | |
| 825 } | |
| 826 { // No refresh token. | |
| 827 std::string rt; | |
| 828 std::string at; | |
| 829 int exp = -1; | |
| 830 EXPECT_FALSE(GaiaAuthFetcher::ParseOAuth2TokenPairResponse( | |
| 831 kGetTokenPairResponseNoRefreshToken, &rt, &at, &exp)); | |
| 832 EXPECT_EQ("", rt); | |
| 833 EXPECT_EQ("", at); | |
| 834 EXPECT_EQ(-1, exp); | |
| 835 } | |
| 836 { // No access token. | |
| 837 std::string rt; | |
| 838 std::string at; | |
| 839 int exp = -1; | |
| 840 EXPECT_FALSE(GaiaAuthFetcher::ParseOAuth2TokenPairResponse( | |
| 841 kGetTokenPairResponseNoAccessToken, &rt, &at, &exp)); | |
| 842 EXPECT_EQ("", rt); | |
| 843 EXPECT_EQ("", at); | |
| 844 EXPECT_EQ(-1, exp); | |
| 845 } | |
| 846 { // No expiration. | |
| 847 std::string rt; | |
| 848 std::string at; | |
| 849 int exp = -1; | |
| 850 EXPECT_FALSE(GaiaAuthFetcher::ParseOAuth2TokenPairResponse( | |
| 851 kGetTokenPairResponseNoExpiresIn, &rt, &at, &exp)); | |
| 852 EXPECT_EQ("", rt); | |
| 853 EXPECT_EQ("", at); | |
| 854 EXPECT_EQ(-1, exp); | |
| 855 } | |
| 856 { // Valid response. | |
| 857 std::string rt; | |
| 858 std::string at; | |
| 859 int exp = -1; | |
| 860 EXPECT_TRUE(GaiaAuthFetcher::ParseOAuth2TokenPairResponse( | |
| 861 kGetTokenPairValidResponse, &rt, &at, &exp)); | |
| 862 EXPECT_EQ("rt1", rt); | |
| 863 EXPECT_EQ("at1", at); | |
| 864 EXPECT_EQ(3600, exp); | |
| 865 } | |
| 866 } | |
| OLD | NEW |