Upload upstream chromium 76.0.3809.146
[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(UInt32 serviceNameLength,
35                                const char* serviceName,
36                                UInt32 accountNameLength,
37                                const char* accountName,
38                                UInt32* passwordLength,
39                                void** passwordData,
40                                AppleSecKeychainItemRef* itemRef) const override;
41   OSStatus ItemFreeContent(void* data) const override;
42   OSStatus AddGenericPassword(UInt32 serviceNameLength,
43                               const char* serviceName,
44                               UInt32 accountNameLength,
45                               const char* accountName,
46                               UInt32 passwordLength,
47                               const void* passwordData,
48                               AppleSecKeychainItemRef* itemRef) const override;
49
50   // Returns the password that OSCrypt uses to generate its encryption key.
51   std::string GetEncryptionPassword() const;
52
53 #if !defined(OS_IOS)
54   OSStatus ItemDelete(SecKeychainItemRef itemRef) const override;
55 #endif  // !defined(OS_IOS)
56
57   // |FindGenericPassword()| can return different results depending on user
58   // interaction with the system Keychain.  For mocking purposes we allow the
59   // user of this class to specify the result code of the
60   // |FindGenericPassword()| call so we can simulate the result of different
61   // user interactions.
62   void set_find_generic_result(OSStatus result) {
63     find_generic_result_ = result;
64   }
65
66   // Returns the true if |AddGenericPassword()| was called.
67   bool called_add_generic() const { return called_add_generic_; }
68
69   // Returns the number of allocations - deallocations for password data.
70   int password_data_count() const { return password_data_count_; }
71
72  private:
73   // Result code for the |FindGenericPassword()| method.
74   OSStatus find_generic_result_;
75
76   // Records whether |AddGenericPassword()| gets called.
77   mutable bool called_add_generic_;
78
79   // Tracks the allocations and frees of password data in |FindGenericPassword|
80   // and |ItemFreeContent|.
81   mutable int password_data_count_;
82
83   DISALLOW_COPY_AND_ASSIGN(MockAppleKeychain);
84 };
85
86 }  // namespace crypto
87
88 #endif  // CRYPTO_MOCK_APPLE_KEYCHAIN_H_