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