trunks_factory_for_test.h revision 4dc4629c415e7ca90ff146d7bb75b5646ecd8b17
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) { tpm_ = tpm; }
77
78  void set_tpm_state(TpmState* tpm_state) { tpm_state_ = tpm_state; }
79
80  void set_tpm_utility(TpmUtility* tpm_utility) { tpm_utility_ = tpm_utility; }
81
82  void set_password_authorization_delegate(AuthorizationDelegate* delegate) {
83    password_authorization_delegate_ = delegate;
84  }
85
86  void set_session_manager(SessionManager* session_manager) {
87    session_manager_ = session_manager;
88  }
89
90  void set_hmac_session(HmacSession* hmac_session) {
91    hmac_session_ = hmac_session;
92  }
93
94  void set_policy_session(PolicySession* policy_session) {
95    policy_session_ = policy_session;
96  }
97
98  void set_blob_parser(BlobParser* blob_parser) { blob_parser_ = blob_parser; }
99
100 private:
101  scoped_ptr<MockTpm> default_tpm_;
102  Tpm* tpm_;
103  scoped_ptr<MockTpmState> default_tpm_state_;
104  TpmState* tpm_state_;
105  scoped_ptr<MockTpmUtility> default_tpm_utility_;
106  TpmUtility* tpm_utility_;
107  scoped_ptr<PasswordAuthorizationDelegate> default_authorization_delegate_;
108  AuthorizationDelegate* password_authorization_delegate_;
109  scoped_ptr<MockSessionManager> default_session_manager_;
110  SessionManager* session_manager_;
111  scoped_ptr<MockHmacSession> default_hmac_session_;
112  HmacSession* hmac_session_;
113  scoped_ptr<MockPolicySession> default_policy_session_;
114  PolicySession* policy_session_;
115  scoped_ptr<MockBlobParser> default_blob_parser_;
116  BlobParser* blob_parser_;
117
118  DISALLOW_COPY_AND_ASSIGN(TrunksFactoryForTest);
119};
120
121}  // namespace trunks
122
123#endif  // TRUNKS_TRUNKS_FACTORY_FOR_TEST_H_
124