| 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 #include "chrome/browser/chromeos/login/parallel_authenticator.h" | 5 #include "chrome/browser/chromeos/login/parallel_authenticator.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| 16 #include "base/stringprintf.h" | 16 #include "base/stringprintf.h" |
| 17 #include "base/test/thread_test_helper.h" | 17 #include "base/test/thread_test_helper.h" |
| 18 #include "chrome/browser/chromeos/cros/cros_library.h" | 18 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 19 #include "chrome/browser/chromeos/cros/mock_cryptohome_library.h" | 19 #include "chrome/browser/chromeos/cros/mock_cryptohome_library.h" |
| 20 #include "chrome/browser/chromeos/cros/mock_library_loader.h" | 20 #include "chrome/browser/chromeos/cros/mock_library_loader.h" |
| 21 #include "chrome/browser/chromeos/cros_settings.h" |
| 21 #include "chrome/browser/chromeos/cryptohome/mock_async_method_caller.h" | 22 #include "chrome/browser/chromeos/cryptohome/mock_async_method_caller.h" |
| 23 #include "chrome/browser/chromeos/dbus/mock_dbus_thread_manager.h" |
| 24 #include "chrome/browser/chromeos/dbus/mock_cryptohome_client.h" |
| 22 #include "chrome/browser/chromeos/login/mock_login_status_consumer.h" | 25 #include "chrome/browser/chromeos/login/mock_login_status_consumer.h" |
| 23 #include "chrome/browser/chromeos/login/mock_url_fetchers.h" | 26 #include "chrome/browser/chromeos/login/mock_url_fetchers.h" |
| 27 #include "chrome/browser/chromeos/login/mock_user_manager.h" |
| 24 #include "chrome/browser/chromeos/login/test_attempt_state.h" | 28 #include "chrome/browser/chromeos/login/test_attempt_state.h" |
| 29 #include "chrome/browser/chromeos/stub_cros_settings_provider.h" |
| 25 #include "chrome/common/chrome_paths.h" | 30 #include "chrome/common/chrome_paths.h" |
| 26 #include "chrome/common/net/gaia/mock_url_fetcher_factory.h" | 31 #include "chrome/common/net/gaia/mock_url_fetcher_factory.h" |
| 27 #include "chrome/test/base/testing_profile.h" | 32 #include "chrome/test/base/testing_profile.h" |
| 28 #include "content/test/test_browser_thread.h" | 33 #include "content/test/test_browser_thread.h" |
| 29 #include "googleurl/src/gurl.h" | 34 #include "googleurl/src/gurl.h" |
| 30 #include "net/base/net_errors.h" | 35 #include "net/base/net_errors.h" |
| 31 #include "net/url_request/url_request_status.h" | 36 #include "net/url_request/url_request_status.h" |
| 32 #include "testing/gmock/include/gmock/gmock.h" | 37 #include "testing/gmock/include/gmock/gmock.h" |
| 33 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
| 34 #include "third_party/cros_system_api/dbus/service_constants.h" | 39 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 35 | 40 |
| 36 using content::BrowserThread; | 41 using content::BrowserThread; |
| 37 using file_util::CloseFile; | 42 using file_util::CloseFile; |
| 38 using file_util::CreateAndOpenTemporaryFile; | 43 using file_util::CreateAndOpenTemporaryFile; |
| 39 using file_util::CreateAndOpenTemporaryFileInDir; | 44 using file_util::CreateAndOpenTemporaryFileInDir; |
| 40 using file_util::Delete; | 45 using file_util::Delete; |
| 41 using file_util::WriteFile; | 46 using file_util::WriteFile; |
| 42 using ::testing::AnyNumber; | 47 using ::testing::AnyNumber; |
| 43 using ::testing::DoAll; | 48 using ::testing::DoAll; |
| 44 using ::testing::Invoke; | 49 using ::testing::Invoke; |
| 45 using ::testing::Return; | 50 using ::testing::Return; |
| 46 using ::testing::SetArgumentPointee; | 51 using ::testing::SetArgPointee; |
| 47 using ::testing::_; | 52 using ::testing::_; |
| 48 | 53 |
| 49 namespace chromeos { | 54 namespace chromeos { |
| 50 | 55 |
| 51 class TestOnlineAttempt : public OnlineAttempt { | 56 class TestOnlineAttempt : public OnlineAttempt { |
| 52 public: | 57 public: |
| 53 TestOnlineAttempt(AuthAttemptState* state, | 58 TestOnlineAttempt(AuthAttemptState* state, |
| 54 AuthAttemptStateResolver* resolver) | 59 AuthAttemptStateResolver* resolver) |
| 55 : OnlineAttempt(false, state, resolver) { | 60 : OnlineAttempt(false, state, resolver) { |
| 56 } | 61 } |
| 57 }; | 62 }; |
| 58 | 63 |
| 59 class ParallelAuthenticatorTest : public testing::Test { | 64 class ParallelAuthenticatorTest : public testing::Test { |
| 60 public: | 65 public: |
| 61 ParallelAuthenticatorTest() | 66 ParallelAuthenticatorTest() |
| 62 : message_loop_(MessageLoop::TYPE_UI), | 67 : message_loop_(MessageLoop::TYPE_UI), |
| 63 ui_thread_(BrowserThread::UI, &message_loop_), | 68 ui_thread_(BrowserThread::UI, &message_loop_), |
| 69 file_thread_(BrowserThread::FILE, &message_loop_), |
| 64 io_thread_(BrowserThread::IO), | 70 io_thread_(BrowserThread::IO), |
| 65 username_("me@nowhere.org"), | 71 username_("me@nowhere.org"), |
| 66 password_("fakepass") { | 72 password_("fakepass") { |
| 67 hash_ascii_.assign("0a010000000000a0"); | 73 hash_ascii_.assign("0a010000000000a0"); |
| 68 hash_ascii_.append(std::string(16, '0')); | 74 hash_ascii_.append(std::string(16, '0')); |
| 69 } | 75 } |
| 70 | 76 |
| 71 ~ParallelAuthenticatorTest() { | 77 ~ParallelAuthenticatorTest() { |
| 72 DCHECK(!mock_caller_); | 78 DCHECK(!mock_caller_); |
| 73 } | 79 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 84 .WillByDefault(Return(true)); | 90 .WillByDefault(Return(true)); |
| 85 EXPECT_CALL(*loader_, Load(_)) | 91 EXPECT_CALL(*loader_, Load(_)) |
| 86 .Times(AnyNumber()); | 92 .Times(AnyNumber()); |
| 87 | 93 |
| 88 test_api->SetLibraryLoader(loader_, true); | 94 test_api->SetLibraryLoader(loader_, true); |
| 89 | 95 |
| 90 mock_library_ = new MockCryptohomeLibrary(); | 96 mock_library_ = new MockCryptohomeLibrary(); |
| 91 test_api->SetCryptohomeLibrary(mock_library_, true); | 97 test_api->SetCryptohomeLibrary(mock_library_, true); |
| 92 io_thread_.Start(); | 98 io_thread_.Start(); |
| 93 | 99 |
| 100 EXPECT_CALL(*mock_user_manager_.user_manager(), LoadKeyStore()) |
| 101 .Times(AnyNumber()); |
| 102 |
| 94 auth_ = new ParallelAuthenticator(&consumer_); | 103 auth_ = new ParallelAuthenticator(&consumer_); |
| 95 auth_->set_using_oauth(false); | 104 auth_->set_using_oauth(false); |
| 96 state_.reset(new TestAttemptState(username_, | 105 state_.reset(new TestAttemptState(username_, |
| 97 password_, | 106 password_, |
| 98 hash_ascii_, | 107 hash_ascii_, |
| 99 "", | 108 "", |
| 100 "", | 109 "", |
| 101 false)); | 110 false)); |
| 102 } | 111 } |
| 103 | 112 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 void SetAttemptState(ParallelAuthenticator* auth, TestAttemptState* state) { | 200 void SetAttemptState(ParallelAuthenticator* auth, TestAttemptState* state) { |
| 192 auth->set_attempt_state(state); | 201 auth->set_attempt_state(state); |
| 193 } | 202 } |
| 194 | 203 |
| 195 ParallelAuthenticator::AuthState SetAndResolveState( | 204 ParallelAuthenticator::AuthState SetAndResolveState( |
| 196 ParallelAuthenticator* auth, TestAttemptState* state) { | 205 ParallelAuthenticator* auth, TestAttemptState* state) { |
| 197 auth->set_attempt_state(state); | 206 auth->set_attempt_state(state); |
| 198 return auth->ResolveState(); | 207 return auth->ResolveState(); |
| 199 } | 208 } |
| 200 | 209 |
| 210 void SetOwnerState(bool owner_check_finished, bool check_result) { |
| 211 auth_->SetOwnerState(owner_check_finished, check_result); |
| 212 } |
| 213 |
| 201 void FakeOnlineAttempt() { | 214 void FakeOnlineAttempt() { |
| 202 auth_->set_online_attempt(new TestOnlineAttempt(state_.get(), auth_.get())); | 215 auth_->set_online_attempt(new TestOnlineAttempt(state_.get(), auth_.get())); |
| 203 } | 216 } |
| 204 | 217 |
| 205 MessageLoop message_loop_; | 218 MessageLoop message_loop_; |
| 206 content::TestBrowserThread ui_thread_; | 219 content::TestBrowserThread ui_thread_; |
| 220 content::TestBrowserThread file_thread_; |
| 207 content::TestBrowserThread io_thread_; | 221 content::TestBrowserThread io_thread_; |
| 208 | 222 |
| 209 std::string username_; | 223 std::string username_; |
| 210 std::string password_; | 224 std::string password_; |
| 211 std::string hash_ascii_; | 225 std::string hash_ascii_; |
| 212 | 226 |
| 213 // Initializes / shuts down a stub CrosLibrary. | 227 // Initializes / shuts down a stub CrosLibrary. |
| 214 chromeos::ScopedStubCrosEnabler stub_cros_enabler_; | 228 chromeos::ScopedStubCrosEnabler stub_cros_enabler_; |
| 215 | 229 |
| 216 // Mocks, destroyed by CrosLibrary class. | 230 // Mocks, destroyed by CrosLibrary class. |
| 217 MockCryptohomeLibrary* mock_library_; | 231 MockCryptohomeLibrary* mock_library_; |
| 218 MockLibraryLoader* loader_; | 232 MockLibraryLoader* loader_; |
| 233 ScopedMockUserManagerEnabler mock_user_manager_; |
| 219 | 234 |
| 220 cryptohome::MockAsyncMethodCaller* mock_caller_; | 235 cryptohome::MockAsyncMethodCaller* mock_caller_; |
| 221 | 236 |
| 222 MockConsumer consumer_; | 237 MockConsumer consumer_; |
| 223 scoped_refptr<ParallelAuthenticator> auth_; | 238 scoped_refptr<ParallelAuthenticator> auth_; |
| 224 scoped_ptr<TestAttemptState> state_; | 239 scoped_ptr<TestAttemptState> state_; |
| 225 }; | 240 }; |
| 226 | 241 |
| 227 TEST_F(ParallelAuthenticatorTest, OnLoginSuccess) { | 242 TEST_F(ParallelAuthenticatorTest, OnLoginSuccess) { |
| 228 EXPECT_CALL(consumer_, OnLoginSuccess(username_, password_, false, false)) | 243 EXPECT_CALL(consumer_, OnLoginSuccess(username_, password_, false, false)) |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 // Set up state as though a cryptohome mount attempt has occurred | 287 // Set up state as though a cryptohome mount attempt has occurred |
| 273 // and been rejected because of unmatched key; additionally, | 288 // and been rejected because of unmatched key; additionally, |
| 274 // an online auth attempt has completed successfully. | 289 // an online auth attempt has completed successfully. |
| 275 state_->PresetCryptohomeStatus(false, cryptohome::MOUNT_ERROR_KEY_FAILURE); | 290 state_->PresetCryptohomeStatus(false, cryptohome::MOUNT_ERROR_KEY_FAILURE); |
| 276 state_->PresetOnlineLoginStatus(LoginFailure::None()); | 291 state_->PresetOnlineLoginStatus(LoginFailure::None()); |
| 277 | 292 |
| 278 EXPECT_EQ(ParallelAuthenticator::NEED_OLD_PW, | 293 EXPECT_EQ(ParallelAuthenticator::NEED_OLD_PW, |
| 279 SetAndResolveState(auth_, state_.release())); | 294 SetAndResolveState(auth_, state_.release())); |
| 280 } | 295 } |
| 281 | 296 |
| 297 TEST_F(ParallelAuthenticatorTest, ResolveOwnerNeededDirectFailedMount) { |
| 298 // Set up state as though a cryptohome mount attempt has occurred |
| 299 // and succeeded but we are in safe mode and the current user is not owner. |
| 300 // This is a high level test to verify the proper transitioning in this mode |
| 301 // only. It is not testing that we properly verify that the user is an owner |
| 302 // or that we really are in "safe-mode". |
| 303 state_->PresetCryptohomeStatus(true, cryptohome::MOUNT_ERROR_NONE); |
| 304 SetOwnerState(true, false); |
| 305 |
| 306 EXPECT_EQ(ParallelAuthenticator::OWNER_REQUIRED, |
| 307 SetAndResolveState(auth_, state_.release())); |
| 308 } |
| 309 |
| 310 TEST_F(ParallelAuthenticatorTest, ResolveOwnerNeededMount) { |
| 311 // Set up state as though a cryptohome mount attempt has occurred |
| 312 // and succeeded but we are in safe mode and the current user is not owner. |
| 313 // This test will check that the "safe-mode" policy is not set and will let |
| 314 // the mount finish successfully. |
| 315 state_->PresetCryptohomeStatus(true, cryptohome::MOUNT_ERROR_NONE); |
| 316 SetOwnerState(false, false); |
| 317 // and test that the mount has succeeded. |
| 318 state_.reset(new TestAttemptState(username_, |
| 319 password_, |
| 320 hash_ascii_, |
| 321 "", |
| 322 "", |
| 323 false)); |
| 324 state_->PresetCryptohomeStatus(true, cryptohome::MOUNT_ERROR_NONE); |
| 325 EXPECT_EQ(ParallelAuthenticator::OFFLINE_LOGIN, |
| 326 SetAndResolveState(auth_, state_.release())); |
| 327 } |
| 328 |
| 329 TEST_F(ParallelAuthenticatorTest, ResolveOwnerNeededFailedMount) { |
| 330 FailOnLoginSuccess(); // Set failing on success as the default... |
| 331 LoginFailure failure = LoginFailure(LoginFailure::OWNER_REQUIRED); |
| 332 ExpectLoginFailure(failure); |
| 333 |
| 334 MockDBusThreadManager* mock_dbus_thread_manager = |
| 335 new MockDBusThreadManager; |
| 336 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager); |
| 337 EXPECT_CALL(*mock_dbus_thread_manager->mock_cryptohome_client(), Unmount(_)) |
| 338 .WillOnce(DoAll(SetArgPointee<0>(true), Return(true))); |
| 339 |
| 340 CrosSettingsProvider* device_settings_provider; |
| 341 StubCrosSettingsProvider stub_settings_provider; |
| 342 // Set up state as though a cryptohome mount attempt has occurred |
| 343 // and succeeded but we are in safe mode and the current user is not owner. |
| 344 state_->PresetCryptohomeStatus(true, cryptohome::MOUNT_ERROR_NONE); |
| 345 SetOwnerState(false, false); |
| 346 // Remove the real DeviceSettingsProvider and replace it with a stub. |
| 347 device_settings_provider = |
| 348 CrosSettings::Get()->GetProvider(chromeos::kReportDeviceVersionInfo); |
| 349 EXPECT_TRUE(device_settings_provider != NULL); |
| 350 EXPECT_TRUE( |
| 351 CrosSettings::Get()->RemoveSettingsProvider(device_settings_provider)); |
| 352 CrosSettings::Get()->AddSettingsProvider(&stub_settings_provider); |
| 353 CrosSettings::Get()->SetBoolean(kPolicyMissingMitigationMode, true); |
| 354 |
| 355 EXPECT_EQ(ParallelAuthenticator::CONTINUE, |
| 356 SetAndResolveState(auth_, state_.release())); |
| 357 // Let the owner verification run on the FILE thread... |
| 358 message_loop_.RunAllPending(); |
| 359 // and test that the mount has succeeded. |
| 360 state_.reset(new TestAttemptState(username_, |
| 361 password_, |
| 362 hash_ascii_, |
| 363 "", |
| 364 "", |
| 365 false)); |
| 366 state_->PresetCryptohomeStatus(true, cryptohome::MOUNT_ERROR_NONE); |
| 367 EXPECT_EQ(ParallelAuthenticator::OWNER_REQUIRED, |
| 368 SetAndResolveState(auth_, state_.release())); |
| 369 |
| 370 EXPECT_TRUE( |
| 371 CrosSettings::Get()->RemoveSettingsProvider(&stub_settings_provider)); |
| 372 CrosSettings::Get()->AddSettingsProvider(device_settings_provider); |
| 373 DBusThreadManager::Get()->Shutdown(); |
| 374 } |
| 375 |
| 282 TEST_F(ParallelAuthenticatorTest, DriveFailedMount) { | 376 TEST_F(ParallelAuthenticatorTest, DriveFailedMount) { |
| 283 FailOnLoginSuccess(); | 377 FailOnLoginSuccess(); |
| 284 ExpectLoginFailure(LoginFailure(LoginFailure::COULD_NOT_MOUNT_CRYPTOHOME)); | 378 ExpectLoginFailure(LoginFailure(LoginFailure::COULD_NOT_MOUNT_CRYPTOHOME)); |
| 285 | 379 |
| 286 // Set up state as though a cryptohome mount attempt has occurred | 380 // Set up state as though a cryptohome mount attempt has occurred |
| 287 // and failed. | 381 // and failed. |
| 288 state_->PresetCryptohomeStatus(false, cryptohome::MOUNT_ERROR_NONE); | 382 state_->PresetCryptohomeStatus(false, cryptohome::MOUNT_ERROR_NONE); |
| 289 SetAttemptState(auth_, state_.release()); | 383 SetAttemptState(auth_, state_.release()); |
| 290 | 384 |
| 291 RunResolve(auth_.get()); | 385 RunResolve(auth_.get()); |
| (...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 672 .RetiresOnSaturation(); | 766 .RetiresOnSaturation(); |
| 673 EXPECT_CALL(*mock_library_, HashPassword(_)) | 767 EXPECT_CALL(*mock_library_, HashPassword(_)) |
| 674 .WillOnce(Return(std::string())) | 768 .WillOnce(Return(std::string())) |
| 675 .RetiresOnSaturation(); | 769 .RetiresOnSaturation(); |
| 676 | 770 |
| 677 auth_->AuthenticateToUnlock(username_, ""); | 771 auth_->AuthenticateToUnlock(username_, ""); |
| 678 message_loop_.Run(); | 772 message_loop_.Run(); |
| 679 } | 773 } |
| 680 | 774 |
| 681 } // namespace chromeos | 775 } // namespace chromeos |
| OLD | NEW |