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() : factory_(new trunks::TrunksFactoryForTest()) {} 36 virtual ~Tpm2StatusTest() = default; 37 38 void SetUp() { 39 factory_->set_tpm_state(&mock_tpm_state_); 40 tpm_status_.reset(new Tpm2StatusImpl(factory_.get())); 41 } 42 43 protected: 44 NiceMock<trunks::MockTpmState> mock_tpm_state_; 45 std::unique_ptr<trunks::TrunksFactoryForTest> factory_; 46 std::unique_ptr<TpmStatus> tpm_status_; 47 }; 48 49 50 TEST_F(Tpm2StatusTest, IsEnabledSuccess) { 51 EXPECT_CALL(mock_tpm_state_, Initialize()) 52 .WillRepeatedly(Return(TPM_RC_SUCCESS)); 53 EXPECT_CALL(mock_tpm_state_, IsEnabled()) 54 .WillRepeatedly(Return(true)); 55 EXPECT_TRUE(tpm_status_->IsTpmEnabled()); 56 } 57 58 TEST_F(Tpm2StatusTest, IsEnabledFailure) { 59 EXPECT_CALL(mock_tpm_state_, IsEnabled()) 60 .WillRepeatedly(Return(false)); 61 EXPECT_FALSE(tpm_status_->IsTpmEnabled()); 62 } 63 64 TEST_F(Tpm2StatusTest, IsEnabledNoRepeatedInitialization) { 65 EXPECT_CALL(mock_tpm_state_, Initialize()) 66 .WillOnce(Return(TPM_RC_SUCCESS)); 67 EXPECT_TRUE(tpm_status_->IsTpmEnabled()); 68 EXPECT_TRUE(tpm_status_->IsTpmEnabled()); 69 } 70 71 TEST_F(Tpm2StatusTest, IsOwnedSuccess) { 72 EXPECT_CALL(mock_tpm_state_, Initialize()) 73 .WillRepeatedly(Return(TPM_RC_SUCCESS)); 74 EXPECT_CALL(mock_tpm_state_, IsOwned()) 75 .WillRepeatedly(Return(true)); 76 EXPECT_TRUE(tpm_status_->IsTpmOwned()); 77 } 78 79 TEST_F(Tpm2StatusTest, IsOwnedFailure) { 80 EXPECT_CALL(mock_tpm_state_, IsOwned()) 81 .WillRepeatedly(Return(false)); 82 EXPECT_FALSE(tpm_status_->IsTpmOwned()); 83 } 84 85 TEST_F(Tpm2StatusTest, IsOwnedRepeatedInitializationOnFalse) { 86 EXPECT_CALL(mock_tpm_state_, Initialize()) 87 .Times(2) 88 .WillRepeatedly(Return(TPM_RC_SUCCESS)); 89 EXPECT_CALL(mock_tpm_state_, IsOwned()) 90 .WillOnce(Return(false)); 91 EXPECT_FALSE(tpm_status_->IsTpmOwned()); 92 EXPECT_CALL(mock_tpm_state_, IsOwned()) 93 .WillRepeatedly(Return(true)); 94 EXPECT_TRUE(tpm_status_->IsTpmOwned()); 95 } 96 97 TEST_F(Tpm2StatusTest, IsOwnedNoRepeatedInitializationOnTrue) { 98 EXPECT_CALL(mock_tpm_state_, Initialize()) 99 .WillOnce(Return(TPM_RC_SUCCESS)); 100 EXPECT_CALL(mock_tpm_state_, IsOwned()) 101 .WillRepeatedly(Return(true)); 102 EXPECT_TRUE(tpm_status_->IsTpmOwned()); 103 EXPECT_TRUE(tpm_status_->IsTpmOwned()); 104 } 105 106 TEST_F(Tpm2StatusTest, GetDictionaryAttackInfoInitializeFailure) { 107 EXPECT_CALL(mock_tpm_state_, Initialize()) 108 .WillRepeatedly(Return(TPM_RC_FAILURE)); 109 int count; 110 int threshold; 111 bool lockout; 112 int seconds_remaining; 113 EXPECT_FALSE(tpm_status_->GetDictionaryAttackInfo(&count, 114 &threshold, 115 &lockout, 116 &seconds_remaining)); 117 } 118 119 TEST_F(Tpm2StatusTest, GetDictionaryAttackInfoForwarding) { 120 int lockout_count = 3; 121 int lockout_threshold = 16; 122 bool is_locked = true; 123 int lockout_interval = 3600; 124 EXPECT_CALL(mock_tpm_state_, GetLockoutCounter()) 125 .WillRepeatedly(Return(lockout_count)); 126 EXPECT_CALL(mock_tpm_state_, GetLockoutThreshold()) 127 .WillRepeatedly(Return(lockout_threshold)); 128 EXPECT_CALL(mock_tpm_state_, IsInLockout()) 129 .WillRepeatedly(Return(is_locked)); 130 EXPECT_CALL(mock_tpm_state_, GetLockoutInterval()) 131 .WillRepeatedly(Return(lockout_interval)); 132 int count; 133 int threshold; 134 bool lockout; 135 int seconds_remaining; 136 EXPECT_TRUE(tpm_status_->GetDictionaryAttackInfo(&count, 137 &threshold, 138 &lockout, 139 &seconds_remaining)); 140 EXPECT_EQ(count, lockout_count); 141 EXPECT_EQ(threshold, lockout_threshold); 142 EXPECT_EQ(lockout, is_locked); 143 EXPECT_EQ(seconds_remaining, lockout_count * lockout_interval); 144 } 145 146 TEST_F(Tpm2StatusTest, GetDictionaryAttackInfoAlwaysRefresh) { 147 EXPECT_CALL(mock_tpm_state_, Initialize()) 148 .WillRepeatedly(Return(TPM_RC_SUCCESS)); 149 int count; 150 int threshold; 151 bool lockout; 152 int seconds_remaining; 153 EXPECT_TRUE(tpm_status_->GetDictionaryAttackInfo(&count, 154 &threshold, 155 &lockout, 156 &seconds_remaining)); 157 } 158 159 } // namespace tpm_manager 160