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