summaryrefslogtreecommitdiff
path: root/trunks/trunks_factory_for_test.h
blob: 5b2b00d15666c152b17370767e5ba4cb8e627e23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//
// Copyright (C) 2014 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 TRUNKS_TRUNKS_FACTORY_FOR_TEST_H_
#define TRUNKS_TRUNKS_FACTORY_FOR_TEST_H_

#include "trunks/trunks_factory.h"

#include <memory>
#include <string>

#include <base/macros.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 {
 public:
  TrunksFactoryForTest();
  ~TrunksFactoryForTest() override;

  // TrunksFactory methods.
  Tpm* GetTpm() const override;
  std::unique_ptr<TpmState> GetTpmState() const override;
  std::unique_ptr<TpmUtility> GetTpmUtility() const override;
  std::unique_ptr<AuthorizationDelegate> GetPasswordAuthorization(
      const std::string& password) const override;
  std::unique_ptr<SessionManager> GetSessionManager() const override;
  std::unique_ptr<HmacSession> GetHmacSession() const override;
  std::unique_ptr<PolicySession> GetPolicySession() const override;
  std::unique_ptr<PolicySession> GetTrialSession() const override;
  std::unique_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_trial_session(PolicySession* trial_session) {
    trial_session_ = trial_session;
  }

  void set_blob_parser(BlobParser* blob_parser) { blob_parser_ = blob_parser; }

 private:
  std::unique_ptr<MockTpm> default_tpm_;
  Tpm* tpm_;
  std::unique_ptr<MockTpmState> default_tpm_state_;
  TpmState* tpm_state_;
  std::unique_ptr<MockTpmUtility> default_tpm_utility_;
  TpmUtility* tpm_utility_;
  std::unique_ptr<PasswordAuthorizationDelegate>
      default_authorization_delegate_;
  AuthorizationDelegate* password_authorization_delegate_;
  std::unique_ptr<MockSessionManager> default_session_manager_;
  SessionManager* session_manager_;
  std::unique_ptr<MockHmacSession> default_hmac_session_;
  HmacSession* hmac_session_;
  std::unique_ptr<MockPolicySession> default_policy_session_;
  PolicySession* policy_session_;
  std::unique_ptr<MockPolicySession> default_trial_session_;
  PolicySession* trial_session_;
  std::unique_ptr<MockBlobParser> default_blob_parser_;
  BlobParser* blob_parser_;

  DISALLOW_COPY_AND_ASSIGN(TrunksFactoryForTest);
};

}  // namespace trunks

#endif  // TRUNKS_TRUNKS_FACTORY_FOR_TEST_H_