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(UInt32 serviceNameLength,
35 const char* serviceName,
36 UInt32 accountNameLength,
37 const char* accountName,
38 UInt32* passwordLength,
40 AppleSecKeychainItemRef* itemRef) const override;
41 OSStatus ItemFreeContent(void* data) const override;
42 OSStatus AddGenericPassword(UInt32 serviceNameLength,
43 const char* serviceName,
44 UInt32 accountNameLength,
45 const char* accountName,
46 UInt32 passwordLength,
47 const void* passwordData,
48 AppleSecKeychainItemRef* itemRef) const override;
50 // Returns the password that OSCrypt uses to generate its encryption key.
51 std::string GetEncryptionPassword() const;
54 OSStatus ItemDelete(SecKeychainItemRef itemRef) const override;
55 #endif // !defined(OS_IOS)
57 // |FindGenericPassword()| can return different results depending on user
58 // interaction with the system Keychain. For mocking purposes we allow the
59 // user of this class to specify the result code of the
60 // |FindGenericPassword()| call so we can simulate the result of different
62 void set_find_generic_result(OSStatus result) {
63 find_generic_result_ = result;
66 // Returns the true if |AddGenericPassword()| was called.
67 bool called_add_generic() const { return called_add_generic_; }
69 // Returns the number of allocations - deallocations for password data.
70 int password_data_count() const { return password_data_count_; }
73 // Result code for the |FindGenericPassword()| method.
74 OSStatus find_generic_result_;
76 // Records whether |AddGenericPassword()| gets called.
77 mutable bool called_add_generic_;
79 // Tracks the allocations and frees of password data in |FindGenericPassword|
80 // and |ItemFreeContent|.
81 mutable int password_data_count_;
83 DISALLOW_COPY_AND_ASSIGN(MockAppleKeychain);
88 #endif // CRYPTO_MOCK_APPLE_KEYCHAIN_H_