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