1 //
2 // Copyright (C) 2014 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "trunks/scoped_key_handle.h"
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 #include "trunks/mock_tpm.h"
23 #include "trunks/tpm_generated.h"
24 #include "trunks/trunks_factory_for_test.h"
25 
26 using testing::_;
27 using testing::DoAll;
28 using testing::Invoke;
29 using testing::NiceMock;
30 using testing::Return;
31 using testing::SetArgPointee;
32 using testing::WithArgs;
33 
34 namespace trunks {
35 
36 // A test fixture for TpmState tests.
37 class ScopedKeyHandleTest : public testing::Test {
38  public:
ScopedKeyHandleTest()39   ScopedKeyHandleTest() {}
~ScopedKeyHandleTest()40   ~ScopedKeyHandleTest() override {}
41 
SetUp()42   void SetUp() override { factory_.set_tpm(&mock_tpm_); }
43 
44  protected:
45   TrunksFactoryForTest factory_;
46   NiceMock<MockTpm> mock_tpm_;
47 };
48 
TEST_F(ScopedKeyHandleTest,FlushHandle)49 TEST_F(ScopedKeyHandleTest, FlushHandle) {
50   TPM_HANDLE handle = TPM_RH_FIRST;
51   ScopedKeyHandle scoped_handle(factory_, handle);
52   EXPECT_CALL(mock_tpm_, FlushContextSync(handle, _))
53       .WillOnce(Return(TPM_RC_SUCCESS));
54 }
55 
TEST_F(ScopedKeyHandleTest,GetTest)56 TEST_F(ScopedKeyHandleTest, GetTest) {
57   TPM_HANDLE handle = TPM_RH_FIRST;
58   ScopedKeyHandle scoped_handle(factory_, handle);
59   EXPECT_EQ(handle, scoped_handle.get());
60 }
61 
TEST_F(ScopedKeyHandleTest,ReleaseTest)62 TEST_F(ScopedKeyHandleTest, ReleaseTest) {
63   TPM_HANDLE handle = TPM_RH_FIRST;
64   ScopedKeyHandle scoped_handle(factory_, handle);
65   EXPECT_EQ(handle, scoped_handle.release());
66   EXPECT_EQ(0u, scoped_handle.get());
67 }
68 
TEST_F(ScopedKeyHandleTest,ResetAndFlush)69 TEST_F(ScopedKeyHandleTest, ResetAndFlush) {
70   TPM_HANDLE old_handle = TPM_RH_FIRST;
71   TPM_HANDLE new_handle = TPM_RH_NULL;
72   ScopedKeyHandle scoped_handle(factory_, old_handle);
73   EXPECT_EQ(old_handle, scoped_handle.get());
74   EXPECT_CALL(mock_tpm_, FlushContextSync(old_handle, _))
75       .WillOnce(Return(TPM_RC_SUCCESS));
76   scoped_handle.reset(new_handle);
77   EXPECT_EQ(new_handle, scoped_handle.get());
78   EXPECT_CALL(mock_tpm_, FlushContextSync(new_handle, _))
79       .WillOnce(Return(TPM_RC_SUCCESS));
80 }
81 
TEST_F(ScopedKeyHandleTest,NullReset)82 TEST_F(ScopedKeyHandleTest, NullReset) {
83   TPM_HANDLE handle = TPM_RH_FIRST;
84   ScopedKeyHandle scoped_handle(factory_, handle);
85   EXPECT_EQ(handle, scoped_handle.get());
86   EXPECT_CALL(mock_tpm_, FlushContextSync(handle, _))
87       .WillOnce(Return(TPM_RC_SUCCESS));
88   scoped_handle.reset();
89   EXPECT_EQ(0u, scoped_handle.get());
90 }
91 
92 }  // namespace trunks
93