Chromium Code Reviews| 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 mock_user_manager_.reset(new MockUserManager()); | |
| 101 old_user_manager_ = UserManager::Set(mock_user_manager_.get()); | |
|
Chris Masone
2012/03/22 16:18:25
This confuses me...don't you have two pointers to
pastarmovj
2012/03/22 16:25:46
Actually UserManager::Set *replaces* the current o
| |
| 102 EXPECT_CALL(*mock_user_manager_, LoadKeyStore()) | |
| 103 .Times(AnyNumber()); | |
| 104 | |
| 94 auth_ = new ParallelAuthenticator(&consumer_); | 105 auth_ = new ParallelAuthenticator(&consumer_); |
| 95 auth_->set_using_oauth(false); | 106 auth_->set_using_oauth(false); |
| 96 state_.reset(new TestAttemptState(username_, | 107 state_.reset(new TestAttemptState(username_, |
| 97 password_, | 108 password_, |
| 98 hash_ascii_, | 109 hash_ascii_, |
| 99 "", | 110 "", |
| 100 "", | 111 "", |
| 101 false)); | 112 false)); |
| 102 } | 113 } |
| 103 | 114 |
| 104 // Tears down the test fixture. | 115 // Tears down the test fixture. |
| 105 virtual void TearDown() { | 116 virtual void TearDown() { |
| 106 // Prevent bogus gMock leak check from firing. | 117 // Prevent bogus gMock leak check from firing. |
| 107 chromeos::CrosLibrary::TestApi* test_api = | 118 chromeos::CrosLibrary::TestApi* test_api = |
| 108 chromeos::CrosLibrary::Get()->GetTestApi(); | 119 chromeos::CrosLibrary::Get()->GetTestApi(); |
| 109 test_api->SetLibraryLoader(NULL, false); | 120 test_api->SetLibraryLoader(NULL, false); |
| 110 test_api->SetCryptohomeLibrary(NULL, false); | 121 test_api->SetCryptohomeLibrary(NULL, false); |
| 111 | 122 |
| 123 UserManager::Set(old_user_manager_); | |
| 124 | |
| 112 cryptohome::AsyncMethodCaller::Shutdown(); | 125 cryptohome::AsyncMethodCaller::Shutdown(); |
| 113 mock_caller_ = NULL; | 126 mock_caller_ = NULL; |
| 114 } | 127 } |
| 115 | 128 |
| 116 FilePath PopulateTempFile(const char* data, int data_len) { | 129 FilePath PopulateTempFile(const char* data, int data_len) { |
| 117 FilePath out; | 130 FilePath out; |
| 118 FILE* tmp_file = CreateAndOpenTemporaryFile(&out); | 131 FILE* tmp_file = CreateAndOpenTemporaryFile(&out); |
| 119 EXPECT_NE(tmp_file, static_cast<FILE*>(NULL)); | 132 EXPECT_NE(tmp_file, static_cast<FILE*>(NULL)); |
| 120 EXPECT_EQ(WriteFile(out, data, data_len), data_len); | 133 EXPECT_EQ(WriteFile(out, data, data_len), data_len); |
| 121 EXPECT_TRUE(CloseFile(tmp_file)); | 134 EXPECT_TRUE(CloseFile(tmp_file)); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 191 void SetAttemptState(ParallelAuthenticator* auth, TestAttemptState* state) { | 204 void SetAttemptState(ParallelAuthenticator* auth, TestAttemptState* state) { |
| 192 auth->set_attempt_state(state); | 205 auth->set_attempt_state(state); |
| 193 } | 206 } |
| 194 | 207 |
| 195 ParallelAuthenticator::AuthState SetAndResolveState( | 208 ParallelAuthenticator::AuthState SetAndResolveState( |
| 196 ParallelAuthenticator* auth, TestAttemptState* state) { | 209 ParallelAuthenticator* auth, TestAttemptState* state) { |
| 197 auth->set_attempt_state(state); | 210 auth->set_attempt_state(state); |
| 198 return auth->ResolveState(); | 211 return auth->ResolveState(); |
| 199 } | 212 } |
| 200 | 213 |
| 214 void SetOwnerState(bool owner_check_finished, bool check_result) { | |
| 215 auth_->SetOwnerState(owner_check_finished, check_result); | |
| 216 } | |
| 217 | |
| 201 void FakeOnlineAttempt() { | 218 void FakeOnlineAttempt() { |
| 202 auth_->set_online_attempt(new TestOnlineAttempt(state_.get(), auth_.get())); | 219 auth_->set_online_attempt(new TestOnlineAttempt(state_.get(), auth_.get())); |
| 203 } | 220 } |
| 204 | 221 |
| 205 MessageLoop message_loop_; | 222 MessageLoop message_loop_; |
| 206 content::TestBrowserThread ui_thread_; | 223 content::TestBrowserThread ui_thread_; |
| 224 content::TestBrowserThread file_thread_; | |
| 207 content::TestBrowserThread io_thread_; | 225 content::TestBrowserThread io_thread_; |
| 208 | 226 |
| 209 std::string username_; | 227 std::string username_; |
| 210 std::string password_; | 228 std::string password_; |
| 211 std::string hash_ascii_; | 229 std::string hash_ascii_; |
| 212 | 230 |
| 213 // Initializes / shuts down a stub CrosLibrary. | 231 // Initializes / shuts down a stub CrosLibrary. |
| 214 chromeos::ScopedStubCrosEnabler stub_cros_enabler_; | 232 chromeos::ScopedStubCrosEnabler stub_cros_enabler_; |
| 215 | 233 |
| 216 // Mocks, destroyed by CrosLibrary class. | 234 // Mocks, destroyed by CrosLibrary class. |
| 217 MockCryptohomeLibrary* mock_library_; | 235 MockCryptohomeLibrary* mock_library_; |
| 218 MockLibraryLoader* loader_; | 236 MockLibraryLoader* loader_; |
| 237 scoped_ptr<MockUserManager> mock_user_manager_; | |
| 238 UserManager* old_user_manager_; | |
| 219 | 239 |
| 220 cryptohome::MockAsyncMethodCaller* mock_caller_; | 240 cryptohome::MockAsyncMethodCaller* mock_caller_; |
| 221 | 241 |
| 222 MockConsumer consumer_; | 242 MockConsumer consumer_; |
| 223 scoped_refptr<ParallelAuthenticator> auth_; | 243 scoped_refptr<ParallelAuthenticator> auth_; |
| 224 scoped_ptr<TestAttemptState> state_; | 244 scoped_ptr<TestAttemptState> state_; |
| 225 }; | 245 }; |
| 226 | 246 |
| 227 TEST_F(ParallelAuthenticatorTest, OnLoginSuccess) { | 247 TEST_F(ParallelAuthenticatorTest, OnLoginSuccess) { |
| 228 EXPECT_CALL(consumer_, OnLoginSuccess(username_, password_, false, false)) | 248 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 | 292 // Set up state as though a cryptohome mount attempt has occurred |
| 273 // and been rejected because of unmatched key; additionally, | 293 // and been rejected because of unmatched key; additionally, |
| 274 // an online auth attempt has completed successfully. | 294 // an online auth attempt has completed successfully. |
| 275 state_->PresetCryptohomeStatus(false, cryptohome::MOUNT_ERROR_KEY_FAILURE); | 295 state_->PresetCryptohomeStatus(false, cryptohome::MOUNT_ERROR_KEY_FAILURE); |
| 276 state_->PresetOnlineLoginStatus(LoginFailure::None()); | 296 state_->PresetOnlineLoginStatus(LoginFailure::None()); |
| 277 | 297 |
| 278 EXPECT_EQ(ParallelAuthenticator::NEED_OLD_PW, | 298 EXPECT_EQ(ParallelAuthenticator::NEED_OLD_PW, |
| 279 SetAndResolveState(auth_, state_.release())); | 299 SetAndResolveState(auth_, state_.release())); |
| 280 } | 300 } |
| 281 | 301 |
| 302 TEST_F(ParallelAuthenticatorTest, ResolveOwnerNeededDirectFailedMount) { | |
| 303 // Set up state as though a cryptohome mount attempt has occurred | |
| 304 // and succeeded but we are in safe mode and the current user is not owner. | |
| 305 // This is a high level test to verify the proper transitioning in this mode | |
| 306 // only. It is not testing that we properly verify that the user is an owner | |
| 307 // or that we really are in "safe-mode". | |
| 308 state_->PresetCryptohomeStatus(true, cryptohome::MOUNT_ERROR_NONE); | |
| 309 SetOwnerState(true, false); | |
| 310 | |
| 311 EXPECT_EQ(ParallelAuthenticator::OWNER_REQUIRED, | |
| 312 SetAndResolveState(auth_, state_.release())); | |
| 313 } | |
| 314 | |
| 315 TEST_F(ParallelAuthenticatorTest, ResolveOwnerNeededMount) { | |
| 316 // Set up state as though a cryptohome mount attempt has occurred | |
| 317 // and succeeded but we are in safe mode and the current user is not owner. | |
| 318 // This test will check that the "safe-mode" policy is not set and will let | |
| 319 // the mount finish successfully. | |
| 320 state_->PresetCryptohomeStatus(true, cryptohome::MOUNT_ERROR_NONE); | |
| 321 SetOwnerState(false, false); | |
| 322 // and test that the mount has succeeded. | |
| 323 state_.reset(new TestAttemptState(username_, | |
| 324 password_, | |
| 325 hash_ascii_, | |
| 326 "", | |
| 327 "", | |
| 328 false)); | |
| 329 state_->PresetCryptohomeStatus(true, cryptohome::MOUNT_ERROR_NONE); | |
| 330 EXPECT_EQ(ParallelAuthenticator::OFFLINE_LOGIN, | |
| 331 SetAndResolveState(auth_, state_.release())); | |
| 332 } | |
| 333 | |
| 334 TEST_F(ParallelAuthenticatorTest, ResolveOwnerNeededFailedMount) { | |
| 335 MockDBusThreadManager* mock_dbus_thread_manager = | |
| 336 new MockDBusThreadManager; | |
| 337 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager); | |
| 338 EXPECT_CALL(*mock_dbus_thread_manager->mock_cryptohome_client(), Unmount(_)) | |
| 339 .WillOnce(DoAll(SetArgPointee<0>(true), Return(true))); | |
| 340 | |
| 341 CrosSettingsProvider* device_settings_provider; | |
| 342 StubCrosSettingsProvider stub_settings_provider; | |
| 343 // Set up state as though a cryptohome mount attempt has occurred | |
| 344 // and succeeded but we are in safe mode and the current user is not owner. | |
| 345 state_->PresetCryptohomeStatus(true, cryptohome::MOUNT_ERROR_NONE); | |
| 346 SetOwnerState(false, false); | |
| 347 // Remove the real DeviceSettingsProvider and replace it with a stub. | |
| 348 device_settings_provider = | |
| 349 CrosSettings::Get()->GetProvider(chromeos::kReportDeviceVersionInfo); | |
| 350 EXPECT_TRUE(device_settings_provider != NULL); | |
| 351 EXPECT_TRUE( | |
| 352 CrosSettings::Get()->RemoveSettingsProvider(device_settings_provider)); | |
| 353 CrosSettings::Get()->AddSettingsProvider(&stub_settings_provider); | |
| 354 CrosSettings::Get()->SetBoolean(kPolicyMissingMitigationMode, true); | |
| 355 | |
| 356 EXPECT_EQ(ParallelAuthenticator::CONTINUE, | |
| 357 SetAndResolveState(auth_, state_.release())); | |
| 358 // Let the owner verification run on the FILE thread... | |
| 359 message_loop_.RunAllPending(); | |
| 360 // and test that the mount has succeeded. | |
| 361 state_.reset(new TestAttemptState(username_, | |
| 362 password_, | |
| 363 hash_ascii_, | |
| 364 "", | |
| 365 "", | |
| 366 false)); | |
| 367 state_->PresetCryptohomeStatus(true, cryptohome::MOUNT_ERROR_NONE); | |
| 368 EXPECT_EQ(ParallelAuthenticator::OWNER_REQUIRED, | |
| 369 SetAndResolveState(auth_, state_.release())); | |
| 370 | |
| 371 EXPECT_TRUE( | |
| 372 CrosSettings::Get()->RemoveSettingsProvider(&stub_settings_provider)); | |
| 373 CrosSettings::Get()->AddSettingsProvider(device_settings_provider); | |
| 374 DBusThreadManager::Get()->Shutdown(); | |
| 375 } | |
| 376 | |
| 282 TEST_F(ParallelAuthenticatorTest, DriveFailedMount) { | 377 TEST_F(ParallelAuthenticatorTest, DriveFailedMount) { |
| 283 FailOnLoginSuccess(); | 378 FailOnLoginSuccess(); |
| 284 ExpectLoginFailure(LoginFailure(LoginFailure::COULD_NOT_MOUNT_CRYPTOHOME)); | 379 ExpectLoginFailure(LoginFailure(LoginFailure::COULD_NOT_MOUNT_CRYPTOHOME)); |
| 285 | 380 |
| 286 // Set up state as though a cryptohome mount attempt has occurred | 381 // Set up state as though a cryptohome mount attempt has occurred |
| 287 // and failed. | 382 // and failed. |
| 288 state_->PresetCryptohomeStatus(false, cryptohome::MOUNT_ERROR_NONE); | 383 state_->PresetCryptohomeStatus(false, cryptohome::MOUNT_ERROR_NONE); |
| 289 SetAttemptState(auth_, state_.release()); | 384 SetAttemptState(auth_, state_.release()); |
| 290 | 385 |
| 291 RunResolve(auth_.get()); | 386 RunResolve(auth_.get()); |
| (...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 672 .RetiresOnSaturation(); | 767 .RetiresOnSaturation(); |
| 673 EXPECT_CALL(*mock_library_, HashPassword(_)) | 768 EXPECT_CALL(*mock_library_, HashPassword(_)) |
| 674 .WillOnce(Return(std::string())) | 769 .WillOnce(Return(std::string())) |
| 675 .RetiresOnSaturation(); | 770 .RetiresOnSaturation(); |
| 676 | 771 |
| 677 auth_->AuthenticateToUnlock(username_, ""); | 772 auth_->AuthenticateToUnlock(username_, ""); |
| 678 message_loop_.Run(); | 773 message_loop_.Run(); |
| 679 } | 774 } |
| 680 | 775 |
| 681 } // namespace chromeos | 776 } // namespace chromeos |
| OLD | NEW |