Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(184)

Side by Side Diff: chrome/browser/chromeos/login/auth/cryptohome_authenticator_unittest.cc

Issue 1019283004: Switch to direct use of OwnerSettingsServiceChromeOS::Set() in tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use settings provider to set read-only device settings. Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chrome/browser/chromeos/login/auth/chrome_cryptohome_authenticator.h" 5 #include "chrome/browser/chromeos/login/auth/chrome_cryptohome_authenticator.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/files/file_path.h" 12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h" 13 #include "base/files/file_util.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
16 #include "base/run_loop.h" 16 #include "base/run_loop.h"
17 #include "base/strings/string_util.h" 17 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h" 18 #include "base/strings/stringprintf.h"
19 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" 19 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
20 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos.h" 20 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos.h"
21 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos_fact ory.h" 21 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos_fact ory.h"
22 #include "chrome/browser/chromeos/profiles/profile_helper.h" 22 #include "chrome/browser/chromeos/profiles/profile_helper.h"
23 #include "chrome/browser/chromeos/settings/cros_settings.h" 23 #include "chrome/browser/chromeos/settings/cros_settings.h"
24 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h" 24 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
25 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" 25 #include "chrome/browser/chromeos/settings/settings_provider_test_base.h"
26 #include "chrome/test/base/testing_browser_process.h" 26 #include "chrome/test/base/testing_browser_process.h"
27 #include "chrome/test/base/testing_profile.h" 27 #include "chrome/test/base/testing_profile.h"
28 #include "chrome/test/base/testing_profile_manager.h" 28 #include "chrome/test/base/testing_profile_manager.h"
29 #include "chromeos/chromeos_switches.h" 29 #include "chromeos/chromeos_switches.h"
30 #include "chromeos/cryptohome/cryptohome_parameters.h" 30 #include "chromeos/cryptohome/cryptohome_parameters.h"
31 #include "chromeos/cryptohome/homedir_methods.h" 31 #include "chromeos/cryptohome/homedir_methods.h"
32 #include "chromeos/cryptohome/mock_async_method_caller.h" 32 #include "chromeos/cryptohome/mock_async_method_caller.h"
33 #include "chromeos/cryptohome/mock_homedir_methods.h" 33 #include "chromeos/cryptohome/mock_homedir_methods.h"
34 #include "chromeos/cryptohome/system_salt_getter.h" 34 #include "chromeos/cryptohome/system_salt_getter.h"
35 #include "chromeos/dbus/cros_disks_client.h" 35 #include "chromeos/dbus/cros_disks_client.h"
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 120
121 scoped_ptr<crypto::RSAPrivateKey> CreateOwnerKeyInSlot(PK11SlotInfo* slot) { 121 scoped_ptr<crypto::RSAPrivateKey> CreateOwnerKeyInSlot(PK11SlotInfo* slot) {
122 const std::vector<uint8> key(kOwnerPrivateKey, 122 const std::vector<uint8> key(kOwnerPrivateKey,
123 kOwnerPrivateKey + arraysize(kOwnerPrivateKey)); 123 kOwnerPrivateKey + arraysize(kOwnerPrivateKey));
124 return make_scoped_ptr( 124 return make_scoped_ptr(
125 crypto::RSAPrivateKey::CreateSensitiveFromPrivateKeyInfo(slot, key)); 125 crypto::RSAPrivateKey::CreateSensitiveFromPrivateKeyInfo(slot, key));
126 } 126 }
127 127
128 } // namespace 128 } // namespace
129 129
130 class CryptohomeAuthenticatorTest : public testing::Test { 130 class CryptohomeAuthenticatorTest : public testing::Test,
131 public chromeos::SettingsProviderTestBase {
131 public: 132 public:
132 CryptohomeAuthenticatorTest() 133 CryptohomeAuthenticatorTest()
133 : user_context_("me@nowhere.org"), 134 : chromeos::SettingsProviderTestBase(false),
135 user_context_("me@nowhere.org"),
134 user_manager_(new user_manager::FakeUserManager()), 136 user_manager_(new user_manager::FakeUserManager()),
135 user_manager_enabler_(user_manager_), 137 user_manager_enabler_(user_manager_),
136 mock_caller_(NULL), 138 mock_caller_(NULL),
137 mock_homedir_methods_(NULL), 139 mock_homedir_methods_(NULL),
138 owner_key_util_(new ownership::MockOwnerKeyUtil()) { 140 owner_key_util_(new ownership::MockOwnerKeyUtil()) {
139 OwnerSettingsServiceChromeOSFactory::GetInstance() 141 OwnerSettingsServiceChromeOSFactory::GetInstance()
140 ->SetOwnerKeyUtilForTesting(owner_key_util_); 142 ->SetOwnerKeyUtilForTesting(owner_key_util_);
141 user_context_.SetKey(Key("fakepass")); 143 user_context_.SetKey(Key("fakepass"));
142 user_context_.SetUserIDHash("me_nowhere_com_hash"); 144 user_context_.SetUserIDHash("me_nowhere_com_hash");
143 const user_manager::User* user = 145 const user_manager::User* user =
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 ASSERT_TRUE(profile_manager_->SetUp()); 429 ASSERT_TRUE(profile_manager_->SetUp());
428 430
429 FailOnLoginSuccess(); // Set failing on success as the default... 431 FailOnLoginSuccess(); // Set failing on success as the default...
430 AuthFailure failure = AuthFailure(AuthFailure::OWNER_REQUIRED); 432 AuthFailure failure = AuthFailure(AuthFailure::OWNER_REQUIRED);
431 ExpectLoginFailure(failure); 433 ExpectLoginFailure(failure);
432 434
433 // Set up state as though a cryptohome mount attempt has occurred 435 // Set up state as though a cryptohome mount attempt has occurred
434 // and succeeded but we are in safe mode and the current user is not owner. 436 // and succeeded but we are in safe mode and the current user is not owner.
435 state_->PresetCryptohomeStatus(true, cryptohome::MOUNT_ERROR_NONE); 437 state_->PresetCryptohomeStatus(true, cryptohome::MOUNT_ERROR_NONE);
436 SetOwnerState(false, false); 438 SetOwnerState(false, false);
437 // Remove the real DeviceSettingsProvider and replace it with a stub. 439 ReplaceProvider(kPolicyMissingMitigationMode);
Mattias Nissler (ping if slow) 2015/03/30 14:45:46 If you made your helper into a ScopedCrosSettingsT
438 CrosSettingsProvider* device_settings_provider = 440 stub_settings_provider_.SetBoolean(kPolicyMissingMitigationMode, true);
439 CrosSettings::Get()->GetProvider(chromeos::kReportDeviceVersionInfo);
440 EXPECT_TRUE(device_settings_provider != NULL);
441 EXPECT_TRUE(
442 CrosSettings::Get()->RemoveSettingsProvider(device_settings_provider));
443 StubCrosSettingsProvider stub_settings_provider;
444 CrosSettings::Get()->AddSettingsProvider(&stub_settings_provider);
445 CrosSettings::Get()->SetBoolean(kPolicyMissingMitigationMode, true);
446 441
447 // Initialize login state for this test to verify the login state is changed 442 // Initialize login state for this test to verify the login state is changed
448 // to SAFE_MODE. 443 // to SAFE_MODE.
449 LoginState::Initialize(); 444 LoginState::Initialize();
450 445
451 EXPECT_EQ(CryptohomeAuthenticator::CONTINUE, 446 EXPECT_EQ(CryptohomeAuthenticator::CONTINUE,
452 SetAndResolveState(auth_.get(), state_.release())); 447 SetAndResolveState(auth_.get(), state_.release()));
453 EXPECT_TRUE(LoginState::Get()->IsInSafeMode()); 448 EXPECT_TRUE(LoginState::Get()->IsInSafeMode());
454 449
455 // Flush all the pending operations. The operations should induce an owner 450 // Flush all the pending operations. The operations should induce an owner
456 // verification. 451 // verification.
457 device_settings_test_helper_.Flush(); 452 device_settings_test_helper_.Flush();
458 453
459 state_.reset(new TestAttemptState(user_context_, false)); 454 state_.reset(new TestAttemptState(user_context_, false));
460 state_->PresetCryptohomeStatus(true, cryptohome::MOUNT_ERROR_NONE); 455 state_->PresetCryptohomeStatus(true, cryptohome::MOUNT_ERROR_NONE);
461 456
462 // The owner key util should not have found the owner key, so login should 457 // The owner key util should not have found the owner key, so login should
463 // not be allowed. 458 // not be allowed.
464 EXPECT_EQ(CryptohomeAuthenticator::OWNER_REQUIRED, 459 EXPECT_EQ(CryptohomeAuthenticator::OWNER_REQUIRED,
465 SetAndResolveState(auth_.get(), state_.release())); 460 SetAndResolveState(auth_.get(), state_.release()));
466 EXPECT_TRUE(LoginState::Get()->IsInSafeMode()); 461 EXPECT_TRUE(LoginState::Get()->IsInSafeMode());
467 462
468 // Unset global objects used by this test. 463 // Unset global objects used by this test.
469 fake_cryptohome_client_->set_unmount_result(true); 464 fake_cryptohome_client_->set_unmount_result(true);
470 LoginState::Shutdown(); 465 LoginState::Shutdown();
471 EXPECT_TRUE( 466 RestoreProvider();
472 CrosSettings::Get()->RemoveSettingsProvider(&stub_settings_provider));
473 CrosSettings::Get()->AddSettingsProvider(device_settings_provider);
474 } 467 }
475 468
476 // Test the case that login switches to SafeMode and the Owner logs in, which 469 // Test the case that login switches to SafeMode and the Owner logs in, which
477 // should lead to a successful login. 470 // should lead to a successful login.
478 TEST_F(CryptohomeAuthenticatorTest, ResolveOwnerNeededSuccess) { 471 TEST_F(CryptohomeAuthenticatorTest, ResolveOwnerNeededSuccess) {
479 crypto::ScopedTestNSSChromeOSUser test_user_db(user_context_.GetUserIDHash()); 472 crypto::ScopedTestNSSChromeOSUser test_user_db(user_context_.GetUserIDHash());
480 owner_key_util_->SetPublicKey(GetOwnerPublicKey()); 473 owner_key_util_->SetPublicKey(GetOwnerPublicKey());
481 474
482 crypto::ScopedPK11Slot user_slot( 475 crypto::ScopedPK11Slot user_slot(
483 crypto::GetPublicSlotForChromeOSUser(user_context_.GetUserIDHash())); 476 crypto::GetPublicSlotForChromeOSUser(user_context_.GetUserIDHash()));
484 CreateOwnerKeyInSlot(user_slot.get()); 477 CreateOwnerKeyInSlot(user_slot.get());
485 478
486 profile_manager_.reset( 479 profile_manager_.reset(
487 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); 480 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
488 ASSERT_TRUE(profile_manager_->SetUp()); 481 ASSERT_TRUE(profile_manager_->SetUp());
489 482
490 ExpectLoginSuccess(user_context_); 483 ExpectLoginSuccess(user_context_);
491 484
492 // Set up state as though a cryptohome mount attempt has occurred 485 // Set up state as though a cryptohome mount attempt has occurred
493 // and succeeded but we are in safe mode and the current user is not owner. 486 // and succeeded but we are in safe mode and the current user is not owner.
494 state_->PresetCryptohomeStatus(true, cryptohome::MOUNT_ERROR_NONE); 487 state_->PresetCryptohomeStatus(true, cryptohome::MOUNT_ERROR_NONE);
495 SetOwnerState(false, false); 488 SetOwnerState(false, false);
496 // Remove the real DeviceSettingsProvider and replace it with a stub. 489 ReplaceProvider(kPolicyMissingMitigationMode);
497 CrosSettingsProvider* device_settings_provider = 490 stub_settings_provider_.SetBoolean(kPolicyMissingMitigationMode, true);
498 CrosSettings::Get()->GetProvider(chromeos::kReportDeviceVersionInfo);
499 EXPECT_TRUE(device_settings_provider != NULL);
500 EXPECT_TRUE(
501 CrosSettings::Get()->RemoveSettingsProvider(device_settings_provider));
502 StubCrosSettingsProvider stub_settings_provider;
503 CrosSettings::Get()->AddSettingsProvider(&stub_settings_provider);
504 CrosSettings::Get()->SetBoolean(kPolicyMissingMitigationMode, true);
505 491
506 // Initialize login state for this test to verify the login state is changed 492 // Initialize login state for this test to verify the login state is changed
507 // to SAFE_MODE. 493 // to SAFE_MODE.
508 LoginState::Initialize(); 494 LoginState::Initialize();
509 495
510 EXPECT_EQ(CryptohomeAuthenticator::CONTINUE, 496 EXPECT_EQ(CryptohomeAuthenticator::CONTINUE,
511 SetAndResolveState(auth_.get(), state_.release())); 497 SetAndResolveState(auth_.get(), state_.release()));
512 EXPECT_TRUE(LoginState::Get()->IsInSafeMode()); 498 EXPECT_TRUE(LoginState::Get()->IsInSafeMode());
513 499
514 // Flush all the pending operations. The operations should induce an owner 500 // Flush all the pending operations. The operations should induce an owner
515 // verification. 501 // verification.
516 device_settings_test_helper_.Flush(); 502 device_settings_test_helper_.Flush();
517 503
518 state_.reset(new TestAttemptState(user_context_, false)); 504 state_.reset(new TestAttemptState(user_context_, false));
519 state_->PresetCryptohomeStatus(true, cryptohome::MOUNT_ERROR_NONE); 505 state_->PresetCryptohomeStatus(true, cryptohome::MOUNT_ERROR_NONE);
520 506
521 // The owner key util should find the owner key, so login should succeed. 507 // The owner key util should find the owner key, so login should succeed.
522 EXPECT_EQ(CryptohomeAuthenticator::OFFLINE_LOGIN, 508 EXPECT_EQ(CryptohomeAuthenticator::OFFLINE_LOGIN,
523 SetAndResolveState(auth_.get(), state_.release())); 509 SetAndResolveState(auth_.get(), state_.release()));
524 EXPECT_TRUE(LoginState::Get()->IsInSafeMode()); 510 EXPECT_TRUE(LoginState::Get()->IsInSafeMode());
525 511
526 // Unset global objects used by this test. 512 // Unset global objects used by this test.
527 fake_cryptohome_client_->set_unmount_result(true); 513 fake_cryptohome_client_->set_unmount_result(true);
528 LoginState::Shutdown(); 514 LoginState::Shutdown();
529 EXPECT_TRUE( 515 RestoreProvider();
530 CrosSettings::Get()->RemoveSettingsProvider(&stub_settings_provider));
531 CrosSettings::Get()->AddSettingsProvider(device_settings_provider);
532 } 516 }
533 517
534 TEST_F(CryptohomeAuthenticatorTest, DriveFailedMount) { 518 TEST_F(CryptohomeAuthenticatorTest, DriveFailedMount) {
535 FailOnLoginSuccess(); 519 FailOnLoginSuccess();
536 ExpectLoginFailure(AuthFailure(AuthFailure::COULD_NOT_MOUNT_CRYPTOHOME)); 520 ExpectLoginFailure(AuthFailure(AuthFailure::COULD_NOT_MOUNT_CRYPTOHOME));
537 521
538 // Set up state as though a cryptohome mount attempt has occurred 522 // Set up state as though a cryptohome mount attempt has occurred
539 // and failed. 523 // and failed.
540 state_->PresetCryptohomeStatus(false, cryptohome::MOUNT_ERROR_NONE); 524 state_->PresetCryptohomeStatus(false, cryptohome::MOUNT_ERROR_NONE);
541 SetAttemptState(auth_.get(), state_.release()); 525 SetAttemptState(auth_.get(), state_.release());
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
790 // salt. 774 // salt.
791 ExpectGetKeyDataExCall( 775 ExpectGetKeyDataExCall(
792 make_scoped_ptr(new int64(Key::KEY_TYPE_SALTED_SHA256)), 776 make_scoped_ptr(new int64(Key::KEY_TYPE_SALTED_SHA256)),
793 scoped_ptr<std::string>()); 777 scoped_ptr<std::string>());
794 778
795 auth_->AuthenticateToLogin(NULL, user_context_); 779 auth_->AuthenticateToLogin(NULL, user_context_);
796 base::RunLoop().Run(); 780 base::RunLoop().Run();
797 } 781 }
798 782
799 } // namespace chromeos 783 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698