xref: /aosp_15_r20/external/cronet/crypto/mock_apple_keychain.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2012 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker #ifndef CRYPTO_MOCK_APPLE_KEYCHAIN_H_
6*6777b538SAndroid Build Coastguard Worker #define CRYPTO_MOCK_APPLE_KEYCHAIN_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include <stddef.h>
9*6777b538SAndroid Build Coastguard Worker #include <stdint.h>
10*6777b538SAndroid Build Coastguard Worker 
11*6777b538SAndroid Build Coastguard Worker #include <map>
12*6777b538SAndroid Build Coastguard Worker #include <set>
13*6777b538SAndroid Build Coastguard Worker #include <string>
14*6777b538SAndroid Build Coastguard Worker #include <vector>
15*6777b538SAndroid Build Coastguard Worker 
16*6777b538SAndroid Build Coastguard Worker #include "base/compiler_specific.h"
17*6777b538SAndroid Build Coastguard Worker #include "build/build_config.h"
18*6777b538SAndroid Build Coastguard Worker #include "crypto/apple_keychain.h"
19*6777b538SAndroid Build Coastguard Worker 
20*6777b538SAndroid Build Coastguard Worker namespace crypto {
21*6777b538SAndroid Build Coastguard Worker 
22*6777b538SAndroid Build Coastguard Worker // Mock Keychain wrapper for testing code that interacts with the OS X
23*6777b538SAndroid Build Coastguard Worker // Keychain.
24*6777b538SAndroid Build Coastguard Worker //
25*6777b538SAndroid Build Coastguard Worker // Note that "const" is pretty much meaningless for this class; the const-ness
26*6777b538SAndroid Build Coastguard Worker // of AppleKeychain doesn't apply to the actual keychain data, so all of the
27*6777b538SAndroid Build Coastguard Worker // Mock data is mutable; don't assume that it won't change over the life of
28*6777b538SAndroid Build Coastguard Worker // tests.
29*6777b538SAndroid Build Coastguard Worker class CRYPTO_EXPORT MockAppleKeychain : public AppleKeychain {
30*6777b538SAndroid Build Coastguard Worker  public:
31*6777b538SAndroid Build Coastguard Worker   MockAppleKeychain();
32*6777b538SAndroid Build Coastguard Worker 
33*6777b538SAndroid Build Coastguard Worker   MockAppleKeychain(const MockAppleKeychain&) = delete;
34*6777b538SAndroid Build Coastguard Worker   MockAppleKeychain& operator=(const MockAppleKeychain&) = delete;
35*6777b538SAndroid Build Coastguard Worker 
36*6777b538SAndroid Build Coastguard Worker   ~MockAppleKeychain() override;
37*6777b538SAndroid Build Coastguard Worker 
38*6777b538SAndroid Build Coastguard Worker   // AppleKeychain implementation.
39*6777b538SAndroid Build Coastguard Worker   OSStatus FindGenericPassword(UInt32 serviceNameLength,
40*6777b538SAndroid Build Coastguard Worker                                const char* serviceName,
41*6777b538SAndroid Build Coastguard Worker                                UInt32 accountNameLength,
42*6777b538SAndroid Build Coastguard Worker                                const char* accountName,
43*6777b538SAndroid Build Coastguard Worker                                UInt32* passwordLength,
44*6777b538SAndroid Build Coastguard Worker                                void** passwordData,
45*6777b538SAndroid Build Coastguard Worker                                AppleSecKeychainItemRef* itemRef) const override;
46*6777b538SAndroid Build Coastguard Worker   OSStatus ItemFreeContent(void* data) const override;
47*6777b538SAndroid Build Coastguard Worker   OSStatus AddGenericPassword(UInt32 serviceNameLength,
48*6777b538SAndroid Build Coastguard Worker                               const char* serviceName,
49*6777b538SAndroid Build Coastguard Worker                               UInt32 accountNameLength,
50*6777b538SAndroid Build Coastguard Worker                               const char* accountName,
51*6777b538SAndroid Build Coastguard Worker                               UInt32 passwordLength,
52*6777b538SAndroid Build Coastguard Worker                               const void* passwordData,
53*6777b538SAndroid Build Coastguard Worker                               AppleSecKeychainItemRef* itemRef) const override;
54*6777b538SAndroid Build Coastguard Worker 
55*6777b538SAndroid Build Coastguard Worker   // Returns the password that OSCrypt uses to generate its encryption key.
56*6777b538SAndroid Build Coastguard Worker   std::string GetEncryptionPassword() const;
57*6777b538SAndroid Build Coastguard Worker 
58*6777b538SAndroid Build Coastguard Worker #if !BUILDFLAG(IS_IOS)
59*6777b538SAndroid Build Coastguard Worker   OSStatus ItemDelete(SecKeychainItemRef itemRef) const override;
60*6777b538SAndroid Build Coastguard Worker #endif  // !BUILDFLAG(IS_IOS)
61*6777b538SAndroid Build Coastguard Worker 
62*6777b538SAndroid Build Coastguard Worker   // |FindGenericPassword()| can return different results depending on user
63*6777b538SAndroid Build Coastguard Worker   // interaction with the system Keychain.  For mocking purposes we allow the
64*6777b538SAndroid Build Coastguard Worker   // user of this class to specify the result code of the
65*6777b538SAndroid Build Coastguard Worker   // |FindGenericPassword()| call so we can simulate the result of different
66*6777b538SAndroid Build Coastguard Worker   // user interactions.
set_find_generic_result(OSStatus result)67*6777b538SAndroid Build Coastguard Worker   void set_find_generic_result(OSStatus result) {
68*6777b538SAndroid Build Coastguard Worker     find_generic_result_ = result;
69*6777b538SAndroid Build Coastguard Worker   }
70*6777b538SAndroid Build Coastguard Worker 
71*6777b538SAndroid Build Coastguard Worker   // Returns the true if |AddGenericPassword()| was called.
called_add_generic()72*6777b538SAndroid Build Coastguard Worker   bool called_add_generic() const { return called_add_generic_; }
73*6777b538SAndroid Build Coastguard Worker 
74*6777b538SAndroid Build Coastguard Worker   // Returns the number of allocations - deallocations for password data.
password_data_count()75*6777b538SAndroid Build Coastguard Worker   int password_data_count() const { return password_data_count_; }
76*6777b538SAndroid Build Coastguard Worker 
77*6777b538SAndroid Build Coastguard Worker  private:
78*6777b538SAndroid Build Coastguard Worker   // Result code for the |FindGenericPassword()| method.
79*6777b538SAndroid Build Coastguard Worker   OSStatus find_generic_result_;
80*6777b538SAndroid Build Coastguard Worker 
81*6777b538SAndroid Build Coastguard Worker   // Records whether |AddGenericPassword()| gets called.
82*6777b538SAndroid Build Coastguard Worker   mutable bool called_add_generic_;
83*6777b538SAndroid Build Coastguard Worker 
84*6777b538SAndroid Build Coastguard Worker   // Tracks the allocations and frees of password data in |FindGenericPassword|
85*6777b538SAndroid Build Coastguard Worker   // and |ItemFreeContent|.
86*6777b538SAndroid Build Coastguard Worker   mutable int password_data_count_;
87*6777b538SAndroid Build Coastguard Worker };
88*6777b538SAndroid Build Coastguard Worker 
89*6777b538SAndroid Build Coastguard Worker }  // namespace crypto
90*6777b538SAndroid Build Coastguard Worker 
91*6777b538SAndroid Build Coastguard Worker #endif  // CRYPTO_MOCK_APPLE_KEYCHAIN_H_
92