OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "chromeos/dbus/fake_cryptohome_client.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "chromeos/attestation/attestation.pb.h" |
| 13 #include "chromeos/cryptohome/cryptohome_parameters.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 using ::testing::_; |
| 18 using ::testing::SaveArg; |
| 19 |
| 20 namespace chromeos { |
| 21 |
| 22 class FakeCryptohomeClientTest : public ::testing::Test { |
| 23 public: |
| 24 FakeCryptohomeClientTest() : weak_ptr_factory_(this) { |
| 25 async_method_callback_ = |
| 26 base::Bind(&FakeCryptohomeClientTest::MockHandleAsyncMethodCallback, |
| 27 weak_ptr_factory_.GetWeakPtr()); |
| 28 fake_cryptohome_client_.SetAsyncCallStatusHandlers( |
| 29 base::Bind( |
| 30 &FakeCryptohomeClientTest::MockHandleAsyncMethodResultResponse, |
| 31 weak_ptr_factory_.GetWeakPtr()), |
| 32 base::Bind(&FakeCryptohomeClientTest::MockHandleAsyncMethodDataResponse, |
| 33 weak_ptr_factory_.GetWeakPtr())); |
| 34 } |
| 35 |
| 36 MOCK_METHOD1(MockHandleAsyncMethodCallback, void(int)); |
| 37 MOCK_METHOD3(MockHandleAsyncMethodResultResponse, void(int, bool, int)); |
| 38 MOCK_METHOD3(MockHandleAsyncMethodDataResponse, |
| 39 void(int, bool, const std::string&)); |
| 40 |
| 41 protected: |
| 42 base::MessageLoop message_loop_; |
| 43 |
| 44 FakeCryptohomeClient fake_cryptohome_client_; |
| 45 CryptohomeClient::AsyncMethodCallback async_method_callback_; |
| 46 |
| 47 base::WeakPtrFactory<FakeCryptohomeClientTest> weak_ptr_factory_; |
| 48 |
| 49 private: |
| 50 DISALLOW_COPY_AND_ASSIGN(FakeCryptohomeClientTest); |
| 51 }; |
| 52 |
| 53 TEST_F(FakeCryptohomeClientTest, SignSimpleChallenge) { |
| 54 const std::string challenge{"challenge"}; |
| 55 |
| 56 EXPECT_CALL(*this, MockHandleAsyncMethodCallback(_)); |
| 57 |
| 58 std::string return_data; |
| 59 EXPECT_CALL(*this, MockHandleAsyncMethodDataResponse(_, true, _)) |
| 60 .WillOnce(SaveArg<2>(&return_data)); |
| 61 |
| 62 cryptohome::Identification cryptohome_id; |
| 63 fake_cryptohome_client_.TpmAttestationSignSimpleChallenge( |
| 64 attestation::AttestationKeyType::KEY_DEVICE, cryptohome_id, "key_name", |
| 65 challenge, async_method_callback_); |
| 66 |
| 67 base::RunLoop().RunUntilIdle(); |
| 68 |
| 69 chromeos::attestation::SignedData signed_data; |
| 70 ASSERT_TRUE(signed_data.ParseFromString(return_data)); |
| 71 ASSERT_EQ(static_cast<size_t>(20), |
| 72 signed_data.data().size() - challenge.size()); |
| 73 ASSERT_EQ(challenge, |
| 74 signed_data.data().substr(0, signed_data.data().size() - 20)); |
| 75 } |
| 76 |
| 77 } // namespace chromeos |
OLD | NEW |