summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUtkarsh Sanghi <usanghi@google.com>2015-09-17 10:11:18 -0700
committerUtkarsh Sanghi <usanghi@google.com>2015-09-21 21:09:45 +0000
commit21637105867fb6b59a0864811f40d73aff944c19 (patch)
tree42ca11a89ebe0153639cad99134066c83f3e4cc4
parentc2460743423995ce27c5d8ede4c63653cdcc1a7b (diff)
downloadtpm_manager-21637105867fb6b59a0864811f40d73aff944c19.tar.gz
tpm_manager: Add MockOpensslCryptoUtil and depend on trunks_test library
BUG=None TEST=FEATURES=test USE=tpm2 emerge-rambi tpm_manager Change-Id: Iaf5a782974d2ec39a62831ac5f315e0c227921c7
-rw-r--r--server/mock_openssl_crypto_util.cc38
-rw-r--r--server/mock_openssl_crypto_util.h39
-rw-r--r--server/openssl_crypto_util.h10
-rw-r--r--server/openssl_crypto_util_impl.cc (renamed from server/openssl_crypto_util.cc)6
-rw-r--r--server/openssl_crypto_util_impl.h50
-rw-r--r--server/tpm2_initializer_impl.cc3
-rw-r--r--server/tpm_initializer_impl.h4
-rw-r--r--tpm_manager.gyp14
8 files changed, 142 insertions, 22 deletions
diff --git a/server/mock_openssl_crypto_util.cc b/server/mock_openssl_crypto_util.cc
new file mode 100644
index 0000000..e9a9d94
--- /dev/null
+++ b/server/mock_openssl_crypto_util.cc
@@ -0,0 +1,38 @@
+//
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include "tpm_manager/server/mock_openssl_crypto_util.h"
+
+using testing::_;
+using testing::Invoke;
+
+namespace tpm_manager {
+
+MockOpensslCryptoUtil::MockOpensslCryptoUtil() {
+ ON_CALL(*this, GetRandomBytes(_, _))
+ .WillByDefault(Invoke(this, &MockOpensslCryptoUtil::FakeGetRandomBytes));
+}
+
+MockOpensslCryptoUtil::~MockOpensslCryptoUtil() {}
+
+bool MockOpensslCryptoUtil::FakeGetRandomBytes(size_t num_bytes,
+ std::string* random_data) {
+ random_data->assign(num_bytes, 'a');
+ return true;
+}
+
+} // namespace tpm_manager
+
diff --git a/server/mock_openssl_crypto_util.h b/server/mock_openssl_crypto_util.h
new file mode 100644
index 0000000..5fd7da0
--- /dev/null
+++ b/server/mock_openssl_crypto_util.h
@@ -0,0 +1,39 @@
+//
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#ifndef TPM_MANAGER_SERVER_MOCK_OPENSSL_CRYPT_UTIL_H_
+#define TPM_MANAGER_SERVER_MOCK_OPENSSL_CRYPT_UTIL_H_
+
+#include <gmock/gmock.h>
+
+#include "tpm_manager/server/openssl_crypto_util.h"
+
+namespace tpm_manager {
+
+class MockOpensslCryptoUtil : public OpensslCryptoUtil {
+ public:
+ MockOpensslCryptoUtil();
+ ~MockOpensslCryptoUtil() override;
+
+ MOCK_METHOD2(GetRandomBytes, bool(size_t, std::string*));
+
+ private:
+ bool FakeGetRandomBytes(size_t num_bytes, std::string* random_data);
+};
+
+} // namespace tpm_manager
+
+#endif // TPM_MANAGER_SERVER_MOCK_OPENSSL_CRYPT_UTIL_H_
diff --git a/server/openssl_crypto_util.h b/server/openssl_crypto_util.h
index 4a26a1d..4371fe8 100644
--- a/server/openssl_crypto_util.h
+++ b/server/openssl_crypto_util.h
@@ -25,19 +25,15 @@
namespace tpm_manager {
// This class is used to provide a mockable interface for openssl calls.
-// Example usage:
-// OpensslCryptoUtil util;
-// std::string random_bytes;
-// bool result = util.GetRandomBytes(5, &random_bytes);
class OpensslCryptoUtil {
public:
OpensslCryptoUtil() = default;
- ~OpensslCryptoUtil() = default;
+ virtual ~OpensslCryptoUtil() = default;
// This method sets the out argument |random_data| to a string with at
// least |num_bytes| of random data and returns true on success.
- bool GetRandomBytes(size_t num_bytes,
- std::string* random_data) WARN_UNUSED_RESULT;
+ virtual bool GetRandomBytes(size_t num_bytes,
+ std::string* random_data) WARN_UNUSED_RESULT = 0;
private:
DISALLOW_COPY_AND_ASSIGN(OpensslCryptoUtil);
diff --git a/server/openssl_crypto_util.cc b/server/openssl_crypto_util_impl.cc
index 810b8dd..c5417cb 100644
--- a/server/openssl_crypto_util.cc
+++ b/server/openssl_crypto_util_impl.cc
@@ -14,7 +14,7 @@
// limitations under the License.
//
-#include "tpm_manager/server/openssl_crypto_util.h"
+#include "tpm_manager/server/openssl_crypto_util_impl.h"
#include <base/logging.h>
#include <base/stl_util.h>
@@ -22,8 +22,8 @@
namespace tpm_manager {
-bool OpensslCryptoUtil::GetRandomBytes(size_t num_bytes,
- std::string* random_data) {
+bool OpensslCryptoUtilImpl::GetRandomBytes(size_t num_bytes,
+ std::string* random_data) {
random_data->resize(num_bytes);
unsigned char* random_buffer =
reinterpret_cast<unsigned char*>(string_as_array(random_data));
diff --git a/server/openssl_crypto_util_impl.h b/server/openssl_crypto_util_impl.h
new file mode 100644
index 0000000..03ab63a
--- /dev/null
+++ b/server/openssl_crypto_util_impl.h
@@ -0,0 +1,50 @@
+//
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#ifndef TPM_MANAGER_SERVER_OPENSSL_CRYPTO_UTIL_IMPL_H_
+#define TPM_MANAGER_SERVER_OPENSSL_CRYPTO_UTIL_IMPL_H_
+
+#include <string>
+
+#include <base/compiler_specific.h>
+#include <base/macros.h>
+
+#include "tpm_manager/server/openssl_crypto_util.h"
+
+namespace tpm_manager {
+
+// OpensslCryptoUtilImpl is the default implementation of the
+// OpensslCryptoUtil interface.
+// Example usage:
+// OpensslCryptoUtilImpl util;
+// std::string random_bytes;
+// bool result = util.GetRandomBytes(5, &random_bytes);
+class OpensslCryptoUtilImpl : public OpensslCryptoUtil {
+ public:
+ OpensslCryptoUtilImpl() = default;
+ ~OpensslCryptoUtilImpl() override = default;
+
+ // OpensslCryptoUtil methods.
+ bool GetRandomBytes(size_t num_bytes,
+ std::string* random_data) override WARN_UNUSED_RESULT;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(OpensslCryptoUtilImpl);
+};
+
+} // namespace tpm_manager
+
+#endif // TPM_MANAGER_SERVER_OPENSSL_CRYPTO_UTIL_IMPL_H_
diff --git a/server/tpm2_initializer_impl.cc b/server/tpm2_initializer_impl.cc
index 2250156..1ae9b2b 100644
--- a/server/tpm2_initializer_impl.cc
+++ b/server/tpm2_initializer_impl.cc
@@ -23,6 +23,7 @@
#include <trunks/trunks_factory_impl.h>
#include "tpm_manager/common/local_data.pb.h"
+#include "tpm_manager/server/openssl_crypto_util_impl.h"
namespace {
const size_t kDefaultPasswordSize = 20;
@@ -33,7 +34,7 @@ namespace tpm_manager {
Tpm2InitializerImpl::Tpm2InitializerImpl(LocalDataStore* local_data_store,
TpmStatus* tpm_status)
: trunks_factory_(new trunks::TrunksFactoryImpl()),
- openssl_util_(new OpensslCryptoUtil()),
+ openssl_util_(new OpensslCryptoUtilImpl()),
local_data_store_(local_data_store),
tpm_status_(tpm_status) {}
diff --git a/server/tpm_initializer_impl.h b/server/tpm_initializer_impl.h
index c65ed66..b7b995c 100644
--- a/server/tpm_initializer_impl.h
+++ b/server/tpm_initializer_impl.h
@@ -23,7 +23,7 @@
#include <trousers/tss.h>
#include <trousers/trousers.h> // NOLINT(build/include_alpha)
-#include "tpm_manager/server/openssl_crypto_util.h"
+#include "tpm_manager/server/openssl_crypto_util_impl.h"
#include "tpm_manager/server/tpm_connection.h"
#include "tpm_manager/server/tpm_initializer.h"
@@ -79,7 +79,7 @@ class TpmInitializerImpl : public TpmInitializer {
// an error communicating with the Tpm.
bool TestTpmAuth(const std::string& owner_password);
- OpensslCryptoUtil openssl_util_;
+ OpensslCryptoUtilImpl openssl_util_;
TpmConnection tpm_connection_;
LocalDataStore* local_data_store_;
TpmStatus* tpm_status_;
diff --git a/tpm_manager.gyp b/tpm_manager.gyp
index aabcbab..3cbd567 100644
--- a/tpm_manager.gyp
+++ b/tpm_manager.gyp
@@ -79,7 +79,7 @@
'sources': [
'server/dbus_service.cc',
'server/local_data_store_impl.cc',
- 'server/openssl_crypto_util.cc',
+ 'server/openssl_crypto_util_impl.cc',
'server/tpm_manager_service.cc',
],
'conditions': [
@@ -147,6 +147,7 @@
'common/mock_tpm_manager_interface.cc',
'server/dbus_service_test.cc',
'server/mock_local_data_store.cc',
+ 'server/mock_openssl_crypto_util.cc',
'server/mock_tpm_initializer.cc',
'server/mock_tpm_status.cc',
'server/tpm_manager_service_test.cc',
@@ -155,17 +156,12 @@
'conditions': [
['USE_tpm2 == 1', {
'sources': [
- '../trunks/mock_blob_parser.cc',
- '../trunks/mock_hmac_session.cc',
- '../trunks/mock_policy_session.cc',
- '../trunks/mock_session_manager.cc',
- '../trunks/mock_tpm.cc',
- '../trunks/mock_tpm_state.cc',
- '../trunks/mock_tpm_utility.cc',
- '../trunks/trunks_factory_for_test.cc',
'server/tpm2_status_test.cc',
'server/tpm2_initializer_test.cc',
],
+ 'libraries': [
+ '-ltrunks_test',
+ ],
}],
],
'dependencies': [