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