OLD | NEW |
1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_MOCK_KEYCHAIN_MAC_H_ | 5 #ifndef CRYPTO_MOCK_KEYCHAIN_MAC_H_ |
6 #define CHROME_BROWSER_MOCK_KEYCHAIN_MAC_H_ | 6 #define CRYPTO_MOCK_KEYCHAIN_MAC_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
15 #include "chrome/browser/keychain_mac.h" | 15 #include "crypto/keychain_mac.h" |
16 | 16 |
17 // Mock Keychain wrapper for testing code that interacts with the OS Keychain. | 17 namespace crypto { |
18 // Implemented by storing SecKeychainAttributeList and KeychainPasswordData | 18 |
19 // values in separate mutable containers and mapping them to integer keys. | 19 // Mock Keychain wrapper for testing code that interacts with the OS X |
| 20 // Keychain. Implemented by storing SecKeychainAttributeList and |
| 21 // KeychainPasswordData values in separate mutable containers and |
| 22 // mapping them to integer keys. |
20 // | 23 // |
21 // Note that "const" is pretty much meaningless for this class; the const-ness | 24 // Note that "const" is pretty much meaningless for this class; the const-ness |
22 // of MacKeychain doesn't apply to the actual keychain data, so all of the Mock | 25 // of MacKeychain doesn't apply to the actual keychain data, so all of the Mock |
23 // data is mutable; don't assume that it won't change over the life of tests. | 26 // data is mutable; don't assume that it won't change over the life of tests. |
24 class MockKeychain : public MacKeychain { | 27 class MockKeychain : public MacKeychain { |
25 public: | 28 public: |
26 MockKeychain(); | 29 MockKeychain(); |
27 virtual ~MockKeychain(); | 30 virtual ~MockKeychain(); |
| 31 |
| 32 // MacKeychain implementation. |
28 virtual OSStatus ItemCopyAttributesAndData( | 33 virtual OSStatus ItemCopyAttributesAndData( |
29 SecKeychainItemRef itemRef, SecKeychainAttributeInfo *info, | 34 SecKeychainItemRef itemRef, |
30 SecItemClass *itemClass, SecKeychainAttributeList **attrList, | 35 SecKeychainAttributeInfo* info, |
31 UInt32 *length, void **outData) const OVERRIDE; | 36 SecItemClass* itemClass, |
| 37 SecKeychainAttributeList** attrList, |
| 38 UInt32* length, |
| 39 void** outData) const OVERRIDE; |
32 // Pass "fail_me" as the data to get errSecAuthFailed. | 40 // Pass "fail_me" as the data to get errSecAuthFailed. |
33 virtual OSStatus ItemModifyAttributesAndData( | 41 virtual OSStatus ItemModifyAttributesAndData( |
34 SecKeychainItemRef itemRef, const SecKeychainAttributeList *attrList, | 42 SecKeychainItemRef itemRef, |
35 UInt32 length, const void *data) const OVERRIDE; | 43 const SecKeychainAttributeList* attrList, |
36 virtual OSStatus ItemFreeAttributesAndData(SecKeychainAttributeList *attrList, | 44 UInt32 length, |
37 void *data) const OVERRIDE; | 45 const void* data) const OVERRIDE; |
| 46 virtual OSStatus ItemFreeAttributesAndData(SecKeychainAttributeList* attrList, |
| 47 void* data) const OVERRIDE; |
38 virtual OSStatus ItemDelete(SecKeychainItemRef itemRef) const OVERRIDE; | 48 virtual OSStatus ItemDelete(SecKeychainItemRef itemRef) const OVERRIDE; |
39 virtual OSStatus SearchCreateFromAttributes( | 49 virtual OSStatus SearchCreateFromAttributes( |
40 CFTypeRef keychainOrArray, SecItemClass itemClass, | 50 CFTypeRef keychainOrArray, |
41 const SecKeychainAttributeList *attrList, | 51 SecItemClass itemClass, |
42 SecKeychainSearchRef *searchRef) const OVERRIDE; | 52 const SecKeychainAttributeList* attrList, |
| 53 SecKeychainSearchRef* searchRef) const OVERRIDE; |
43 virtual OSStatus SearchCopyNext(SecKeychainSearchRef searchRef, | 54 virtual OSStatus SearchCopyNext(SecKeychainSearchRef searchRef, |
44 SecKeychainItemRef *itemRef) const OVERRIDE; | 55 SecKeychainItemRef* itemRef) const OVERRIDE; |
45 // Pass "some.domain.com" as the serverName to get errSecDuplicateItem. | 56 // Pass "some.domain.com" as the serverName to get errSecDuplicateItem. |
46 virtual OSStatus AddInternetPassword( | 57 virtual OSStatus AddInternetPassword( |
47 SecKeychainRef keychain, | 58 SecKeychainRef keychain, |
48 UInt32 serverNameLength, | 59 UInt32 serverNameLength, |
49 const char *serverName, | 60 const char* serverName, |
50 UInt32 securityDomainLength, | 61 UInt32 securityDomainLength, |
51 const char *securityDomain, | 62 const char* securityDomain, |
52 UInt32 accountNameLength, | 63 UInt32 accountNameLength, |
53 const char *accountName, | 64 const char* accountName, |
54 UInt32 pathLength, const char *path, | 65 UInt32 pathLength, const char* path, |
55 UInt16 port, SecProtocolType protocol, | 66 UInt16 port, SecProtocolType protocol, |
56 SecAuthenticationType authenticationType, | 67 SecAuthenticationType authenticationType, |
57 UInt32 passwordLength, | 68 UInt32 passwordLength, |
58 const void *passwordData, | 69 const void* passwordData, |
59 SecKeychainItemRef *itemRef) const OVERRIDE; | 70 SecKeychainItemRef* itemRef) const OVERRIDE; |
60 virtual OSStatus FindGenericPassword( | 71 virtual OSStatus FindGenericPassword( |
61 CFTypeRef keychainOrArray, | 72 CFTypeRef keychainOrArray, |
62 UInt32 serviceNameLength, | 73 UInt32 serviceNameLength, |
63 const char *serviceName, | 74 const char* serviceName, |
64 UInt32 accountNameLength, | 75 UInt32 accountNameLength, |
65 const char *accountName, | 76 const char* accountName, |
66 UInt32 *passwordLength, | 77 UInt32* passwordLength, |
67 void **passwordData, | 78 void** passwordData, |
68 SecKeychainItemRef *itemRef) const OVERRIDE; | 79 SecKeychainItemRef* itemRef) const OVERRIDE; |
69 virtual OSStatus ItemFreeContent(SecKeychainAttributeList *attrList, | 80 virtual OSStatus ItemFreeContent(SecKeychainAttributeList* attrList, |
70 void *data) const OVERRIDE; | 81 void* data) const OVERRIDE; |
71 virtual OSStatus AddGenericPassword( | 82 virtual OSStatus AddGenericPassword( |
72 SecKeychainRef keychain, | 83 SecKeychainRef keychain, |
73 UInt32 serviceNameLength, | 84 UInt32 serviceNameLength, |
74 const char *serviceName, | 85 const char* serviceName, |
75 UInt32 accountNameLength, | 86 UInt32 accountNameLength, |
76 const char *accountName, | 87 const char* accountName, |
77 UInt32 passwordLength, | 88 UInt32 passwordLength, |
78 const void *passwordData, | 89 const void* passwordData, |
79 SecKeychainItemRef *itemRef) const OVERRIDE; | 90 SecKeychainItemRef* itemRef) const OVERRIDE; |
80 virtual void Free(CFTypeRef ref) const OVERRIDE; | 91 virtual void Free(CFTypeRef ref) const OVERRIDE; |
81 | 92 |
82 // Return the counts of objects returned by Create/Copy functions but never | 93 // Return the counts of objects returned by Create/Copy functions but never |
83 // Free'd as they should have been. | 94 // Free'd as they should have been. |
84 int UnfreedSearchCount() const; | 95 int UnfreedSearchCount() const; |
85 int UnfreedKeychainItemCount() const; | 96 int UnfreedKeychainItemCount() const; |
86 int UnfreedAttributeDataCount() const; | 97 int UnfreedAttributeDataCount() const; |
87 | 98 |
88 // Returns true if all items added with AddInternetPassword have a creator | 99 // Returns true if all items added with AddInternetPassword have a creator |
89 // code set. | 100 // code set. |
(...skipping 30 matching lines...) Expand all Loading... |
120 // called. | 131 // called. |
121 std::string add_generic_password() const { return add_generic_password_; } | 132 std::string add_generic_password() const { return add_generic_password_; } |
122 | 133 |
123 // Returns the number of allocations - deallocations for password data. | 134 // Returns the number of allocations - deallocations for password data. |
124 int password_data_count() const { return password_data_count_; } | 135 int password_data_count() const { return password_data_count_; } |
125 | 136 |
126 private: | 137 private: |
127 // Returns true if the keychain already contains a password that matches the | 138 // Returns true if the keychain already contains a password that matches the |
128 // attributes provided. | 139 // attributes provided. |
129 bool AlreadyContainsInternetPassword( | 140 bool AlreadyContainsInternetPassword( |
130 UInt32 serverNameLength, const char *serverName, | 141 UInt32 serverNameLength, |
131 UInt32 securityDomainLength, const char *securityDomain, | 142 const char* serverName, |
132 UInt32 accountNameLength, const char *accountName, | 143 UInt32 securityDomainLength, |
133 UInt32 pathLength, const char *path, | 144 const char* securityDomain, |
134 UInt16 port, SecProtocolType protocol, | 145 UInt32 accountNameLength, |
| 146 const char* accountName, |
| 147 UInt32 pathLength, |
| 148 const char* path, |
| 149 UInt16 port, |
| 150 SecProtocolType protocol, |
135 SecAuthenticationType authenticationType) const; | 151 SecAuthenticationType authenticationType) const; |
136 // Initializes storage for keychain data at |key|. | 152 // Initializes storage for keychain data at |key|. |
137 void InitializeKeychainData(unsigned int key) const; | 153 void InitializeKeychainData(unsigned int key) const; |
138 // Sets the data and length of |tag| in the item-th test item. | 154 // Sets the data and length of |tag| in the item-th test item. |
139 void SetTestDataBytes(int item, UInt32 tag, const void* data, size_t length); | 155 void SetTestDataBytes(int item, UInt32 tag, const void* data, size_t length); |
140 // Sets the data and length of |tag| in the item-th test item based on | 156 // Sets the data and length of |tag| in the item-th test item based on |
141 // |value|. The null-terminator will not be included; the Keychain Services | 157 // |value|. The null-terminator will not be included; the Keychain Services |
142 // docs don't indicate whether it is or not, so clients should not assume | 158 // docs don't indicate whether it is or not, so clients should not assume |
143 // that it will be. | 159 // that it will be. |
144 void SetTestDataString(int item, UInt32 tag, const char* value); | 160 void SetTestDataString(int item, UInt32 tag, const char* value); |
145 // Sets the data of the corresponding attribute of the item-th test item to | 161 // Sets the data of the corresponding attribute of the item-th test item to |
146 // |value|. Assumes that the space has alread been allocated, and the length | 162 // |value|. Assumes that the space has alread been allocated, and the length |
147 // set. | 163 // set. |
148 void SetTestDataPort(int item, UInt32 value); | 164 void SetTestDataPort(int item, UInt32 value); |
149 void SetTestDataProtocol(int item, SecProtocolType value); | 165 void SetTestDataProtocol(int item, SecProtocolType value); |
150 void SetTestDataAuthType(int item, SecAuthenticationType value); | 166 void SetTestDataAuthType(int item, SecAuthenticationType value); |
151 void SetTestDataNegativeItem(int item, Boolean value); | 167 void SetTestDataNegativeItem(int item, Boolean value); |
152 void SetTestDataCreator(int item, OSType value); | 168 void SetTestDataCreator(int item, OSType value); |
153 // Sets the password data and length for the item-th test item. | 169 // Sets the password data and length for the item-th test item. |
154 void SetTestDataPasswordBytes(int item, const void* data, size_t length); | 170 void SetTestDataPasswordBytes(int item, const void* data, size_t length); |
155 // Sets the password for the item-th test item. As with SetTestDataString, | 171 // Sets the password for the item-th test item. As with SetTestDataString, |
156 // the data will not be null-terminated. | 172 // the data will not be null-terminated. |
157 void SetTestDataPasswordString(int item, const char* value); | 173 void SetTestDataPasswordString(int item, const char* value); |
158 | 174 |
159 // Returns the address of the attribute in attribute_list with tag |tag|. | 175 // Returns the address of the attribute in attribute_list with tag |tag|. |
160 static SecKeychainAttribute* AttributeWithTag( | 176 static SecKeychainAttribute* AttributeWithTag( |
161 const SecKeychainAttributeList& attribute_list, UInt32 tag); | 177 const SecKeychainAttributeList& attribute_list, |
| 178 UInt32 tag); |
162 | 179 |
163 static const int kDummySearchRef = 1000; | 180 static const int kDummySearchRef = 1000; |
164 | 181 |
165 typedef struct KeychainPasswordData { | 182 typedef struct KeychainPasswordData { |
166 KeychainPasswordData() : data(NULL), length(0) {} | 183 KeychainPasswordData() : data(NULL), length(0) {} |
167 void* data; | 184 void* data; |
168 UInt32 length; | 185 UInt32 length; |
169 } KeychainPasswordData; | 186 } KeychainPasswordData; |
170 | 187 |
171 // Mutable because the MockKeychain API requires its internal keychain storage | 188 // Mutable because the MockKeychain API requires its internal keychain storage |
(...skipping 25 matching lines...) Expand all Loading... |
197 mutable bool called_add_generic_; | 214 mutable bool called_add_generic_; |
198 | 215 |
199 // Tracks the allocations and frees of password data in |FindGenericPassword| | 216 // Tracks the allocations and frees of password data in |FindGenericPassword| |
200 // and |ItemFreeContent|. | 217 // and |ItemFreeContent|. |
201 mutable unsigned int password_data_count_; | 218 mutable unsigned int password_data_count_; |
202 | 219 |
203 // Records the password being set when |AddGenericPassword()| gets called. | 220 // Records the password being set when |AddGenericPassword()| gets called. |
204 mutable std::string add_generic_password_; | 221 mutable std::string add_generic_password_; |
205 }; | 222 }; |
206 | 223 |
207 #endif // CHROME_BROWSER_MOCK_KEYCHAIN_MAC_H_ | 224 } // namespace crypto |
| 225 |
| 226 #endif // CRYPTO_MOCK_KEYCHAIN_MAC_H_ |
OLD | NEW |