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#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>
26
27#include "trunks/password_authorization_delegate.h"
28#include "trunks/trunks_export.h"
29
30namespace trunks {
31
32class AuthorizationDelegate;
33class MockBlobParser;
34class MockHmacSession;
35class MockPolicySession;
36class MockSessionManager;
37class MockTpm;
38class MockTpmState;
39class MockTpmUtility;
40class HmacSession;
41class PasswordAuthorizationDelegate;
42class PolicySession;
43class SessionManager;
44class 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...
58class TRUNKS_EXPORT TrunksFactoryForTest : public TrunksFactory {
59 public:
60  TrunksFactoryForTest();
61  ~TrunksFactoryForTest() override;
62
63  // TrunksFactory methods.
64  Tpm* GetTpm() const override;
65  scoped_ptr<TpmState> GetTpmState() const override;
66  scoped_ptr<TpmUtility> GetTpmUtility() const override;
67  scoped_ptr<AuthorizationDelegate> GetPasswordAuthorization(
68      const std::string& password) const override;
69  scoped_ptr<SessionManager> GetSessionManager() const override;
70  scoped_ptr<HmacSession> GetHmacSession() const override;
71  scoped_ptr<PolicySession> GetPolicySession() const override;
72  scoped_ptr<PolicySession> GetTrialSession() const override;
73  scoped_ptr<BlobParser> GetBlobParser() const override;
74
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
84  void set_tpm_utility(TpmUtility* tpm_utility) {
85    tpm_utility_ = tpm_utility;
86  }
87
88  void set_password_authorization_delegate(AuthorizationDelegate* delegate) {
89    password_authorization_delegate_ = delegate;
90  }
91
92  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;
102  }
103
104  void set_blob_parser(BlobParser* blob_parser) {
105    blob_parser_ = blob_parser;
106  }
107
108 private:
109  scoped_ptr<MockTpm> default_tpm_;
110  Tpm* tpm_;
111  scoped_ptr<MockTpmState> default_tpm_state_;
112  TpmState* tpm_state_;
113  scoped_ptr<MockTpmUtility> default_tpm_utility_;
114  TpmUtility* tpm_utility_;
115  scoped_ptr<PasswordAuthorizationDelegate> default_authorization_delegate_;
116  AuthorizationDelegate* password_authorization_delegate_;
117  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_;
123  scoped_ptr<MockBlobParser> default_blob_parser_;
124  BlobParser* blob_parser_;
125
126  DISALLOW_COPY_AND_ASSIGN(TrunksFactoryForTest);
127};
128
129}  // namespace trunks
130
131#endif  // TRUNKS_TRUNKS_FACTORY_FOR_TEST_H_
132