| 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" |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 MOCK_KEY_UPLOADED = (1 << 1), // Configure so an upload has occurred. | 141 MOCK_KEY_UPLOADED = (1 << 1), // Configure so an upload has occurred. |
| 142 MOCK_NEW_KEY = (1 << 2) // Configure expecting new key generation. | 142 MOCK_NEW_KEY = (1 << 2) // Configure expecting new key generation. |
| 143 }; | 143 }; |
| 144 | 144 |
| 145 // Configures mock expectations according to |mock_options|. If options | 145 // Configures mock expectations according to |mock_options|. If options |
| 146 // require that a certificate exists, |certificate| will be used. | 146 // require that a certificate exists, |certificate| will be used. |
| 147 void SetupMocks(int mock_options, const std::string& certificate) { | 147 void SetupMocks(int mock_options, const std::string& certificate) { |
| 148 bool key_exists = (mock_options & MOCK_KEY_EXISTS); | 148 bool key_exists = (mock_options & MOCK_KEY_EXISTS); |
| 149 // Setup expected key / cert queries. | 149 // Setup expected key / cert queries. |
| 150 if (key_exists) { | 150 if (key_exists) { |
| 151 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _)) | 151 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _, _)) |
| 152 .WillRepeatedly(WithArgs<2>(Invoke(DBusCallbackTrue))); | 152 .WillRepeatedly(WithArgs<3>(Invoke(DBusCallbackTrue))); |
| 153 EXPECT_CALL(cryptohome_client_, TpmAttestationGetCertificate(_, _, _)) | 153 EXPECT_CALL(cryptohome_client_, TpmAttestationGetCertificate(_, _, _, _)) |
| 154 .WillRepeatedly(WithArgs<2>(Invoke(FakeDBusData(certificate)))); | 154 .WillRepeatedly(WithArgs<3>(Invoke(FakeDBusData(certificate)))); |
| 155 } else { | 155 } else { |
| 156 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _)) | 156 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _, _)) |
| 157 .WillRepeatedly(WithArgs<2>(Invoke(DBusCallbackFalse))); | 157 .WillRepeatedly(WithArgs<3>(Invoke(DBusCallbackFalse))); |
| 158 } | 158 } |
| 159 | 159 |
| 160 // Setup expected key payload queries. | 160 // Setup expected key payload queries. |
| 161 bool key_uploaded = (mock_options & MOCK_KEY_UPLOADED); | 161 bool key_uploaded = (mock_options & MOCK_KEY_UPLOADED); |
| 162 std::string payload = CreatePayload(); | 162 std::string payload = CreatePayload(); |
| 163 EXPECT_CALL(cryptohome_client_, TpmAttestationGetKeyPayload(_, _, _)) | 163 EXPECT_CALL(cryptohome_client_, TpmAttestationGetKeyPayload(_, _, _, _)) |
| 164 .WillRepeatedly(WithArgs<2>(Invoke( | 164 .WillRepeatedly(WithArgs<3>(Invoke( |
| 165 FakeDBusData(key_uploaded ? payload : "")))); | 165 FakeDBusData(key_uploaded ? payload : "")))); |
| 166 | 166 |
| 167 // Setup expected key uploads. Use WillOnce() so StrictMock will trigger an | 167 // Setup expected key uploads. Use WillOnce() so StrictMock will trigger an |
| 168 // error if our expectations are not met exactly. We want to verify that | 168 // error if our expectations are not met exactly. We want to verify that |
| 169 // during a single run through the observer only one upload operation occurs | 169 // during a single run through the observer only one upload operation occurs |
| 170 // (because it is costly) and similarly, that the writing of the uploaded | 170 // (because it is costly) and similarly, that the writing of the uploaded |
| 171 // status in the key payload matches the upload operation. | 171 // status in the key payload matches the upload operation. |
| 172 bool new_key = (mock_options & MOCK_NEW_KEY); | 172 bool new_key = (mock_options & MOCK_NEW_KEY); |
| 173 if (new_key || !key_uploaded) { | 173 if (new_key || !key_uploaded) { |
| 174 EXPECT_CALL(policy_client_, | 174 EXPECT_CALL(policy_client_, |
| 175 UploadCertificate(new_key ? "fake_cert" : certificate, _)) | 175 UploadCertificate(new_key ? "fake_cert" : certificate, _)) |
| 176 .WillOnce(WithArgs<1>(Invoke(StatusCallbackSuccess))); | 176 .WillOnce(WithArgs<1>(Invoke(StatusCallbackSuccess))); |
| 177 EXPECT_CALL(cryptohome_client_, | 177 EXPECT_CALL(cryptohome_client_, |
| 178 TpmAttestationSetKeyPayload(_, _, payload, _)) | 178 TpmAttestationSetKeyPayload(_, _, _, payload, _)) |
| 179 .WillOnce(WithArgs<3>(Invoke(DBusCallbackTrue))); | 179 .WillOnce(WithArgs<4>(Invoke(DBusCallbackTrue))); |
| 180 } | 180 } |
| 181 | 181 |
| 182 // Setup expected key generations. Again use WillOnce(). Key generation is | 182 // Setup expected key generations. Again use WillOnce(). Key generation is |
| 183 // another costly operation and if it gets triggered more than once during | 183 // another costly operation and if it gets triggered more than once during |
| 184 // a single pass this indicates a logical problem in the observer. | 184 // a single pass this indicates a logical problem in the observer. |
| 185 if (new_key) { | 185 if (new_key) { |
| 186 EXPECT_CALL(attestation_flow_, GetCertificate(_, _, _, _, _)) | 186 EXPECT_CALL(attestation_flow_, GetCertificate(_, _, _, _, _)) |
| 187 .WillOnce(WithArgs<4>(Invoke(CertCallbackSuccess))); | 187 .WillOnce(WithArgs<4>(Invoke(CertCallbackSuccess))); |
| 188 } | 188 } |
| 189 } | 189 } |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 } | 290 } |
| 291 | 291 |
| 292 TEST_F(AttestationPolicyObserverTest, IgnoreUnknownCertFormat) { | 292 TEST_F(AttestationPolicyObserverTest, IgnoreUnknownCertFormat) { |
| 293 SetupMocks(MOCK_KEY_EXISTS | MOCK_KEY_UPLOADED, "unsupported"); | 293 SetupMocks(MOCK_KEY_EXISTS | MOCK_KEY_UPLOADED, "unsupported"); |
| 294 Run(); | 294 Run(); |
| 295 } | 295 } |
| 296 | 296 |
| 297 TEST_F(AttestationPolicyObserverTest, DBusFailureRetry) { | 297 TEST_F(AttestationPolicyObserverTest, DBusFailureRetry) { |
| 298 SetupMocks(MOCK_NEW_KEY, ""); | 298 SetupMocks(MOCK_NEW_KEY, ""); |
| 299 // Simulate a DBus failure. | 299 // Simulate a DBus failure. |
| 300 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _)) | 300 EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _, _)) |
| 301 .WillOnce(WithArgs<2>(Invoke(DBusCallbackError))) | 301 .WillOnce(WithArgs<3>(Invoke(DBusCallbackError))) |
| 302 .WillRepeatedly(WithArgs<2>(Invoke(DBusCallbackFalse))); | 302 .WillRepeatedly(WithArgs<3>(Invoke(DBusCallbackFalse))); |
| 303 Run(); | 303 Run(); |
| 304 } | 304 } |
| 305 | 305 |
| 306 } // namespace attestation | 306 } // namespace attestation |
| 307 } // namespace chromeos | 307 } // namespace chromeos |
| OLD | NEW |