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