scoped_test_system_nss_key_slot.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2014 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. #ifndef CRYPTO_SCOPED_TEST_SYSTEM_NSS_KEY_SLOT_H_
  5. #define CRYPTO_SCOPED_TEST_SYSTEM_NSS_KEY_SLOT_H_
  6. #include <memory>
  7. #include "crypto/crypto_export.h"
  8. // Forward declaration, from <pk11pub.h>
  9. typedef struct PK11SlotInfoStr PK11SlotInfo;
  10. namespace crypto {
  11. class ScopedTestNSSDB;
  12. // Helper object to override the behavior of `crypto::GetSystemNSSKeySlot()`
  13. // to return a slot from a temporary directory (i.e. bypassing the TPM).
  14. // This object MUST be created before any call to
  15. // `crypto::InitializeTPMTokenAndSystemSlot()`. Note: As noted in
  16. // `crypto::ResetSystemSlotForTesting()`, once a fake slot has been configured
  17. // for a process, it cannot be undone. As such, only one instance of this object
  18. // must be created for a process.
  19. class CRYPTO_EXPORT ScopedTestSystemNSSKeySlot {
  20. public:
  21. // If `simulate_token_loader` is false, this class only prepares a software
  22. // system slot, which will be made available through `GetSystemNSSKeySlot`
  23. // when something else (presumably the TpmTokenLoader) calls
  24. // `crypto::FinishInitializingTPMTokenAndSystemSlot`. Setting
  25. // `simulate_token_loader` to true emulates the "initialization finished"
  26. // signal immediately (e.g. in unit tests).
  27. ScopedTestSystemNSSKeySlot(bool simulate_token_loader);
  28. ScopedTestSystemNSSKeySlot(const ScopedTestSystemNSSKeySlot&) = delete;
  29. ScopedTestSystemNSSKeySlot& operator=(const ScopedTestSystemNSSKeySlot&) =
  30. delete;
  31. ~ScopedTestSystemNSSKeySlot();
  32. bool ConstructedSuccessfully() const;
  33. PK11SlotInfo* slot() const;
  34. private:
  35. std::unique_ptr<ScopedTestNSSDB> test_db_;
  36. };
  37. } // namespace crypto
  38. #endif // CRYPTO_SCOPED_TEST_SYSTEM_NSS_KEY_SLOT_H_