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