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. 4 5 #ifndef CRYPTO_MOCK_APPLE_KEYCHAIN_H_ 6 #define CRYPTO_MOCK_APPLE_KEYCHAIN_H_ 7 8 #include <stddef.h> 9 #include <stdint.h> 10 11 #include <map> 12 #include <set> 13 #include <string> 14 #include <vector> 15 16 #include "base/compiler_specific.h" 17 #include "build/build_config.h" 18 #include "crypto/apple_keychain.h" 19 20 namespace crypto { 21 22 // Mock Keychain wrapper for testing code that interacts with the OS X 23 // Keychain. 24 // 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 28 // tests. 29 class CRYPTO_EXPORT MockAppleKeychain : public AppleKeychain { 30 public: 31 MockAppleKeychain(); 32 33 MockAppleKeychain(const MockAppleKeychain&) = delete; 34 MockAppleKeychain& operator=(const MockAppleKeychain&) = delete; 35 36 ~MockAppleKeychain() override; 37 38 // AppleKeychain implementation. 39 OSStatus FindGenericPassword(UInt32 serviceNameLength, 40 const char* serviceName, 41 UInt32 accountNameLength, 42 const char* accountName, 43 UInt32* passwordLength, 44 void** passwordData, 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; 54 55 // Returns the password that OSCrypt uses to generate its encryption key. 56 std::string GetEncryptionPassword() const; 57 58 #if !BUILDFLAG(IS_IOS) 59 OSStatus ItemDelete(SecKeychainItemRef itemRef) const override; 60 #endif // !BUILDFLAG(IS_IOS) 61 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 66 // user interactions. set_find_generic_result(OSStatus result)67 void set_find_generic_result(OSStatus result) { 68 find_generic_result_ = result; 69 } 70 71 // Returns the true if |AddGenericPassword()| was called. called_add_generic()72 bool called_add_generic() const { return called_add_generic_; } 73 74 // Returns the number of allocations - deallocations for password data. password_data_count()75 int password_data_count() const { return password_data_count_; } 76 77 private: 78 // Result code for the |FindGenericPassword()| method. 79 OSStatus find_generic_result_; 80 81 // Records whether |AddGenericPassword()| gets called. 82 mutable bool called_add_generic_; 83 84 // Tracks the allocations and frees of password data in |FindGenericPassword| 85 // and |ItemFreeContent|. 86 mutable int password_data_count_; 87 }; 88 89 } // namespace crypto 90 91 #endif // CRYPTO_MOCK_APPLE_KEYCHAIN_H_ 92