Chromium Code Reviews| 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> | |
| 6 | |
| 5 #include "base/bind.h" | 7 #include "base/bind.h" |
| 6 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 7 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "chrome/browser/chromeos/attestation/attestation_key_payload.pb.h" | |
| 8 #include "chrome/browser/chromeos/attestation/attestation_policy_observer.h" | 11 #include "chrome/browser/chromeos/attestation/attestation_policy_observer.h" |
| 9 #include "chrome/browser/chromeos/settings/cros_settings.h" | 12 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 10 #include "chrome/browser/chromeos/settings/cros_settings_names.h" | 13 #include "chrome/browser/chromeos/settings/cros_settings_names.h" |
| 11 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" | 14 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" |
| 12 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" | 15 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" |
| 13 #include "chromeos/attestation/mock_attestation_flow.h" | 16 #include "chromeos/attestation/mock_attestation_flow.h" |
| 14 #include "chromeos/dbus/mock_cryptohome_client.h" | 17 #include "chromeos/dbus/mock_cryptohome_client.h" |
| 15 #include "content/public/test/test_browser_thread.h" | 18 #include "content/public/test/test_browser_thread.h" |
| 19 #include "crypto/rsa_private_key.h" | |
| 20 #include "net/cert/x509_certificate.h" | |
| 21 #include "net/cert/x509_util_nss.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 23 |
| 24 using std::string; | |
|
Mattias Nissler (ping if slow)
2013/04/22 10:25:42
We usually don't do this given the little benefit
dkrahn
2013/04/22 22:33:31
Done.
| |
| 18 using testing::_; | 25 using testing::_; |
| 19 using testing::Invoke; | 26 using testing::Invoke; |
| 20 using testing::StrictMock; | 27 using testing::StrictMock; |
| 21 using testing::WithArgs; | 28 using testing::WithArgs; |
| 22 | 29 |
| 23 namespace chromeos { | 30 namespace chromeos { |
| 24 namespace attestation { | 31 namespace attestation { |
| 25 | 32 |
| 26 namespace { | 33 namespace { |
| 27 | 34 |
| 28 void DBusCallbackFalse(const BoolDBusMethodCallback& callback) { | 35 void DBusCallbackFalse(const BoolDBusMethodCallback& callback) { |
| 29 MessageLoop::current()->PostTask( | 36 MessageLoop::current()->PostTask( |
| 30 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, false)); | 37 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, false)); |
| 31 } | 38 } |
| 32 | 39 |
| 33 void DBusCallbackTrue(const BoolDBusMethodCallback& callback) { | 40 void DBusCallbackTrue(const BoolDBusMethodCallback& callback) { |
| 34 MessageLoop::current()->PostTask( | 41 MessageLoop::current()->PostTask( |
| 35 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true)); | 42 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true)); |
| 36 } | 43 } |
| 37 | 44 |
| 38 void DBusDataCallback(const CryptohomeClient::DataMethodCallback& callback) { | |
| 39 MessageLoop::current()->PostTask( | |
| 40 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true, "fake")); | |
| 41 } | |
| 42 | |
| 43 void CertCallbackSuccess(const AttestationFlow::CertificateCallback& callback) { | 45 void CertCallbackSuccess(const AttestationFlow::CertificateCallback& callback) { |
| 44 MessageLoop::current()->PostTask( | 46 MessageLoop::current()->PostTask( |
| 45 FROM_HERE, base::Bind(callback, true, "fake_cert")); | 47 FROM_HERE, base::Bind(callback, true, "fake_cert")); |
| 46 } | 48 } |
| 47 | 49 |
| 50 void StatusCallbackSuccess( | |
| 51 const policy::CloudPolicyClient::StatusCallback& callback) { | |
| 52 MessageLoop::current()->PostTask( | |
| 53 FROM_HERE, base::Bind(callback, true)); | |
| 54 } | |
| 55 | |
| 56 class FakeDBusData { | |
| 57 public: | |
| 58 explicit FakeDBusData(const string& data) : data_(data) {} | |
| 59 | |
| 60 void operator() (const CryptohomeClient::DataMethodCallback& callback) { | |
| 61 MessageLoop::current()->PostTask( | |
| 62 FROM_HERE, | |
| 63 base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true, data_)); | |
| 64 } | |
| 65 | |
| 66 private: | |
| 67 string data_; | |
| 68 }; | |
| 69 | |
| 70 // A cached private key so we only need to generate once. | |
| 71 scoped_ptr<crypto::RSAPrivateKey> g_private_key; | |
|
Mattias Nissler (ping if slow)
2013/04/22 10:25:42
No non-POD globals please.
dkrahn
2013/04/22 22:33:31
Done.
| |
| 72 | |
| 48 } // namespace | 73 } // namespace |
| 49 | 74 |
| 50 class AttestationPolicyObserverTest : public ::testing::Test { | 75 class AttestationPolicyObserverTest : public ::testing::Test { |
| 51 public: | 76 public: |
| 52 AttestationPolicyObserverTest() | 77 AttestationPolicyObserverTest() |
| 53 : message_loop_(MessageLoop::TYPE_UI), | 78 : message_loop_(MessageLoop::TYPE_UI), |
| 54 ui_thread_(content::BrowserThread::UI, &message_loop_) { | 79 ui_thread_(content::BrowserThread::UI, &message_loop_) { |
| 55 // Remove the real DeviceSettingsProvider and replace it with a stub. | 80 // Remove the real DeviceSettingsProvider and replace it with a stub. |
| 56 CrosSettings* cros_settings = CrosSettings::Get(); | 81 CrosSettings* cros_settings = CrosSettings::Get(); |
| 57 device_settings_provider_ = | 82 device_settings_provider_ = |
| 58 cros_settings->GetProvider(kDeviceAttestationEnabled); | 83 cros_settings->GetProvider(kDeviceAttestationEnabled); |
| 59 cros_settings->RemoveSettingsProvider(device_settings_provider_); | 84 cros_settings->RemoveSettingsProvider(device_settings_provider_); |
| 60 cros_settings->AddSettingsProvider(&stub_settings_provider_); | 85 cros_settings->AddSettingsProvider(&stub_settings_provider_); |
| 61 cros_settings->SetBoolean(kDeviceAttestationEnabled, true); | 86 cros_settings->SetBoolean(kDeviceAttestationEnabled, true); |
| 62 policy_client_.SetDMToken("fake_dm_token"); | 87 policy_client_.SetDMToken("fake_dm_token"); |
| 63 } | 88 } |
| 64 | 89 |
| 65 virtual ~AttestationPolicyObserverTest() { | 90 virtual ~AttestationPolicyObserverTest() { |
| 66 // Restore the real DeviceSettingsProvider. | 91 // Restore the real DeviceSettingsProvider. |
| 67 CrosSettings* cros_settings = CrosSettings::Get(); | 92 CrosSettings* cros_settings = CrosSettings::Get(); |
| 68 cros_settings->RemoveSettingsProvider(&stub_settings_provider_); | 93 cros_settings->RemoveSettingsProvider(&stub_settings_provider_); |
| 69 cros_settings->AddSettingsProvider(device_settings_provider_); | 94 cros_settings->AddSettingsProvider(device_settings_provider_); |
| 70 } | 95 } |
| 71 | 96 |
| 72 protected: | 97 protected: |
| 98 enum CertExpiryOptions { | |
| 99 CERT_VALID, | |
| 100 CERT_EXPIRING_SOON, | |
| 101 CERT_EXPIRED | |
| 102 }; | |
| 103 | |
| 73 void Run() { | 104 void Run() { |
| 74 AttestationPolicyObserver observer(&policy_client_, | 105 AttestationPolicyObserver observer(&policy_client_, |
| 75 &cryptohome_client_, | 106 &cryptohome_client_, |
| 76 &attestation_flow_); | 107 &attestation_flow_); |
| 77 base::RunLoop().RunUntilIdle(); | 108 base::RunLoop().RunUntilIdle(); |
| 78 } | 109 } |
| 79 | 110 |
| 111 string CreatePayload() { | |
| 112 AttestationKeyPayload proto; | |
| 113 proto.set_is_certificate_uploaded(true); | |
| 114 string serialized; | |
| 115 proto.SerializeToString(&serialized); | |
| 116 return serialized; | |
| 117 } | |
| 118 | |
| 119 bool CreateCertificate(CertExpiryOptions options, string* certificate) { | |
| 120 base::Time valid_start = base::Time::Now() - base::TimeDelta::FromDays(90); | |
| 121 base::Time valid_expiry; | |
| 122 switch (options) { | |
| 123 case CERT_VALID: | |
| 124 valid_expiry = base::Time::Now() + base::TimeDelta::FromDays(90); | |
| 125 break; | |
| 126 case CERT_EXPIRING_SOON: | |
| 127 valid_expiry = base::Time::Now() + base::TimeDelta::FromDays(20); | |
| 128 break; | |
| 129 case CERT_EXPIRED: | |
| 130 valid_expiry = base::Time::Now() - base::TimeDelta::FromDays(20); | |
| 131 break; | |
| 132 default: | |
| 133 NOTREACHED(); | |
| 134 } | |
| 135 if (!g_private_key.get()) | |
| 136 g_private_key.reset(crypto::RSAPrivateKey::Create(2048)); | |
|
Mattias Nissler (ping if slow)
2013/04/22 10:25:42
How costly is this? policy_builder.cc has pre-crea
dkrahn
2013/04/22 22:33:31
Good idea -- used a key from policy_builder.cc.
| |
| 137 if (!g_private_key.get()) | |
| 138 return false; | |
| 139 net::X509Certificate::OSCertHandle handle = | |
| 140 net::x509_util::CreateSelfSignedCert(g_private_key->public_key(), | |
| 141 g_private_key->key(), | |
| 142 "CN=subject", | |
| 143 12345, | |
| 144 valid_start, | |
| 145 valid_expiry); | |
|
Mattias Nissler (ping if slow)
2013/04/22 10:25:42
Do we have any suitable certs in chrome/test/data
dkrahn
2013/04/22 22:33:31
The reason I'm generating dynamically is to fiddle
Mattias Nissler (ping if slow)
2013/04/23 10:53:01
I see.
| |
| 146 | |
| 147 if (!handle) | |
| 148 return false; | |
| 149 bool result = net::X509Certificate::GetDEREncoded(handle, certificate); | |
| 150 net::X509Certificate::FreeOSCertHandle(handle); | |
| 151 return result; | |
| 152 } | |
| 153 | |
| 80 MessageLoop message_loop_; | 154 MessageLoop message_loop_; |
| 81 content::TestBrowserThread ui_thread_; | 155 content::TestBrowserThread ui_thread_; |
| 82 ScopedTestCrosSettings test_cros_settings_; | 156 ScopedTestCrosSettings test_cros_settings_; |
| 83 CrosSettingsProvider* device_settings_provider_; | 157 CrosSettingsProvider* device_settings_provider_; |
| 84 StubCrosSettingsProvider stub_settings_provider_; | 158 StubCrosSettingsProvider stub_settings_provider_; |
| 85 StrictMock<MockCryptohomeClient> cryptohome_client_; | 159 StrictMock<MockCryptohomeClient> cryptohome_client_; |
| 86 StrictMock<MockAttestationFlow> attestation_flow_; | 160 StrictMock<MockAttestationFlow> attestation_flow_; |
| 87 StrictMock<policy::MockCloudPolicyClient> policy_client_; | 161 StrictMock<policy::MockCloudPolicyClient> policy_client_; |
| 88 }; | 162 }; |
| 89 | 163 |
| 90 TEST_F(AttestationPolicyObserverTest, FeatureDisabled) { | 164 TEST_F(AttestationPolicyObserverTest, FeatureDisabled) { |
| 91 CrosSettings* cros_settings = CrosSettings::Get(); | 165 CrosSettings* cros_settings = CrosSettings::Get(); |
| 92 cros_settings->SetBoolean(kDeviceAttestationEnabled, false); | 166 cros_settings->SetBoolean(kDeviceAttestationEnabled, false); |
| 93 Run(); | 167 Run(); |
| 94 } | 168 } |
| 95 | 169 |
| 96 TEST_F(AttestationPolicyObserverTest, UnregisteredPolicyClient) { | 170 TEST_F(AttestationPolicyObserverTest, UnregisteredPolicyClient) { |
| 97 policy_client_.SetDMToken(""); | 171 policy_client_.SetDMToken(""); |
| 98 Run(); | 172 Run(); |
| 99 } | 173 } |
| 100 | 174 |
| 101 TEST_F(AttestationPolicyObserverTest, NewCertificate) { | 175 TEST_F(AttestationPolicyObserverTest, NewCertificate) { |
| 102 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _)) | 176 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _)) |
| 103 .WillOnce(WithArgs<2>(Invoke(DBusCallbackFalse))); | 177 .WillOnce(WithArgs<2>(Invoke(DBusCallbackFalse))); |
| 178 EXPECT_CALL(cryptohome_client_, TpmAttestationGetKeyPayload(_, _, _)) | |
| 179 .WillOnce(WithArgs<2>(Invoke(FakeDBusData("")))); | |
| 180 EXPECT_CALL(cryptohome_client_, | |
| 181 TpmAttestationSetKeyPayload(_, _, CreatePayload(), _)) | |
| 182 .WillOnce(WithArgs<3>(Invoke(DBusCallbackTrue))); | |
| 104 EXPECT_CALL(attestation_flow_, GetCertificate(_, _)) | 183 EXPECT_CALL(attestation_flow_, GetCertificate(_, _)) |
| 105 .WillOnce(WithArgs<1>(Invoke(CertCallbackSuccess))); | 184 .WillOnce(WithArgs<1>(Invoke(CertCallbackSuccess))); |
| 185 EXPECT_CALL(policy_client_, UploadCertificate("fake_cert", _)) | |
| 186 .WillOnce(WithArgs<1>(Invoke(StatusCallbackSuccess))); | |
| 106 Run(); | 187 Run(); |
| 107 } | 188 } |
| 108 | 189 |
| 109 TEST_F(AttestationPolicyObserverTest, KeyExists) { | 190 TEST_F(AttestationPolicyObserverTest, KeyExistsNotUploaded) { |
| 191 string certificate; | |
| 192 ASSERT_TRUE(CreateCertificate(CERT_VALID, &certificate)); | |
| 110 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _)) | 193 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _)) |
| 111 .WillOnce(WithArgs<2>(Invoke(DBusCallbackTrue))); | 194 .WillOnce(WithArgs<2>(Invoke(DBusCallbackTrue))); |
| 112 EXPECT_CALL(cryptohome_client_, TpmAttestationGetCertificate(_, _, _)) | 195 EXPECT_CALL(cryptohome_client_, TpmAttestationGetCertificate(_, _, _)) |
| 113 .WillOnce(WithArgs<2>(Invoke(DBusDataCallback))); | 196 .WillOnce(WithArgs<2>(Invoke(FakeDBusData(certificate)))); |
| 197 EXPECT_CALL(cryptohome_client_, TpmAttestationGetKeyPayload(_, _, _)) | |
| 198 .WillRepeatedly(WithArgs<2>(Invoke(FakeDBusData("")))); | |
| 199 EXPECT_CALL(cryptohome_client_, | |
| 200 TpmAttestationSetKeyPayload(_, _, CreatePayload(), _)) | |
| 201 .WillOnce(WithArgs<3>(Invoke(DBusCallbackTrue))); | |
| 202 EXPECT_CALL(policy_client_, UploadCertificate(certificate, _)) | |
| 203 .WillOnce(WithArgs<1>(Invoke(StatusCallbackSuccess))); | |
| 114 Run(); | 204 Run(); |
| 115 } | 205 } |
| 116 | 206 |
| 207 TEST_F(AttestationPolicyObserverTest, KeyExistsAlreadyUploaded) { | |
| 208 string certificate; | |
| 209 ASSERT_TRUE(CreateCertificate(CERT_VALID, &certificate)); | |
| 210 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _)) | |
| 211 .WillOnce(WithArgs<2>(Invoke(DBusCallbackTrue))); | |
| 212 EXPECT_CALL(cryptohome_client_, TpmAttestationGetCertificate(_, _, _)) | |
| 213 .WillOnce(WithArgs<2>(Invoke(FakeDBusData(certificate)))); | |
| 214 EXPECT_CALL(cryptohome_client_, TpmAttestationGetKeyPayload(_, _, _)) | |
| 215 .WillOnce(WithArgs<2>(Invoke(FakeDBusData(CreatePayload())))); | |
| 216 Run(); | |
| 217 } | |
| 218 | |
| 219 TEST_F(AttestationPolicyObserverTest, KeyExistsCertExpiringSoon) { | |
| 220 string certificate; | |
| 221 ASSERT_TRUE(CreateCertificate(CERT_EXPIRING_SOON, &certificate)); | |
| 222 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _)) | |
| 223 .WillOnce(WithArgs<2>(Invoke(DBusCallbackTrue))); | |
| 224 EXPECT_CALL(cryptohome_client_, TpmAttestationGetCertificate(_, _, _)) | |
| 225 .WillOnce(WithArgs<2>(Invoke(FakeDBusData(certificate)))); | |
| 226 EXPECT_CALL(cryptohome_client_, TpmAttestationGetKeyPayload(_, _, _)) | |
| 227 .WillRepeatedly(WithArgs<2>(Invoke(FakeDBusData("")))); | |
| 228 EXPECT_CALL(cryptohome_client_, | |
| 229 TpmAttestationSetKeyPayload(_, _, CreatePayload(), _)) | |
| 230 .WillOnce(WithArgs<3>(Invoke(DBusCallbackTrue))); | |
| 231 EXPECT_CALL(attestation_flow_, GetCertificate(_, _)) | |
| 232 .WillOnce(WithArgs<1>(Invoke(CertCallbackSuccess))); | |
| 233 EXPECT_CALL(policy_client_, UploadCertificate("fake_cert", _)) | |
| 234 .WillOnce(WithArgs<1>(Invoke(StatusCallbackSuccess))); | |
| 235 Run(); | |
| 236 } | |
| 237 | |
| 238 TEST_F(AttestationPolicyObserverTest, KeyExistsCertExpired) { | |
| 239 string certificate; | |
| 240 ASSERT_TRUE(CreateCertificate(CERT_EXPIRED, &certificate)); | |
| 241 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _)) | |
| 242 .WillOnce(WithArgs<2>(Invoke(DBusCallbackTrue))); | |
| 243 EXPECT_CALL(cryptohome_client_, TpmAttestationGetCertificate(_, _, _)) | |
| 244 .WillOnce(WithArgs<2>(Invoke(FakeDBusData(certificate)))); | |
| 245 EXPECT_CALL(cryptohome_client_, TpmAttestationGetKeyPayload(_, _, _)) | |
| 246 .WillRepeatedly(WithArgs<2>(Invoke(FakeDBusData("")))); | |
| 247 EXPECT_CALL(cryptohome_client_, | |
| 248 TpmAttestationSetKeyPayload(_, _, CreatePayload(), _)) | |
| 249 .WillOnce(WithArgs<3>(Invoke(DBusCallbackTrue))); | |
| 250 EXPECT_CALL(attestation_flow_, GetCertificate(_, _)) | |
| 251 .WillOnce(WithArgs<1>(Invoke(CertCallbackSuccess))); | |
| 252 EXPECT_CALL(policy_client_, UploadCertificate("fake_cert", _)) | |
| 253 .WillOnce(WithArgs<1>(Invoke(StatusCallbackSuccess))); | |
| 254 Run(); | |
| 255 } | |
| 256 | |
| 257 TEST_F(AttestationPolicyObserverTest, IgnoreUnknownCertFormat) { | |
| 258 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _)) | |
| 259 .WillOnce(WithArgs<2>(Invoke(DBusCallbackTrue))); | |
| 260 EXPECT_CALL(cryptohome_client_, TpmAttestationGetCertificate(_, _, _)) | |
| 261 .WillOnce(WithArgs<2>(Invoke(FakeDBusData("unsupported_cert")))); | |
| 262 EXPECT_CALL(cryptohome_client_, TpmAttestationGetKeyPayload(_, _, _)) | |
| 263 .WillOnce(WithArgs<2>(Invoke(FakeDBusData(CreatePayload())))); | |
| 264 Run(); | |
| 265 } | |
|
Mattias Nissler (ping if slow)
2013/04/22 10:25:42
And what you're doing here is why some people hate
dkrahn
2013/04/22 22:33:31
Each time I update the CryptohomeClient interface,
Mattias Nissler (ping if slow)
2013/04/23 10:53:01
Well, the root cause with the pain here is not gmo
| |
| 266 | |
| 117 } // namespace attestation | 267 } // namespace attestation |
| 118 } // namespace chromeos | 268 } // namespace chromeos |
| OLD | NEW |