mock_apple_keychain.cc 2.2 KB

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