| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <string> | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/memory/ref_counted.h" | |
| 9 #include "base/run_loop.h" | |
| 10 #include "chrome/browser/chromeos/login/auth/mock_url_fetchers.h" | |
| 11 #include "chrome/browser/chromeos/login/auth/online_attempt.h" | |
| 12 #include "chrome/test/base/testing_profile.h" | |
| 13 #include "chromeos/login/auth/auth_attempt_state.h" | |
| 14 #include "chromeos/login/auth/mock_auth_attempt_state_resolver.h" | |
| 15 #include "chromeos/login/auth/test_attempt_state.h" | |
| 16 #include "chromeos/login/auth/user_context.h" | |
| 17 #include "content/public/browser/browser_thread.h" | |
| 18 #include "content/public/test/test_browser_thread_bundle.h" | |
| 19 #include "google_apis/gaia/gaia_auth_consumer.h" | |
| 20 #include "google_apis/gaia/mock_url_fetcher_factory.h" | |
| 21 #include "testing/gmock/include/gmock/gmock.h" | |
| 22 #include "testing/gtest/include/gtest/gtest.h" | |
| 23 #include "url/gurl.h" | |
| 24 | |
| 25 using ::testing::AnyNumber; | |
| 26 using ::testing::Invoke; | |
| 27 using ::testing::Return; | |
| 28 using ::testing::_; | |
| 29 using content::BrowserThread; | |
| 30 | |
| 31 namespace chromeos { | |
| 32 | |
| 33 class OnlineAttemptTest : public testing::Test { | |
| 34 public: | |
| 35 OnlineAttemptTest() | |
| 36 : state_(UserContext(), false), | |
| 37 attempt_(new OnlineAttempt(&state_, &resolver_)) { | |
| 38 } | |
| 39 | |
| 40 void RunFailureTest(const GoogleServiceAuthError& error) { | |
| 41 EXPECT_CALL(resolver_, Resolve()) | |
| 42 .Times(1) | |
| 43 .RetiresOnSaturation(); | |
| 44 | |
| 45 BrowserThread::PostTask( | |
| 46 BrowserThread::UI, FROM_HERE, | |
| 47 base::Bind(&OnlineAttempt::OnClientLoginFailure, | |
| 48 attempt_->weak_factory_.GetWeakPtr(), | |
| 49 error)); | |
| 50 // Force UI thread to finish tasks so I can verify |state_|. | |
| 51 base::RunLoop().RunUntilIdle(); | |
| 52 EXPECT_TRUE(error == state_.online_outcome().error()); | |
| 53 } | |
| 54 | |
| 55 void CancelLogin(OnlineAttempt* auth) { | |
| 56 BrowserThread::PostTask( | |
| 57 BrowserThread::UI, FROM_HERE, | |
| 58 base::Bind(&OnlineAttempt::CancelClientLogin, | |
| 59 auth->weak_factory_.GetWeakPtr())); | |
| 60 } | |
| 61 | |
| 62 content::TestBrowserThreadBundle thread_bundle_; | |
| 63 TestAttemptState state_; | |
| 64 MockAuthAttemptStateResolver resolver_; | |
| 65 scoped_ptr<OnlineAttempt> attempt_; | |
| 66 }; | |
| 67 | |
| 68 TEST_F(OnlineAttemptTest, LoginSuccess) { | |
| 69 EXPECT_CALL(resolver_, Resolve()) | |
| 70 .Times(1) | |
| 71 .RetiresOnSaturation(); | |
| 72 | |
| 73 BrowserThread::PostTask( | |
| 74 BrowserThread::UI, FROM_HERE, | |
| 75 base::Bind(&OnlineAttempt::OnClientLoginSuccess, | |
| 76 attempt_->weak_factory_.GetWeakPtr(), | |
| 77 GaiaAuthConsumer::ClientLoginResult())); | |
| 78 // Force UI thread to finish tasks so I can verify |state_|. | |
| 79 base::RunLoop().RunUntilIdle(); | |
| 80 } | |
| 81 | |
| 82 TEST_F(OnlineAttemptTest, LoginCancelRetry) { | |
| 83 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); | |
| 84 TestingProfile profile; | |
| 85 | |
| 86 base::RunLoop run_loop; | |
| 87 EXPECT_CALL(resolver_, Resolve()) | |
| 88 .WillOnce(Invoke(&run_loop, &base::RunLoop::Quit)) | |
| 89 .RetiresOnSaturation(); | |
| 90 | |
| 91 // This is how we inject fake URLFetcher objects, with a factory. | |
| 92 // This factory creates fake URLFetchers that Start() a fake fetch attempt | |
| 93 // and then come back on the UI thread saying they've been canceled. | |
| 94 MockURLFetcherFactory<GotCanceledFetcher> factory; | |
| 95 | |
| 96 attempt_->Initiate(&profile); | |
| 97 | |
| 98 run_loop.Run(); | |
| 99 | |
| 100 EXPECT_TRUE(error == state_.online_outcome().error()); | |
| 101 EXPECT_EQ(AuthFailure::NETWORK_AUTH_FAILED, state_.online_outcome().reason()); | |
| 102 } | |
| 103 | |
| 104 TEST_F(OnlineAttemptTest, LoginTimeout) { | |
| 105 AuthFailure error(AuthFailure::LOGIN_TIMED_OUT); | |
| 106 TestingProfile profile; | |
| 107 | |
| 108 base::RunLoop run_loop; | |
| 109 EXPECT_CALL(resolver_, Resolve()) | |
| 110 .WillOnce(Invoke(&run_loop, &base::RunLoop::Quit)) | |
| 111 .RetiresOnSaturation(); | |
| 112 | |
| 113 // This is how we inject fake URLFetcher objects, with a factory. | |
| 114 // This factory creates fake URLFetchers that Start() a fake fetch attempt | |
| 115 // and then come back on the UI thread saying they've been canceled. | |
| 116 MockURLFetcherFactory<ExpectCanceledFetcher> factory; | |
| 117 | |
| 118 attempt_->Initiate(&profile); | |
| 119 | |
| 120 // Post a task to cancel the login attempt. | |
| 121 CancelLogin(attempt_.get()); | |
| 122 | |
| 123 run_loop.Run(); | |
| 124 | |
| 125 EXPECT_EQ(AuthFailure::LOGIN_TIMED_OUT, state_.online_outcome().reason()); | |
| 126 } | |
| 127 | |
| 128 TEST_F(OnlineAttemptTest, HostedLoginRejected) { | |
| 129 AuthFailure error(AuthFailure::FromNetworkAuthFailure( | |
| 130 GoogleServiceAuthError(GoogleServiceAuthError::HOSTED_NOT_ALLOWED))); | |
| 131 TestingProfile profile; | |
| 132 | |
| 133 base::RunLoop run_loop; | |
| 134 EXPECT_CALL(resolver_, Resolve()) | |
| 135 .WillOnce(Invoke(&run_loop, &base::RunLoop::Quit)) | |
| 136 .RetiresOnSaturation(); | |
| 137 | |
| 138 // This is how we inject fake URLFetcher objects, with a factory. | |
| 139 MockURLFetcherFactory<HostedFetcher> factory; | |
| 140 | |
| 141 TestAttemptState local_state(UserContext(), true); | |
| 142 attempt_.reset(new OnlineAttempt(&local_state, &resolver_)); | |
| 143 attempt_->Initiate(&profile); | |
| 144 | |
| 145 run_loop.Run(); | |
| 146 | |
| 147 EXPECT_EQ(error, local_state.online_outcome()); | |
| 148 EXPECT_EQ(AuthFailure::NETWORK_AUTH_FAILED, | |
| 149 local_state.online_outcome().reason()); | |
| 150 } | |
| 151 | |
| 152 TEST_F(OnlineAttemptTest, FullLogin) { | |
| 153 TestingProfile profile; | |
| 154 | |
| 155 base::RunLoop run_loop; | |
| 156 EXPECT_CALL(resolver_, Resolve()) | |
| 157 .WillOnce(Invoke(&run_loop, &base::RunLoop::Quit)) | |
| 158 .RetiresOnSaturation(); | |
| 159 | |
| 160 // This is how we inject fake URLFetcher objects, with a factory. | |
| 161 MockURLFetcherFactory<SuccessFetcher> factory; | |
| 162 | |
| 163 TestAttemptState local_state(UserContext(), true); | |
| 164 attempt_.reset(new OnlineAttempt(&local_state, &resolver_)); | |
| 165 attempt_->Initiate(&profile); | |
| 166 | |
| 167 run_loop.Run(); | |
| 168 | |
| 169 EXPECT_EQ(AuthFailure::AuthFailureNone(), local_state.online_outcome()); | |
| 170 } | |
| 171 | |
| 172 TEST_F(OnlineAttemptTest, LoginNetFailure) { | |
| 173 RunFailureTest( | |
| 174 GoogleServiceAuthError::FromConnectionError(net::ERR_CONNECTION_RESET)); | |
| 175 } | |
| 176 | |
| 177 TEST_F(OnlineAttemptTest, LoginDenied) { | |
| 178 RunFailureTest( | |
| 179 GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); | |
| 180 } | |
| 181 | |
| 182 TEST_F(OnlineAttemptTest, LoginAccountDisabled) { | |
| 183 RunFailureTest( | |
| 184 GoogleServiceAuthError(GoogleServiceAuthError::ACCOUNT_DISABLED)); | |
| 185 } | |
| 186 | |
| 187 TEST_F(OnlineAttemptTest, LoginAccountDeleted) { | |
| 188 RunFailureTest( | |
| 189 GoogleServiceAuthError(GoogleServiceAuthError::ACCOUNT_DELETED)); | |
| 190 } | |
| 191 | |
| 192 TEST_F(OnlineAttemptTest, LoginServiceUnavailable) { | |
| 193 RunFailureTest( | |
| 194 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE)); | |
| 195 } | |
| 196 | |
| 197 TEST_F(OnlineAttemptTest, CaptchaErrorOutputted) { | |
| 198 GoogleServiceAuthError auth_error = | |
| 199 GoogleServiceAuthError::FromClientLoginCaptchaChallenge( | |
| 200 "CCTOKEN", | |
| 201 GURL("http://accounts.google.com/Captcha?ctoken=CCTOKEN"), | |
| 202 GURL("http://www.google.com/login/captcha")); | |
| 203 RunFailureTest(auth_error); | |
| 204 } | |
| 205 | |
| 206 TEST_F(OnlineAttemptTest, TwoFactorSuccess) { | |
| 207 EXPECT_CALL(resolver_, Resolve()) | |
| 208 .Times(1) | |
| 209 .RetiresOnSaturation(); | |
| 210 GoogleServiceAuthError error(GoogleServiceAuthError::TWO_FACTOR); | |
| 211 BrowserThread::PostTask( | |
| 212 BrowserThread::UI, FROM_HERE, | |
| 213 base::Bind(&OnlineAttempt::OnClientLoginFailure, | |
| 214 attempt_->weak_factory_.GetWeakPtr(), | |
| 215 error)); | |
| 216 | |
| 217 // Force UI thread to finish tasks so I can verify |state_|. | |
| 218 base::RunLoop().RunUntilIdle(); | |
| 219 EXPECT_TRUE(GoogleServiceAuthError::AuthErrorNone() == | |
| 220 state_.online_outcome().error()); | |
| 221 } | |
| 222 | |
| 223 } // namespace chromeos | |
| OLD | NEW |