OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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/run_loop.h" |
| 7 #include "chromeos/attestation/mock_attestation_flow.h" |
| 8 #include "chromeos/cryptohome/mock_async_method_caller.h" |
| 9 #include "chromeos/dbus/mock_cryptohome_client.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 using testing::_; |
| 14 using testing::Invoke; |
| 15 using testing::Sequence; |
| 16 using testing::StrictMock; |
| 17 using testing::WithArgs; |
| 18 |
| 19 namespace chromeos { |
| 20 namespace attestation { |
| 21 |
| 22 namespace { |
| 23 |
| 24 void DBusCallbackFalse(const BoolDBusMethodCallback& callback) { |
| 25 MessageLoop::current()->PostTask( |
| 26 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, false)); |
| 27 } |
| 28 |
| 29 void DBusCallbackTrue(const BoolDBusMethodCallback& callback) { |
| 30 MessageLoop::current()->PostTask( |
| 31 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true)); |
| 32 } |
| 33 |
| 34 void DBusCallbackFail(const BoolDBusMethodCallback& callback) { |
| 35 MessageLoop::current()->PostTask( |
| 36 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_FAILURE, false)); |
| 37 } |
| 38 |
| 39 void AsyncCallbackFalse(cryptohome::AsyncMethodCaller::Callback callback) { |
| 40 callback.Run(false, cryptohome::MOUNT_ERROR_NONE); |
| 41 } |
| 42 |
| 43 } // namespace |
| 44 |
| 45 class AttestationFlowTest : public testing::Test { |
| 46 protected: |
| 47 void Run() { |
| 48 base::RunLoop run_loop; |
| 49 run_loop.RunUntilIdle(); |
| 50 } |
| 51 MessageLoop message_loop_; |
| 52 }; |
| 53 |
| 54 TEST_F(AttestationFlowTest, GetCertificate) { |
| 55 // Verify the order of calls in a sequence. |
| 56 Sequence flow_order; |
| 57 |
| 58 // Use DBusCallbackFalse so the full enrollment flow is triggered. |
| 59 chromeos::MockCryptohomeClient client; |
| 60 EXPECT_CALL(client, TpmAttestationIsEnrolled(_)) |
| 61 .InSequence(flow_order) |
| 62 .WillRepeatedly(Invoke(DBusCallbackFalse)); |
| 63 |
| 64 // Use StrictMock when we want to verify invocation frequency. |
| 65 StrictMock<cryptohome::MockAsyncMethodCaller> async_caller; |
| 66 async_caller.SetUp(true, cryptohome::MOUNT_ERROR_NONE); |
| 67 EXPECT_CALL(async_caller, AsyncTpmAttestationCreateEnrollRequest(_)) |
| 68 .Times(1) |
| 69 .InSequence(flow_order); |
| 70 |
| 71 StrictMock<MockServerProxy> proxy; |
| 72 proxy.DeferToFake(true); |
| 73 EXPECT_CALL(proxy, SendEnrollRequest( |
| 74 cryptohome::MockAsyncMethodCaller::kFakeAttestationEnrollRequest, |
| 75 _)).Times(1) |
| 76 .InSequence(flow_order); |
| 77 |
| 78 std::string fake_enroll_response = |
| 79 cryptohome::MockAsyncMethodCaller::kFakeAttestationEnrollRequest; |
| 80 fake_enroll_response += "_response"; |
| 81 EXPECT_CALL(async_caller, AsyncTpmAttestationEnroll(fake_enroll_response, _)) |
| 82 .Times(1) |
| 83 .InSequence(flow_order); |
| 84 |
| 85 EXPECT_CALL(async_caller, AsyncTpmAttestationCreateCertRequest(false, _)) |
| 86 .Times(1) |
| 87 .InSequence(flow_order); |
| 88 |
| 89 EXPECT_CALL(proxy, SendCertificateRequest( |
| 90 cryptohome::MockAsyncMethodCaller::kFakeAttestationCertRequest, |
| 91 _)).Times(1) |
| 92 .InSequence(flow_order); |
| 93 |
| 94 std::string fake_cert_response = |
| 95 cryptohome::MockAsyncMethodCaller::kFakeAttestationCertRequest; |
| 96 fake_cert_response += "_response"; |
| 97 EXPECT_CALL(async_caller, |
| 98 AsyncTpmAttestationFinishCertRequest(fake_cert_response, _)) |
| 99 .Times(1) |
| 100 .InSequence(flow_order); |
| 101 |
| 102 StrictMock<MockObserver> observer; |
| 103 EXPECT_CALL(observer, MockCertificateCallback( |
| 104 true, |
| 105 cryptohome::MockAsyncMethodCaller::kFakeAttestationCert)) |
| 106 .Times(1) |
| 107 .InSequence(flow_order); |
| 108 AttestationFlow::CertificateCallback mock_callback = base::Bind( |
| 109 &MockObserver::MockCertificateCallback, |
| 110 base::Unretained(&observer)); |
| 111 |
| 112 AttestationFlow flow(&async_caller, &client, &proxy); |
| 113 flow.GetCertificate("test", mock_callback); |
| 114 Run(); |
| 115 } |
| 116 |
| 117 TEST_F(AttestationFlowTest, GetCertificate_NoEK) { |
| 118 StrictMock<cryptohome::MockAsyncMethodCaller> async_caller; |
| 119 async_caller.SetUp(false, cryptohome::MOUNT_ERROR_NONE); |
| 120 EXPECT_CALL(async_caller, AsyncTpmAttestationCreateEnrollRequest(_)) |
| 121 .Times(1); |
| 122 |
| 123 chromeos::MockCryptohomeClient client; |
| 124 EXPECT_CALL(client, TpmAttestationIsEnrolled(_)) |
| 125 .WillRepeatedly(Invoke(DBusCallbackFalse)); |
| 126 |
| 127 // We're not expecting any server calls in this case; StrictMock will verify. |
| 128 StrictMock<MockServerProxy> proxy; |
| 129 |
| 130 StrictMock<MockObserver> observer; |
| 131 EXPECT_CALL(observer, MockCertificateCallback(false, "")) |
| 132 .Times(1); |
| 133 AttestationFlow::CertificateCallback mock_callback = base::Bind( |
| 134 &MockObserver::MockCertificateCallback, |
| 135 base::Unretained(&observer)); |
| 136 |
| 137 AttestationFlow flow(&async_caller, &client, &proxy); |
| 138 flow.GetCertificate("test", mock_callback); |
| 139 Run(); |
| 140 } |
| 141 |
| 142 TEST_F(AttestationFlowTest, GetCertificate_EKRejected) { |
| 143 StrictMock<cryptohome::MockAsyncMethodCaller> async_caller; |
| 144 async_caller.SetUp(true, cryptohome::MOUNT_ERROR_NONE); |
| 145 EXPECT_CALL(async_caller, AsyncTpmAttestationCreateEnrollRequest(_)) |
| 146 .Times(1); |
| 147 |
| 148 chromeos::MockCryptohomeClient client; |
| 149 EXPECT_CALL(client, TpmAttestationIsEnrolled(_)) |
| 150 .WillRepeatedly(Invoke(DBusCallbackFalse)); |
| 151 |
| 152 StrictMock<MockServerProxy> proxy; |
| 153 proxy.DeferToFake(false); |
| 154 EXPECT_CALL(proxy, SendEnrollRequest( |
| 155 cryptohome::MockAsyncMethodCaller::kFakeAttestationEnrollRequest, |
| 156 _)).Times(1); |
| 157 |
| 158 StrictMock<MockObserver> observer; |
| 159 EXPECT_CALL(observer, MockCertificateCallback(false, "")) |
| 160 .Times(1); |
| 161 AttestationFlow::CertificateCallback mock_callback = base::Bind( |
| 162 &MockObserver::MockCertificateCallback, |
| 163 base::Unretained(&observer)); |
| 164 |
| 165 AttestationFlow flow(&async_caller, &client, &proxy); |
| 166 flow.GetCertificate("test", mock_callback); |
| 167 Run(); |
| 168 } |
| 169 |
| 170 TEST_F(AttestationFlowTest, GetCertificate_FailEnroll) { |
| 171 StrictMock<cryptohome::MockAsyncMethodCaller> async_caller; |
| 172 async_caller.SetUp(true, cryptohome::MOUNT_ERROR_NONE); |
| 173 EXPECT_CALL(async_caller, AsyncTpmAttestationCreateEnrollRequest(_)) |
| 174 .Times(1); |
| 175 std::string fake_enroll_response = |
| 176 cryptohome::MockAsyncMethodCaller::kFakeAttestationEnrollRequest; |
| 177 fake_enroll_response += "_response"; |
| 178 EXPECT_CALL(async_caller, AsyncTpmAttestationEnroll(fake_enroll_response, _)) |
| 179 .WillOnce(WithArgs<1>(Invoke(AsyncCallbackFalse))); |
| 180 |
| 181 chromeos::MockCryptohomeClient client; |
| 182 EXPECT_CALL(client, TpmAttestationIsEnrolled(_)) |
| 183 .WillRepeatedly(Invoke(DBusCallbackFalse)); |
| 184 |
| 185 StrictMock<MockServerProxy> proxy; |
| 186 proxy.DeferToFake(true); |
| 187 EXPECT_CALL(proxy, SendEnrollRequest( |
| 188 cryptohome::MockAsyncMethodCaller::kFakeAttestationEnrollRequest, |
| 189 _)).Times(1); |
| 190 |
| 191 StrictMock<MockObserver> observer; |
| 192 EXPECT_CALL(observer, MockCertificateCallback(false, "")).Times(1); |
| 193 AttestationFlow::CertificateCallback mock_callback = base::Bind( |
| 194 &MockObserver::MockCertificateCallback, |
| 195 base::Unretained(&observer)); |
| 196 |
| 197 AttestationFlow flow(&async_caller, &client, &proxy); |
| 198 flow.GetCertificate("test", mock_callback); |
| 199 Run(); |
| 200 } |
| 201 |
| 202 TEST_F(AttestationFlowTest, GetOwnerCertificateAlreadyEnrolled) { |
| 203 StrictMock<cryptohome::MockAsyncMethodCaller> async_caller; |
| 204 async_caller.SetUp(true, cryptohome::MOUNT_ERROR_NONE); |
| 205 EXPECT_CALL(async_caller, AsyncTpmAttestationCreateCertRequest(true, _)) |
| 206 .Times(1); |
| 207 std::string fake_cert_response = |
| 208 cryptohome::MockAsyncMethodCaller::kFakeAttestationCertRequest; |
| 209 fake_cert_response += "_response"; |
| 210 EXPECT_CALL(async_caller, |
| 211 AsyncTpmAttestationFinishCertRequest(fake_cert_response, _)) |
| 212 .Times(1); |
| 213 |
| 214 chromeos::MockCryptohomeClient client; |
| 215 EXPECT_CALL(client, TpmAttestationIsEnrolled(_)) |
| 216 .WillRepeatedly(Invoke(DBusCallbackTrue)); |
| 217 |
| 218 StrictMock<MockServerProxy> proxy; |
| 219 proxy.DeferToFake(true); |
| 220 EXPECT_CALL(proxy, SendCertificateRequest( |
| 221 cryptohome::MockAsyncMethodCaller::kFakeAttestationCertRequest, |
| 222 _)).Times(1); |
| 223 |
| 224 StrictMock<MockObserver> observer; |
| 225 EXPECT_CALL(observer, MockCertificateCallback( |
| 226 true, |
| 227 cryptohome::MockAsyncMethodCaller::kFakeAttestationCert)).Times(1); |
| 228 AttestationFlow::CertificateCallback mock_callback = base::Bind( |
| 229 &MockObserver::MockCertificateCallback, |
| 230 base::Unretained(&observer)); |
| 231 |
| 232 AttestationFlow flow(&async_caller, &client, &proxy); |
| 233 flow.GetCertificate("attest-ent-machine", mock_callback); |
| 234 Run(); |
| 235 } |
| 236 |
| 237 TEST_F(AttestationFlowTest, GetCertificate_FailCreateCertRequest) { |
| 238 StrictMock<cryptohome::MockAsyncMethodCaller> async_caller; |
| 239 async_caller.SetUp(false, cryptohome::MOUNT_ERROR_NONE); |
| 240 EXPECT_CALL(async_caller, AsyncTpmAttestationCreateCertRequest(false, _)) |
| 241 .Times(1); |
| 242 |
| 243 chromeos::MockCryptohomeClient client; |
| 244 EXPECT_CALL(client, TpmAttestationIsEnrolled(_)) |
| 245 .WillRepeatedly(Invoke(DBusCallbackTrue)); |
| 246 |
| 247 // We're not expecting any server calls in this case; StrictMock will verify. |
| 248 StrictMock<MockServerProxy> proxy; |
| 249 |
| 250 StrictMock<MockObserver> observer; |
| 251 EXPECT_CALL(observer, MockCertificateCallback(false, "")).Times(1); |
| 252 AttestationFlow::CertificateCallback mock_callback = base::Bind( |
| 253 &MockObserver::MockCertificateCallback, |
| 254 base::Unretained(&observer)); |
| 255 |
| 256 AttestationFlow flow(&async_caller, &client, &proxy); |
| 257 flow.GetCertificate("test", mock_callback); |
| 258 Run(); |
| 259 } |
| 260 |
| 261 TEST_F(AttestationFlowTest, GetCertificate_CertRequestRejected) { |
| 262 StrictMock<cryptohome::MockAsyncMethodCaller> async_caller; |
| 263 async_caller.SetUp(true, cryptohome::MOUNT_ERROR_NONE); |
| 264 EXPECT_CALL(async_caller, AsyncTpmAttestationCreateCertRequest(false, _)) |
| 265 .Times(1); |
| 266 |
| 267 chromeos::MockCryptohomeClient client; |
| 268 EXPECT_CALL(client, TpmAttestationIsEnrolled(_)) |
| 269 .WillRepeatedly(Invoke(DBusCallbackTrue)); |
| 270 |
| 271 StrictMock<MockServerProxy> proxy; |
| 272 proxy.DeferToFake(false); |
| 273 EXPECT_CALL(proxy, SendCertificateRequest( |
| 274 cryptohome::MockAsyncMethodCaller::kFakeAttestationCertRequest, |
| 275 _)).Times(1); |
| 276 |
| 277 StrictMock<MockObserver> observer; |
| 278 EXPECT_CALL(observer, MockCertificateCallback(false, "")).Times(1); |
| 279 AttestationFlow::CertificateCallback mock_callback = base::Bind( |
| 280 &MockObserver::MockCertificateCallback, |
| 281 base::Unretained(&observer)); |
| 282 |
| 283 AttestationFlow flow(&async_caller, &client, &proxy); |
| 284 flow.GetCertificate("test", mock_callback); |
| 285 Run(); |
| 286 } |
| 287 |
| 288 TEST_F(AttestationFlowTest, GetCertificate_FailIsEnrolled) { |
| 289 // We're not expecting any server calls in this case; StrictMock will verify. |
| 290 StrictMock<cryptohome::MockAsyncMethodCaller> async_caller; |
| 291 |
| 292 chromeos::MockCryptohomeClient client; |
| 293 EXPECT_CALL(client, TpmAttestationIsEnrolled(_)) |
| 294 .WillRepeatedly(Invoke(DBusCallbackFail)); |
| 295 |
| 296 // We're not expecting any server calls in this case; StrictMock will verify. |
| 297 StrictMock<MockServerProxy> proxy; |
| 298 |
| 299 StrictMock<MockObserver> observer; |
| 300 EXPECT_CALL(observer, MockCertificateCallback(false, "")).Times(1); |
| 301 AttestationFlow::CertificateCallback mock_callback = base::Bind( |
| 302 &MockObserver::MockCertificateCallback, |
| 303 base::Unretained(&observer)); |
| 304 |
| 305 AttestationFlow flow(&async_caller, &client, &proxy); |
| 306 flow.GetCertificate("test", mock_callback); |
| 307 Run(); |
| 308 } |
| 309 |
| 310 } // namespace attestation |
| 311 } // namespace chromeos |
OLD | NEW |