OLD | NEW |
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 // 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 <string> | 8 #include <string> |
9 | 9 |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 static const char kClientOAuthValidResponse[] = | 50 static const char kClientOAuthValidResponse[] = |
51 "{" | 51 "{" |
52 " \"oauth2\": {" | 52 " \"oauth2\": {" |
53 " \"refresh_token\": \"rt1\"," | 53 " \"refresh_token\": \"rt1\"," |
54 " \"access_token\": \"at1\"," | 54 " \"access_token\": \"at1\"," |
55 " \"expires_in\": 3600," | 55 " \"expires_in\": 3600," |
56 " \"token_type\": \"Bearer\"" | 56 " \"token_type\": \"Bearer\"" |
57 " }" | 57 " }" |
58 "}"; | 58 "}"; |
59 | 59 |
60 static void ExpectCaptchaChallenge(const GoogleServiceAuthError& error) { | |
61 // Make sure this is a captcha server challange. | |
62 EXPECT_EQ(GoogleServiceAuthError::CAPTCHA_REQUIRED, error.state()); | |
63 EXPECT_EQ("challengetokenblob", error.captcha().token); | |
64 EXPECT_EQ("http://www.audio.com/", error.captcha().audio_url.spec()); | |
65 EXPECT_EQ("http://www.image.com/", error.captcha().image_url.spec()); | |
66 EXPECT_EQ(640, error.captcha().image_width); | |
67 EXPECT_EQ(480, error.captcha().image_height); | |
68 } | |
69 | |
70 static void ExpectBadAuth(const GoogleServiceAuthError& error) { | |
71 EXPECT_EQ(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS, error.state()); | |
72 } | |
73 | |
74 static void ExpectTwoFactorChallenge(const GoogleServiceAuthError& error) { | |
75 // Make sure this is a captcha server challange. | |
76 EXPECT_EQ(GoogleServiceAuthError::TWO_FACTOR, error.state()); | |
77 EXPECT_EQ("challengetokenblob", error.second_factor().token); | |
78 EXPECT_EQ("prompt_text", error.second_factor().prompt_text); | |
79 EXPECT_EQ("alternate_text", error.second_factor().alternate_text); | |
80 EXPECT_EQ(10, error.second_factor().field_length); | |
81 } | |
82 | |
83 } // namespace | 60 } // namespace |
84 | 61 |
85 MockFetcher::MockFetcher(bool success, | 62 MockFetcher::MockFetcher(bool success, |
86 const GURL& url, | 63 const GURL& url, |
87 const std::string& results, | 64 const std::string& results, |
88 net::URLFetcher::RequestType request_type, | 65 net::URLFetcher::RequestType request_type, |
89 net::URLFetcherDelegate* d) | 66 net::URLFetcherDelegate* d) |
90 : TestURLFetcher(0, url, d) { | 67 : TestURLFetcher(0, url, d) { |
91 set_url(url); | 68 set_url(url); |
92 net::URLRequestStatus::Status code; | 69 net::URLRequestStatus::Status code; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 issue_auth_token_source_( | 108 issue_auth_token_source_( |
132 GaiaUrls::GetInstance()->issue_auth_token_url()), | 109 GaiaUrls::GetInstance()->issue_auth_token_url()), |
133 client_login_to_oauth2_source_( | 110 client_login_to_oauth2_source_( |
134 GaiaUrls::GetInstance()->client_login_to_oauth2_url()), | 111 GaiaUrls::GetInstance()->client_login_to_oauth2_url()), |
135 oauth2_token_source_(GaiaUrls::GetInstance()->oauth2_token_url()), | 112 oauth2_token_source_(GaiaUrls::GetInstance()->oauth2_token_url()), |
136 token_auth_source_(GaiaUrls::GetInstance()->token_auth_url()), | 113 token_auth_source_(GaiaUrls::GetInstance()->token_auth_url()), |
137 merge_session_source_(GaiaUrls::GetInstance()->merge_session_url()), | 114 merge_session_source_(GaiaUrls::GetInstance()->merge_session_url()), |
138 uberauth_token_source_(base::StringPrintf( | 115 uberauth_token_source_(base::StringPrintf( |
139 "%s?source=&issueuberauth=1", | 116 "%s?source=&issueuberauth=1", |
140 GaiaUrls::GetInstance()->oauth1_login_url().c_str())), | 117 GaiaUrls::GetInstance()->oauth1_login_url().c_str())), |
141 client_oauth_source_(GaiaUrls::GetInstance()->client_oauth_url()), | |
142 oauth_login_gurl_(GaiaUrls::GetInstance()->oauth1_login_url()) {} | 118 oauth_login_gurl_(GaiaUrls::GetInstance()->oauth1_login_url()) {} |
143 | 119 |
144 void RunParsingTest(const std::string& data, | 120 void RunParsingTest(const std::string& data, |
145 const std::string& sid, | 121 const std::string& sid, |
146 const std::string& lsid, | 122 const std::string& lsid, |
147 const std::string& token) { | 123 const std::string& token) { |
148 std::string out_sid; | 124 std::string out_sid; |
149 std::string out_lsid; | 125 std::string out_lsid; |
150 std::string out_token; | 126 std::string out_token; |
151 | 127 |
(...skipping 28 matching lines...) Expand all Loading... |
180 } | 156 } |
181 | 157 |
182 net::ResponseCookies cookies_; | 158 net::ResponseCookies cookies_; |
183 GURL client_login_source_; | 159 GURL client_login_source_; |
184 GURL issue_auth_token_source_; | 160 GURL issue_auth_token_source_; |
185 GURL client_login_to_oauth2_source_; | 161 GURL client_login_to_oauth2_source_; |
186 GURL oauth2_token_source_; | 162 GURL oauth2_token_source_; |
187 GURL token_auth_source_; | 163 GURL token_auth_source_; |
188 GURL merge_session_source_; | 164 GURL merge_session_source_; |
189 GURL uberauth_token_source_; | 165 GURL uberauth_token_source_; |
190 GURL client_oauth_source_; | |
191 GURL oauth_login_gurl_; | 166 GURL oauth_login_gurl_; |
192 TestingProfile profile_; | 167 TestingProfile profile_; |
193 protected: | 168 protected: |
194 MessageLoop message_loop_; | 169 MessageLoop message_loop_; |
195 }; | 170 }; |
196 | 171 |
197 class MockGaiaConsumer : public GaiaAuthConsumer { | 172 class MockGaiaConsumer : public GaiaAuthConsumer { |
198 public: | 173 public: |
199 MockGaiaConsumer() {} | 174 MockGaiaConsumer() {} |
200 ~MockGaiaConsumer() {} | 175 ~MockGaiaConsumer() {} |
(...skipping 606 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
807 { // Single valid cookie (like in real responses). | 782 { // Single valid cookie (like in real responses). |
808 std::string auth_code; | 783 std::string auth_code; |
809 net::ResponseCookies cookies; | 784 net::ResponseCookies cookies; |
810 cookies.push_back(kGetAuthCodeValidCookie); | 785 cookies.push_back(kGetAuthCodeValidCookie); |
811 EXPECT_TRUE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response( | 786 EXPECT_TRUE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response( |
812 cookies, &auth_code)); | 787 cookies, &auth_code)); |
813 EXPECT_EQ("test-code", auth_code); | 788 EXPECT_EQ("test-code", auth_code); |
814 } | 789 } |
815 } | 790 } |
816 | 791 |
817 TEST_F(GaiaAuthFetcherTest, ClientOAuthSuccess) { | |
818 MockURLFetcherFactory<MockFetcher> factory; | |
819 factory.set_results(kClientOAuthValidResponse); | |
820 | |
821 MockGaiaConsumer consumer; | |
822 EXPECT_CALL(consumer, OnClientOAuthSuccess( | |
823 GaiaAuthConsumer::ClientOAuthResult("rt1", "at1", 3600))).Times(1); | |
824 | |
825 GaiaAuthFetcher auth(&consumer, "tests", profile_.GetRequestContext()); | |
826 std::vector<std::string> scopes; | |
827 scopes.push_back(GaiaUrls::GetInstance()->oauth1_login_scope()); | |
828 scopes.push_back("https://some.other.scope.com"); | |
829 auth.StartClientOAuth("username", "password", scopes, std::string(), "en"); | |
830 | |
831 std::string expected_text = base::StringPrintf( | |
832 "{" | |
833 "\"email\": \"username\"," | |
834 "\"password\": \"password\"," | |
835 "\"scopes\": [\"https://www.google.com/accounts/OAuthLogin\"," | |
836 " \"https://some.other.scope.com\"]," | |
837 "\"oauth2_client_id\": \"%s\"," | |
838 "\"friendly_device_name\": \"tests\"," | |
839 "\"accepts_challenges\": [\"Captcha\", \"TwoStep\"]," | |
840 "\"locale\": \"en\"," | |
841 "\"fallback\": { \"name\": \"GetOAuth2Token\" }" | |
842 "}", | |
843 google_apis::GetOAuth2ClientID(google_apis::CLIENT_MAIN).c_str()); | |
844 | |
845 scoped_ptr<base::Value> actual(base::JSONReader::Read(auth.request_body_)); | |
846 scoped_ptr<base::Value> expected(base::JSONReader::Read(expected_text)); | |
847 EXPECT_TRUE(expected->Equals(actual.get())); | |
848 } | |
849 | |
850 TEST_F(GaiaAuthFetcherTest, ClientOAuthWithQuote) { | |
851 MockURLFetcherFactory<MockFetcher> factory; | |
852 factory.set_results(kClientOAuthValidResponse); | |
853 | |
854 MockGaiaConsumer consumer; | |
855 EXPECT_CALL(consumer, OnClientOAuthSuccess( | |
856 GaiaAuthConsumer::ClientOAuthResult("rt1", "at1", 3600))).Times(1); | |
857 | |
858 GaiaAuthFetcher auth(&consumer, "te\"sts", profile_.GetRequestContext()); | |
859 std::vector<std::string> scopes; | |
860 scopes.push_back("https://some.\"other.scope.com"); | |
861 auth.StartClientOAuth( | |
862 "user\"name", "pass\"word", scopes, std::string(), "e\"n"); | |
863 | |
864 std::string expected_text = base::StringPrintf( | |
865 "{" | |
866 "\"email\": \"user\\\"name\"," | |
867 "\"password\": \"pass\\\"word\"," | |
868 "\"scopes\": [\"https://some.\\\"other.scope.com\"]," | |
869 "\"oauth2_client_id\": \"%s\"," | |
870 "\"friendly_device_name\": \"te\\\"sts\"," | |
871 "\"accepts_challenges\": [\"Captcha\", \"TwoStep\"]," | |
872 "\"locale\": \"e\\\"n\"," | |
873 "\"fallback\": { \"name\": \"GetOAuth2Token\" }" | |
874 "}", | |
875 google_apis::GetOAuth2ClientID(google_apis::CLIENT_MAIN).c_str()); | |
876 scoped_ptr<base::Value> actual(base::JSONReader::Read(auth.request_body_)); | |
877 scoped_ptr<base::Value> expected(base::JSONReader::Read(expected_text)); | |
878 EXPECT_TRUE(expected->Equals(actual.get())); | |
879 } | |
880 | |
881 TEST_F(GaiaAuthFetcherTest, ClientOAuthBadAuth) { | |
882 MockURLFetcherFactory<MockFetcher> factory; | |
883 factory.set_success(false); | |
884 factory.set_results("{" | |
885 " \"cause\" : \"BadAuthentication\"," | |
886 " \"fallback\" : {" | |
887 " \"name\" : \"Terminating\"," | |
888 " \"url\" : \"https://www.terminating.com\"" | |
889 " }" | |
890 "}"); | |
891 | |
892 MockGaiaConsumer consumer; | |
893 EXPECT_CALL(consumer, OnClientOAuthFailure(_)) | |
894 .WillOnce(Invoke(ExpectBadAuth)); | |
895 | |
896 GaiaAuthFetcher auth(&consumer, "tests", profile_.GetRequestContext()); | |
897 std::vector<std::string> scopes; | |
898 scopes.push_back(GaiaUrls::GetInstance()->oauth1_login_scope()); | |
899 auth.StartClientOAuth("username", "password", scopes, std::string(), "en"); | |
900 } | |
901 | |
902 TEST_F(GaiaAuthFetcherTest, ClientOAuthCaptchaChallenge) { | |
903 MockURLFetcherFactory<MockFetcher> factory; | |
904 factory.set_success(false); | |
905 factory.set_results("{" | |
906 " \"cause\" : \"NeedsAdditional\"," | |
907 " \"fallback\" : {" | |
908 " \"name\" : \"Terminating\"," | |
909 " \"url\" : \"https://www.terminating.com\"" | |
910 " }," | |
911 " \"challenge\" : {" | |
912 " \"name\" : \"Captcha\"," | |
913 " \"image_url\" : \"http://www.image.com/\"," | |
914 " \"image_width\" : 640," | |
915 " \"image_height\" : 480," | |
916 " \"audio_url\" : \"http://www.audio.com/\"," | |
917 " \"challenge_token\" : \"challengetokenblob\"" | |
918 " }" | |
919 "}"); | |
920 | |
921 MockGaiaConsumer consumer; | |
922 EXPECT_CALL(consumer, OnClientOAuthFailure(_)) | |
923 .WillOnce(Invoke(ExpectCaptchaChallenge)); | |
924 | |
925 GaiaAuthFetcher auth(&consumer, "tests", profile_.GetRequestContext()); | |
926 std::vector<std::string> scopes; | |
927 scopes.push_back(GaiaUrls::GetInstance()->oauth1_login_scope()); | |
928 auth.StartClientOAuth("username", "password", scopes, std::string(), "en"); | |
929 } | |
930 | |
931 TEST_F(GaiaAuthFetcherTest, ClientOAuthTwoFactorChallenge) { | |
932 MockURLFetcherFactory<MockFetcher> factory; | |
933 factory.set_success(false); | |
934 factory.set_results("{" | |
935 " \"cause\" : \"NeedsAdditional\"," | |
936 " \"fallback\" : {" | |
937 " \"name\" : \"Terminating\"," | |
938 " \"url\" : \"https://www.terminating.com\"" | |
939 " }," | |
940 " \"challenge\" : {" | |
941 " \"name\" : \"TwoStep\"," | |
942 " \"prompt_text\" : \"prompt_text\"," | |
943 " \"alternate_text\" : \"alternate_text\"," | |
944 " \"challenge_token\" : \"challengetokenblob\"," | |
945 " \"field_length\" : 10" | |
946 " }" | |
947 "}"); | |
948 | |
949 MockGaiaConsumer consumer; | |
950 EXPECT_CALL(consumer, OnClientOAuthFailure(_)) | |
951 .WillOnce(Invoke(ExpectTwoFactorChallenge)); | |
952 | |
953 GaiaAuthFetcher auth(&consumer, "tests", profile_.GetRequestContext()); | |
954 std::vector<std::string> scopes; | |
955 scopes.push_back(GaiaUrls::GetInstance()->oauth1_login_scope()); | |
956 auth.StartClientOAuth("username", "password", scopes, std::string(), "en"); | |
957 } | |
958 | |
959 TEST_F(GaiaAuthFetcherTest, ClientOAuthChallengeSuccess) { | |
960 MockURLFetcherFactory<MockFetcher> factory; | |
961 factory.set_results(kClientOAuthValidResponse); | |
962 | |
963 MockGaiaConsumer consumer; | |
964 EXPECT_CALL(consumer, OnClientOAuthSuccess( | |
965 GaiaAuthConsumer::ClientOAuthResult("rt1", "at1", 3600))).Times(2); | |
966 | |
967 GaiaAuthFetcher auth1(&consumer, std::string(), profile_.GetRequestContext()); | |
968 auth1.StartClientOAuthChallengeResponse(GoogleServiceAuthError::TWO_FACTOR, | |
969 "token", "mysolution"); | |
970 | |
971 scoped_ptr<base::Value> actual1(base::JSONReader::Read(auth1.request_body_)); | |
972 scoped_ptr<base::Value> expected1(base::JSONReader::Read( | |
973 "{" | |
974 " \"challenge_reply\" : {" | |
975 " \"name\" : \"TwoStep\"," | |
976 " \"challenge_token\" : \"token\"," | |
977 " \"otp\" : \"mysolution\"" | |
978 " }" | |
979 "}")); | |
980 EXPECT_TRUE(expected1->Equals(actual1.get())); | |
981 | |
982 GaiaAuthFetcher auth2(&consumer, "tests", profile_.GetRequestContext()); | |
983 auth2.StartClientOAuthChallengeResponse( | |
984 GoogleServiceAuthError::CAPTCHA_REQUIRED, "token", "mysolution"); | |
985 | |
986 scoped_ptr<base::Value> actual2(base::JSONReader::Read(auth2.request_body_)); | |
987 scoped_ptr<base::Value> expected2(base::JSONReader::Read( | |
988 "{" | |
989 " \"challenge_reply\" : {" | |
990 " \"name\" : \"Captcha\"," | |
991 " \"challenge_token\" : \"token\"," | |
992 " \"solution\" : \"mysolution\"" | |
993 " }" | |
994 "}")); | |
995 EXPECT_TRUE(expected2->Equals(actual2.get())); | |
996 } | |
997 | |
998 TEST_F(GaiaAuthFetcherTest, ClientOAuthChallengeQuote) { | |
999 MockURLFetcherFactory<MockFetcher> factory; | |
1000 factory.set_results(kClientOAuthValidResponse); | |
1001 | |
1002 MockGaiaConsumer consumer; | |
1003 EXPECT_CALL(consumer, OnClientOAuthSuccess( | |
1004 GaiaAuthConsumer::ClientOAuthResult("rt1", "at1", 3600))).Times(1); | |
1005 | |
1006 GaiaAuthFetcher auth(&consumer, std::string(), profile_.GetRequestContext()); | |
1007 auth.StartClientOAuthChallengeResponse(GoogleServiceAuthError::TWO_FACTOR, | |
1008 "to\"ken", "my\"solution"); | |
1009 | |
1010 scoped_ptr<base::Value> actual(base::JSONReader::Read(auth.request_body_)); | |
1011 scoped_ptr<base::Value> expected(base::JSONReader::Read( | |
1012 "{" | |
1013 " \"challenge_reply\" : {" | |
1014 " \"name\" : \"TwoStep\"," | |
1015 " \"challenge_token\" : \"to\\\"ken\"," | |
1016 " \"otp\" : \"my\\\"solution\"" | |
1017 " }" | |
1018 "}")); | |
1019 EXPECT_TRUE(expected->Equals(actual.get())); | |
1020 } | |
1021 | |
1022 TEST_F(GaiaAuthFetcherTest, StartOAuthLogin) { | 792 TEST_F(GaiaAuthFetcherTest, StartOAuthLogin) { |
1023 // OAuthLogin returns the same as the ClientLogin endpoint, minus CAPTCHA | 793 // OAuthLogin returns the same as the ClientLogin endpoint, minus CAPTCHA |
1024 // responses. | 794 // responses. |
1025 std::string data("SID=sid\nLSID=lsid\nAuth=auth\n"); | 795 std::string data("SID=sid\nLSID=lsid\nAuth=auth\n"); |
1026 | 796 |
1027 GaiaAuthConsumer::ClientLoginResult result; | 797 GaiaAuthConsumer::ClientLoginResult result; |
1028 result.lsid = "lsid"; | 798 result.lsid = "lsid"; |
1029 result.sid = "sid"; | 799 result.sid = "sid"; |
1030 result.token = "auth"; | 800 result.token = "auth"; |
1031 result.data = data; | 801 result.data = data; |
1032 | 802 |
1033 MockGaiaConsumer consumer; | 803 MockGaiaConsumer consumer; |
1034 EXPECT_CALL(consumer, OnClientLoginSuccess(result)) | 804 EXPECT_CALL(consumer, OnClientLoginSuccess(result)) |
1035 .Times(1); | 805 .Times(1); |
1036 | 806 |
1037 GaiaAuthFetcher auth(&consumer, std::string(), | 807 GaiaAuthFetcher auth(&consumer, std::string(), |
1038 profile_.GetRequestContext()); | 808 profile_.GetRequestContext()); |
1039 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); | 809 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); |
1040 MockFetcher mock_fetcher( | 810 MockFetcher mock_fetcher( |
1041 oauth_login_gurl_, status, net::HTTP_OK, cookies_, data, | 811 oauth_login_gurl_, status, net::HTTP_OK, cookies_, data, |
1042 net::URLFetcher::GET, &auth); | 812 net::URLFetcher::GET, &auth); |
1043 auth.OnURLFetchComplete(&mock_fetcher); | 813 auth.OnURLFetchComplete(&mock_fetcher); |
1044 } | 814 } |
OLD | NEW |