mock_apple_keychain.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #ifndef CRYPTO_MOCK_APPLE_KEYCHAIN_H_
  5. #define CRYPTO_MOCK_APPLE_KEYCHAIN_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <map>
  9. #include <set>
  10. #include <string>
  11. #include <vector>
  12. #include "base/compiler_specific.h"
  13. #include "build/build_config.h"
  14. #include "crypto/apple_keychain.h"
  15. namespace crypto {
  16. // Mock Keychain wrapper for testing code that interacts with the OS X
  17. // Keychain.
  18. //
  19. // Note that "const" is pretty much meaningless for this class; the const-ness
  20. // of AppleKeychain doesn't apply to the actual keychain data, so all of the
  21. // Mock data is mutable; don't assume that it won't change over the life of
  22. // tests.
  23. class CRYPTO_EXPORT MockAppleKeychain : public AppleKeychain {
  24. public:
  25. MockAppleKeychain();
  26. MockAppleKeychain(const MockAppleKeychain&) = delete;
  27. MockAppleKeychain& operator=(const MockAppleKeychain&) = delete;
  28. ~MockAppleKeychain() override;
  29. // AppleKeychain implementation.
  30. OSStatus FindGenericPassword(UInt32 serviceNameLength,
  31. const char* serviceName,
  32. UInt32 accountNameLength,
  33. const char* accountName,
  34. UInt32* passwordLength,
  35. void** passwordData,
  36. AppleSecKeychainItemRef* itemRef) const override;
  37. OSStatus ItemFreeContent(void* data) const override;
  38. OSStatus AddGenericPassword(UInt32 serviceNameLength,
  39. const char* serviceName,
  40. UInt32 accountNameLength,
  41. const char* accountName,
  42. UInt32 passwordLength,
  43. const void* passwordData,
  44. AppleSecKeychainItemRef* itemRef) const override;
  45. // Returns the password that OSCrypt uses to generate its encryption key.
  46. std::string GetEncryptionPassword() const;
  47. #if !BUILDFLAG(IS_IOS)
  48. OSStatus ItemDelete(SecKeychainItemRef itemRef) const override;
  49. #endif // !BUILDFLAG(IS_IOS)
  50. // |FindGenericPassword()| can return different results depending on user
  51. // interaction with the system Keychain. For mocking purposes we allow the
  52. // user of this class to specify the result code of the
  53. // |FindGenericPassword()| call so we can simulate the result of different
  54. // user interactions.
  55. void set_find_generic_result(OSStatus result) {
  56. find_generic_result_ = result;
  57. }
  58. // Returns the true if |AddGenericPassword()| was called.
  59. bool called_add_generic() const { return called_add_generic_; }
  60. // Returns the number of allocations - deallocations for password data.
  61. int password_data_count() const { return password_data_count_; }
  62. private:
  63. // Result code for the |FindGenericPassword()| method.
  64. OSStatus find_generic_result_;
  65. // Records whether |AddGenericPassword()| gets called.
  66. mutable bool called_add_generic_;
  67. // Tracks the allocations and frees of password data in |FindGenericPassword|
  68. // and |ItemFreeContent|.
  69. mutable int password_data_count_;
  70. };
  71. } // namespace crypto
  72. #endif // CRYPTO_MOCK_APPLE_KEYCHAIN_H_