OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/bind.h" |
| 6 #include "base/message_loop.h" |
| 7 #include "base/run_loop.h" |
| 8 #include "chrome/browser/chromeos/attestation/attestation_policy_observer.h" |
| 9 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 10 #include "chrome/browser/chromeos/settings/cros_settings_names.h" |
| 11 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" |
| 12 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" |
| 13 #include "chromeos/attestation/mock_attestation_flow.h" |
| 14 #include "chromeos/dbus/mock_cryptohome_client.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 using testing::_; |
| 18 using testing::Invoke; |
| 19 using testing::StrictMock; |
| 20 using testing::WithArgs; |
| 21 |
| 22 namespace chromeos { |
| 23 namespace attestation { |
| 24 |
| 25 namespace { |
| 26 |
| 27 void DBusCallbackFalse(const BoolDBusMethodCallback& callback) { |
| 28 MessageLoop::current()->PostTask( |
| 29 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, false)); |
| 30 } |
| 31 |
| 32 void DBusCallbackTrue(const BoolDBusMethodCallback& callback) { |
| 33 MessageLoop::current()->PostTask( |
| 34 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true)); |
| 35 } |
| 36 |
| 37 void DBusDataCallback(const CryptohomeClient::DataMethodCallback& callback) { |
| 38 MessageLoop::current()->PostTask( |
| 39 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true, "fake")); |
| 40 } |
| 41 |
| 42 void CertCallbackSuccess(const AttestationFlow::CertificateCallback& callback) { |
| 43 MessageLoop::current()->PostTask( |
| 44 FROM_HERE, base::Bind(callback, true, "fake_cert")); |
| 45 } |
| 46 |
| 47 } // namespace |
| 48 |
| 49 class AttestationPolicyObserverTest : public ::testing::Test { |
| 50 public: |
| 51 AttestationPolicyObserverTest() { |
| 52 // Remove the real DeviceSettingsProvider and replace it with a stub. |
| 53 CrosSettings* cros_settings = CrosSettings::Get(); |
| 54 device_settings_provider_ = |
| 55 cros_settings->GetProvider(kDeviceAttestationEnabled); |
| 56 cros_settings->RemoveSettingsProvider(device_settings_provider_); |
| 57 cros_settings->AddSettingsProvider(&stub_settings_provider_); |
| 58 cros_settings->SetBoolean(kDeviceAttestationEnabled, true); |
| 59 policy_client_.SetDMToken("fake_dm_token"); |
| 60 } |
| 61 |
| 62 virtual ~AttestationPolicyObserverTest() { |
| 63 // Restore the real DeviceSettingsProvider. |
| 64 CrosSettings* cros_settings = CrosSettings::Get(); |
| 65 cros_settings->RemoveSettingsProvider(&stub_settings_provider_); |
| 66 cros_settings->AddSettingsProvider(device_settings_provider_); |
| 67 } |
| 68 |
| 69 protected: |
| 70 void Run() { |
| 71 AttestationPolicyObserver observer(&policy_client_, |
| 72 &cryptohome_client_, |
| 73 &attestation_flow_); |
| 74 base::RunLoop().RunUntilIdle(); |
| 75 } |
| 76 |
| 77 MessageLoop message_loop_; |
| 78 CrosSettingsProvider* device_settings_provider_; |
| 79 StubCrosSettingsProvider stub_settings_provider_; |
| 80 StrictMock<MockCryptohomeClient> cryptohome_client_; |
| 81 StrictMock<MockAttestationFlow> attestation_flow_; |
| 82 StrictMock<policy::MockCloudPolicyClient> policy_client_; |
| 83 }; |
| 84 |
| 85 TEST_F(AttestationPolicyObserverTest, FeatureDisabled) { |
| 86 CrosSettings* cros_settings = CrosSettings::Get(); |
| 87 cros_settings->SetBoolean(kDeviceAttestationEnabled, false); |
| 88 Run(); |
| 89 } |
| 90 |
| 91 TEST_F(AttestationPolicyObserverTest, UnregisteredPolicyClient) { |
| 92 policy_client_.SetDMToken(""); |
| 93 Run(); |
| 94 } |
| 95 |
| 96 TEST_F(AttestationPolicyObserverTest, NewCertificate) { |
| 97 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _)) |
| 98 .WillOnce(WithArgs<2>(Invoke(DBusCallbackFalse))); |
| 99 EXPECT_CALL(attestation_flow_, GetCertificate(_, _)) |
| 100 .WillOnce(WithArgs<1>(Invoke(CertCallbackSuccess))); |
| 101 Run(); |
| 102 } |
| 103 |
| 104 TEST_F(AttestationPolicyObserverTest, KeyExists) { |
| 105 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _)) |
| 106 .WillOnce(WithArgs<2>(Invoke(DBusCallbackTrue))); |
| 107 EXPECT_CALL(cryptohome_client_, TpmAttestationGetCertificate(_, _, _)) |
| 108 .WillOnce(WithArgs<2>(Invoke(DBusDataCallback))); |
| 109 Run(); |
| 110 } |
| 111 |
| 112 } // namespace attestation |
| 113 } // namespace chromeos |
OLD | NEW |