OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/logging.h" | 5 #include "base/logging.h" |
| 6 #include "base/metrics/histogram.h" |
6 #include "base/time/time.h" | 7 #include "base/time/time.h" |
7 #include "crypto/mock_apple_keychain.h" | 8 #include "crypto/mock_apple_keychain.h" |
8 | 9 |
| 10 namespace { |
| 11 |
| 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); |
| 19 } |
| 20 |
| 21 } // namespace |
| 22 |
9 namespace crypto { | 23 namespace crypto { |
10 | 24 |
11 OSStatus MockAppleKeychain::FindGenericPassword( | 25 OSStatus MockAppleKeychain::FindGenericPassword( |
12 CFTypeRef keychainOrArray, | 26 CFTypeRef keychainOrArray, |
13 UInt32 serviceNameLength, | 27 UInt32 serviceNameLength, |
14 const char* serviceName, | 28 const char* serviceName, |
15 UInt32 accountNameLength, | 29 UInt32 accountNameLength, |
16 const char* accountName, | 30 const char* accountName, |
17 UInt32* passwordLength, | 31 UInt32* passwordLength, |
18 void** passwordData, | 32 void** passwordData, |
19 SecKeychainItemRef* itemRef) const { | 33 SecKeychainItemRef* itemRef) const { |
| 34 IncrementKeychainAccessHistogram(); |
| 35 |
20 // When simulating |noErr|, return canned |passwordData| and | 36 // When simulating |noErr|, return canned |passwordData| and |
21 // |passwordLength|. Otherwise, just return given code. | 37 // |passwordLength|. Otherwise, just return given code. |
22 if (find_generic_result_ == noErr) { | 38 if (find_generic_result_ == noErr) { |
23 static const char kPassword[] = "my_password"; | 39 static const char kPassword[] = "my_password"; |
24 DCHECK(passwordData); | 40 DCHECK(passwordData); |
25 // The function to free this data is mocked so the cast is fine. | 41 // The function to free this data is mocked so the cast is fine. |
26 *passwordData = const_cast<char*>(kPassword); | 42 *passwordData = const_cast<char*>(kPassword); |
27 DCHECK(passwordLength); | 43 DCHECK(passwordLength); |
28 *passwordLength = arraysize(kPassword); | 44 *passwordLength = arraysize(kPassword); |
29 password_data_count_++; | 45 password_data_count_++; |
(...skipping 11 matching lines...) Expand all Loading... |
41 | 57 |
42 OSStatus MockAppleKeychain::AddGenericPassword( | 58 OSStatus MockAppleKeychain::AddGenericPassword( |
43 SecKeychainRef keychain, | 59 SecKeychainRef keychain, |
44 UInt32 serviceNameLength, | 60 UInt32 serviceNameLength, |
45 const char* serviceName, | 61 const char* serviceName, |
46 UInt32 accountNameLength, | 62 UInt32 accountNameLength, |
47 const char* accountName, | 63 const char* accountName, |
48 UInt32 passwordLength, | 64 UInt32 passwordLength, |
49 const void* passwordData, | 65 const void* passwordData, |
50 SecKeychainItemRef* itemRef) const { | 66 SecKeychainItemRef* itemRef) const { |
| 67 IncrementKeychainAccessHistogram(); |
| 68 |
51 called_add_generic_ = true; | 69 called_add_generic_ = true; |
52 | 70 |
53 DCHECK_GT(passwordLength, 0U); | 71 DCHECK_GT(passwordLength, 0U); |
54 DCHECK(passwordData); | 72 DCHECK(passwordData); |
55 add_generic_password_ = | 73 add_generic_password_ = |
56 std::string(const_cast<char*>(static_cast<const char*>(passwordData)), | 74 std::string(const_cast<char*>(static_cast<const char*>(passwordData)), |
57 passwordLength); | 75 passwordLength); |
58 return noErr; | 76 return noErr; |
59 } | 77 } |
60 | 78 |
| 79 std::string MockAppleKeychain::GetEncryptionPassword() const { |
| 80 IncrementKeychainAccessHistogram(); |
| 81 return "mock_password"; |
| 82 } |
| 83 |
61 } // namespace crypto | 84 } // namespace crypto |
OLD | NEW |