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 #include "base/logging.h" | 5 #include "base/logging.h" |
6 #include "base/time.h" | 6 #include "base/time.h" |
7 #include "crypto/mock_keychain_mac.h" | 7 #include "crypto/mock_keychain_mac.h" |
8 | 8 |
9 namespace crypto { | 9 namespace crypto { |
10 | 10 |
| 11 const SecKeychainSearchRef MockKeychain::kDummySearchRef = |
| 12 reinterpret_cast<SecKeychainSearchRef>(1000); |
| 13 |
11 MockKeychain::MockKeychain() | 14 MockKeychain::MockKeychain() |
12 : next_item_key_(0), | 15 : next_item_key_(0), |
13 search_copy_count_(0), | 16 search_copy_count_(0), |
14 keychain_item_copy_count_(0), | 17 keychain_item_copy_count_(0), |
15 attribute_data_copy_count_(0), | 18 attribute_data_copy_count_(0), |
16 find_generic_result_(noErr), | 19 find_generic_result_(noErr), |
17 called_add_generic_(false), | 20 called_add_generic_(false), |
18 password_data_count_(0) {} | 21 password_data_count_(0) {} |
19 | 22 |
20 void MockKeychain::InitializeKeychainData(unsigned int key) const { | 23 void MockKeychain::InitializeKeychainData(MockKeychainItemType key) const { |
21 UInt32 tags[] = { kSecAccountItemAttr, | 24 UInt32 tags[] = { kSecAccountItemAttr, |
22 kSecServerItemAttr, | 25 kSecServerItemAttr, |
23 kSecPortItemAttr, | 26 kSecPortItemAttr, |
24 kSecPathItemAttr, | 27 kSecPathItemAttr, |
25 kSecProtocolItemAttr, | 28 kSecProtocolItemAttr, |
26 kSecAuthenticationTypeItemAttr, | 29 kSecAuthenticationTypeItemAttr, |
27 kSecSecurityDomainItemAttr, | 30 kSecSecurityDomainItemAttr, |
28 kSecCreationDateItemAttr, | 31 kSecCreationDateItemAttr, |
29 kSecNegativeItemAttr, | 32 kSecNegativeItemAttr, |
30 kSecCreatorItemAttr }; | 33 kSecCreatorItemAttr }; |
(...skipping 23 matching lines...) Expand all Loading... |
54 break; | 57 break; |
55 } | 58 } |
56 if (data_size > 0) { | 59 if (data_size > 0) { |
57 keychain_attr_list_[key].attr[i].length = data_size; | 60 keychain_attr_list_[key].attr[i].length = data_size; |
58 keychain_attr_list_[key].attr[i].data = calloc(1, data_size); | 61 keychain_attr_list_[key].attr[i].data = calloc(1, data_size); |
59 } | 62 } |
60 } | 63 } |
61 } | 64 } |
62 | 65 |
63 MockKeychain::~MockKeychain() { | 66 MockKeychain::~MockKeychain() { |
64 for (std::map<unsigned int, SecKeychainAttributeList>::iterator it = | 67 for (std::map<MockKeychainItemType, SecKeychainAttributeList>::iterator it = |
65 keychain_attr_list_.begin(); it != keychain_attr_list_.end(); ++it) { | 68 keychain_attr_list_.begin(); it != keychain_attr_list_.end(); ++it) { |
66 for (unsigned int i = 0; i < it->second.count; ++i) { | 69 for (unsigned int i = 0; i < it->second.count; ++i) { |
67 if (it->second.attr[i].data) | 70 if (it->second.attr[i].data) |
68 free(it->second.attr[i].data); | 71 free(it->second.attr[i].data); |
69 } | 72 } |
70 free(it->second.attr); | 73 free(it->second.attr); |
71 if (keychain_data_[it->first].data) | 74 if (keychain_data_[it->first].data) |
72 free(keychain_data_[it->first].data); | 75 free(keychain_data_[it->first].data); |
73 } | 76 } |
74 keychain_attr_list_.clear(); | 77 keychain_attr_list_.clear(); |
(...skipping 10 matching lines...) Expand all Loading... |
85 break; | 88 break; |
86 } | 89 } |
87 } | 90 } |
88 if (attribute_index == -1) { | 91 if (attribute_index == -1) { |
89 NOTREACHED() << "Unsupported attribute: " << tag; | 92 NOTREACHED() << "Unsupported attribute: " << tag; |
90 return NULL; | 93 return NULL; |
91 } | 94 } |
92 return &(attribute_list.attr[attribute_index]); | 95 return &(attribute_list.attr[attribute_index]); |
93 } | 96 } |
94 | 97 |
95 void MockKeychain::SetTestDataBytes(int item, | 98 void MockKeychain::SetTestDataBytes(MockKeychainItemType item, |
96 UInt32 tag, | 99 UInt32 tag, |
97 const void* data, | 100 const void* data, |
98 size_t length) { | 101 size_t length) { |
99 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], | 102 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], |
100 tag); | 103 tag); |
101 attribute->length = length; | 104 attribute->length = length; |
102 if (length > 0) { | 105 if (length > 0) { |
103 if (attribute->data) | 106 if (attribute->data) |
104 free(attribute->data); | 107 free(attribute->data); |
105 attribute->data = malloc(length); | 108 attribute->data = malloc(length); |
106 CHECK(attribute->data); | 109 CHECK(attribute->data); |
107 memcpy(attribute->data, data, length); | 110 memcpy(attribute->data, data, length); |
108 } else { | 111 } else { |
109 attribute->data = NULL; | 112 attribute->data = NULL; |
110 } | 113 } |
111 } | 114 } |
112 | 115 |
113 void MockKeychain::SetTestDataString(int item, UInt32 tag, const char* value) { | 116 void MockKeychain::SetTestDataString( |
| 117 MockKeychainItemType item, |
| 118 UInt32 tag, |
| 119 const char* value) { |
114 SetTestDataBytes(item, tag, value, value ? strlen(value) : 0); | 120 SetTestDataBytes(item, tag, value, value ? strlen(value) : 0); |
115 } | 121 } |
116 | 122 |
117 void MockKeychain::SetTestDataPort(int item, UInt32 value) { | 123 void MockKeychain::SetTestDataPort(MockKeychainItemType item, UInt32 value) { |
118 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], | 124 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], |
119 kSecPortItemAttr); | 125 kSecPortItemAttr); |
120 UInt32* data = static_cast<UInt32*>(attribute->data); | 126 UInt32* data = static_cast<UInt32*>(attribute->data); |
121 *data = value; | 127 *data = value; |
122 } | 128 } |
123 | 129 |
124 void MockKeychain::SetTestDataProtocol(int item, SecProtocolType value) { | 130 void MockKeychain::SetTestDataProtocol(MockKeychainItemType item, |
| 131 SecProtocolType value) { |
125 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], | 132 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], |
126 kSecProtocolItemAttr); | 133 kSecProtocolItemAttr); |
127 SecProtocolType* data = static_cast<SecProtocolType*>(attribute->data); | 134 SecProtocolType* data = static_cast<SecProtocolType*>(attribute->data); |
128 *data = value; | 135 *data = value; |
129 } | 136 } |
130 | 137 |
131 void MockKeychain::SetTestDataAuthType(int item, SecAuthenticationType value) { | 138 void MockKeychain::SetTestDataAuthType( |
| 139 MockKeychainItemType item, |
| 140 SecAuthenticationType value) { |
132 SecKeychainAttribute* attribute = AttributeWithTag( | 141 SecKeychainAttribute* attribute = AttributeWithTag( |
133 keychain_attr_list_[item], kSecAuthenticationTypeItemAttr); | 142 keychain_attr_list_[item], kSecAuthenticationTypeItemAttr); |
134 SecAuthenticationType* data = static_cast<SecAuthenticationType*>( | 143 SecAuthenticationType* data = static_cast<SecAuthenticationType*>( |
135 attribute->data); | 144 attribute->data); |
136 *data = value; | 145 *data = value; |
137 } | 146 } |
138 | 147 |
139 void MockKeychain::SetTestDataNegativeItem(int item, Boolean value) { | 148 void MockKeychain::SetTestDataNegativeItem(MockKeychainItemType item, |
| 149 Boolean value) { |
140 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], | 150 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], |
141 kSecNegativeItemAttr); | 151 kSecNegativeItemAttr); |
142 Boolean* data = static_cast<Boolean*>(attribute->data); | 152 Boolean* data = static_cast<Boolean*>(attribute->data); |
143 *data = value; | 153 *data = value; |
144 } | 154 } |
145 | 155 |
146 void MockKeychain::SetTestDataCreator(int item, OSType value) { | 156 void MockKeychain::SetTestDataCreator(MockKeychainItemType item, OSType value) { |
147 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], | 157 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], |
148 kSecCreatorItemAttr); | 158 kSecCreatorItemAttr); |
149 OSType* data = static_cast<OSType*>(attribute->data); | 159 OSType* data = static_cast<OSType*>(attribute->data); |
150 *data = value; | 160 *data = value; |
151 } | 161 } |
152 | 162 |
153 void MockKeychain::SetTestDataPasswordBytes(int item, const void* data, | 163 void MockKeychain::SetTestDataPasswordBytes(MockKeychainItemType item, |
| 164 const void* data, |
154 size_t length) { | 165 size_t length) { |
155 keychain_data_[item].length = length; | 166 keychain_data_[item].length = length; |
156 if (length > 0) { | 167 if (length > 0) { |
157 if (keychain_data_[item].data) | 168 if (keychain_data_[item].data) |
158 free(keychain_data_[item].data); | 169 free(keychain_data_[item].data); |
159 keychain_data_[item].data = malloc(length); | 170 keychain_data_[item].data = malloc(length); |
160 memcpy(keychain_data_[item].data, data, length); | 171 memcpy(keychain_data_[item].data, data, length); |
161 } else { | 172 } else { |
162 keychain_data_[item].data = NULL; | 173 keychain_data_[item].data = NULL; |
163 } | 174 } |
164 } | 175 } |
165 | 176 |
166 void MockKeychain::SetTestDataPasswordString(int item, const char* value) { | 177 void MockKeychain::SetTestDataPasswordString( |
| 178 MockKeychainItemType item, |
| 179 const char* value) { |
167 SetTestDataPasswordBytes(item, value, value ? strlen(value) : 0); | 180 SetTestDataPasswordBytes(item, value, value ? strlen(value) : 0); |
168 } | 181 } |
169 | 182 |
170 OSStatus MockKeychain::ItemCopyAttributesAndData( | 183 OSStatus MockKeychain::ItemCopyAttributesAndData( |
171 SecKeychainItemRef itemRef, | 184 SecKeychainItemRef itemRef, |
172 SecKeychainAttributeInfo* info, | 185 SecKeychainAttributeInfo* info, |
173 SecItemClass* itemClass, | 186 SecItemClass* itemClass, |
174 SecKeychainAttributeList** attrList, | 187 SecKeychainAttributeList** attrList, |
175 UInt32* length, | 188 UInt32* length, |
176 void** outData) const { | 189 void** outData) const { |
177 DCHECK(itemRef); | 190 DCHECK(itemRef); |
178 unsigned int key = reinterpret_cast<unsigned int>(itemRef) - 1; | 191 MockKeychainItemType key = |
| 192 reinterpret_cast<MockKeychainItemType>(itemRef) - 1; |
179 if (keychain_attr_list_.find(key) == keychain_attr_list_.end()) | 193 if (keychain_attr_list_.find(key) == keychain_attr_list_.end()) |
180 return errSecInvalidItemRef; | 194 return errSecInvalidItemRef; |
181 | 195 |
182 DCHECK(!itemClass); // itemClass not implemented in the Mock. | 196 DCHECK(!itemClass); // itemClass not implemented in the Mock. |
183 if (attrList) | 197 if (attrList) |
184 *attrList = &(keychain_attr_list_[key]); | 198 *attrList = &(keychain_attr_list_[key]); |
185 if (outData) { | 199 if (outData) { |
186 *outData = keychain_data_[key].data; | 200 *outData = keychain_data_[key].data; |
187 DCHECK(length); | 201 DCHECK(length); |
188 *length = keychain_data_[key].length; | 202 *length = keychain_data_[key].length; |
189 } | 203 } |
190 | 204 |
191 ++attribute_data_copy_count_; | 205 ++attribute_data_copy_count_; |
192 return noErr; | 206 return noErr; |
193 } | 207 } |
194 | 208 |
195 OSStatus MockKeychain::ItemModifyAttributesAndData( | 209 OSStatus MockKeychain::ItemModifyAttributesAndData( |
196 SecKeychainItemRef itemRef, | 210 SecKeychainItemRef itemRef, |
197 const SecKeychainAttributeList* attrList, | 211 const SecKeychainAttributeList* attrList, |
198 UInt32 length, | 212 UInt32 length, |
199 const void* data) const { | 213 const void* data) const { |
200 DCHECK(itemRef); | 214 DCHECK(itemRef); |
201 const char* fail_trigger = "fail_me"; | 215 const char* fail_trigger = "fail_me"; |
202 if (length == strlen(fail_trigger) && | 216 if (length == strlen(fail_trigger) && |
203 memcmp(data, fail_trigger, length) == 0) { | 217 memcmp(data, fail_trigger, length) == 0) { |
204 return errSecAuthFailed; | 218 return errSecAuthFailed; |
205 } | 219 } |
206 | 220 |
207 unsigned int key = reinterpret_cast<unsigned int>(itemRef) - 1; | 221 MockKeychainItemType key = |
| 222 reinterpret_cast<MockKeychainItemType>(itemRef) - 1; |
208 if (keychain_attr_list_.find(key) == keychain_attr_list_.end()) | 223 if (keychain_attr_list_.find(key) == keychain_attr_list_.end()) |
209 return errSecInvalidItemRef; | 224 return errSecInvalidItemRef; |
210 | 225 |
211 MockKeychain* mutable_this = const_cast<MockKeychain*>(this); | 226 MockKeychain* mutable_this = const_cast<MockKeychain*>(this); |
212 if (attrList) { | 227 if (attrList) { |
213 for (UInt32 change_attr = 0; change_attr < attrList->count; ++change_attr) { | 228 for (UInt32 change_attr = 0; change_attr < attrList->count; ++change_attr) { |
214 if (attrList->attr[change_attr].tag == kSecCreatorItemAttr) { | 229 if (attrList->attr[change_attr].tag == kSecCreatorItemAttr) { |
215 void* data = attrList->attr[change_attr].data; | 230 void* data = attrList->attr[change_attr].data; |
216 mutable_this->SetTestDataCreator(key, *(static_cast<OSType*>(data))); | 231 mutable_this->SetTestDataCreator(key, *(static_cast<OSType*>(data))); |
217 } else { | 232 } else { |
218 NOTIMPLEMENTED(); | 233 NOTIMPLEMENTED(); |
219 } | 234 } |
220 } | 235 } |
221 } | 236 } |
222 if (data) | 237 if (data) |
223 mutable_this->SetTestDataPasswordBytes(key, data, length); | 238 mutable_this->SetTestDataPasswordBytes(key, data, length); |
224 return noErr; | 239 return noErr; |
225 } | 240 } |
226 | 241 |
227 OSStatus MockKeychain::ItemFreeAttributesAndData( | 242 OSStatus MockKeychain::ItemFreeAttributesAndData( |
228 SecKeychainAttributeList* attrList, | 243 SecKeychainAttributeList* attrList, |
229 void* data) const { | 244 void* data) const { |
230 --attribute_data_copy_count_; | 245 --attribute_data_copy_count_; |
231 return noErr; | 246 return noErr; |
232 } | 247 } |
233 | 248 |
234 OSStatus MockKeychain::ItemDelete(SecKeychainItemRef itemRef) const { | 249 OSStatus MockKeychain::ItemDelete(SecKeychainItemRef itemRef) const { |
235 unsigned int key = reinterpret_cast<unsigned int>(itemRef) - 1; | 250 MockKeychainItemType key = |
| 251 reinterpret_cast<MockKeychainItemType>(itemRef) - 1; |
236 | 252 |
237 for (unsigned int i = 0; i < keychain_attr_list_[key].count; ++i) { | 253 for (unsigned int i = 0; i < keychain_attr_list_[key].count; ++i) { |
238 if (keychain_attr_list_[key].attr[i].data) | 254 if (keychain_attr_list_[key].attr[i].data) |
239 free(keychain_attr_list_[key].attr[i].data); | 255 free(keychain_attr_list_[key].attr[i].data); |
240 } | 256 } |
241 free(keychain_attr_list_[key].attr); | 257 free(keychain_attr_list_[key].attr); |
242 if (keychain_data_[key].data) | 258 if (keychain_data_[key].data) |
243 free(keychain_data_[key].data); | 259 free(keychain_data_[key].data); |
244 | 260 |
245 keychain_attr_list_.erase(key); | 261 keychain_attr_list_.erase(key); |
246 keychain_data_.erase(key); | 262 keychain_data_.erase(key); |
247 added_via_api_.erase(key); | 263 added_via_api_.erase(key); |
248 return noErr; | 264 return noErr; |
249 } | 265 } |
250 | 266 |
251 OSStatus MockKeychain::SearchCreateFromAttributes( | 267 OSStatus MockKeychain::SearchCreateFromAttributes( |
252 CFTypeRef keychainOrArray, | 268 CFTypeRef keychainOrArray, |
253 SecItemClass itemClass, | 269 SecItemClass itemClass, |
254 const SecKeychainAttributeList* attrList, | 270 const SecKeychainAttributeList* attrList, |
255 SecKeychainSearchRef* searchRef) const { | 271 SecKeychainSearchRef* searchRef) const { |
256 // Figure out which of our mock items matches, and set up the array we'll use | 272 // Figure out which of our mock items matches, and set up the array we'll use |
257 // to generate results out of SearchCopyNext. | 273 // to generate results out of SearchCopyNext. |
258 remaining_search_results_.clear(); | 274 remaining_search_results_.clear(); |
259 for (std::map<unsigned int, SecKeychainAttributeList>::const_iterator it = | 275 for (std::map<MockKeychainItemType, SecKeychainAttributeList>::const_iterator |
260 keychain_attr_list_.begin(); it != keychain_attr_list_.end(); ++it) { | 276 it = keychain_attr_list_.begin(); |
| 277 it != keychain_attr_list_.end(); |
| 278 ++it) { |
261 bool mock_item_matches = true; | 279 bool mock_item_matches = true; |
262 for (UInt32 search_attr = 0; search_attr < attrList->count; ++search_attr) { | 280 for (UInt32 search_attr = 0; search_attr < attrList->count; ++search_attr) { |
263 SecKeychainAttribute* mock_attribute = | 281 SecKeychainAttribute* mock_attribute = |
264 AttributeWithTag(it->second, attrList->attr[search_attr].tag); | 282 AttributeWithTag(it->second, attrList->attr[search_attr].tag); |
265 if (mock_attribute->length != attrList->attr[search_attr].length || | 283 if (mock_attribute->length != attrList->attr[search_attr].length || |
266 memcmp(mock_attribute->data, attrList->attr[search_attr].data, | 284 memcmp(mock_attribute->data, attrList->attr[search_attr].data, |
267 attrList->attr[search_attr].length) != 0) { | 285 attrList->attr[search_attr].length) != 0) { |
268 mock_item_matches = false; | 286 mock_item_matches = false; |
269 break; | 287 break; |
270 } | 288 } |
271 } | 289 } |
272 if (mock_item_matches) | 290 if (mock_item_matches) |
273 remaining_search_results_.push_back(it->first); | 291 remaining_search_results_.push_back(it->first); |
274 } | 292 } |
275 | 293 |
276 DCHECK(searchRef); | 294 DCHECK(searchRef); |
277 *searchRef = reinterpret_cast<SecKeychainSearchRef>(kDummySearchRef); | 295 *searchRef = kDummySearchRef; |
278 ++search_copy_count_; | 296 ++search_copy_count_; |
279 return noErr; | 297 return noErr; |
280 } | 298 } |
281 | 299 |
282 bool MockKeychain::AlreadyContainsInternetPassword( | 300 bool MockKeychain::AlreadyContainsInternetPassword( |
283 UInt32 serverNameLength, | 301 UInt32 serverNameLength, |
284 const char* serverName, | 302 const char* serverName, |
285 UInt32 securityDomainLength, | 303 UInt32 securityDomainLength, |
286 const char* securityDomain, | 304 const char* securityDomain, |
287 UInt32 accountNameLength, | 305 UInt32 accountNameLength, |
288 const char* accountName, | 306 const char* accountName, |
289 UInt32 pathLength, | 307 UInt32 pathLength, |
290 const char* path, | 308 const char* path, |
291 UInt16 port, | 309 UInt16 port, |
292 SecProtocolType protocol, | 310 SecProtocolType protocol, |
293 SecAuthenticationType authenticationType) const { | 311 SecAuthenticationType authenticationType) const { |
294 for (std::map<unsigned int, SecKeychainAttributeList>::const_iterator it = | 312 for (std::map<MockKeychainItemType, SecKeychainAttributeList>::const_iterator |
295 keychain_attr_list_.begin(); it != keychain_attr_list_.end(); ++it) { | 313 it = keychain_attr_list_.begin(); |
| 314 it != keychain_attr_list_.end(); |
| 315 ++it) { |
296 SecKeychainAttribute* attribute; | 316 SecKeychainAttribute* attribute; |
297 attribute = AttributeWithTag(it->second, kSecServerItemAttr); | 317 attribute = AttributeWithTag(it->second, kSecServerItemAttr); |
298 if ((attribute->length != serverNameLength) || | 318 if ((attribute->length != serverNameLength) || |
299 (attribute->data == NULL && *serverName != '\0') || | 319 (attribute->data == NULL && *serverName != '\0') || |
300 (attribute->data != NULL && *serverName == '\0') || | 320 (attribute->data != NULL && *serverName == '\0') || |
301 strncmp(serverName, | 321 strncmp(serverName, |
302 (const char*) attribute->data, | 322 (const char*) attribute->data, |
303 serverNameLength) != 0) { | 323 serverNameLength) != 0) { |
304 continue; | 324 continue; |
305 } | 325 } |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 if (AlreadyContainsInternetPassword(serverNameLength, serverName, | 398 if (AlreadyContainsInternetPassword(serverNameLength, serverName, |
379 securityDomainLength, securityDomain, | 399 securityDomainLength, securityDomain, |
380 accountNameLength, accountName, | 400 accountNameLength, accountName, |
381 pathLength, path, | 401 pathLength, path, |
382 port, protocol, | 402 port, protocol, |
383 authenticationType)) { | 403 authenticationType)) { |
384 return errSecDuplicateItem; | 404 return errSecDuplicateItem; |
385 } | 405 } |
386 | 406 |
387 // Pick the next unused slot. | 407 // Pick the next unused slot. |
388 unsigned int key = next_item_key_++; | 408 MockKeychainItemType key = next_item_key_++; |
389 | 409 |
390 // Initialize keychain data storage at the target location. | 410 // Initialize keychain data storage at the target location. |
391 InitializeKeychainData(key); | 411 InitializeKeychainData(key); |
392 | 412 |
393 MockKeychain* mutable_this = const_cast<MockKeychain*>(this); | 413 MockKeychain* mutable_this = const_cast<MockKeychain*>(this); |
394 mutable_this->SetTestDataBytes(key, kSecServerItemAttr, serverName, | 414 mutable_this->SetTestDataBytes(key, kSecServerItemAttr, serverName, |
395 serverNameLength); | 415 serverNameLength); |
396 mutable_this->SetTestDataBytes(key, kSecSecurityDomainItemAttr, | 416 mutable_this->SetTestDataBytes(key, kSecSecurityDomainItemAttr, |
397 securityDomain, securityDomainLength); | 417 securityDomain, securityDomainLength); |
398 mutable_this->SetTestDataBytes(key, kSecAccountItemAttr, accountName, | 418 mutable_this->SetTestDataBytes(key, kSecAccountItemAttr, accountName, |
(...skipping 18 matching lines...) Expand all Loading... |
417 *itemRef = reinterpret_cast<SecKeychainItemRef>(key + 1); | 437 *itemRef = reinterpret_cast<SecKeychainItemRef>(key + 1); |
418 ++keychain_item_copy_count_; | 438 ++keychain_item_copy_count_; |
419 } | 439 } |
420 return noErr; | 440 return noErr; |
421 } | 441 } |
422 | 442 |
423 OSStatus MockKeychain::SearchCopyNext(SecKeychainSearchRef searchRef, | 443 OSStatus MockKeychain::SearchCopyNext(SecKeychainSearchRef searchRef, |
424 SecKeychainItemRef* itemRef) const { | 444 SecKeychainItemRef* itemRef) const { |
425 if (remaining_search_results_.empty()) | 445 if (remaining_search_results_.empty()) |
426 return errSecItemNotFound; | 446 return errSecItemNotFound; |
427 unsigned int key = remaining_search_results_.front(); | 447 MockKeychainItemType key = remaining_search_results_.front(); |
428 remaining_search_results_.erase(remaining_search_results_.begin()); | 448 remaining_search_results_.erase(remaining_search_results_.begin()); |
429 *itemRef = reinterpret_cast<SecKeychainItemRef>(key + 1); | 449 *itemRef = reinterpret_cast<SecKeychainItemRef>(key + 1); |
430 ++keychain_item_copy_count_; | 450 ++keychain_item_copy_count_; |
431 return noErr; | 451 return noErr; |
432 } | 452 } |
433 | 453 |
434 OSStatus MockKeychain::FindGenericPassword(CFTypeRef keychainOrArray, | 454 OSStatus MockKeychain::FindGenericPassword(CFTypeRef keychainOrArray, |
435 UInt32 serviceNameLength, | 455 UInt32 serviceNameLength, |
436 const char* serviceName, | 456 const char* serviceName, |
437 UInt32 accountNameLength, | 457 UInt32 accountNameLength, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
476 add_generic_password_ = | 496 add_generic_password_ = |
477 std::string(const_cast<char*>(static_cast<const char*>(passwordData)), | 497 std::string(const_cast<char*>(static_cast<const char*>(passwordData)), |
478 passwordLength); | 498 passwordLength); |
479 return noErr; | 499 return noErr; |
480 } | 500 } |
481 | 501 |
482 void MockKeychain::Free(CFTypeRef ref) const { | 502 void MockKeychain::Free(CFTypeRef ref) const { |
483 if (!ref) | 503 if (!ref) |
484 return; | 504 return; |
485 | 505 |
486 if (reinterpret_cast<int>(ref) == kDummySearchRef) { | 506 if (ref == kDummySearchRef) { |
487 --search_copy_count_; | 507 --search_copy_count_; |
488 } else { | 508 } else { |
489 --keychain_item_copy_count_; | 509 --keychain_item_copy_count_; |
490 } | 510 } |
491 } | 511 } |
492 | 512 |
493 int MockKeychain::UnfreedSearchCount() const { | 513 int MockKeychain::UnfreedSearchCount() const { |
494 return search_copy_count_; | 514 return search_copy_count_; |
495 } | 515 } |
496 | 516 |
497 int MockKeychain::UnfreedKeychainItemCount() const { | 517 int MockKeychain::UnfreedKeychainItemCount() const { |
498 return keychain_item_copy_count_; | 518 return keychain_item_copy_count_; |
499 } | 519 } |
500 | 520 |
501 int MockKeychain::UnfreedAttributeDataCount() const { | 521 int MockKeychain::UnfreedAttributeDataCount() const { |
502 return attribute_data_copy_count_; | 522 return attribute_data_copy_count_; |
503 } | 523 } |
504 | 524 |
505 bool MockKeychain::CreatorCodesSetForAddedItems() const { | 525 bool MockKeychain::CreatorCodesSetForAddedItems() const { |
506 for (std::set<unsigned int>::const_iterator i = added_via_api_.begin(); | 526 for (std::set<MockKeychainItemType>::const_iterator |
507 i != added_via_api_.end(); ++i) { | 527 i = added_via_api_.begin(); |
| 528 i != added_via_api_.end(); |
| 529 ++i) { |
508 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[*i], | 530 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[*i], |
509 kSecCreatorItemAttr); | 531 kSecCreatorItemAttr); |
510 OSType* data = static_cast<OSType*>(attribute->data); | 532 OSType* data = static_cast<OSType*>(attribute->data); |
511 if (*data == 0) | 533 if (*data == 0) |
512 return false; | 534 return false; |
513 } | 535 } |
514 return true; | 536 return true; |
515 } | 537 } |
516 | 538 |
517 void MockKeychain::AddTestItem(const KeychainTestData& item_data) { | 539 void MockKeychain::AddTestItem(const KeychainTestData& item_data) { |
518 unsigned int key = next_item_key_++; | 540 MockKeychainItemType key = next_item_key_++; |
519 | 541 |
520 InitializeKeychainData(key); | 542 InitializeKeychainData(key); |
521 SetTestDataAuthType(key, item_data.auth_type); | 543 SetTestDataAuthType(key, item_data.auth_type); |
522 SetTestDataString(key, kSecServerItemAttr, item_data.server); | 544 SetTestDataString(key, kSecServerItemAttr, item_data.server); |
523 SetTestDataProtocol(key, item_data.protocol); | 545 SetTestDataProtocol(key, item_data.protocol); |
524 SetTestDataString(key, kSecPathItemAttr, item_data.path); | 546 SetTestDataString(key, kSecPathItemAttr, item_data.path); |
525 SetTestDataPort(key, item_data.port); | 547 SetTestDataPort(key, item_data.port); |
526 SetTestDataString(key, kSecSecurityDomainItemAttr, | 548 SetTestDataString(key, kSecSecurityDomainItemAttr, |
527 item_data.security_domain); | 549 item_data.security_domain); |
528 SetTestDataString(key, kSecCreationDateItemAttr, item_data.creation_date); | 550 SetTestDataString(key, kSecCreationDateItemAttr, item_data.creation_date); |
529 SetTestDataString(key, kSecAccountItemAttr, item_data.username); | 551 SetTestDataString(key, kSecAccountItemAttr, item_data.username); |
530 SetTestDataPasswordString(key, item_data.password); | 552 SetTestDataPasswordString(key, item_data.password); |
531 SetTestDataNegativeItem(key, item_data.negative_item); | 553 SetTestDataNegativeItem(key, item_data.negative_item); |
532 } | 554 } |
533 | 555 |
534 } // namespace crypto | 556 } // namespace crypto |
OLD | NEW |