• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_status_impl.h"
18 
19 #include <memory>
20 
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 #include <trunks/mock_tpm_state.h>
24 #include <trunks/trunks_factory_for_test.h>
25 
26 using testing::NiceMock;
27 using testing::Return;
28 using trunks::TPM_RC_FAILURE;
29 using trunks::TPM_RC_SUCCESS;
30 
31 namespace tpm_manager {
32 
33 class Tpm2StatusTest : public testing::Test {
34  public:
35   Tpm2StatusTest() = default;
36   virtual ~Tpm2StatusTest() = default;
37 
SetUp()38   void SetUp() {
39     factory_.set_tpm_state(&mock_tpm_state_);
40     tpm_status_.reset(new Tpm2StatusImpl(factory_));
41   }
42 
43  protected:
44   NiceMock<trunks::MockTpmState> mock_tpm_state_;
45   trunks::TrunksFactoryForTest factory_;
46   std::unique_ptr<TpmStatus> tpm_status_;
47 };
48 
TEST_F(Tpm2StatusTest,IsEnabledSuccess)49 TEST_F(Tpm2StatusTest, IsEnabledSuccess) {
50   EXPECT_CALL(mock_tpm_state_, Initialize())
51       .WillRepeatedly(Return(TPM_RC_SUCCESS));
52   EXPECT_CALL(mock_tpm_state_, IsEnabled()).WillRepeatedly(Return(true));
53   EXPECT_TRUE(tpm_status_->IsTpmEnabled());
54 }
55 
TEST_F(Tpm2StatusTest,IsEnabledFailure)56 TEST_F(Tpm2StatusTest, IsEnabledFailure) {
57   EXPECT_CALL(mock_tpm_state_, IsEnabled()).WillRepeatedly(Return(false));
58   EXPECT_FALSE(tpm_status_->IsTpmEnabled());
59 }
60 
TEST_F(Tpm2StatusTest,IsEnabledNoRepeatedInitialization)61 TEST_F(Tpm2StatusTest, IsEnabledNoRepeatedInitialization) {
62   EXPECT_CALL(mock_tpm_state_, Initialize()).WillOnce(Return(TPM_RC_SUCCESS));
63   EXPECT_TRUE(tpm_status_->IsTpmEnabled());
64   EXPECT_TRUE(tpm_status_->IsTpmEnabled());
65 }
66 
TEST_F(Tpm2StatusTest,IsOwnedSuccess)67 TEST_F(Tpm2StatusTest, IsOwnedSuccess) {
68   EXPECT_CALL(mock_tpm_state_, Initialize())
69       .WillRepeatedly(Return(TPM_RC_SUCCESS));
70   EXPECT_CALL(mock_tpm_state_, IsOwned()).WillRepeatedly(Return(true));
71   EXPECT_TRUE(tpm_status_->IsTpmOwned());
72 }
73 
TEST_F(Tpm2StatusTest,IsOwnedFailure)74 TEST_F(Tpm2StatusTest, IsOwnedFailure) {
75   EXPECT_CALL(mock_tpm_state_, IsOwned()).WillRepeatedly(Return(false));
76   EXPECT_FALSE(tpm_status_->IsTpmOwned());
77 }
78 
TEST_F(Tpm2StatusTest,IsOwnedRepeatedInitializationOnFalse)79 TEST_F(Tpm2StatusTest, IsOwnedRepeatedInitializationOnFalse) {
80   EXPECT_CALL(mock_tpm_state_, Initialize())
81       .Times(2)
82       .WillRepeatedly(Return(TPM_RC_SUCCESS));
83   EXPECT_CALL(mock_tpm_state_, IsOwned()).WillOnce(Return(false));
84   EXPECT_FALSE(tpm_status_->IsTpmOwned());
85   EXPECT_CALL(mock_tpm_state_, IsOwned()).WillRepeatedly(Return(true));
86   EXPECT_TRUE(tpm_status_->IsTpmOwned());
87 }
88 
TEST_F(Tpm2StatusTest,IsOwnedNoRepeatedInitializationOnTrue)89 TEST_F(Tpm2StatusTest, IsOwnedNoRepeatedInitializationOnTrue) {
90   EXPECT_CALL(mock_tpm_state_, Initialize()).WillOnce(Return(TPM_RC_SUCCESS));
91   EXPECT_CALL(mock_tpm_state_, IsOwned()).WillRepeatedly(Return(true));
92   EXPECT_TRUE(tpm_status_->IsTpmOwned());
93   EXPECT_TRUE(tpm_status_->IsTpmOwned());
94 }
95 
TEST_F(Tpm2StatusTest,GetDictionaryAttackInfoInitializeFailure)96 TEST_F(Tpm2StatusTest, GetDictionaryAttackInfoInitializeFailure) {
97   EXPECT_CALL(mock_tpm_state_, Initialize())
98       .WillRepeatedly(Return(TPM_RC_FAILURE));
99   int count;
100   int threshold;
101   bool lockout;
102   int seconds_remaining;
103   EXPECT_FALSE(tpm_status_->GetDictionaryAttackInfo(
104       &count, &threshold, &lockout, &seconds_remaining));
105 }
106 
TEST_F(Tpm2StatusTest,GetDictionaryAttackInfoForwarding)107 TEST_F(Tpm2StatusTest, GetDictionaryAttackInfoForwarding) {
108   int lockout_count = 3;
109   int lockout_threshold = 16;
110   bool is_locked = true;
111   int lockout_interval = 3600;
112   EXPECT_CALL(mock_tpm_state_, GetLockoutCounter())
113       .WillRepeatedly(Return(lockout_count));
114   EXPECT_CALL(mock_tpm_state_, GetLockoutThreshold())
115       .WillRepeatedly(Return(lockout_threshold));
116   EXPECT_CALL(mock_tpm_state_, IsInLockout()).WillRepeatedly(Return(is_locked));
117   EXPECT_CALL(mock_tpm_state_, GetLockoutInterval())
118       .WillRepeatedly(Return(lockout_interval));
119   int count;
120   int threshold;
121   bool lockout;
122   int seconds_remaining;
123   EXPECT_TRUE(tpm_status_->GetDictionaryAttackInfo(&count, &threshold, &lockout,
124                                                    &seconds_remaining));
125   EXPECT_EQ(count, lockout_count);
126   EXPECT_EQ(threshold, lockout_threshold);
127   EXPECT_EQ(lockout, is_locked);
128   EXPECT_EQ(seconds_remaining, lockout_count * lockout_interval);
129 }
130 
TEST_F(Tpm2StatusTest,GetDictionaryAttackInfoAlwaysRefresh)131 TEST_F(Tpm2StatusTest, GetDictionaryAttackInfoAlwaysRefresh) {
132   EXPECT_CALL(mock_tpm_state_, Initialize())
133       .WillRepeatedly(Return(TPM_RC_SUCCESS));
134   int count;
135   int threshold;
136   bool lockout;
137   int seconds_remaining;
138   EXPECT_TRUE(tpm_status_->GetDictionaryAttackInfo(&count, &threshold, &lockout,
139                                                    &seconds_remaining));
140 }
141 
142 }  // namespace tpm_manager
143