xref: /aosp_15_r20/external/cronet/crypto/mock_apple_keychain.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 #include "crypto/mock_apple_keychain.h"
6 
7 #include "base/check_op.h"
8 #include "base/metrics/histogram_macros.h"
9 #include "base/time/time.h"
10 
11 namespace {
12 
13 // Adds an entry to a local histogram to indicate that the Apple Keychain would
14 // have been accessed, if this class were not a mock of the Apple Keychain.
IncrementKeychainAccessHistogram()15 void IncrementKeychainAccessHistogram() {
16   // This local histogram is accessed by Telemetry to track the number of times
17   // the keychain is accessed, since keychain access is known to be synchronous
18   // and slow.
19   LOCAL_HISTOGRAM_BOOLEAN("OSX.Keychain.Access", true);
20 }
21 
22 }  // namespace
23 
24 namespace crypto {
25 
FindGenericPassword(UInt32 serviceNameLength,const char * serviceName,UInt32 accountNameLength,const char * accountName,UInt32 * passwordLength,void ** passwordData,AppleSecKeychainItemRef * itemRef) const26 OSStatus MockAppleKeychain::FindGenericPassword(
27     UInt32 serviceNameLength,
28     const char* serviceName,
29     UInt32 accountNameLength,
30     const char* accountName,
31     UInt32* passwordLength,
32     void** passwordData,
33     AppleSecKeychainItemRef* itemRef) const {
34   IncrementKeychainAccessHistogram();
35 
36   // When simulating |noErr|, return canned |passwordData| and
37   // |passwordLength|.  Otherwise, just return given code.
38   if (find_generic_result_ == noErr) {
39     static const char kPassword[] = "my_password";
40     DCHECK(passwordData);
41     // The function to free this data is mocked so the cast is fine.
42     *passwordData = const_cast<char*>(kPassword);
43     DCHECK(passwordLength);
44     *passwordLength = std::size(kPassword);
45     password_data_count_++;
46   }
47 
48   return find_generic_result_;
49 }
50 
ItemFreeContent(void * data) const51 OSStatus MockAppleKeychain::ItemFreeContent(void* data) const {
52   // No-op.
53   password_data_count_--;
54   return noErr;
55 }
56 
AddGenericPassword(UInt32 serviceNameLength,const char * serviceName,UInt32 accountNameLength,const char * accountName,UInt32 passwordLength,const void * passwordData,AppleSecKeychainItemRef * itemRef) const57 OSStatus MockAppleKeychain::AddGenericPassword(
58     UInt32 serviceNameLength,
59     const char* serviceName,
60     UInt32 accountNameLength,
61     const char* accountName,
62     UInt32 passwordLength,
63     const void* passwordData,
64     AppleSecKeychainItemRef* itemRef) const {
65   IncrementKeychainAccessHistogram();
66 
67   called_add_generic_ = true;
68 
69   DCHECK_GT(passwordLength, 0U);
70   DCHECK(passwordData);
71   return noErr;
72 }
73 
GetEncryptionPassword() const74 std::string MockAppleKeychain::GetEncryptionPassword() const {
75   IncrementKeychainAccessHistogram();
76   return "mock_password";
77 }
78 
79 }  // namespace crypto
80