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/hmac_session_impl.h" 18 19 #include <base/logging.h> 20 #include <gmock/gmock.h> 21 #include <gtest/gtest.h> 22 23 #include "trunks/mock_session_manager.h" 24 #include "trunks/mock_tpm.h" 25 #include "trunks/tpm_generated.h" 26 #include "trunks/trunks_factory_for_test.h" 27 28 using testing::_; 29 using testing::DoAll; 30 using testing::NiceMock; 31 using testing::Return; 32 using testing::SaveArg; 33 using testing::SetArgPointee; 34 35 namespace trunks { 36 37 class HmacSessionTest : public testing::Test { 38 public: 39 HmacSessionTest() {} 40 ~HmacSessionTest() override {} 41 42 void SetUp() override { 43 factory_.set_tpm(&mock_tpm_); 44 factory_.set_session_manager(&mock_session_manager_); 45 } 46 47 HmacAuthorizationDelegate* GetHmacDelegate(HmacSessionImpl* session) { 48 return &(session->hmac_delegate_); 49 } 50 51 protected: 52 TrunksFactoryForTest factory_; 53 NiceMock<MockTpm> mock_tpm_; 54 NiceMock<MockSessionManager> mock_session_manager_; 55 }; 56 57 TEST_F(HmacSessionTest, GetDelegateUninitialized) { 58 HmacSessionImpl session(factory_); 59 EXPECT_CALL(mock_session_manager_, GetSessionHandle()) 60 .WillRepeatedly(Return(kUninitializedHandle)); 61 EXPECT_EQ(nullptr, session.GetDelegate()); 62 } 63 64 TEST_F(HmacSessionTest, GetDelegateSuccess) { 65 HmacSessionImpl session(factory_); 66 EXPECT_EQ(GetHmacDelegate(&session), session.GetDelegate()); 67 } 68 69 TEST_F(HmacSessionTest, StartBoundSessionSuccess) { 70 HmacSessionImpl session(factory_); 71 TPM_HANDLE bind_entity = TPM_RH_FIRST; 72 EXPECT_CALL(mock_session_manager_, StartSession(TPM_SE_HMAC, bind_entity, 73 _, true, _)) 74 .WillOnce(Return(TPM_RC_SUCCESS)); 75 EXPECT_EQ(TPM_RC_SUCCESS, session.StartBoundSession(bind_entity, "", true)); 76 } 77 78 TEST_F(HmacSessionTest, StartBoundSessionFailure) { 79 HmacSessionImpl session(factory_); 80 TPM_HANDLE bind_entity = TPM_RH_FIRST; 81 EXPECT_CALL(mock_session_manager_, StartSession(TPM_SE_HMAC, bind_entity, 82 _, true, _)) 83 .WillOnce(Return(TPM_RC_FAILURE)); 84 EXPECT_EQ(TPM_RC_FAILURE, session.StartBoundSession(bind_entity, "", true)); 85 } 86 87 TEST_F(HmacSessionTest, EntityAuthorizationForwardingTest) { 88 HmacSessionImpl session(factory_); 89 std::string test_auth("test_auth"); 90 session.SetEntityAuthorizationValue(test_auth); 91 HmacAuthorizationDelegate* hmac_delegate = GetHmacDelegate(&session); 92 std::string entity_auth = hmac_delegate->entity_authorization_value(); 93 EXPECT_EQ(0, test_auth.compare(entity_auth)); 94 } 95 96 TEST_F(HmacSessionTest, FutureAuthorizationForwardingTest) { 97 HmacSessionImpl session(factory_); 98 std::string test_auth("test_auth"); 99 session.SetFutureAuthorizationValue(test_auth); 100 HmacAuthorizationDelegate* hmac_delegate = GetHmacDelegate(&session); 101 std::string entity_auth = hmac_delegate->future_authorization_value(); 102 EXPECT_EQ(0, test_auth.compare(entity_auth)); 103 } 104 105 } // namespace trunks 106