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 CRYPTO_MOCK_APPLE_KEYCHAIN_H_
6 #define CRYPTO_MOCK_APPLE_KEYCHAIN_H_
16 #include "base/compiler_specific.h"
17 #include "build/build_config.h"
18 #include "crypto/apple_keychain.h"
22 // Mock Keychain wrapper for testing code that interacts with the OS X
25 // Note that "const" is pretty much meaningless for this class; the const-ness
26 // of AppleKeychain doesn't apply to the actual keychain data, so all of the
27 // Mock data is mutable; don't assume that it won't change over the life of
29 class CRYPTO_EXPORT MockAppleKeychain : public AppleKeychain {
33 MockAppleKeychain(const MockAppleKeychain&) = delete;
34 MockAppleKeychain& operator=(const MockAppleKeychain&) = delete;
36 ~MockAppleKeychain() override;
38 // AppleKeychain implementation.
39 OSStatus FindGenericPassword(UInt32 serviceNameLength,
40 const char* serviceName,
41 UInt32 accountNameLength,
42 const char* accountName,
43 UInt32* passwordLength,
45 AppleSecKeychainItemRef* itemRef) const override;
46 OSStatus ItemFreeContent(void* data) const override;
47 OSStatus AddGenericPassword(UInt32 serviceNameLength,
48 const char* serviceName,
49 UInt32 accountNameLength,
50 const char* accountName,
51 UInt32 passwordLength,
52 const void* passwordData,
53 AppleSecKeychainItemRef* itemRef) const override;
55 // Returns the password that OSCrypt uses to generate its encryption key.
56 std::string GetEncryptionPassword() const;
58 #if !BUILDFLAG(IS_IOS)
59 OSStatus ItemDelete(SecKeychainItemRef itemRef) const override;
60 #endif // !BUILDFLAG(IS_IOS)
62 // |FindGenericPassword()| can return different results depending on user
63 // interaction with the system Keychain. For mocking purposes we allow the
64 // user of this class to specify the result code of the
65 // |FindGenericPassword()| call so we can simulate the result of different
67 void set_find_generic_result(OSStatus result) {
68 find_generic_result_ = result;
71 // Returns the true if |AddGenericPassword()| was called.
72 bool called_add_generic() const { return called_add_generic_; }
74 // Returns the number of allocations - deallocations for password data.
75 int password_data_count() const { return password_data_count_; }
78 // Result code for the |FindGenericPassword()| method.
79 OSStatus find_generic_result_;
81 // Records whether |AddGenericPassword()| gets called.
82 mutable bool called_add_generic_;
84 // Tracks the allocations and frees of password data in |FindGenericPassword|
85 // and |ItemFreeContent|.
86 mutable int password_data_count_;
91 #endif // CRYPTO_MOCK_APPLE_KEYCHAIN_H_