Home | History | Annotate | Download | only in dbus
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROMEOS_DBUS_FAKE_CRYPTOHOME_CLIENT_H_
      6 #define CHROMEOS_DBUS_FAKE_CRYPTOHOME_CLIENT_H_
      7 
      8 #include <map>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/memory/weak_ptr.h"
     12 #include "chromeos/dbus/cryptohome_client.h"
     13 
     14 namespace chromeos {
     15 
     16 class CHROMEOS_EXPORT FakeCryptohomeClient : public CryptohomeClient {
     17  public:
     18   FakeCryptohomeClient();
     19   virtual ~FakeCryptohomeClient();
     20 
     21   virtual void Init(dbus::Bus* bus) OVERRIDE;
     22   virtual void SetAsyncCallStatusHandlers(
     23       const AsyncCallStatusHandler& handler,
     24       const AsyncCallStatusWithDataHandler& data_handler) OVERRIDE;
     25   virtual void ResetAsyncCallStatusHandlers() OVERRIDE;
     26   virtual void WaitForServiceToBeAvailable(
     27       const WaitForServiceToBeAvailableCallback& callback) OVERRIDE;
     28   virtual void IsMounted(const BoolDBusMethodCallback& callback) OVERRIDE;
     29   virtual bool Unmount(bool* success) OVERRIDE;
     30   virtual void AsyncCheckKey(const std::string& username,
     31                              const std::string& key,
     32                              const AsyncMethodCallback& callback) OVERRIDE;
     33   virtual void AsyncMigrateKey(const std::string& username,
     34                                const std::string& from_key,
     35                                const std::string& to_key,
     36                                const AsyncMethodCallback& callback) OVERRIDE;
     37   virtual void AsyncRemove(const std::string& username,
     38                            const AsyncMethodCallback& callback) OVERRIDE;
     39   virtual void GetSystemSalt(const GetSystemSaltCallback& callback) OVERRIDE;
     40   virtual void GetSanitizedUsername(
     41       const std::string& username,
     42       const StringDBusMethodCallback& callback) OVERRIDE;
     43   virtual std::string BlockingGetSanitizedUsername(
     44       const std::string& username) OVERRIDE;
     45   virtual void AsyncMount(const std::string& username,
     46                           const std::string& key,
     47                           int flags,
     48                           const AsyncMethodCallback& callback) OVERRIDE;
     49   virtual void AsyncAddKey(const std::string& username,
     50                            const std::string& key,
     51                            const std::string& new_key,
     52                            const AsyncMethodCallback& callback) OVERRIDE;
     53   virtual void AsyncMountGuest(const AsyncMethodCallback& callback) OVERRIDE;
     54   virtual void AsyncMountPublic(const std::string& public_mount_id,
     55                                 int flags,
     56                                 const AsyncMethodCallback& callback) OVERRIDE;
     57   virtual void TpmIsReady(const BoolDBusMethodCallback& callback) OVERRIDE;
     58   virtual void TpmIsEnabled(const BoolDBusMethodCallback& callback) OVERRIDE;
     59   virtual bool CallTpmIsEnabledAndBlock(bool* enabled) OVERRIDE;
     60   virtual void TpmGetPassword(
     61       const StringDBusMethodCallback& callback) OVERRIDE;
     62   virtual void TpmIsOwned(const BoolDBusMethodCallback& callback) OVERRIDE;
     63   virtual bool CallTpmIsOwnedAndBlock(bool* owned) OVERRIDE;
     64   virtual void TpmIsBeingOwned(const BoolDBusMethodCallback& callback) OVERRIDE;
     65   virtual bool CallTpmIsBeingOwnedAndBlock(bool* owning) OVERRIDE;
     66   virtual void TpmCanAttemptOwnership(
     67       const VoidDBusMethodCallback& callback) OVERRIDE;
     68   virtual void TpmClearStoredPassword(
     69       const VoidDBusMethodCallback& callback) OVERRIDE;
     70   virtual bool CallTpmClearStoredPasswordAndBlock() OVERRIDE;
     71   virtual void Pkcs11IsTpmTokenReady(
     72       const BoolDBusMethodCallback& callback) OVERRIDE;
     73   virtual void Pkcs11GetTpmTokenInfo(
     74       const Pkcs11GetTpmTokenInfoCallback& callback) OVERRIDE;
     75   virtual void Pkcs11GetTpmTokenInfoForUser(
     76       const std::string& username,
     77       const Pkcs11GetTpmTokenInfoCallback& callback) OVERRIDE;
     78   virtual bool InstallAttributesGet(const std::string& name,
     79                                     std::vector<uint8>* value,
     80                                     bool* successful) OVERRIDE;
     81   virtual bool InstallAttributesSet(const std::string& name,
     82                                     const std::vector<uint8>& value,
     83                                     bool* successful) OVERRIDE;
     84   virtual bool InstallAttributesFinalize(bool* successful) OVERRIDE;
     85   virtual void InstallAttributesIsReady(
     86       const BoolDBusMethodCallback& callback) OVERRIDE;
     87   virtual bool InstallAttributesIsInvalid(bool* is_invalid) OVERRIDE;
     88   virtual bool InstallAttributesIsFirstInstall(bool* is_first_install) OVERRIDE;
     89   virtual void TpmAttestationIsPrepared(
     90       const BoolDBusMethodCallback& callback) OVERRIDE;
     91   virtual void TpmAttestationIsEnrolled(
     92       const BoolDBusMethodCallback& callback) OVERRIDE;
     93   virtual void AsyncTpmAttestationCreateEnrollRequest(
     94       const AsyncMethodCallback& callback) OVERRIDE;
     95   virtual void AsyncTpmAttestationEnroll(
     96       const std::string& pca_response,
     97       const AsyncMethodCallback& callback) OVERRIDE;
     98   virtual void AsyncTpmAttestationCreateCertRequest(
     99       attestation::AttestationCertificateProfile certificate_profile,
    100       const std::string& user_id,
    101       const std::string& request_origin,
    102       const AsyncMethodCallback& callback) OVERRIDE;
    103   virtual void AsyncTpmAttestationFinishCertRequest(
    104       const std::string& pca_response,
    105       attestation::AttestationKeyType key_type,
    106       const std::string& user_id,
    107       const std::string& key_name,
    108       const AsyncMethodCallback& callback) OVERRIDE;
    109   virtual void TpmAttestationDoesKeyExist(
    110       attestation::AttestationKeyType key_type,
    111       const std::string& user_id,
    112       const std::string& key_name,
    113       const BoolDBusMethodCallback& callback) OVERRIDE;
    114   virtual void TpmAttestationGetCertificate(
    115       attestation::AttestationKeyType key_type,
    116       const std::string& user_id,
    117       const std::string& key_name,
    118       const DataMethodCallback& callback) OVERRIDE;
    119   virtual void TpmAttestationGetPublicKey(
    120       attestation::AttestationKeyType key_type,
    121       const std::string& user_id,
    122       const std::string& key_name,
    123       const DataMethodCallback& callback) OVERRIDE;
    124   virtual void TpmAttestationRegisterKey(
    125       attestation::AttestationKeyType key_type,
    126       const std::string& user_id,
    127       const std::string& key_name,
    128       const AsyncMethodCallback& callback) OVERRIDE;
    129   virtual void TpmAttestationSignEnterpriseChallenge(
    130       attestation::AttestationKeyType key_type,
    131       const std::string& user_id,
    132       const std::string& key_name,
    133       const std::string& domain,
    134       const std::string& device_id,
    135       attestation::AttestationChallengeOptions options,
    136       const std::string& challenge,
    137       const AsyncMethodCallback& callback) OVERRIDE;
    138   virtual void TpmAttestationSignSimpleChallenge(
    139       attestation::AttestationKeyType key_type,
    140       const std::string& user_id,
    141       const std::string& key_name,
    142       const std::string& challenge,
    143       const AsyncMethodCallback& callback) OVERRIDE;
    144   virtual void TpmAttestationGetKeyPayload(
    145       attestation::AttestationKeyType key_type,
    146       const std::string& user_id,
    147       const std::string& key_name,
    148       const DataMethodCallback& callback) OVERRIDE;
    149   virtual void TpmAttestationSetKeyPayload(
    150       attestation::AttestationKeyType key_type,
    151       const std::string& user_id,
    152       const std::string& key_name,
    153       const std::string& payload,
    154       const BoolDBusMethodCallback& callback) OVERRIDE;
    155   virtual void TpmAttestationDeleteKeys(
    156       attestation::AttestationKeyType key_type,
    157       const std::string& user_id,
    158       const std::string& key_prefix,
    159       const BoolDBusMethodCallback& callback) OVERRIDE;
    160 
    161   // Changes the behavior of WaitForServiceToBeAvailable(). This method runs
    162   // pending callbacks if is_available is true.
    163   void SetServiceIsAvailable(bool is_available);
    164 
    165   // Sets the unmount result of Unmount() call.
    166   void set_unmount_result(bool result) {
    167     unmount_result_= result;
    168   }
    169 
    170   // Sets the system salt which will be returned from GetSystemSalt(). By
    171   // default, GetSystemSalt() returns the value generated by
    172   // GetStubSystemSalt().
    173   void set_system_salt(const std::vector<uint8>& system_salt) {
    174     system_salt_ = system_salt;
    175   }
    176 
    177   // Returns the stub system salt as raw bytes. (not as a string encoded in the
    178   // format used by SystemSaltGetter::ConvertRawSaltToHexString()).
    179   static std::vector<uint8> GetStubSystemSalt();
    180 
    181  private:
    182   // Posts tasks which return fake results to the UI thread.
    183   void ReturnAsyncMethodResult(const AsyncMethodCallback& callback,
    184                                bool returns_data);
    185 
    186   // This method is used to implement ReturnAsyncMethodResult.
    187   void ReturnAsyncMethodResultInternal(const AsyncMethodCallback& callback,
    188                                        bool returns_data);
    189 
    190   bool service_is_available_;
    191   int async_call_id_;
    192   AsyncCallStatusHandler async_call_status_handler_;
    193   AsyncCallStatusWithDataHandler async_call_status_data_handler_;
    194   int tpm_is_ready_counter_;
    195   bool unmount_result_;
    196   std::vector<uint8> system_salt_;
    197 
    198   std::vector<WaitForServiceToBeAvailableCallback>
    199       pending_wait_for_service_to_be_available_callbacks_;
    200 
    201   // A stub store for InstallAttributes, mapping an attribute name to the
    202   // associated data blob. Used to implement InstallAttributesSet and -Get.
    203   std::map<std::string, std::vector<uint8> > install_attrs_;
    204   bool locked_;
    205   base::WeakPtrFactory<FakeCryptohomeClient> weak_ptr_factory_;
    206 
    207   DISALLOW_COPY_AND_ASSIGN(FakeCryptohomeClient);
    208 };
    209 
    210 }  // namespace chromeos
    211 
    212 #endif  // CHROMEOS_DBUS_FAKE_CRYPTOHOME_CLIENT_H_
    213