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