1 // Copyright 2012 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.
5 #ifndef COMPONENTS_OWNERSHIP_MOCK_OWNER_KEY_UTIL_H_
6 #define COMPONENTS_OWNERSHIP_MOCK_OWNER_KEY_UTIL_H_
13 #include "base/compiler_specific.h"
14 #include "components/ownership/owner_key_util.h"
15 #include "components/ownership/ownership_export.h"
23 // Implementation of OwnerKeyUtil which should be used only for
25 class OWNERSHIP_EXPORT MockOwnerKeyUtil : public OwnerKeyUtil {
29 MockOwnerKeyUtil(const MockOwnerKeyUtil&) = delete;
30 MockOwnerKeyUtil& operator=(const MockOwnerKeyUtil&) = delete;
32 // OwnerKeyUtil implementation:
33 scoped_refptr<PublicKey> ImportPublicKey() override;
34 crypto::ScopedSECKEYPrivateKey GenerateKeyPair(PK11SlotInfo* slot) override;
35 crypto::ScopedSECKEYPrivateKey FindPrivateKeyInSlot(
36 const std::vector<uint8_t>& key,
37 PK11SlotInfo* slot) override;
38 bool IsPublicKeyPresent() override;
40 // Clears the public and private keys.
43 // Configures the mock to return the given public key.
44 void SetPublicKey(const std::vector<uint8_t>& key);
46 // Sets the public key to use from the given private key, but doesn't
47 // configure the private key.
48 void SetPublicKeyFromPrivateKey(const crypto::RSAPrivateKey& key);
50 // Imports the private key into NSS, so it can be found later.
51 // Also extracts the public key and sets it for this mock object (equivalent
52 // to calling `SetPublicKeyFromPrivateKey`).
53 void ImportPrivateKeyAndSetPublicKey(
54 std::unique_ptr<crypto::RSAPrivateKey> key);
56 // Makes next `fail_times` number of calls to OwnerKeyUtil::GenerateKeyPair
58 void SimulateGenerateKeyFailure(int fail_times);
61 ~MockOwnerKeyUtil() override;
63 int generate_key_fail_times_ = 0;
64 std::vector<uint8_t> public_key_;
65 crypto::ScopedSECKEYPrivateKey private_key_;
68 } // namespace ownership
70 #endif // COMPONENTS_OWNERSHIP_MOCK_OWNER_KEY_UTIL_H_