blob: 476784c061b791b4a40138a5ae3054329b9f5cbb [file] [log] [blame]
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "trunks/trunks_factory.h"
#include <string>
#include <base/macros.h>
#include <base/memory/scoped_ptr.h>
#include "trunks/password_authorization_delegate.h"
#include "trunks/trunks_export.h"
namespace trunks {
class AuthorizationDelegate;
class MockBlobParser;
class MockHmacSession;
class MockPolicySession;
class MockSessionManager;
class MockTpm;
class MockTpmState;
class MockTpmUtility;
class HmacSession;
class PasswordAuthorizationDelegate;
class PolicySession;
class SessionManager;
class Tpm;
class TpmState;
class TpmUtility;
// A factory implementation for testing. Custom instances can be injected. If no
// instance has been injected, a default mock instance will be used. Objects for
// which ownership is passed to the caller are instantiated as forwarders which
// simply forward calls to the current instance set for the class.
// Example usage:
// TrunksFactoryForTest factory;
// MockTpmState mock_tpm_state;
// factory.set_tpm_state(mock_tpm_state);
// // Set expectations on mock_tpm_state...
class TRUNKS_EXPORT TrunksFactoryForTest : public TrunksFactory {
~TrunksFactoryForTest() override;
// TrunksFactory methods.
Tpm* GetTpm() const override;
scoped_ptr<TpmState> GetTpmState() const override;
scoped_ptr<TpmUtility> GetTpmUtility() const override;
scoped_ptr<AuthorizationDelegate> GetPasswordAuthorization(
const std::string& password) const override;
scoped_ptr<SessionManager> GetSessionManager() const override;
scoped_ptr<HmacSession> GetHmacSession() const override;
scoped_ptr<PolicySession> GetPolicySession() const override;
scoped_ptr<PolicySession> GetTrialSession() const override;
scoped_ptr<BlobParser> GetBlobParser() const override;
// Mutators to inject custom mocks.
void set_tpm(Tpm* tpm) {
tpm_ = tpm;
void set_tpm_state(TpmState* tpm_state) {
tpm_state_ = tpm_state;
void set_tpm_utility(TpmUtility* tpm_utility) {
tpm_utility_ = tpm_utility;
void set_password_authorization_delegate(AuthorizationDelegate* delegate) {
password_authorization_delegate_ = delegate;
void set_session_manager(SessionManager* session_manager) {
session_manager_ = session_manager;
void set_hmac_session(HmacSession* hmac_session) {
hmac_session_ = hmac_session;
void set_policy_session(PolicySession* policy_session) {
policy_session_ = policy_session;
void set_blob_parser(BlobParser* blob_parser) {
blob_parser_ = blob_parser;
scoped_ptr<MockTpm> default_tpm_;
Tpm* tpm_;
scoped_ptr<MockTpmState> default_tpm_state_;
TpmState* tpm_state_;
scoped_ptr<MockTpmUtility> default_tpm_utility_;
TpmUtility* tpm_utility_;
scoped_ptr<PasswordAuthorizationDelegate> default_authorization_delegate_;
AuthorizationDelegate* password_authorization_delegate_;
scoped_ptr<MockSessionManager> default_session_manager_;
SessionManager* session_manager_;
scoped_ptr<MockHmacSession> default_hmac_session_;
HmacSession* hmac_session_;
scoped_ptr<MockPolicySession> default_policy_session_;
PolicySession* policy_session_;
scoped_ptr<MockBlobParser> default_blob_parser_;
BlobParser* blob_parser_;
} // namespace trunks