1 //
2 // Copyright (C) 2015 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 "tpm_manager/server/tpm2_initializer_impl.h"
18
19 #include <memory>
20
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 #include <trunks/mock_tpm_utility.h>
24 #include <trunks/trunks_factory_for_test.h>
25
26 #include "tpm_manager/common/tpm_manager_constants.h"
27 #include "tpm_manager/server/mock_local_data_store.h"
28 #include "tpm_manager/server/mock_openssl_crypto_util.h"
29 #include "tpm_manager/server/mock_tpm_status.h"
30
31 using testing::_;
32 using testing::DoAll;
33 using testing::NiceMock;
34 using testing::Return;
35 using testing::SaveArg;
36 using testing::SetArgPointee;
37
38 namespace tpm_manager {
39
40 class Tpm2InitializerTest : public testing::Test {
41 public:
42 Tpm2InitializerTest() = default;
43 virtual ~Tpm2InitializerTest() = default;
44
SetUp()45 void SetUp() {
46 trunks::TrunksFactoryForTest* factory = new trunks::TrunksFactoryForTest();
47 factory->set_tpm_utility(&mock_tpm_utility_);
48 tpm_initializer_.reset(new Tpm2InitializerImpl(factory,
49 &mock_openssl_util_,
50 &mock_data_store_,
51 &mock_tpm_status_));
52 }
53
54 protected:
55 NiceMock<MockOpensslCryptoUtil> mock_openssl_util_;
56 NiceMock<MockLocalDataStore> mock_data_store_;
57 NiceMock<MockTpmStatus> mock_tpm_status_;
58 NiceMock<trunks::MockTpmUtility> mock_tpm_utility_;
59 std::unique_ptr<TpmInitializer> tpm_initializer_;
60 };
61
TEST_F(Tpm2InitializerTest,InitializeTpmNoSeedTpm)62 TEST_F(Tpm2InitializerTest, InitializeTpmNoSeedTpm) {
63 EXPECT_CALL(mock_tpm_utility_, StirRandom(_, _))
64 .WillRepeatedly(Return(trunks::TPM_RC_FAILURE));
65 EXPECT_FALSE(tpm_initializer_->InitializeTpm());
66 }
67
TEST_F(Tpm2InitializerTest,InitializeTpmAlreadyOwned)68 TEST_F(Tpm2InitializerTest, InitializeTpmAlreadyOwned) {
69 EXPECT_CALL(mock_tpm_status_, IsTpmOwned())
70 .WillRepeatedly(Return(true));
71 EXPECT_CALL(mock_tpm_utility_, TakeOwnership(_, _, _))
72 .Times(0);
73 EXPECT_TRUE(tpm_initializer_->InitializeTpm());
74 }
75
TEST_F(Tpm2InitializerTest,InitializeTpmLocalDataReadError)76 TEST_F(Tpm2InitializerTest, InitializeTpmLocalDataReadError) {
77 EXPECT_CALL(mock_tpm_status_, IsTpmOwned())
78 .WillRepeatedly(Return(false));
79 EXPECT_CALL(mock_data_store_, Read(_))
80 .WillRepeatedly(Return(false));
81 EXPECT_CALL(mock_tpm_utility_, TakeOwnership(_, _, _))
82 .Times(0);
83 EXPECT_FALSE(tpm_initializer_->InitializeTpm());
84 }
85
TEST_F(Tpm2InitializerTest,InitializeTpmLocalDataWriteError)86 TEST_F(Tpm2InitializerTest, InitializeTpmLocalDataWriteError) {
87 EXPECT_CALL(mock_tpm_status_, IsTpmOwned())
88 .WillRepeatedly(Return(false));
89 EXPECT_CALL(mock_data_store_, Write(_))
90 .WillRepeatedly(Return(false));
91 EXPECT_CALL(mock_tpm_utility_, TakeOwnership(_, _, _))
92 .Times(0);
93 EXPECT_FALSE(tpm_initializer_->InitializeTpm());
94 }
95
TEST_F(Tpm2InitializerTest,InitializeTpmOwnershipError)96 TEST_F(Tpm2InitializerTest, InitializeTpmOwnershipError) {
97 EXPECT_CALL(mock_tpm_status_, IsTpmOwned())
98 .WillOnce(Return(false));
99 EXPECT_CALL(mock_tpm_utility_, TakeOwnership(_, _, _))
100 .WillRepeatedly(Return(trunks::TPM_RC_FAILURE));
101 EXPECT_FALSE(tpm_initializer_->InitializeTpm());
102 }
103
TEST_F(Tpm2InitializerTest,InitializeTpmSuccess)104 TEST_F(Tpm2InitializerTest, InitializeTpmSuccess) {
105 EXPECT_CALL(mock_tpm_status_, IsTpmOwned())
106 .WillOnce(Return(false));
107 std::string owner_password;
108 std::string endorsement_password;
109 std::string lockout_password;
110 LocalData local_data;
111 EXPECT_CALL(mock_data_store_, Read(_))
112 .WillOnce(DoAll(SetArgPointee<0>(local_data),
113 Return(true)));
114 EXPECT_CALL(mock_tpm_utility_, GenerateRandom(_, _, _))
115 .Times(3) // Once for owner, endorsement and lockout passwords
116 .WillRepeatedly(Return(trunks::TPM_RC_SUCCESS));
117 EXPECT_CALL(mock_tpm_utility_, TakeOwnership(_, _, _))
118 .WillOnce(Return(trunks::TPM_RC_SUCCESS));
119 EXPECT_TRUE(tpm_initializer_->InitializeTpm());
120 }
121
TEST_F(Tpm2InitializerTest,InitializeTpmSuccessAfterError)122 TEST_F(Tpm2InitializerTest, InitializeTpmSuccessAfterError) {
123 EXPECT_CALL(mock_tpm_status_, IsTpmOwned())
124 .WillOnce(Return(false));
125 std::string owner_password("owner");
126 std::string endorsement_password("endorsement");
127 std::string lockout_password("lockout");
128 LocalData local_data;
129 local_data.add_owner_dependency(kTestDependency);
130 local_data.set_owner_password(owner_password);
131 local_data.set_endorsement_password(endorsement_password);
132 local_data.set_lockout_password(lockout_password);
133 EXPECT_CALL(mock_data_store_, Read(_))
134 .WillOnce(DoAll(SetArgPointee<0>(local_data),
135 Return(true)));
136 EXPECT_CALL(mock_data_store_, Write(_))
137 .WillOnce(DoAll(SaveArg<0>(&local_data),
138 Return(true)));
139 EXPECT_EQ(1, local_data.owner_dependency_size());
140 EXPECT_EQ(kTestDependency, local_data.owner_dependency(0));
141 EXPECT_EQ(owner_password, local_data.owner_password());
142 EXPECT_EQ(endorsement_password, local_data.endorsement_password());
143 EXPECT_EQ(lockout_password, local_data.lockout_password());
144 EXPECT_CALL(mock_tpm_utility_, TakeOwnership(owner_password,
145 endorsement_password,
146 lockout_password))
147 .WillOnce(Return(trunks::TPM_RC_SUCCESS));
148 EXPECT_TRUE(tpm_initializer_->InitializeTpm());
149 }
150
151 } // namespace tpm_manager
152