Roll src/third_party/WebKit 75a2fa9:2546356 (svn 202272:202273)
[chromium-blink-merge.git] / crypto / mock_apple_keychain.cc
bloba1faa65382a788ff5d8ac81b21b9723b41fb537c
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/logging.h"
6 #include "base/metrics/histogram.h"
7 #include "base/time/time.h"
8 #include "crypto/mock_apple_keychain.h"
10 namespace {
12 // Adds an entry to a local histogram to indicate that the Apple Keychain would
13 // have been accessed, if this class were not a mock of the Apple Keychain.
14 void IncrementKeychainAccessHistogram() {
15 // This local histogram is accessed by Telemetry to track the number of times
16 // the keychain is accessed, since keychain access is known to be synchronous
17 // and slow.
18 LOCAL_HISTOGRAM_BOOLEAN("OSX.Keychain.Access", true);
21 } // namespace
23 namespace crypto {
25 OSStatus MockAppleKeychain::FindGenericPassword(
26 CFTypeRef keychainOrArray,
27 UInt32 serviceNameLength,
28 const char* serviceName,
29 UInt32 accountNameLength,
30 const char* accountName,
31 UInt32* passwordLength,
32 void** passwordData,
33 SecKeychainItemRef* 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";
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 = arraysize(kPassword);
45 password_data_count_++;
48 return find_generic_result_;
51 OSStatus MockAppleKeychain::ItemFreeContent(SecKeychainAttributeList* attrList,
52 void* data) const {
53 // No-op.
54 password_data_count_--;
55 return noErr;
58 OSStatus MockAppleKeychain::AddGenericPassword(
59 SecKeychainRef keychain,
60 UInt32 serviceNameLength,
61 const char* serviceName,
62 UInt32 accountNameLength,
63 const char* accountName,
64 UInt32 passwordLength,
65 const void* passwordData,
66 SecKeychainItemRef* itemRef) const {
67 IncrementKeychainAccessHistogram();
69 called_add_generic_ = true;
71 DCHECK_GT(passwordLength, 0U);
72 DCHECK(passwordData);
73 add_generic_password_ =
74 std::string(const_cast<char*>(static_cast<const char*>(passwordData)),
75 passwordLength);
76 return noErr;
79 std::string MockAppleKeychain::GetEncryptionPassword() const {
80 IncrementKeychainAccessHistogram();
81 return "mock_password";
84 } // namespace crypto