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

Side by Side Diff: chrome/common/net/gaia/gaia_auth_fetcher_unittest.cc

Issue 8662002: Revive reverted CL with fix for CrOS build. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 1 month 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/common/net/gaia/gaia_auth_fetcher.cc ('k') | chrome/common/net/gaia/gaia_urls.h » ('j') | 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) 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
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
31 MockFetcher::MockFetcher(bool success, 67 MockFetcher::MockFetcher(bool success,
32 const GURL& url, 68 const GURL& url,
33 const std::string& results, 69 const std::string& results,
34 content::URLFetcher::RequestType request_type, 70 content::URLFetcher::RequestType request_type,
35 content::URLFetcherDelegate* d) 71 content::URLFetcherDelegate* d)
36 : TestURLFetcher(0, url, d) { 72 : TestURLFetcher(0, url, d) {
37 set_url(url); 73 set_url(url);
38 net::URLRequestStatus::Status code; 74 net::URLRequestStatus::Status code;
39 75
40 if (success) { 76 if (success) {
(...skipping 28 matching lines...) Expand all
69 void MockFetcher::Start() { 105 void MockFetcher::Start() {
70 delegate()->OnURLFetchComplete(this); 106 delegate()->OnURLFetchComplete(this);
71 } 107 }
72 108
73 class GaiaAuthFetcherTest : public testing::Test { 109 class GaiaAuthFetcherTest : public testing::Test {
74 public: 110 public:
75 GaiaAuthFetcherTest() 111 GaiaAuthFetcherTest()
76 : client_login_source_(GaiaUrls::GetInstance()->client_login_url()), 112 : client_login_source_(GaiaUrls::GetInstance()->client_login_url()),
77 issue_auth_token_source_( 113 issue_auth_token_source_(
78 GaiaUrls::GetInstance()->issue_auth_token_url()), 114 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()),
79 token_auth_source_(GaiaUrls::GetInstance()->token_auth_url()), 118 token_auth_source_(GaiaUrls::GetInstance()->token_auth_url()),
80 merge_session_source_(GaiaUrls::GetInstance()->merge_session_url()) {} 119 merge_session_source_(GaiaUrls::GetInstance()->merge_session_url()) {}
81 120
82 void RunParsingTest(const std::string& data, 121 void RunParsingTest(const std::string& data,
83 const std::string& sid, 122 const std::string& sid,
84 const std::string& lsid, 123 const std::string& lsid,
85 const std::string& token) { 124 const std::string& token) {
86 std::string out_sid; 125 std::string out_sid;
87 std::string out_lsid; 126 std::string out_lsid;
88 std::string out_token; 127 std::string out_token;
(...skipping 24 matching lines...) Expand all
113 &out_captcha_token); 152 &out_captcha_token);
114 EXPECT_EQ(error, out_error); 153 EXPECT_EQ(error, out_error);
115 EXPECT_EQ(error_url, out_error_url); 154 EXPECT_EQ(error_url, out_error_url);
116 EXPECT_EQ(captcha_url, out_captcha_url); 155 EXPECT_EQ(captcha_url, out_captcha_url);
117 EXPECT_EQ(captcha_token, out_captcha_token); 156 EXPECT_EQ(captcha_token, out_captcha_token);
118 } 157 }
119 158
120 net::ResponseCookies cookies_; 159 net::ResponseCookies cookies_;
121 GURL client_login_source_; 160 GURL client_login_source_;
122 GURL issue_auth_token_source_; 161 GURL issue_auth_token_source_;
162 GURL client_login_to_oauth2_source_;
163 GURL oauth2_token_source_;
123 GURL token_auth_source_; 164 GURL token_auth_source_;
124 GURL merge_session_source_; 165 GURL merge_session_source_;
125 TestingProfile profile_; 166 TestingProfile profile_;
126 protected: 167 protected:
127 MessageLoop message_loop_; 168 MessageLoop message_loop_;
128 }; 169 };
129 170
130 class MockGaiaConsumer : public GaiaAuthConsumer { 171 class MockGaiaConsumer : public GaiaAuthConsumer {
131 public: 172 public:
132 MockGaiaConsumer() {} 173 MockGaiaConsumer() {}
133 ~MockGaiaConsumer() {} 174 ~MockGaiaConsumer() {}
134 175
135 MOCK_METHOD1(OnClientLoginSuccess, void(const ClientLoginResult& result)); 176 MOCK_METHOD1(OnClientLoginSuccess, void(const ClientLoginResult& result));
136 MOCK_METHOD2(OnIssueAuthTokenSuccess, void(const std::string& service, 177 MOCK_METHOD2(OnIssueAuthTokenSuccess, void(const std::string& service,
137 const std::string& token)); 178 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));
138 MOCK_METHOD1(OnTokenAuthSuccess, void(const std::string& data)); 183 MOCK_METHOD1(OnTokenAuthSuccess, void(const std::string& data));
139 MOCK_METHOD1(OnMergeSessionSuccess, void(const std::string& data)); 184 MOCK_METHOD1(OnMergeSessionSuccess, void(const std::string& data));
140 MOCK_METHOD1(OnClientLoginFailure, 185 MOCK_METHOD1(OnClientLoginFailure,
141 void(const GoogleServiceAuthError& error)); 186 void(const GoogleServiceAuthError& error));
142 MOCK_METHOD2(OnIssueAuthTokenFailure, void(const std::string& service, 187 MOCK_METHOD2(OnIssueAuthTokenFailure, void(const std::string& service,
143 const GoogleServiceAuthError& error)); 188 const GoogleServiceAuthError& error));
189 MOCK_METHOD1(OnOAuthLoginTokenFailure,
190 void(const GoogleServiceAuthError& error));
144 MOCK_METHOD1(OnTokenAuthFailure, void(const GoogleServiceAuthError& error)); 191 MOCK_METHOD1(OnTokenAuthFailure, void(const GoogleServiceAuthError& error));
145 MOCK_METHOD1(OnMergeSessionFailure, void( 192 MOCK_METHOD1(OnMergeSessionFailure, void(
146 const GoogleServiceAuthError& error)); 193 const GoogleServiceAuthError& error));
147 }; 194 };
148 195
149 TEST_F(GaiaAuthFetcherTest, ErrorComparator) { 196 TEST_F(GaiaAuthFetcherTest, ErrorComparator) {
150 GoogleServiceAuthError expected_error = 197 GoogleServiceAuthError expected_error =
151 GoogleServiceAuthError::FromConnectionError(-101); 198 GoogleServiceAuthError::FromConnectionError(-101);
152 199
153 GoogleServiceAuthError matching_error = 200 GoogleServiceAuthError matching_error =
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 563
517 EXPECT_TRUE(auth.HasPendingFetch()); 564 EXPECT_TRUE(auth.HasPendingFetch());
518 MockFetcher mock_fetcher( 565 MockFetcher mock_fetcher(
519 issue_auth_token_source_, 566 issue_auth_token_source_,
520 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), 567 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
521 RC_FORBIDDEN, cookies_, "", content::URLFetcher::GET, &auth); 568 RC_FORBIDDEN, cookies_, "", content::URLFetcher::GET, &auth);
522 auth.OnURLFetchComplete(&mock_fetcher); 569 auth.OnURLFetchComplete(&mock_fetcher);
523 EXPECT_FALSE(auth.HasPendingFetch()); 570 EXPECT_FALSE(auth.HasPendingFetch());
524 } 571 }
525 572
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 EXPECT_TRUE(auth.HasPendingFetch());
618 MockFetcher mock_fetcher(
619 client_login_to_oauth2_source_,
620 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
621 RC_FORBIDDEN, cookies, "",
622 content::URLFetcher::POST, &auth);
623 auth.OnURLFetchComplete(&mock_fetcher);
624 EXPECT_FALSE(auth.HasPendingFetch());
625 }
626
627 TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenOAuth2TokenPairFailure) {
628 MockGaiaConsumer consumer;
629 EXPECT_CALL(consumer, OnOAuthLoginTokenFailure(_))
630 .Times(1);
631
632 TestingProfile profile;
633
634 TestURLFetcherFactory factory;
635 GaiaAuthFetcher auth(&consumer, std::string(),
636 profile_.GetRequestContext());
637 auth.StartOAuthLoginTokenFetch("lso_token");
638
639 net::ResponseCookies cookies;
640 cookies.push_back(kGetAuthCodeValidCookie);
641 EXPECT_TRUE(auth.HasPendingFetch());
642 MockFetcher mock_fetcher1(
643 client_login_to_oauth2_source_,
644 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
645 RC_REQUEST_OK, cookies, "",
646 content::URLFetcher::POST, &auth);
647 auth.OnURLFetchComplete(&mock_fetcher1);
648 EXPECT_TRUE(auth.HasPendingFetch());
649 MockFetcher mock_fetcher2(
650 oauth2_token_source_,
651 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
652 RC_FORBIDDEN, cookies_, "",
653 content::URLFetcher::POST, &auth);
654 auth.OnURLFetchComplete(&mock_fetcher2);
655 EXPECT_FALSE(auth.HasPendingFetch());
656 }
657
526 TEST_F(GaiaAuthFetcherTest, TokenAuthSuccess) { 658 TEST_F(GaiaAuthFetcherTest, TokenAuthSuccess) {
527 MockGaiaConsumer consumer; 659 MockGaiaConsumer consumer;
528 EXPECT_CALL(consumer, OnTokenAuthSuccess("<html></html>")) 660 EXPECT_CALL(consumer, OnTokenAuthSuccess("<html></html>"))
529 .Times(1); 661 .Times(1);
530 662
531 TestingProfile profile; 663 TestingProfile profile;
532 TestURLFetcherFactory factory; 664 TestURLFetcherFactory factory;
533 665
534 GaiaAuthFetcher auth(&consumer, std::string(), 666 GaiaAuthFetcher auth(&consumer, std::string(),
535 profile_.GetRequestContext()); 667 profile_.GetRequestContext());
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
632 test_fetcher->set_url(final_url); 764 test_fetcher->set_url(final_url);
633 test_fetcher->set_status( 765 test_fetcher->set_status(
634 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); 766 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0));
635 test_fetcher->set_response_code(RC_REQUEST_OK); 767 test_fetcher->set_response_code(RC_REQUEST_OK);
636 test_fetcher->set_cookies(cookies_); 768 test_fetcher->set_cookies(cookies_);
637 test_fetcher->SetResponseString("<html></html>"); 769 test_fetcher->SetResponseString("<html></html>");
638 770
639 auth.OnURLFetchComplete(test_fetcher); 771 auth.OnURLFetchComplete(test_fetcher);
640 EXPECT_FALSE(auth.HasPendingFetch()); 772 EXPECT_FALSE(auth.HasPendingFetch());
641 } 773 }
774
775 TEST_F(GaiaAuthFetcherTest, ParseClientLoginToOAuth2Response) {
776 { // No cookies.
777 std::string auth_code;
778 net::ResponseCookies cookies;
779 EXPECT_FALSE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
780 cookies, &auth_code));
781 EXPECT_EQ("", auth_code);
782 }
783 { // Few cookies, nothing appropriate.
784 std::string auth_code;
785 net::ResponseCookies cookies;
786 cookies.push_back(kGetAuthCodeCookieNoSecure);
787 cookies.push_back(kGetAuthCodeCookieNoHttpOnly);
788 cookies.push_back(kGetAuthCodeCookieNoOAuthCode);
789 EXPECT_FALSE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
790 cookies, &auth_code));
791 EXPECT_EQ("", auth_code);
792 }
793 { // Few cookies, one of them is valid.
794 std::string auth_code;
795 net::ResponseCookies cookies;
796 cookies.push_back(kGetAuthCodeCookieNoSecure);
797 cookies.push_back(kGetAuthCodeCookieNoHttpOnly);
798 cookies.push_back(kGetAuthCodeCookieNoOAuthCode);
799 cookies.push_back(kGetAuthCodeValidCookie);
800 EXPECT_TRUE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
801 cookies, &auth_code));
802 EXPECT_EQ("test-code", auth_code);
803 }
804 { // Single valid cookie (like in real responses).
805 std::string auth_code;
806 net::ResponseCookies cookies;
807 cookies.push_back(kGetAuthCodeValidCookie);
808 EXPECT_TRUE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
809 cookies, &auth_code));
810 EXPECT_EQ("test-code", auth_code);
811 }
812 }
813
814 TEST_F(GaiaAuthFetcherTest, ParseOAuth2TokenPairResponse) {
815 { // No data.
816 std::string rt;
817 std::string at;
818 int exp = -1;
819 EXPECT_FALSE(GaiaAuthFetcher::ParseOAuth2TokenPairResponse(
820 "", &rt, &at, &exp));
821 EXPECT_EQ("", rt);
822 EXPECT_EQ("", at);
823 EXPECT_EQ(-1, exp);
824 }
825 { // No refresh token.
826 std::string rt;
827 std::string at;
828 int exp = -1;
829 EXPECT_FALSE(GaiaAuthFetcher::ParseOAuth2TokenPairResponse(
830 kGetTokenPairResponseNoRefreshToken, &rt, &at, &exp));
831 EXPECT_EQ("", rt);
832 EXPECT_EQ("", at);
833 EXPECT_EQ(-1, exp);
834 }
835 { // No access token.
836 std::string rt;
837 std::string at;
838 int exp = -1;
839 EXPECT_FALSE(GaiaAuthFetcher::ParseOAuth2TokenPairResponse(
840 kGetTokenPairResponseNoAccessToken, &rt, &at, &exp));
841 EXPECT_EQ("", rt);
842 EXPECT_EQ("", at);
843 EXPECT_EQ(-1, exp);
844 }
845 { // No expiration.
846 std::string rt;
847 std::string at;
848 int exp = -1;
849 EXPECT_FALSE(GaiaAuthFetcher::ParseOAuth2TokenPairResponse(
850 kGetTokenPairResponseNoExpiresIn, &rt, &at, &exp));
851 EXPECT_EQ("", rt);
852 EXPECT_EQ("", at);
853 EXPECT_EQ(-1, exp);
854 }
855 { // Valid response.
856 std::string rt;
857 std::string at;
858 int exp = -1;
859 EXPECT_TRUE(GaiaAuthFetcher::ParseOAuth2TokenPairResponse(
860 kGetTokenPairValidResponse, &rt, &at, &exp));
861 EXPECT_EQ("rt1", rt);
862 EXPECT_EQ("at1", at);
863 EXPECT_EQ(3600, exp);
864 }
865 }
OLDNEW
« no previous file with comments | « chrome/common/net/gaia/gaia_auth_fetcher.cc ('k') | chrome/common/net/gaia/gaia_urls.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698