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