| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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" | |
| 9 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 10 #include "chrome/browser/chromeos/attestation/attestation_key_payload.pb.h" | 9 #include "chrome/browser/chromeos/attestation/attestation_key_payload.pb.h" |
| 11 #include "chrome/browser/chromeos/attestation/attestation_policy_observer.h" | 10 #include "chrome/browser/chromeos/attestation/attestation_policy_observer.h" |
| 12 #include "chrome/browser/chromeos/attestation/fake_certificate.h" | 11 #include "chrome/browser/chromeos/attestation/fake_certificate.h" |
| 13 #include "chrome/browser/chromeos/settings/cros_settings.h" | 12 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h" |
| 14 #include "chrome/browser/chromeos/settings/device_settings_service.h" | |
| 15 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" | |
| 16 #include "chromeos/attestation/mock_attestation_flow.h" | 13 #include "chromeos/attestation/mock_attestation_flow.h" |
| 17 #include "chromeos/dbus/mock_cryptohome_client.h" | 14 #include "chromeos/dbus/mock_cryptohome_client.h" |
| 18 #include "chromeos/settings/cros_settings_names.h" | 15 #include "chromeos/settings/cros_settings_names.h" |
| 19 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h" | 16 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h" |
| 20 #include "content/public/test/test_browser_thread.h" | |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 18 |
| 23 using testing::_; | 19 using testing::_; |
| 24 using testing::Invoke; | 20 using testing::Invoke; |
| 25 using testing::StrictMock; | 21 using testing::StrictMock; |
| 26 using testing::WithArgs; | 22 using testing::WithArgs; |
| 27 | 23 |
| 28 namespace chromeos { | 24 namespace chromeos { |
| 29 namespace attestation { | 25 namespace attestation { |
| 30 | 26 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 FROM_HERE, | 65 FROM_HERE, |
| 70 base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true, data_)); | 66 base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true, data_)); |
| 71 } | 67 } |
| 72 | 68 |
| 73 private: | 69 private: |
| 74 std::string data_; | 70 std::string data_; |
| 75 }; | 71 }; |
| 76 | 72 |
| 77 } // namespace | 73 } // namespace |
| 78 | 74 |
| 79 class AttestationPolicyObserverTest : public ::testing::Test { | 75 class AttestationPolicyObserverTest : public chromeos::OwnerSettingsTestBase { |
| 80 public: | 76 public: |
| 81 AttestationPolicyObserverTest() | 77 AttestationPolicyObserverTest() { |
| 82 : ui_thread_(content::BrowserThread::UI, &message_loop_) { | |
| 83 // Remove the real DeviceSettingsProvider and replace it with a stub. | |
| 84 CrosSettings* cros_settings = CrosSettings::Get(); | |
| 85 device_settings_provider_ = | |
| 86 cros_settings->GetProvider(kDeviceAttestationEnabled); | |
| 87 cros_settings->RemoveSettingsProvider(device_settings_provider_); | |
| 88 cros_settings->AddSettingsProvider(&stub_settings_provider_); | |
| 89 cros_settings->SetBoolean(kDeviceAttestationEnabled, true); | |
| 90 policy_client_.SetDMToken("fake_dm_token"); | 78 policy_client_.SetDMToken("fake_dm_token"); |
| 91 } | 79 } |
| 92 | 80 |
| 93 virtual ~AttestationPolicyObserverTest() { | |
| 94 // Restore the real DeviceSettingsProvider. | |
| 95 CrosSettings* cros_settings = CrosSettings::Get(); | |
| 96 cros_settings->RemoveSettingsProvider(&stub_settings_provider_); | |
| 97 cros_settings->AddSettingsProvider(device_settings_provider_); | |
| 98 } | |
| 99 | |
| 100 protected: | 81 protected: |
| 101 enum MockOptions { | 82 enum MockOptions { |
| 102 MOCK_KEY_EXISTS = 1, // Configure so a certified key exists. | 83 MOCK_KEY_EXISTS = 1, // Configure so a certified key exists. |
| 103 MOCK_KEY_UPLOADED = (1 << 1), // Configure so an upload has occurred. | 84 MOCK_KEY_UPLOADED = (1 << 1), // Configure so an upload has occurred. |
| 104 MOCK_NEW_KEY = (1 << 2) // Configure expecting new key generation. | 85 MOCK_NEW_KEY = (1 << 2) // Configure expecting new key generation. |
| 105 }; | 86 }; |
| 106 | 87 |
| 88 void SetUp() override { |
| 89 chromeos::OwnerSettingsTestBase::SetUp(); |
| 90 ReplaceProvider(kDeviceAttestationEnabled); |
| 91 owner_settings_service_->SetBoolean(kDeviceAttestationEnabled, true); |
| 92 } |
| 93 |
| 107 // Configures mock expectations according to |mock_options|. If options | 94 // Configures mock expectations according to |mock_options|. If options |
| 108 // require that a certificate exists, |certificate| will be used. | 95 // require that a certificate exists, |certificate| will be used. |
| 109 void SetupMocks(int mock_options, const std::string& certificate) { | 96 void SetupMocks(int mock_options, const std::string& certificate) { |
| 110 bool key_exists = (mock_options & MOCK_KEY_EXISTS); | 97 bool key_exists = (mock_options & MOCK_KEY_EXISTS); |
| 111 // Setup expected key / cert queries. | 98 // Setup expected key / cert queries. |
| 112 if (key_exists) { | 99 if (key_exists) { |
| 113 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _, _)) | 100 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _, _)) |
| 114 .WillRepeatedly(WithArgs<3>(Invoke(DBusCallbackTrue))); | 101 .WillRepeatedly(WithArgs<3>(Invoke(DBusCallbackTrue))); |
| 115 EXPECT_CALL(cryptohome_client_, TpmAttestationGetCertificate(_, _, _, _)) | 102 EXPECT_CALL(cryptohome_client_, TpmAttestationGetCertificate(_, _, _, _)) |
| 116 .WillRepeatedly(WithArgs<3>(Invoke(FakeDBusData(certificate)))); | 103 .WillRepeatedly(WithArgs<3>(Invoke(FakeDBusData(certificate)))); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 } | 146 } |
| 160 | 147 |
| 161 std::string CreatePayload() { | 148 std::string CreatePayload() { |
| 162 AttestationKeyPayload proto; | 149 AttestationKeyPayload proto; |
| 163 proto.set_is_certificate_uploaded(true); | 150 proto.set_is_certificate_uploaded(true); |
| 164 std::string serialized; | 151 std::string serialized; |
| 165 proto.SerializeToString(&serialized); | 152 proto.SerializeToString(&serialized); |
| 166 return serialized; | 153 return serialized; |
| 167 } | 154 } |
| 168 | 155 |
| 169 base::MessageLoopForUI message_loop_; | |
| 170 content::TestBrowserThread ui_thread_; | |
| 171 ScopedTestDeviceSettingsService test_device_settings_service_; | |
| 172 ScopedTestCrosSettings test_cros_settings_; | |
| 173 CrosSettingsProvider* device_settings_provider_; | |
| 174 StubCrosSettingsProvider stub_settings_provider_; | |
| 175 StrictMock<MockCryptohomeClient> cryptohome_client_; | 156 StrictMock<MockCryptohomeClient> cryptohome_client_; |
| 176 StrictMock<MockAttestationFlow> attestation_flow_; | 157 StrictMock<MockAttestationFlow> attestation_flow_; |
| 177 StrictMock<policy::MockCloudPolicyClient> policy_client_; | 158 StrictMock<policy::MockCloudPolicyClient> policy_client_; |
| 178 }; | 159 }; |
| 179 | 160 |
| 180 TEST_F(AttestationPolicyObserverTest, FeatureDisabled) { | 161 TEST_F(AttestationPolicyObserverTest, FeatureDisabled) { |
| 181 CrosSettings* cros_settings = CrosSettings::Get(); | 162 owner_settings_service_->SetBoolean(kDeviceAttestationEnabled, false); |
| 182 cros_settings->SetBoolean(kDeviceAttestationEnabled, false); | |
| 183 Run(); | 163 Run(); |
| 184 } | 164 } |
| 185 | 165 |
| 186 TEST_F(AttestationPolicyObserverTest, UnregisteredPolicyClient) { | 166 TEST_F(AttestationPolicyObserverTest, UnregisteredPolicyClient) { |
| 187 policy_client_.SetDMToken(""); | 167 policy_client_.SetDMToken(""); |
| 188 Run(); | 168 Run(); |
| 189 } | 169 } |
| 190 | 170 |
| 191 TEST_F(AttestationPolicyObserverTest, NewCertificate) { | 171 TEST_F(AttestationPolicyObserverTest, NewCertificate) { |
| 192 SetupMocks(MOCK_NEW_KEY, ""); | 172 SetupMocks(MOCK_NEW_KEY, ""); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 SetupMocks(MOCK_NEW_KEY, ""); | 214 SetupMocks(MOCK_NEW_KEY, ""); |
| 235 // Simulate a DBus failure. | 215 // Simulate a DBus failure. |
| 236 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _, _)) | 216 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _, _)) |
| 237 .WillOnce(WithArgs<3>(Invoke(DBusCallbackError))) | 217 .WillOnce(WithArgs<3>(Invoke(DBusCallbackError))) |
| 238 .WillRepeatedly(WithArgs<3>(Invoke(DBusCallbackFalse))); | 218 .WillRepeatedly(WithArgs<3>(Invoke(DBusCallbackFalse))); |
| 239 Run(); | 219 Run(); |
| 240 } | 220 } |
| 241 | 221 |
| 242 } // namespace attestation | 222 } // namespace attestation |
| 243 } // namespace chromeos | 223 } // namespace chromeos |
| OLD | NEW |