| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/prefs/pref_registry_simple.h" | 9 #include "base/prefs/pref_registry_simple.h" |
| 10 #include "base/prefs/testing_pref_service.h" | 10 #include "base/prefs/testing_pref_service.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "chrome/browser/chromeos/attestation/attestation_signed_data.pb.h" | 12 #include "chrome/browser/chromeos/attestation/attestation_signed_data.pb.h" |
| 13 #include "chrome/browser/chromeos/attestation/platform_verification_flow.h" | 13 #include "chrome/browser/chromeos/attestation/platform_verification_flow.h" |
| 14 #include "chrome/browser/chromeos/login/mock_user_manager.h" | 14 #include "chrome/browser/chromeos/login/mock_user_manager.h" |
| 15 #include "chrome/browser/chromeos/settings/cros_settings.h" | 15 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 16 #include "chrome/browser/chromeos/settings/device_settings_service.h" | 16 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
| 17 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" | 17 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" |
| 18 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
| 19 #include "chromeos/attestation/mock_attestation_flow.h" | 19 #include "chromeos/attestation/mock_attestation_flow.h" |
| 20 #include "chromeos/cryptohome/mock_async_method_caller.h" | 20 #include "chromeos/cryptohome/mock_async_method_caller.h" |
| 21 #include "chromeos/dbus/fake_cryptohome_client.h" | 21 #include "chromeos/dbus/fake_cryptohome_client.h" |
| 22 #include "chromeos/settings/cros_settings_names.h" | 22 #include "chromeos/settings/cros_settings_names.h" |
| 23 #include "chromeos/system/mock_statistics_provider.h" | |
| 24 #include "content/public/test/test_browser_thread.h" | 23 #include "content/public/test/test_browser_thread.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 25 |
| 27 using testing::_; | 26 using testing::_; |
| 28 using testing::DoAll; | 27 using testing::DoAll; |
| 29 using testing::Invoke; | 28 using testing::Invoke; |
| 30 using testing::Return; | 29 using testing::Return; |
| 31 using testing::SetArgumentPointee; | 30 using testing::SetArgumentPointee; |
| 32 using testing::StrictMock; | 31 using testing::StrictMock; |
| 33 using testing::WithArgs; | 32 using testing::WithArgs; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 | 116 |
| 118 } // namespace | 117 } // namespace |
| 119 | 118 |
| 120 class PlatformVerificationFlowTest : public ::testing::Test { | 119 class PlatformVerificationFlowTest : public ::testing::Test { |
| 121 public: | 120 public: |
| 122 PlatformVerificationFlowTest() | 121 PlatformVerificationFlowTest() |
| 123 : message_loop_(base::MessageLoop::TYPE_UI), | 122 : message_loop_(base::MessageLoop::TYPE_UI), |
| 124 ui_thread_(content::BrowserThread::UI, &message_loop_), | 123 ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 125 certificate_success_(true), | 124 certificate_success_(true), |
| 126 sign_challenge_success_(true), | 125 sign_challenge_success_(true), |
| 127 result_(PlatformVerificationFlow::INTERNAL_ERROR), | 126 result_(PlatformVerificationFlow::INTERNAL_ERROR) {} |
| 128 check_state_result_(false) {} | |
| 129 | 127 |
| 130 void SetUp() { | 128 void SetUp() { |
| 131 // Configure a user for the mock user manager. | 129 // Configure a user for the mock user manager. |
| 132 mock_user_manager_.SetActiveUser(kTestEmail); | 130 mock_user_manager_.SetActiveUser(kTestEmail); |
| 133 | 131 |
| 134 // Configure the statistics provider to report verified mode. | |
| 135 EXPECT_CALL(mock_statistics_provider_, | |
| 136 GetMachineStatistic(system::kDevSwitchBootMode, _)) | |
| 137 .WillRepeatedly(DoAll(SetArgumentPointee<1>(std::string("0")), | |
| 138 Return(true))); | |
| 139 | |
| 140 // Create a verifier for tests to call. | 132 // Create a verifier for tests to call. |
| 141 verifier_.reset(new PlatformVerificationFlow(&mock_attestation_flow_, | 133 verifier_.reset(new PlatformVerificationFlow(&mock_attestation_flow_, |
| 142 &mock_async_caller_, | 134 &mock_async_caller_, |
| 143 &fake_cryptohome_client_, | 135 &fake_cryptohome_client_, |
| 144 &mock_user_manager_, | 136 &mock_user_manager_, |
| 145 &mock_statistics_provider_, | |
| 146 &fake_delegate_)); | 137 &fake_delegate_)); |
| 147 | 138 |
| 148 // Create callbacks for tests to use with verifier_. | 139 // Create callbacks for tests to use with verifier_. |
| 149 callback_ = base::Bind(&PlatformVerificationFlowTest::FakeChallengeCallback, | 140 callback_ = base::Bind(&PlatformVerificationFlowTest::FakeChallengeCallback, |
| 150 base::Unretained(this)); | 141 base::Unretained(this)); |
| 151 check_state_callback_ = base::Bind( | |
| 152 &PlatformVerificationFlowTest::FakeCheckStateCallback, | |
| 153 base::Unretained(this)); | |
| 154 | 142 |
| 155 // Configure the test pref service. | 143 // Configure the test pref service. |
| 156 pref_service_.registry()->RegisterBooleanPref(prefs::kEnableDRM, true); | 144 pref_service_.registry()->RegisterBooleanPref(prefs::kEnableDRM, true); |
| 157 pref_service_.registry()->RegisterBooleanPref(prefs::kRAConsentFirstTime, | 145 pref_service_.registry()->RegisterBooleanPref(prefs::kRAConsentFirstTime, |
| 158 true); | 146 true); |
| 159 pref_service_.registry()->RegisterBooleanPref(prefs::kRAConsentAlways, | 147 pref_service_.registry()->RegisterBooleanPref(prefs::kRAConsentAlways, |
| 160 false); | 148 false); |
| 161 pref_service_.registry()->RegisterDictionaryPref(prefs::kRAConsentDomains); | 149 pref_service_.registry()->RegisterDictionaryPref(prefs::kRAConsentDomains); |
| 162 verifier_->set_testing_prefs(&pref_service_); | 150 verifier_->set_testing_prefs(&pref_service_); |
| 163 | 151 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 void FakeChallengeCallback(PlatformVerificationFlow::Result result, | 211 void FakeChallengeCallback(PlatformVerificationFlow::Result result, |
| 224 const std::string& salt, | 212 const std::string& salt, |
| 225 const std::string& signature, | 213 const std::string& signature, |
| 226 const std::string& certificate) { | 214 const std::string& certificate) { |
| 227 result_ = result; | 215 result_ = result; |
| 228 challenge_salt_ = salt; | 216 challenge_salt_ = salt; |
| 229 challenge_signature_ = signature; | 217 challenge_signature_ = signature; |
| 230 certificate_ = certificate; | 218 certificate_ = certificate; |
| 231 } | 219 } |
| 232 | 220 |
| 233 void FakeCheckStateCallback(bool result) { | |
| 234 check_state_result_ = result; | |
| 235 } | |
| 236 | |
| 237 std::string CreateFakeResponseProto() { | 221 std::string CreateFakeResponseProto() { |
| 238 SignedData pb; | 222 SignedData pb; |
| 239 pb.set_data(kTestSignedData); | 223 pb.set_data(kTestSignedData); |
| 240 pb.set_signature(kTestSignature); | 224 pb.set_signature(kTestSignature); |
| 241 std::string serial; | 225 std::string serial; |
| 242 CHECK(pb.SerializeToString(&serial)); | 226 CHECK(pb.SerializeToString(&serial)); |
| 243 return serial; | 227 return serial; |
| 244 } | 228 } |
| 245 | 229 |
| 246 protected: | 230 protected: |
| 247 base::MessageLoop message_loop_; | 231 base::MessageLoop message_loop_; |
| 248 content::TestBrowserThread ui_thread_; | 232 content::TestBrowserThread ui_thread_; |
| 249 StrictMock<MockAttestationFlow> mock_attestation_flow_; | 233 StrictMock<MockAttestationFlow> mock_attestation_flow_; |
| 250 cryptohome::MockAsyncMethodCaller mock_async_caller_; | 234 cryptohome::MockAsyncMethodCaller mock_async_caller_; |
| 251 CustomFakeCryptohomeClient fake_cryptohome_client_; | 235 CustomFakeCryptohomeClient fake_cryptohome_client_; |
| 252 MockUserManager mock_user_manager_; | 236 MockUserManager mock_user_manager_; |
| 253 system::MockStatisticsProvider mock_statistics_provider_; | |
| 254 FakeDelegate fake_delegate_; | 237 FakeDelegate fake_delegate_; |
| 255 TestingPrefServiceSimple pref_service_; | 238 TestingPrefServiceSimple pref_service_; |
| 256 CrosSettingsProvider* device_settings_provider_; | 239 CrosSettingsProvider* device_settings_provider_; |
| 257 StubCrosSettingsProvider stub_settings_provider_; | 240 StubCrosSettingsProvider stub_settings_provider_; |
| 258 ScopedTestDeviceSettingsService test_device_settings_service_; | 241 ScopedTestDeviceSettingsService test_device_settings_service_; |
| 259 ScopedTestCrosSettings test_cros_settings_; | 242 ScopedTestCrosSettings test_cros_settings_; |
| 260 scoped_ptr<PlatformVerificationFlow> verifier_; | 243 scoped_ptr<PlatformVerificationFlow> verifier_; |
| 261 | 244 |
| 262 // Controls result of FakeGetCertificate. | 245 // Controls result of FakeGetCertificate. |
| 263 bool certificate_success_; | 246 bool certificate_success_; |
| 264 | 247 |
| 265 // Controls result of FakeSignChallenge. | 248 // Controls result of FakeSignChallenge. |
| 266 bool sign_challenge_success_; | 249 bool sign_challenge_success_; |
| 267 | 250 |
| 268 // Callback functions and data. | 251 // Callback functions and data. |
| 269 PlatformVerificationFlow::ChallengeCallback callback_; | 252 PlatformVerificationFlow::ChallengeCallback callback_; |
| 270 PlatformVerificationFlow::Result result_; | 253 PlatformVerificationFlow::Result result_; |
| 271 std::string challenge_salt_; | 254 std::string challenge_salt_; |
| 272 std::string challenge_signature_; | 255 std::string challenge_signature_; |
| 273 std::string certificate_; | 256 std::string certificate_; |
| 274 base::Callback<void(bool result)> check_state_callback_; | |
| 275 bool check_state_result_; | |
| 276 }; | 257 }; |
| 277 | 258 |
| 278 TEST_F(PlatformVerificationFlowTest, SuccessNoConsent) { | 259 TEST_F(PlatformVerificationFlowTest, SuccessNoConsent) { |
| 279 // Make sure the call will fail if consent is requested. | 260 // Make sure the call will fail if consent is requested. |
| 280 fake_delegate_.set_response(PlatformVerificationFlow::CONSENT_RESPONSE_DENY); | 261 fake_delegate_.set_response(PlatformVerificationFlow::CONSENT_RESPONSE_DENY); |
| 281 ExpectAttestationFlow(); | 262 ExpectAttestationFlow(); |
| 282 verifier_->ChallengePlatformKey(NULL, kTestID, kTestChallenge, callback_); | 263 verifier_->ChallengePlatformKey(NULL, kTestID, kTestChallenge, callback_); |
| 283 base::RunLoop().RunUntilIdle(); | 264 base::RunLoop().RunUntilIdle(); |
| 284 EXPECT_EQ(PlatformVerificationFlow::SUCCESS, result_); | 265 EXPECT_EQ(PlatformVerificationFlow::SUCCESS, result_); |
| 285 EXPECT_EQ(kTestSignedData, challenge_salt_); | 266 EXPECT_EQ(kTestSignedData, challenge_salt_); |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 | 370 |
| 390 TEST_F(PlatformVerificationFlowTest, ConsentNoResponse) { | 371 TEST_F(PlatformVerificationFlowTest, ConsentNoResponse) { |
| 391 fake_delegate_.set_response(PlatformVerificationFlow::CONSENT_RESPONSE_NONE); | 372 fake_delegate_.set_response(PlatformVerificationFlow::CONSENT_RESPONSE_NONE); |
| 392 pref_service_.SetUserPref(prefs::kRAConsentAlways, | 373 pref_service_.SetUserPref(prefs::kRAConsentAlways, |
| 393 new base::FundamentalValue(true)); | 374 new base::FundamentalValue(true)); |
| 394 verifier_->ChallengePlatformKey(NULL, kTestID, kTestChallenge, callback_); | 375 verifier_->ChallengePlatformKey(NULL, kTestID, kTestChallenge, callback_); |
| 395 base::RunLoop().RunUntilIdle(); | 376 base::RunLoop().RunUntilIdle(); |
| 396 EXPECT_EQ(PlatformVerificationFlow::USER_REJECTED, result_); | 377 EXPECT_EQ(PlatformVerificationFlow::USER_REJECTED, result_); |
| 397 } | 378 } |
| 398 | 379 |
| 399 TEST_F(PlatformVerificationFlowTest, FastCheck) { | |
| 400 verifier_->CheckPlatformState(check_state_callback_); | |
| 401 base::RunLoop().RunUntilIdle(); | |
| 402 EXPECT_TRUE(check_state_result_); | |
| 403 } | |
| 404 | |
| 405 TEST_F(PlatformVerificationFlowTest, FastCheckNoStat) { | |
| 406 // Configure the stats provider to fail. | |
| 407 EXPECT_CALL(mock_statistics_provider_, | |
| 408 GetMachineStatistic(system::kDevSwitchBootMode, _)) | |
| 409 .WillRepeatedly(Return(false)); | |
| 410 | |
| 411 verifier_->CheckPlatformState(check_state_callback_); | |
| 412 base::RunLoop().RunUntilIdle(); | |
| 413 EXPECT_FALSE(check_state_result_); | |
| 414 } | |
| 415 | |
| 416 TEST_F(PlatformVerificationFlowTest, FastCheckStatDevMode) { | |
| 417 // Configure the stats provider to fail. | |
| 418 EXPECT_CALL(mock_statistics_provider_, | |
| 419 GetMachineStatistic(system::kDevSwitchBootMode, _)) | |
| 420 .WillRepeatedly(DoAll(SetArgumentPointee<1>(std::string("1")), | |
| 421 Return(true))); | |
| 422 | |
| 423 verifier_->CheckPlatformState(check_state_callback_); | |
| 424 base::RunLoop().RunUntilIdle(); | |
| 425 EXPECT_FALSE(check_state_result_); | |
| 426 } | |
| 427 | |
| 428 TEST_F(PlatformVerificationFlowTest, FastCheckStatInvalidMode) { | |
| 429 // Configure the stats provider to fail. | |
| 430 EXPECT_CALL(mock_statistics_provider_, | |
| 431 GetMachineStatistic(system::kDevSwitchBootMode, _)) | |
| 432 .WillRepeatedly(DoAll(SetArgumentPointee<1>(std::string("INVALID")), | |
| 433 Return(true))); | |
| 434 | |
| 435 verifier_->CheckPlatformState(check_state_callback_); | |
| 436 base::RunLoop().RunUntilIdle(); | |
| 437 EXPECT_FALSE(check_state_result_); | |
| 438 } | |
| 439 | |
| 440 TEST_F(PlatformVerificationFlowTest, FastCheckNoAttestation) { | |
| 441 fake_cryptohome_client_.set_attestation_prepared(false); | |
| 442 verifier_->CheckPlatformState(check_state_callback_); | |
| 443 base::RunLoop().RunUntilIdle(); | |
| 444 EXPECT_FALSE(check_state_result_); | |
| 445 } | |
| 446 | |
| 447 TEST_F(PlatformVerificationFlowTest, FastCheckDBusFailure) { | |
| 448 fake_cryptohome_client_.set_call_status(DBUS_METHOD_CALL_FAILURE); | |
| 449 verifier_->CheckPlatformState(check_state_callback_); | |
| 450 base::RunLoop().RunUntilIdle(); | |
| 451 EXPECT_FALSE(check_state_result_); | |
| 452 } | |
| 453 | |
| 454 } // namespace attestation | 380 } // namespace attestation |
| 455 } // namespace chromeos | 381 } // namespace chromeos |
| OLD | NEW |