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

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

Issue 2834042: Add IssueAuthToken and unit tests to GaiaAuthenticator2. (Closed)
Patch Set: Created 10 years, 5 months 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
OLDNEW
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 // A complete set of unit tests for GaiaAuthenticator2. 5 // A complete set of unit tests for GaiaAuthenticator2.
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/message_loop.h" 10 #include "base/message_loop.h"
11 #include "base/string_util.h" 11 #include "base/string_util.h"
12 #include "chrome/common/net/gaia/gaia_auth_consumer.h" 12 #include "chrome/common/net/gaia/gaia_auth_consumer.h"
13 #include "chrome/common/net/gaia/gaia_authenticator2.h" 13 #include "chrome/common/net/gaia/gaia_authenticator2.h"
14 #include "chrome/common/net/gaia/gaia_authenticator2_unittest.h" 14 #include "chrome/common/net/gaia/gaia_authenticator2_unittest.h"
15 #include "chrome/common/net/http_return.h" 15 #include "chrome/common/net/http_return.h"
16 #include "chrome/common/net/test_url_fetcher_factory.h" 16 #include "chrome/common/net/test_url_fetcher_factory.h"
17 #include "chrome/common/net/url_fetcher.h" 17 #include "chrome/common/net/url_fetcher.h"
18 #include "chrome/test/testing_profile.h" 18 #include "chrome/test/testing_profile.h"
19 #include "googleurl/src/gurl.h" 19 #include "googleurl/src/gurl.h"
20 #include "net/base/net_errors.h" 20 #include "net/base/net_errors.h"
21 #include "net/url_request/url_request_status.h" 21 #include "net/url_request/url_request_status.h"
22 #include "testing/gmock/include/gmock/gmock.h" 22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
24 24
25 using ::testing::_; 25 using ::testing::_;
26 26
27 class GaiaAuthenticator2Test : public testing::Test { 27 class GaiaAuthenticator2Test : public testing::Test {
28 public: 28 public:
29 GaiaAuthenticator2Test() 29 GaiaAuthenticator2Test()
30 : source_(GaiaAuthenticator2::kClientLoginUrl) {} 30 : client_login_source_(GaiaAuthenticator2::kClientLoginUrl),
31 issue_auth_token_source_(GaiaAuthenticator2::kIssueAuthTokenUrl) {}
31 32
32 void RunParsingTest(const std::string& data, 33 void RunParsingTest(const std::string& data,
33 const std::string& sid, 34 const std::string& sid,
34 const std::string& lsid, 35 const std::string& lsid,
35 const std::string& token) { 36 const std::string& token) {
36 std::string out_sid; 37 std::string out_sid;
37 std::string out_lsid; 38 std::string out_lsid;
38 std::string out_token; 39 std::string out_token;
39 40
40 GaiaAuthenticator2::ParseClientLoginResponse(data, 41 GaiaAuthenticator2::ParseClientLoginResponse(data,
41 &out_sid, 42 &out_sid,
42 &out_lsid, 43 &out_lsid,
43 &out_token); 44 &out_token);
44 EXPECT_EQ(lsid, out_lsid); 45 EXPECT_EQ(lsid, out_lsid);
45 EXPECT_EQ(sid, out_sid); 46 EXPECT_EQ(sid, out_sid);
46 EXPECT_EQ(token, out_token); 47 EXPECT_EQ(token, out_token);
47 } 48 }
48 49
49 ResponseCookies cookies_; 50 ResponseCookies cookies_;
50 GURL source_; 51 GURL client_login_source_;
52 GURL issue_auth_token_source_;
51 TestingProfile profile_; 53 TestingProfile profile_;
52 }; 54 };
53 55
54 class MockGaiaConsumer : public GaiaAuthConsumer { 56 class MockGaiaConsumer : public GaiaAuthConsumer {
55 public: 57 public:
56 MockGaiaConsumer() {} 58 MockGaiaConsumer() {}
57 ~MockGaiaConsumer() {} 59 ~MockGaiaConsumer() {}
58 MOCK_METHOD1(OnClientLoginFailure, void(const ClientLoginError& error)); 60
59 MOCK_METHOD1(OnClientLoginSuccess, void(const ClientLoginResult& result)); 61 MOCK_METHOD1(OnClientLoginSuccess, void(const ClientLoginResult& result));
62 MOCK_METHOD2(OnIssueAuthTokenSuccess, void(const std::string& service,
63 const std::string& token));
64 MOCK_METHOD1(OnClientLoginFailure, void(const GaiaAuthError& error));
65 MOCK_METHOD2(OnIssueAuthTokenFailure, void(const std::string& service,
66 const GaiaAuthError& error));
60 }; 67 };
61 68
62 TEST_F(GaiaAuthenticator2Test, ErrorComparator) { 69 TEST_F(GaiaAuthenticator2Test, ErrorComparator) {
63 GaiaAuthConsumer::ClientLoginError expected_error; 70 GaiaAuthConsumer::GaiaAuthError expected_error;
64 expected_error.code = GaiaAuthConsumer::NETWORK_ERROR; 71 expected_error.code = GaiaAuthConsumer::NETWORK_ERROR;
65 expected_error.network_error = -101; 72 expected_error.network_error = -101;
66 73
67 GaiaAuthConsumer::ClientLoginError matching_error; 74 GaiaAuthConsumer::GaiaAuthError matching_error;
68 matching_error.code = GaiaAuthConsumer::NETWORK_ERROR; 75 matching_error.code = GaiaAuthConsumer::NETWORK_ERROR;
69 matching_error.network_error = -101; 76 matching_error.network_error = -101;
70 77
71 EXPECT_TRUE(expected_error == matching_error); 78 EXPECT_TRUE(expected_error == matching_error);
72 79
73 expected_error.network_error = 6; 80 expected_error.network_error = 6;
74 81
75 EXPECT_FALSE(expected_error == matching_error); 82 EXPECT_FALSE(expected_error == matching_error);
76 83
77 expected_error.code = GaiaAuthConsumer::PERMISSION_DENIED; 84 expected_error.code = GaiaAuthConsumer::PERMISSION_DENIED;
78 matching_error.code = GaiaAuthConsumer::PERMISSION_DENIED; 85 matching_error.code = GaiaAuthConsumer::PERMISSION_DENIED;
79 86
80 EXPECT_TRUE(expected_error == matching_error); 87 EXPECT_TRUE(expected_error == matching_error);
81 } 88 }
82 89
83 TEST_F(GaiaAuthenticator2Test, LoginNetFailure) { 90 TEST_F(GaiaAuthenticator2Test, LoginNetFailure) {
84 int error_no = net::ERR_CONNECTION_RESET; 91 int error_no = net::ERR_CONNECTION_RESET;
85 URLRequestStatus status(URLRequestStatus::FAILED, error_no); 92 URLRequestStatus status(URLRequestStatus::FAILED, error_no);
86 93
87 GaiaAuthConsumer::ClientLoginError expected_error; 94 GaiaAuthConsumer::GaiaAuthError expected_error;
88 expected_error.code = GaiaAuthConsumer::NETWORK_ERROR; 95 expected_error.code = GaiaAuthConsumer::NETWORK_ERROR;
89 expected_error.network_error = error_no; 96 expected_error.network_error = error_no;
90 97
91 MockGaiaConsumer consumer; 98 MockGaiaConsumer consumer;
92 EXPECT_CALL(consumer, OnClientLoginFailure(expected_error)) 99 EXPECT_CALL(consumer, OnClientLoginFailure(expected_error))
93 .Times(1); 100 .Times(1);
94 101
95 GaiaAuthenticator2 auth(&consumer, std::string(), 102 GaiaAuthenticator2 auth(&consumer, std::string(),
96 profile_.GetRequestContext()); 103 profile_.GetRequestContext());
97 104
98 auth.OnURLFetchComplete(NULL, source_, status, 0, cookies_, std::string()); 105 auth.OnURLFetchComplete(NULL,
106 client_login_source_,
107 status,
108 0,
109 cookies_,
110 std::string());
111 }
112
113 TEST_F(GaiaAuthenticator2Test, TokenNetFailure) {
114 int error_no = net::ERR_CONNECTION_RESET;
115 URLRequestStatus status(URLRequestStatus::FAILED, error_no);
116
117 GaiaAuthConsumer::GaiaAuthError expected_error;
118 expected_error.code = GaiaAuthConsumer::NETWORK_ERROR;
119 expected_error.network_error = error_no;
120
121 MockGaiaConsumer consumer;
122 EXPECT_CALL(consumer, OnIssueAuthTokenFailure(_, expected_error))
123 .Times(1);
124
125 GaiaAuthenticator2 auth(&consumer, std::string(),
126 profile_.GetRequestContext());
127
128 auth.OnURLFetchComplete(NULL,
129 issue_auth_token_source_,
130 status,
131 0,
132 cookies_,
133 std::string());
99 } 134 }
100 135
101 136
102 TEST_F(GaiaAuthenticator2Test, LoginDenied) { 137 TEST_F(GaiaAuthenticator2Test, LoginDenied) {
103 std::string data("Error: NO!"); 138 std::string data("Error: NO!");
104 URLRequestStatus status(URLRequestStatus::SUCCESS, 0); 139 URLRequestStatus status(URLRequestStatus::SUCCESS, 0);
105 140
106 GaiaAuthConsumer::ClientLoginError expected_error; 141 GaiaAuthConsumer::GaiaAuthError expected_error;
107 expected_error.code = GaiaAuthConsumer::PERMISSION_DENIED; 142 expected_error.code = GaiaAuthConsumer::PERMISSION_DENIED;
108 143
109 MockGaiaConsumer consumer; 144 MockGaiaConsumer consumer;
110 EXPECT_CALL(consumer, OnClientLoginFailure(expected_error)) 145 EXPECT_CALL(consumer, OnClientLoginFailure(expected_error))
111 .Times(1); 146 .Times(1);
112 147
113 GaiaAuthenticator2 auth(&consumer, std::string(), 148 GaiaAuthenticator2 auth(&consumer, std::string(),
114 profile_.GetRequestContext()); 149 profile_.GetRequestContext());
115 auth.OnURLFetchComplete(NULL, source_, status, RC_FORBIDDEN, cookies_, data); 150 auth.OnURLFetchComplete(NULL,
151 client_login_source_,
152 status,
153 RC_FORBIDDEN,
154 cookies_,
155 data);
116 } 156 }
117 157
118 TEST_F(GaiaAuthenticator2Test, ParseRequest) { 158 TEST_F(GaiaAuthenticator2Test, ParseRequest) {
119 RunParsingTest("SID=sid\nLSID=lsid\nAuth=auth\n", "sid", "lsid", "auth"); 159 RunParsingTest("SID=sid\nLSID=lsid\nAuth=auth\n", "sid", "lsid", "auth");
120 RunParsingTest("LSID=lsid\nSID=sid\nAuth=auth\n", "sid", "lsid", "auth"); 160 RunParsingTest("LSID=lsid\nSID=sid\nAuth=auth\n", "sid", "lsid", "auth");
121 RunParsingTest("SID=sid\nLSID=lsid\nAuth=auth", "sid", "lsid", "auth"); 161 RunParsingTest("SID=sid\nLSID=lsid\nAuth=auth", "sid", "lsid", "auth");
122 RunParsingTest("SID=sid\nAuth=auth\n", "sid", "", "auth"); 162 RunParsingTest("SID=sid\nAuth=auth\n", "sid", "", "auth");
123 RunParsingTest("LSID=lsid\nAuth=auth\n", "", "lsid", "auth"); 163 RunParsingTest("LSID=lsid\nAuth=auth\n", "", "lsid", "auth");
124 RunParsingTest("\nAuth=auth\n", "", "", "auth"); 164 RunParsingTest("\nAuth=auth\n", "", "", "auth");
125 RunParsingTest("SID=sid", "sid", "", ""); 165 RunParsingTest("SID=sid", "sid", "", "");
126 } 166 }
127 167
128 TEST_F(GaiaAuthenticator2Test, OnlineLogin) { 168 TEST_F(GaiaAuthenticator2Test, OnlineLogin) {
129 std::string data("SID=sid\nLSID=lsid\nAuth=auth\n"); 169 std::string data("SID=sid\nLSID=lsid\nAuth=auth\n");
130 170
131 GaiaAuthConsumer::ClientLoginResult result; 171 GaiaAuthConsumer::ClientLoginResult result;
132 result.lsid = "lsid"; 172 result.lsid = "lsid";
133 result.sid = "sid"; 173 result.sid = "sid";
134 result.token = "auth"; 174 result.token = "auth";
135 result.data = data; 175 result.data = data;
136 176
137 MockGaiaConsumer consumer; 177 MockGaiaConsumer consumer;
138 EXPECT_CALL(consumer, OnClientLoginSuccess(result)) 178 EXPECT_CALL(consumer, OnClientLoginSuccess(result))
139 .Times(1); 179 .Times(1);
140 180
141 GaiaAuthenticator2 auth(&consumer, std::string(), 181 GaiaAuthenticator2 auth(&consumer, std::string(),
142 profile_.GetRequestContext()); 182 profile_.GetRequestContext());
143 URLRequestStatus status(URLRequestStatus::SUCCESS, 0); 183 URLRequestStatus status(URLRequestStatus::SUCCESS, 0);
144 auth.OnURLFetchComplete(NULL, 184 auth.OnURLFetchComplete(NULL,
145 source_, 185 client_login_source_,
146 status, 186 status,
147 RC_REQUEST_OK, 187 RC_REQUEST_OK,
148 cookies_, 188 cookies_,
149 data); 189 data);
150 } 190 }
151 191
192 TEST_F(GaiaAuthenticator2Test, WorkingIssueAuthToken) {
193 MockGaiaConsumer consumer;
194 EXPECT_CALL(consumer, OnIssueAuthTokenSuccess(_, "token"))
195 .Times(1);
196
197 GaiaAuthenticator2 auth(&consumer, std::string(),
198 profile_.GetRequestContext());
199 URLRequestStatus status(URLRequestStatus::SUCCESS, 0);
200 auth.OnURLFetchComplete(NULL,
201 issue_auth_token_source_,
202 status,
203 RC_REQUEST_OK,
204 cookies_,
205 "token");
206 }
207
152 TEST_F(GaiaAuthenticator2Test, CheckTwoFactorResponse) { 208 TEST_F(GaiaAuthenticator2Test, CheckTwoFactorResponse) {
153 std::string response = 209 std::string response =
154 StringPrintf("Error=BadAuthentication\n%s\n", 210 StringPrintf("Error=BadAuthentication\n%s\n",
155 GaiaAuthenticator2::kSecondFactor); 211 GaiaAuthenticator2::kSecondFactor);
156 EXPECT_TRUE(GaiaAuthenticator2::IsSecondFactorSuccess(response)); 212 EXPECT_TRUE(GaiaAuthenticator2::IsSecondFactorSuccess(response));
157 } 213 }
158 214
159 TEST_F(GaiaAuthenticator2Test, CheckNormalErrorCode) { 215 TEST_F(GaiaAuthenticator2Test, CheckNormalErrorCode) {
160 std::string response = "Error=BadAuthentication\n"; 216 std::string response = "Error=BadAuthentication\n";
161 EXPECT_FALSE(GaiaAuthenticator2::IsSecondFactorSuccess(response)); 217 EXPECT_FALSE(GaiaAuthenticator2::IsSecondFactorSuccess(response));
162 } 218 }
163 219
164 TEST_F(GaiaAuthenticator2Test, TwoFactorLogin) { 220 TEST_F(GaiaAuthenticator2Test, TwoFactorLogin) {
165 std::string response = 221 std::string response =
166 StringPrintf("Error=BadAuthentication\n%s\n", 222 StringPrintf("Error=BadAuthentication\n%s\n",
167 GaiaAuthenticator2::kSecondFactor); 223 GaiaAuthenticator2::kSecondFactor);
168 224
169 GaiaAuthConsumer::ClientLoginError error; 225 GaiaAuthConsumer::GaiaAuthError error;
170 error.code = GaiaAuthConsumer::TWO_FACTOR; 226 error.code = GaiaAuthConsumer::TWO_FACTOR;
171 227
172 MockGaiaConsumer consumer; 228 MockGaiaConsumer consumer;
173 EXPECT_CALL(consumer, OnClientLoginFailure(error)) 229 EXPECT_CALL(consumer, OnClientLoginFailure(error))
174 .Times(1); 230 .Times(1);
175 231
176 GaiaAuthenticator2 auth(&consumer, std::string(), 232 GaiaAuthenticator2 auth(&consumer, std::string(),
177 profile_.GetRequestContext()); 233 profile_.GetRequestContext());
178 URLRequestStatus status(URLRequestStatus::SUCCESS, 0); 234 URLRequestStatus status(URLRequestStatus::SUCCESS, 0);
179 auth.OnURLFetchComplete(NULL, 235 auth.OnURLFetchComplete(NULL,
180 source_, 236 client_login_source_,
181 status, 237 status,
182 RC_FORBIDDEN, 238 RC_FORBIDDEN,
183 cookies_, 239 cookies_,
184 response); 240 response);
185 } 241 }
186 242
187 TEST_F(GaiaAuthenticator2Test, FullLogin) { 243 TEST_F(GaiaAuthenticator2Test, FullLogin) {
188 MockGaiaConsumer consumer; 244 MockGaiaConsumer consumer;
189 EXPECT_CALL(consumer, OnClientLoginSuccess(_)) 245 EXPECT_CALL(consumer, OnClientLoginSuccess(_))
190 .Times(1); 246 .Times(1);
(...skipping 29 matching lines...) Expand all
220 profile_.GetRequestContext()); 276 profile_.GetRequestContext());
221 auth.StartClientLogin("username", 277 auth.StartClientLogin("username",
222 "password", 278 "password",
223 "service", 279 "service",
224 std::string(), 280 std::string(),
225 std::string()); 281 std::string());
226 282
227 URLFetcher::set_factory(NULL); 283 URLFetcher::set_factory(NULL);
228 } 284 }
229 285
230 TEST_F(GaiaAuthenticator2Test, FetchPending) { 286 TEST_F(GaiaAuthenticator2Test, ClientFetchPending) {
231 MockGaiaConsumer consumer; 287 MockGaiaConsumer consumer;
232 EXPECT_CALL(consumer, OnClientLoginSuccess(_)) 288 EXPECT_CALL(consumer, OnClientLoginSuccess(_))
233 .Times(1); 289 .Times(1);
234 290
235 TestingProfile profile; 291 TestingProfile profile;
236 TestURLFetcherFactory factory; 292 TestURLFetcherFactory factory;
237 URLFetcher::set_factory(&factory); 293 URLFetcher::set_factory(&factory);
238 294
239 GaiaAuthenticator2 auth(&consumer, std::string(), 295 GaiaAuthenticator2 auth(&consumer, std::string(),
240 profile_.GetRequestContext()); 296 profile_.GetRequestContext());
241 auth.StartClientLogin("username", 297 auth.StartClientLogin("username",
242 "password", 298 "password",
243 "service", 299 "service",
244 std::string(), 300 std::string(),
245 std::string()); 301 std::string());
246 302
247 URLFetcher::set_factory(NULL); 303 URLFetcher::set_factory(NULL);
248 EXPECT_TRUE(auth.HasPendingFetch()); 304 EXPECT_TRUE(auth.HasPendingFetch());
249 auth.OnURLFetchComplete(NULL, 305 auth.OnURLFetchComplete(NULL,
250 source_, 306 client_login_source_,
251 URLRequestStatus(URLRequestStatus::SUCCESS, 0), 307 URLRequestStatus(URLRequestStatus::SUCCESS, 0),
252 RC_REQUEST_OK, 308 RC_REQUEST_OK,
253 cookies_, 309 cookies_,
254 std::string()); 310 "SID=sid\nLSID=lsid\nAuth=auth\n");
255 EXPECT_FALSE(auth.HasPendingFetch()); 311 EXPECT_FALSE(auth.HasPendingFetch());
256 } 312 }
257 313
314 TEST_F(GaiaAuthenticator2Test, FullTokenSuccess) {
315 MockGaiaConsumer consumer;
316 EXPECT_CALL(consumer, OnIssueAuthTokenSuccess("service", "token"))
317 .Times(1);
258 318
319 TestingProfile profile;
320 TestURLFetcherFactory factory;
321 URLFetcher::set_factory(&factory);
322
323 GaiaAuthenticator2 auth(&consumer, std::string(),
324 profile_.GetRequestContext());
325 auth.StartIssueAuthToken("sid", "lsid", "service");
326
327 URLFetcher::set_factory(NULL);
328 EXPECT_TRUE(auth.HasPendingFetch());
329 auth.OnURLFetchComplete(NULL,
330 issue_auth_token_source_,
331 URLRequestStatus(URLRequestStatus::SUCCESS, 0),
332 RC_REQUEST_OK,
333 cookies_,
334 "token");
335 EXPECT_FALSE(auth.HasPendingFetch());
336 }
337
338 TEST_F(GaiaAuthenticator2Test, FullTokenFailure) {
339 MockGaiaConsumer consumer;
340 EXPECT_CALL(consumer, OnIssueAuthTokenFailure("service", _))
341 .Times(1);
342
343 TestingProfile profile;
344 TestURLFetcherFactory factory;
345 URLFetcher::set_factory(&factory);
346
347 GaiaAuthenticator2 auth(&consumer, std::string(),
348 profile_.GetRequestContext());
349 auth.StartIssueAuthToken("sid", "lsid", "service");
350
351 URLFetcher::set_factory(NULL);
352 EXPECT_TRUE(auth.HasPendingFetch());
353 auth.OnURLFetchComplete(NULL,
354 issue_auth_token_source_,
355 URLRequestStatus(URLRequestStatus::SUCCESS, 0),
356 RC_FORBIDDEN,
357 cookies_,
358 "");
359 EXPECT_FALSE(auth.HasPendingFetch());
360 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698