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.
5 #include "crypto/mock_apple_keychain.h"
7 #include "base/check_op.h"
8 #include "base/metrics/histogram_macros.h"
9 #include "base/time/time.h"
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.
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
19 LOCAL_HISTOGRAM_BOOLEAN("OSX.Keychain.Access", true);
26 OSStatus MockAppleKeychain::FindGenericPassword(
27 UInt32 serviceNameLength,
28 const char* serviceName,
29 UInt32 accountNameLength,
30 const char* accountName,
31 UInt32* passwordLength,
33 AppleSecKeychainItemRef* itemRef) const {
34 IncrementKeychainAccessHistogram();
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";
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_++;
48 return find_generic_result_;
51 OSStatus MockAppleKeychain::ItemFreeContent(void* data) const {
53 password_data_count_--;
57 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();
67 called_add_generic_ = true;
69 DCHECK_GT(passwordLength, 0U);
74 std::string MockAppleKeychain::GetEncryptionPassword() const {
75 IncrementKeychainAccessHistogram();
76 return "mock_password";