OLD | NEW |
1 // Copyright (c) 2012 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 CRYPTO_MOCK_KEYCHAIN_MAC_H_ | 5 #ifndef CRYPTO_MOCK_KEYCHAIN_MAC_H_ |
6 #define CRYPTO_MOCK_KEYCHAIN_MAC_H_ | 6 #define CRYPTO_MOCK_KEYCHAIN_MAC_H_ |
7 | 7 |
| 8 #include <stdint.h> |
| 9 |
8 #include <map> | 10 #include <map> |
9 #include <set> | 11 #include <set> |
10 #include <string> | 12 #include <string> |
11 #include <vector> | 13 #include <vector> |
12 | 14 |
13 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
14 #include "crypto/keychain_mac.h" | 16 #include "crypto/keychain_mac.h" |
15 | 17 |
16 namespace crypto { | 18 namespace crypto { |
17 | 19 |
| 20 // Type used for the keys in the std::map(s) and MockKeychain items. |
| 21 typedef uintptr_t MockKeychainItemType; |
| 22 |
18 // Mock Keychain wrapper for testing code that interacts with the OS X | 23 // Mock Keychain wrapper for testing code that interacts with the OS X |
19 // Keychain. Implemented by storing SecKeychainAttributeList and | 24 // Keychain. Implemented by storing SecKeychainAttributeList and |
20 // KeychainPasswordData values in separate mutable containers and | 25 // KeychainPasswordData values in separate mutable containers and |
21 // mapping them to integer keys. | 26 // mapping them to integer keys. |
22 // | 27 // |
23 // Note that "const" is pretty much meaningless for this class; the const-ness | 28 // Note that "const" is pretty much meaningless for this class; the const-ness |
24 // of MacKeychain doesn't apply to the actual keychain data, so all of the Mock | 29 // of MacKeychain doesn't apply to the actual keychain data, so all of the Mock |
25 // data is mutable; don't assume that it won't change over the life of tests. | 30 // data is mutable; don't assume that it won't change over the life of tests. |
26 class CRYPTO_EXPORT MockKeychain : public MacKeychain { | 31 class CRYPTO_EXPORT MockKeychain : public MacKeychain { |
27 public: | 32 public: |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 UInt32 securityDomainLength, | 147 UInt32 securityDomainLength, |
143 const char* securityDomain, | 148 const char* securityDomain, |
144 UInt32 accountNameLength, | 149 UInt32 accountNameLength, |
145 const char* accountName, | 150 const char* accountName, |
146 UInt32 pathLength, | 151 UInt32 pathLength, |
147 const char* path, | 152 const char* path, |
148 UInt16 port, | 153 UInt16 port, |
149 SecProtocolType protocol, | 154 SecProtocolType protocol, |
150 SecAuthenticationType authenticationType) const; | 155 SecAuthenticationType authenticationType) const; |
151 // Initializes storage for keychain data at |key|. | 156 // Initializes storage for keychain data at |key|. |
152 void InitializeKeychainData(unsigned int key) const; | 157 void InitializeKeychainData(MockKeychainItemType key) const; |
153 // Sets the data and length of |tag| in the item-th test item. | 158 // Sets the data and length of |tag| in the item-th test item. |
154 void SetTestDataBytes(int item, UInt32 tag, const void* data, size_t length); | 159 void SetTestDataBytes( |
| 160 MockKeychainItemType item, |
| 161 UInt32 tag, |
| 162 const void* data, |
| 163 size_t length); |
155 // Sets the data and length of |tag| in the item-th test item based on | 164 // Sets the data and length of |tag| in the item-th test item based on |
156 // |value|. The null-terminator will not be included; the Keychain Services | 165 // |value|. The null-terminator will not be included; the Keychain Services |
157 // docs don't indicate whether it is or not, so clients should not assume | 166 // docs don't indicate whether it is or not, so clients should not assume |
158 // that it will be. | 167 // that it will be. |
159 void SetTestDataString(int item, UInt32 tag, const char* value); | 168 void SetTestDataString(MockKeychainItemType item, |
| 169 UInt32 tag, |
| 170 const char* value); |
160 // Sets the data of the corresponding attribute of the item-th test item to | 171 // Sets the data of the corresponding attribute of the item-th test item to |
161 // |value|. Assumes that the space has alread been allocated, and the length | 172 // |value|. Assumes that the space has alread been allocated, and the length |
162 // set. | 173 // set. |
163 void SetTestDataPort(int item, UInt32 value); | 174 void SetTestDataPort(MockKeychainItemType item, UInt32 value); |
164 void SetTestDataProtocol(int item, SecProtocolType value); | 175 void SetTestDataProtocol(MockKeychainItemType item, SecProtocolType value); |
165 void SetTestDataAuthType(int item, SecAuthenticationType value); | 176 void SetTestDataAuthType(MockKeychainItemType item, |
166 void SetTestDataNegativeItem(int item, Boolean value); | 177 SecAuthenticationType value); |
167 void SetTestDataCreator(int item, OSType value); | 178 void SetTestDataNegativeItem(MockKeychainItemType item, Boolean value); |
| 179 void SetTestDataCreator(MockKeychainItemType item, OSType value); |
168 // Sets the password data and length for the item-th test item. | 180 // Sets the password data and length for the item-th test item. |
169 void SetTestDataPasswordBytes(int item, const void* data, size_t length); | 181 void SetTestDataPasswordBytes( |
| 182 MockKeychainItemType item, |
| 183 const void* data, |
| 184 size_t length); |
170 // Sets the password for the item-th test item. As with SetTestDataString, | 185 // Sets the password for the item-th test item. As with SetTestDataString, |
171 // the data will not be null-terminated. | 186 // the data will not be null-terminated. |
172 void SetTestDataPasswordString(int item, const char* value); | 187 void SetTestDataPasswordString(MockKeychainItemType item, const char* value); |
173 | 188 |
174 // Returns the address of the attribute in attribute_list with tag |tag|. | 189 // Returns the address of the attribute in attribute_list with tag |tag|. |
175 static SecKeychainAttribute* AttributeWithTag( | 190 static SecKeychainAttribute* AttributeWithTag( |
176 const SecKeychainAttributeList& attribute_list, | 191 const SecKeychainAttributeList& attribute_list, |
177 UInt32 tag); | 192 UInt32 tag); |
178 | 193 |
179 static const int kDummySearchRef = 1000; | 194 static const SecKeychainSearchRef kDummySearchRef; |
180 | 195 |
181 typedef struct KeychainPasswordData { | 196 typedef struct KeychainPasswordData { |
182 KeychainPasswordData() : data(NULL), length(0) {} | 197 KeychainPasswordData() : data(NULL), length(0) {} |
183 void* data; | 198 void* data; |
184 UInt32 length; | 199 UInt32 length; |
185 } KeychainPasswordData; | 200 } KeychainPasswordData; |
186 | 201 |
187 // Mutable because the MockKeychain API requires its internal keychain storage | 202 // Mutable because the MockKeychain API requires its internal keychain storage |
188 // to be modifiable by users of this class. | 203 // to be modifiable by users of this class. |
189 mutable std::map<unsigned int, SecKeychainAttributeList> keychain_attr_list_; | 204 mutable std::map<MockKeychainItemType, |
190 mutable std::map<unsigned int, KeychainPasswordData> keychain_data_; | 205 SecKeychainAttributeList> keychain_attr_list_; |
191 mutable unsigned int next_item_key_; | 206 mutable std::map<MockKeychainItemType, KeychainPasswordData> keychain_data_; |
| 207 mutable MockKeychainItemType next_item_key_; |
192 | 208 |
193 // Tracks the items that should be returned in subsequent calls to | 209 // Tracks the items that should be returned in subsequent calls to |
194 // SearchCopyNext, based on the last call to SearchCreateFromAttributes. | 210 // SearchCopyNext, based on the last call to SearchCreateFromAttributes. |
195 // We can't handle multiple active searches, since we don't track the search | 211 // We can't handle multiple active searches, since we don't track the search |
196 // ref we return, but we don't need to for our mocking. | 212 // ref we return, but we don't need to for our mocking. |
197 mutable std::vector<unsigned int> remaining_search_results_; | 213 mutable std::vector<MockKeychainItemType> remaining_search_results_; |
198 | 214 |
199 // Track copies and releases to make sure they balance. Really these should | 215 // Track copies and releases to make sure they balance. Really these should |
200 // be maps to track per item, but this should be good enough to catch | 216 // be maps to track per item, but this should be good enough to catch |
201 // real mistakes. | 217 // real mistakes. |
202 mutable int search_copy_count_; | 218 mutable int search_copy_count_; |
203 mutable int keychain_item_copy_count_; | 219 mutable int keychain_item_copy_count_; |
204 mutable int attribute_data_copy_count_; | 220 mutable int attribute_data_copy_count_; |
205 | 221 |
206 // Tracks which items (by key) were added with AddInternetPassword. | 222 // Tracks which items (by key) were added with AddInternetPassword. |
207 mutable std::set<unsigned int> added_via_api_; | 223 mutable std::set<MockKeychainItemType> added_via_api_; |
208 | 224 |
209 // Result code for the |FindGenericPassword()| method. | 225 // Result code for the |FindGenericPassword()| method. |
210 OSStatus find_generic_result_; | 226 OSStatus find_generic_result_; |
211 | 227 |
212 // Records whether |AddGenericPassword()| gets called. | 228 // Records whether |AddGenericPassword()| gets called. |
213 mutable bool called_add_generic_; | 229 mutable bool called_add_generic_; |
214 | 230 |
215 // Tracks the allocations and frees of password data in |FindGenericPassword| | 231 // Tracks the allocations and frees of password data in |FindGenericPassword| |
216 // and |ItemFreeContent|. | 232 // and |ItemFreeContent|. |
217 mutable int password_data_count_; | 233 mutable int password_data_count_; |
218 | 234 |
219 // Records the password being set when |AddGenericPassword()| gets called. | 235 // Records the password being set when |AddGenericPassword()| gets called. |
220 mutable std::string add_generic_password_; | 236 mutable std::string add_generic_password_; |
221 }; | 237 }; |
222 | 238 |
223 } // namespace crypto | 239 } // namespace crypto |
224 | 240 |
225 #endif // CRYPTO_MOCK_KEYCHAIN_MAC_H_ | 241 #endif // CRYPTO_MOCK_KEYCHAIN_MAC_H_ |
OLD | NEW |