Home | History | Annotate | Download | only in trunks
      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 <memory>
     23 #include <string>
     24 
     25 #include <base/macros.h>
     26 
     27 #include "trunks/password_authorization_delegate.h"
     28 #include "trunks/trunks_export.h"
     29 
     30 namespace trunks {
     31 
     32 class AuthorizationDelegate;
     33 class MockBlobParser;
     34 class MockHmacSession;
     35 class MockPolicySession;
     36 class MockSessionManager;
     37 class MockTpm;
     38 class MockTpmState;
     39 class MockTpmUtility;
     40 class HmacSession;
     41 class PasswordAuthorizationDelegate;
     42 class PolicySession;
     43 class SessionManager;
     44 class Tpm;
     45 class TpmState;
     46 class 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...
     58 class TRUNKS_EXPORT TrunksFactoryForTest : public TrunksFactory {
     59  public:
     60   TrunksFactoryForTest();
     61   ~TrunksFactoryForTest() override;
     62 
     63   // TrunksFactory methods.
     64   Tpm* GetTpm() const override;
     65   std::unique_ptr<TpmState> GetTpmState() const override;
     66   std::unique_ptr<TpmUtility> GetTpmUtility() const override;
     67   std::unique_ptr<AuthorizationDelegate> GetPasswordAuthorization(
     68       const std::string& password) const override;
     69   std::unique_ptr<SessionManager> GetSessionManager() const override;
     70   std::unique_ptr<HmacSession> GetHmacSession() const override;
     71   std::unique_ptr<PolicySession> GetPolicySession() const override;
     72   std::unique_ptr<PolicySession> GetTrialSession() const override;
     73   std::unique_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_trial_session(PolicySession* trial_session) {
     99     trial_session_ = trial_session;
    100   }
    101 
    102   void set_blob_parser(BlobParser* blob_parser) { blob_parser_ = blob_parser; }
    103 
    104  private:
    105   std::unique_ptr<MockTpm> default_tpm_;
    106   Tpm* tpm_;
    107   std::unique_ptr<MockTpmState> default_tpm_state_;
    108   TpmState* tpm_state_;
    109   std::unique_ptr<MockTpmUtility> default_tpm_utility_;
    110   TpmUtility* tpm_utility_;
    111   std::unique_ptr<PasswordAuthorizationDelegate>
    112       default_authorization_delegate_;
    113   AuthorizationDelegate* password_authorization_delegate_;
    114   std::unique_ptr<MockSessionManager> default_session_manager_;
    115   SessionManager* session_manager_;
    116   std::unique_ptr<MockHmacSession> default_hmac_session_;
    117   HmacSession* hmac_session_;
    118   std::unique_ptr<MockPolicySession> default_policy_session_;
    119   PolicySession* policy_session_;
    120   std::unique_ptr<MockPolicySession> default_trial_session_;
    121   PolicySession* trial_session_;
    122   std::unique_ptr<MockBlobParser> default_blob_parser_;
    123   BlobParser* blob_parser_;
    124 
    125   DISALLOW_COPY_AND_ASSIGN(TrunksFactoryForTest);
    126 };
    127 
    128 }  // namespace trunks
    129 
    130 #endif  // TRUNKS_TRUNKS_FACTORY_FOR_TEST_H_
    131