blob: 190326e5dd0d625d13b5ccdbc5ea4ab3b03a88bf [file] [log] [blame]
Utkarsh Sanghibbef5df2015-09-09 19:34:06 -07001//
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//
Darren Krahnc364caa2014-09-24 14:50:27 -070016
17#ifndef TRUNKS_TRUNKS_FACTORY_FOR_TEST_H_
18#define TRUNKS_TRUNKS_FACTORY_FOR_TEST_H_
19
20#include "trunks/trunks_factory.h"
21
22#include <string>
23
24#include <base/macros.h>
25#include <base/memory/scoped_ptr.h>
Darren Krahnc364caa2014-09-24 14:50:27 -070026
Utkarsh Sanghi6318d442014-11-25 08:13:33 -080027#include "trunks/password_authorization_delegate.h"
Darren Krahnef87f3e2015-03-10 19:38:31 -070028#include "trunks/trunks_export.h"
Utkarsh Sanghi6318d442014-11-25 08:13:33 -080029
Darren Krahnc364caa2014-09-24 14:50:27 -070030namespace trunks {
31
32class AuthorizationDelegate;
Utkarsh Sanghi0ebbc582015-07-16 11:01:03 -070033class MockBlobParser;
Utkarsh Sanghiff7f2da2015-04-13 10:15:24 -070034class MockHmacSession;
35class MockPolicySession;
36class MockSessionManager;
Darren Krahnc364caa2014-09-24 14:50:27 -070037class MockTpm;
38class MockTpmState;
Darren Krahn03d54df2014-10-14 17:36:54 -070039class MockTpmUtility;
Utkarsh Sanghiff7f2da2015-04-13 10:15:24 -070040class HmacSession;
Utkarsh Sanghi6318d442014-11-25 08:13:33 -080041class PasswordAuthorizationDelegate;
Utkarsh Sanghiff7f2da2015-04-13 10:15:24 -070042class PolicySession;
43class SessionManager;
Darren Krahnc364caa2014-09-24 14:50:27 -070044class Tpm;
45class TpmState;
46class TpmUtility;
47
48// A factory implementation for testing. Custom instances can be injected. If no
49// instance has been injected, a default mock instance will be used. Objects for
50// which ownership is passed to the caller are instantiated as forwarders which
51// simply forward calls to the current instance set for the class.
52//
53// Example usage:
54// TrunksFactoryForTest factory;
55// MockTpmState mock_tpm_state;
56// factory.set_tpm_state(mock_tpm_state);
57// // Set expectations on mock_tpm_state...
Darren Krahnef87f3e2015-03-10 19:38:31 -070058class TRUNKS_EXPORT TrunksFactoryForTest : public TrunksFactory {
Darren Krahnc364caa2014-09-24 14:50:27 -070059 public:
60 TrunksFactoryForTest();
Darren Krahn295e8512015-03-17 10:20:13 -070061 ~TrunksFactoryForTest() override;
Darren Krahnc364caa2014-09-24 14:50:27 -070062
63 // TrunksFactory methods.
64 Tpm* GetTpm() const override;
65 scoped_ptr<TpmState> GetTpmState() const override;
Darren Krahn03d54df2014-10-14 17:36:54 -070066 scoped_ptr<TpmUtility> GetTpmUtility() const override;
Darren Krahnc364caa2014-09-24 14:50:27 -070067 scoped_ptr<AuthorizationDelegate> GetPasswordAuthorization(
68 const std::string& password) const override;
Utkarsh Sanghiff7f2da2015-04-13 10:15:24 -070069 scoped_ptr<SessionManager> GetSessionManager() const override;
70 scoped_ptr<HmacSession> GetHmacSession() const override;
71 scoped_ptr<PolicySession> GetPolicySession() const override;
Utkarsh Sanghibe411152015-05-15 14:16:19 -070072 scoped_ptr<PolicySession> GetTrialSession() const override;
Utkarsh Sanghi0ebbc582015-07-16 11:01:03 -070073 scoped_ptr<BlobParser> GetBlobParser() const override;
Darren Krahnc364caa2014-09-24 14:50:27 -070074
75 // Mutators to inject custom mocks.
76 void set_tpm(Tpm* tpm) {
77 tpm_ = tpm;
78 }
79
80 void set_tpm_state(TpmState* tpm_state) {
81 tpm_state_ = tpm_state;
82 }
83
Darren Krahn03d54df2014-10-14 17:36:54 -070084 void set_tpm_utility(TpmUtility* tpm_utility) {
85 tpm_utility_ = tpm_utility;
86 }
87
Darren Krahna19238f2014-10-15 14:31:26 -070088 void set_password_authorization_delegate(AuthorizationDelegate* delegate) {
89 password_authorization_delegate_ = delegate;
Darren Krahnc364caa2014-09-24 14:50:27 -070090 }
91
Utkarsh Sanghiff7f2da2015-04-13 10:15:24 -070092 void set_session_manager(SessionManager* session_manager) {
93 session_manager_ = session_manager;
94 }
95
96 void set_hmac_session(HmacSession* hmac_session) {
97 hmac_session_ = hmac_session;
98 }
99
100 void set_policy_session(PolicySession* policy_session) {
101 policy_session_ = policy_session;
Darren Krahn52e2a452014-10-17 11:14:13 -0700102 }
103
Utkarsh Sanghi0ebbc582015-07-16 11:01:03 -0700104 void set_blob_parser(BlobParser* blob_parser) {
105 blob_parser_ = blob_parser;
106 }
107
Darren Krahnc364caa2014-09-24 14:50:27 -0700108 private:
109 scoped_ptr<MockTpm> default_tpm_;
110 Tpm* tpm_;
111 scoped_ptr<MockTpmState> default_tpm_state_;
112 TpmState* tpm_state_;
Darren Krahn03d54df2014-10-14 17:36:54 -0700113 scoped_ptr<MockTpmUtility> default_tpm_utility_;
114 TpmUtility* tpm_utility_;
Utkarsh Sanghi6318d442014-11-25 08:13:33 -0800115 scoped_ptr<PasswordAuthorizationDelegate> default_authorization_delegate_;
Darren Krahna19238f2014-10-15 14:31:26 -0700116 AuthorizationDelegate* password_authorization_delegate_;
Utkarsh Sanghiff7f2da2015-04-13 10:15:24 -0700117 scoped_ptr<MockSessionManager> default_session_manager_;
118 SessionManager* session_manager_;
119 scoped_ptr<MockHmacSession> default_hmac_session_;
120 HmacSession* hmac_session_;
121 scoped_ptr<MockPolicySession> default_policy_session_;
122 PolicySession* policy_session_;
Utkarsh Sanghi0ebbc582015-07-16 11:01:03 -0700123 scoped_ptr<MockBlobParser> default_blob_parser_;
124 BlobParser* blob_parser_;
Darren Krahnc364caa2014-09-24 14:50:27 -0700125
126 DISALLOW_COPY_AND_ASSIGN(TrunksFactoryForTest);
127};
128
129} // namespace trunks
130
131#endif // TRUNKS_TRUNKS_FACTORY_FOR_TEST_H_