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