aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUtkarsh Sanghi <usanghi@chromium.org>2015-06-15 13:20:13 -0700
committerChromeOS Commit Bot <chromeos-commit-bot@chromium.org>2015-06-26 01:47:51 +0000
commit45e57ff54637d95252144544c3b051107473844e (patch)
tree6496791f7b875b0823d572512d17f7cf85bc5164
parente3d689daed51122570f236380fb463afd2ed85f4 (diff)
downloadtrunks-45e57ff54637d95252144544c3b051107473844e.tar.gz
trunks: Use ON_CALL for testing
This CL modifies trunks unit tests to use the ON_CALL pattern where ever possible BUG=chromium:495632 TEST=unit Change-Id: Ifd90570dc017bce5f23b19bc568cff729b277c84 Reviewed-on: https://chromium-review.googlesource.com/277690 Trybot-Ready: Utkarsh Sanghi <usanghi@chromium.org> Tested-by: Utkarsh Sanghi <usanghi@chromium.org> Reviewed-by: Jocelyn Bohr <bohr@chromium.org> Commit-Queue: Utkarsh Sanghi <usanghi@chromium.org>
-rw-r--r--hmac_session_test.cc2
-rw-r--r--mock_session_manager.cc8
-rw-r--r--mock_tpm.cc14
-rw-r--r--mock_tpm_state.cc17
-rw-r--r--policy_session_test.cc2
-rw-r--r--tpm_utility_test.cc37
6 files changed, 32 insertions, 48 deletions
diff --git a/hmac_session_test.cc b/hmac_session_test.cc
index 3d44549..c2de1d2 100644
--- a/hmac_session_test.cc
+++ b/hmac_session_test.cc
@@ -51,8 +51,6 @@ TEST_F(HmacSessionTest, GetDelegateUninitialized) {
TEST_F(HmacSessionTest, GetDelegateSuccess) {
HmacSessionImpl session(factory_);
- EXPECT_CALL(mock_session_manager_, GetSessionHandle())
- .WillRepeatedly(Return(TPM_RH_FIRST));
EXPECT_EQ(GetHmacDelegate(&session), session.GetDelegate());
}
diff --git a/mock_session_manager.cc b/mock_session_manager.cc
index b36ff99..d9e6329 100644
--- a/mock_session_manager.cc
+++ b/mock_session_manager.cc
@@ -4,9 +4,15 @@
#include "trunks/mock_session_manager.h"
+#include "trunks/tpm_generated.h"
+
namespace trunks {
-MockSessionManager::MockSessionManager() {}
+MockSessionManager::MockSessionManager() {
+ ON_CALL(*this, GetSessionHandle())
+ .WillByDefault(testing::Return(TPM_RH_FIRST));
+}
+
MockSessionManager::~MockSessionManager() {}
} // namespace trunks
diff --git a/mock_tpm.cc b/mock_tpm.cc
index e5ef83b..5035504 100644
--- a/mock_tpm.cc
+++ b/mock_tpm.cc
@@ -4,9 +4,21 @@
#include "trunks/mock_tpm.h"
+#include "trunks/tpm_utility.h"
+
+using testing::_;
+using testing::DoAll;
+using testing::Return;
+using testing::SetArgPointee;
+
namespace trunks {
-MockTpm::MockTpm() : Tpm(nullptr) {}
+MockTpm::MockTpm() : Tpm(nullptr) {
+ ON_CALL(*this, PCR_AllocateSync(_, _, _, _, _, _, _, _))
+ .WillByDefault(DoAll(SetArgPointee<3>(YES),
+ Return(TPM_RC_SUCCESS)));
+}
+
MockTpm::~MockTpm() {}
void MockTpm::StartAuthSession(
diff --git a/mock_tpm_state.cc b/mock_tpm_state.cc
index 8d9d275..e02901a 100644
--- a/mock_tpm_state.cc
+++ b/mock_tpm_state.cc
@@ -6,17 +6,20 @@
#include <gmock/gmock.h>
+using testing::Return;
+
namespace trunks {
MockTpmState::MockTpmState() {
- ON_CALL(*this, IsOwnerPasswordSet()).WillByDefault(testing::Return(true));
- ON_CALL(*this, IsEndorsementPasswordSet())
- .WillByDefault(testing::Return(true));
- ON_CALL(*this, IsLockoutPasswordSet()).WillByDefault(testing::Return(true));
- ON_CALL(*this, WasShutdownOrderly()).WillByDefault(testing::Return(true));
- ON_CALL(*this, IsRSASupported()).WillByDefault(testing::Return(true));
- ON_CALL(*this, IsECCSupported()).WillByDefault(testing::Return(true));
+ ON_CALL(*this, IsOwnerPasswordSet()).WillByDefault(Return(true));
+ ON_CALL(*this, IsEndorsementPasswordSet()).WillByDefault(Return(true));
+ ON_CALL(*this, IsLockoutPasswordSet()).WillByDefault(Return(true));
+ ON_CALL(*this, WasShutdownOrderly()).WillByDefault(Return(true));
+ ON_CALL(*this, IsRSASupported()).WillByDefault(Return(true));
+ ON_CALL(*this, IsECCSupported()).WillByDefault(Return(true));
+ ON_CALL(*this, IsPlatformHierarchyEnabled()).WillByDefault(Return(true));
}
+
MockTpmState::~MockTpmState() {}
} // namespace trunks
diff --git a/policy_session_test.cc b/policy_session_test.cc
index 75c29ee..1f580a4 100644
--- a/policy_session_test.cc
+++ b/policy_session_test.cc
@@ -51,8 +51,6 @@ TEST_F(PolicySessionTest, GetDelegateUninitialized) {
TEST_F(PolicySessionTest, GetDelegateSuccess) {
PolicySessionImpl session(factory_);
- EXPECT_CALL(mock_session_manager_, GetSessionHandle())
- .WillRepeatedly(Return(TPM_RH_FIRST));
EXPECT_EQ(GetHmacDelegate(&session), session.GetDelegate());
}
diff --git a/tpm_utility_test.cc b/tpm_utility_test.cc
index 9f88446..e0d8119 100644
--- a/tpm_utility_test.cc
+++ b/tpm_utility_test.cc
@@ -102,8 +102,6 @@ TEST_F(TpmUtilityTest, StartupSelfTestFailure) {
}
TEST_F(TpmUtilityTest, ClearSuccess) {
- EXPECT_CALL(mock_tpm_, ClearSync(_, _, _))
- .WillOnce(Return(TPM_RC_SUCCESS));
EXPECT_EQ(TPM_RC_SUCCESS, utility_.Clear());
}
@@ -131,9 +129,6 @@ TEST_F(TpmUtilityTest, InitializeTpmAlreadyInit) {
}
TEST_F(TpmUtilityTest, InitializeTpmSuccess) {
- // Setup a hierarchy that needs to be disabled.
- EXPECT_CALL(mock_tpm_state_, IsPlatformHierarchyEnabled())
- .WillOnce(Return(true));
EXPECT_CALL(mock_tpm_, PCR_AllocateSync(_, _, _, _, _, _, _, _))
.WillOnce(DoAll(SetArgPointee<3>(YES),
Return(TPM_RC_SUCCESS)));
@@ -141,9 +136,6 @@ TEST_F(TpmUtilityTest, InitializeTpmSuccess) {
}
TEST_F(TpmUtilityTest, InitializeTpmBadAuth) {
- // Setup a hierarchy that needs to be disabled.
- EXPECT_CALL(mock_tpm_state_, IsPlatformHierarchyEnabled())
- .WillOnce(Return(true));
// Reject attempts to set platform auth.
EXPECT_CALL(mock_tpm_, HierarchyChangeAuthSync(TPM_RH_PLATFORM, _, _, _))
.WillRepeatedly(Return(TPM_RC_FAILURE));
@@ -151,9 +143,6 @@ TEST_F(TpmUtilityTest, InitializeTpmBadAuth) {
}
TEST_F(TpmUtilityTest, InitializeTpmDisablePHFails) {
- // Setup a hierarchy that needs to be disabled.
- EXPECT_CALL(mock_tpm_state_, IsPlatformHierarchyEnabled())
- .WillOnce(Return(true));
EXPECT_CALL(mock_tpm_, PCR_AllocateSync(_, _, _, _, _, _, _, _))
.WillOnce(DoAll(SetArgPointee<3>(YES),
Return(TPM_RC_SUCCESS)));
@@ -203,12 +192,6 @@ TEST_F(TpmUtilityTest, TakeOwnershipSuccess) {
}
TEST_F(TpmUtilityTest, TakeOwnershipOwnershipDone) {
- EXPECT_CALL(mock_tpm_state_, IsOwnerPasswordSet())
- .WillRepeatedly(Return(true));
- EXPECT_CALL(mock_tpm_state_, IsEndorsementPasswordSet())
- .WillRepeatedly(Return(true));
- EXPECT_CALL(mock_tpm_state_, IsLockoutPasswordSet())
- .WillRepeatedly(Return(true));
EXPECT_EQ(TPM_RC_SUCCESS, utility_.TakeOwnership("owner",
"endorsement",
"lockout"));
@@ -233,8 +216,6 @@ TEST_F(TpmUtilityTest, TakeOwnershipFailure) {
TEST_F(TpmUtilityTest, ChangeOwnerPasswordEndorsementDone) {
EXPECT_CALL(mock_tpm_state_, IsOwnerPasswordSet())
.WillRepeatedly(Return(false));
- EXPECT_CALL(mock_tpm_state_, IsEndorsementPasswordSet())
- .WillRepeatedly(Return(true));
EXPECT_CALL(mock_tpm_state_, IsLockoutPasswordSet())
.WillRepeatedly(Return(false));
EXPECT_EQ(TPM_RC_SUCCESS, utility_.TakeOwnership("owner",
@@ -247,8 +228,6 @@ TEST_F(TpmUtilityTest, ChangeOwnerPasswordLockoutDone) {
.WillRepeatedly(Return(false));
EXPECT_CALL(mock_tpm_state_, IsEndorsementPasswordSet())
.WillRepeatedly(Return(false));
- EXPECT_CALL(mock_tpm_state_, IsLockoutPasswordSet())
- .WillRepeatedly(Return(true));
EXPECT_EQ(TPM_RC_SUCCESS, utility_.TakeOwnership("owner",
"endorsement",
"lockout"));
@@ -257,16 +236,12 @@ TEST_F(TpmUtilityTest, ChangeOwnerPasswordLockoutDone) {
TEST_F(TpmUtilityTest, ChangeOwnerPasswordEndorsementLockoutDone) {
EXPECT_CALL(mock_tpm_state_, IsOwnerPasswordSet())
.WillRepeatedly(Return(false));
- EXPECT_CALL(mock_tpm_state_, IsEndorsementPasswordSet())
- .WillRepeatedly(Return(true));
- EXPECT_CALL(mock_tpm_state_, IsLockoutPasswordSet())
- .WillRepeatedly(Return(true));
EXPECT_EQ(TPM_RC_SUCCESS, utility_.TakeOwnership("owner",
"endorsement",
"lockout"));
}
-TEST_F(TpmUtilityTest, ChangeOwnerPasswordEndoresmentFail) {
+TEST_F(TpmUtilityTest, ChangeOwnerPasswordEndorsementFail) {
EXPECT_CALL(mock_tpm_state_, IsOwnerPasswordSet())
.WillRepeatedly(Return(false));
EXPECT_CALL(mock_tpm_state_, IsEndorsementPasswordSet())
@@ -298,8 +273,6 @@ TEST_F(TpmUtilityTest, ChangeOwnerPasswordLockoutFailure) {
TEST_F(TpmUtilityTest, StirRandomSuccess) {
std::string entropy_data("large test data", 100);
- EXPECT_CALL(mock_tpm_, StirRandomSync(_, &mock_authorization_delegate_))
- .WillOnce(Return(TPM_RC_SUCCESS));
EXPECT_EQ(TPM_RC_SUCCESS,
utility_.StirRandom(entropy_data, &mock_authorization_delegate_));
}
@@ -401,8 +374,6 @@ TEST_F(TpmUtilityTest, ReadPCRFail) {
TEST_F(TpmUtilityTest, ReadPCRBadReturn) {
std::string pcr_value;
- EXPECT_CALL(mock_tpm_, PCR_ReadSync(_, _, _, _, _))
- .WillOnce(Return(TPM_RC_SUCCESS));
EXPECT_EQ(TPM_RC_FAILURE, utility_.ReadPCR(1, &pcr_value));
}
@@ -1189,8 +1160,6 @@ TEST_F(TpmUtilityTest, ImportRSAKeySuccessWithNoBlob) {
std::string modulus(256, 'a');
std::string prime_factor(128, 'b');
std::string password;
- EXPECT_CALL(mock_tpm_, ImportSync(_, _, _, _, _, _, _, _, _))
- .WillOnce(Return(TPM_RC_SUCCESS));
EXPECT_EQ(TPM_RC_SUCCESS, utility_.ImportRSAKey(
TpmUtility::AsymmetricKeyUsage::kDecryptKey,
modulus,
@@ -1595,8 +1564,6 @@ TEST_F(TpmUtilityTest, SetKnownPasswordSuccess) {
}
TEST_F(TpmUtilityTest, SetKnownPasswordOwnershipDone) {
- EXPECT_CALL(mock_tpm_state_, IsOwnerPasswordSet())
- .WillOnce(Return(true));
EXPECT_EQ(TPM_RC_SUCCESS, SetKnownOwnerPassword("password"));
}
@@ -1667,9 +1634,9 @@ TEST_F(TpmUtilityTest, SaltingKeySuccess) {
TEST_F(TpmUtilityTest, SaltingKeyConsistency) {
EXPECT_CALL(mock_tpm_, ReadPublicSync(_, _, _, _, _, _))
.WillOnce(Return(TPM_RC_SUCCESS));
- TPM_HANDLE test_handle = 42;
EXPECT_CALL(mock_tpm_, ReadPublicSync(kSaltingKey, _, _, _, _, _))
.WillOnce(Return(TPM_RC_FAILURE));
+ TPM_HANDLE test_handle = 42;
EXPECT_CALL(mock_tpm_, LoadSync(_, _, _, _, _, _, _))
.WillRepeatedly(DoAll(SetArgPointee<4>(test_handle),
Return(TPM_RC_SUCCESS)));