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