1 // Copyright (c) 2012 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.
5 #ifndef CRYPTO_MOCK_APPLE_KEYCHAIN_H_
6 #define CRYPTO_MOCK_APPLE_KEYCHAIN_H_
16 #include "base/compiler_specific.h"
17 #include "crypto/apple_keychain.h"
21 // Mock Keychain wrapper for testing code that interacts with the OS X
24 // Note that "const" is pretty much meaningless for this class; the const-ness
25 // of AppleKeychain doesn't apply to the actual keychain data, so all of the
26 // Mock data is mutable; don't assume that it won't change over the life of
28 class CRYPTO_EXPORT MockAppleKeychain : public AppleKeychain {
31 ~MockAppleKeychain() override;
33 // AppleKeychain implementation.
34 OSStatus FindGenericPassword(CFTypeRef keychainOrArray,
35 UInt32 serviceNameLength,
36 const char* serviceName,
37 UInt32 accountNameLength,
38 const char* accountName,
39 UInt32* passwordLength,
41 SecKeychainItemRef* itemRef) const override;
42 OSStatus ItemFreeContent(SecKeychainAttributeList* attrList,
43 void* data) const override;
44 OSStatus AddGenericPassword(SecKeychainRef keychain,
45 UInt32 serviceNameLength,
46 const char* serviceName,
47 UInt32 accountNameLength,
48 const char* accountName,
49 UInt32 passwordLength,
50 const void* passwordData,
51 SecKeychainItemRef* itemRef) const override;
53 // Returns the password that OSCrypt uses to generate its encryption key.
54 std::string GetEncryptionPassword() const;
57 OSStatus ItemDelete(SecKeychainItemRef itemRef) const override;
58 #endif // !defined(OS_IOS)
60 // |FindGenericPassword()| can return different results depending on user
61 // interaction with the system Keychain. For mocking purposes we allow the
62 // user of this class to specify the result code of the
63 // |FindGenericPassword()| call so we can simulate the result of different
65 void set_find_generic_result(OSStatus result) {
66 find_generic_result_ = result;
69 // Returns the true if |AddGenericPassword()| was called.
70 bool called_add_generic() const { return called_add_generic_; }
72 // Returns the number of allocations - deallocations for password data.
73 int password_data_count() const { return password_data_count_; }
76 // Result code for the |FindGenericPassword()| method.
77 OSStatus find_generic_result_;
79 // Records whether |AddGenericPassword()| gets called.
80 mutable bool called_add_generic_;
82 // Tracks the allocations and frees of password data in |FindGenericPassword|
83 // and |ItemFreeContent|.
84 mutable int password_data_count_;
86 DISALLOW_COPY_AND_ASSIGN(MockAppleKeychain);
91 #endif // CRYPTO_MOCK_APPLE_KEYCHAIN_H_