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/time.h" | 6 #include "base/time/time.h" |
7 #include "crypto/mock_apple_keychain.h" | 7 #include "crypto/mock_apple_keychain.h" |
8 | 8 |
9 namespace crypto { | 9 namespace crypto { |
10 | 10 |
11 // static | 11 // static |
12 const SecKeychainSearchRef MockAppleKeychain::kDummySearchRef = | 12 const SecKeychainSearchRef MockAppleKeychain::kDummySearchRef = |
13 reinterpret_cast<SecKeychainSearchRef>(1000); | 13 reinterpret_cast<SecKeychainSearchRef>(1000); |
14 | 14 |
15 MockAppleKeychain::MockAppleKeychain() | 15 MockAppleKeychain::MockAppleKeychain() |
16 : next_item_key_(0), | 16 : locked_(false), |
| 17 next_item_key_(0), |
17 search_copy_count_(0), | 18 search_copy_count_(0), |
18 keychain_item_copy_count_(0), | 19 keychain_item_copy_count_(0), |
19 attribute_data_copy_count_(0), | 20 attribute_data_copy_count_(0), |
20 find_generic_result_(noErr), | 21 find_generic_result_(noErr), |
21 called_add_generic_(false), | 22 called_add_generic_(false), |
22 password_data_count_(0) {} | 23 password_data_count_(0) {} |
23 | 24 |
24 void MockAppleKeychain::InitializeKeychainData(MockKeychainItemType key) const { | 25 void MockAppleKeychain::InitializeKeychainData(MockKeychainItemType key) const { |
25 UInt32 tags[] = { kSecAccountItemAttr, | 26 UInt32 tags[] = { kSecAccountItemAttr, |
26 kSecServerItemAttr, | 27 kSecServerItemAttr, |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 SecKeychainAttributeList** attrList, | 189 SecKeychainAttributeList** attrList, |
189 UInt32* length, | 190 UInt32* length, |
190 void** outData) const { | 191 void** outData) const { |
191 DCHECK(itemRef); | 192 DCHECK(itemRef); |
192 MockKeychainItemType key = | 193 MockKeychainItemType key = |
193 reinterpret_cast<MockKeychainItemType>(itemRef) - 1; | 194 reinterpret_cast<MockKeychainItemType>(itemRef) - 1; |
194 if (keychain_attr_list_.find(key) == keychain_attr_list_.end()) | 195 if (keychain_attr_list_.find(key) == keychain_attr_list_.end()) |
195 return errSecInvalidItemRef; | 196 return errSecInvalidItemRef; |
196 | 197 |
197 DCHECK(!itemClass); // itemClass not implemented in the Mock. | 198 DCHECK(!itemClass); // itemClass not implemented in the Mock. |
| 199 if (locked_ && outData) |
| 200 return errSecAuthFailed; |
| 201 |
198 if (attrList) | 202 if (attrList) |
199 *attrList = &(keychain_attr_list_[key]); | 203 *attrList = &(keychain_attr_list_[key]); |
200 if (outData) { | 204 if (outData) { |
201 *outData = keychain_data_[key].data; | 205 *outData = keychain_data_[key].data; |
202 DCHECK(length); | 206 DCHECK(length); |
203 *length = keychain_data_[key].length; | 207 *length = keychain_data_[key].length; |
204 } | 208 } |
205 | 209 |
206 ++attribute_data_copy_count_; | 210 ++attribute_data_copy_count_; |
207 return noErr; | 211 return noErr; |
208 } | 212 } |
209 | 213 |
210 OSStatus MockAppleKeychain::ItemModifyAttributesAndData( | 214 OSStatus MockAppleKeychain::ItemModifyAttributesAndData( |
211 SecKeychainItemRef itemRef, | 215 SecKeychainItemRef itemRef, |
212 const SecKeychainAttributeList* attrList, | 216 const SecKeychainAttributeList* attrList, |
213 UInt32 length, | 217 UInt32 length, |
214 const void* data) const { | 218 const void* data) const { |
215 DCHECK(itemRef); | 219 DCHECK(itemRef); |
| 220 if (locked_) |
| 221 return errSecAuthFailed; |
216 const char* fail_trigger = "fail_me"; | 222 const char* fail_trigger = "fail_me"; |
217 if (length == strlen(fail_trigger) && | 223 if (length == strlen(fail_trigger) && |
218 memcmp(data, fail_trigger, length) == 0) { | 224 memcmp(data, fail_trigger, length) == 0) { |
219 return errSecAuthFailed; | 225 return errSecAuthFailed; |
220 } | 226 } |
221 | 227 |
222 MockKeychainItemType key = | 228 MockKeychainItemType key = |
223 reinterpret_cast<MockKeychainItemType>(itemRef) - 1; | 229 reinterpret_cast<MockKeychainItemType>(itemRef) - 1; |
224 if (keychain_attr_list_.find(key) == keychain_attr_list_.end()) | 230 if (keychain_attr_list_.find(key) == keychain_attr_list_.end()) |
225 return errSecInvalidItemRef; | 231 return errSecInvalidItemRef; |
(...skipping 15 matching lines...) Expand all Loading... |
241 } | 247 } |
242 | 248 |
243 OSStatus MockAppleKeychain::ItemFreeAttributesAndData( | 249 OSStatus MockAppleKeychain::ItemFreeAttributesAndData( |
244 SecKeychainAttributeList* attrList, | 250 SecKeychainAttributeList* attrList, |
245 void* data) const { | 251 void* data) const { |
246 --attribute_data_copy_count_; | 252 --attribute_data_copy_count_; |
247 return noErr; | 253 return noErr; |
248 } | 254 } |
249 | 255 |
250 OSStatus MockAppleKeychain::ItemDelete(SecKeychainItemRef itemRef) const { | 256 OSStatus MockAppleKeychain::ItemDelete(SecKeychainItemRef itemRef) const { |
| 257 if (locked_) |
| 258 return errSecAuthFailed; |
251 MockKeychainItemType key = | 259 MockKeychainItemType key = |
252 reinterpret_cast<MockKeychainItemType>(itemRef) - 1; | 260 reinterpret_cast<MockKeychainItemType>(itemRef) - 1; |
253 | 261 |
254 for (unsigned int i = 0; i < keychain_attr_list_[key].count; ++i) { | 262 for (unsigned int i = 0; i < keychain_attr_list_[key].count; ++i) { |
255 if (keychain_attr_list_[key].attr[i].data) | 263 if (keychain_attr_list_[key].attr[i].data) |
256 free(keychain_attr_list_[key].attr[i].data); | 264 free(keychain_attr_list_[key].attr[i].data); |
257 } | 265 } |
258 free(keychain_attr_list_[key].attr); | 266 free(keychain_attr_list_[key].attr); |
259 if (keychain_data_[key].data) | 267 if (keychain_data_[key].data) |
260 free(keychain_data_[key].data); | 268 free(keychain_data_[key].data); |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 UInt32 accountNameLength, | 391 UInt32 accountNameLength, |
384 const char* accountName, | 392 const char* accountName, |
385 UInt32 pathLength, | 393 UInt32 pathLength, |
386 const char* path, | 394 const char* path, |
387 UInt16 port, | 395 UInt16 port, |
388 SecProtocolType protocol, | 396 SecProtocolType protocol, |
389 SecAuthenticationType authenticationType, | 397 SecAuthenticationType authenticationType, |
390 UInt32 passwordLength, | 398 UInt32 passwordLength, |
391 const void* passwordData, | 399 const void* passwordData, |
392 SecKeychainItemRef* itemRef) const { | 400 SecKeychainItemRef* itemRef) const { |
| 401 if (locked_) |
| 402 return errSecAuthFailed; |
393 | 403 |
394 // Check for the magic duplicate item trigger. | 404 // Check for the magic duplicate item trigger. |
395 if (strcmp(serverName, "some.domain.com") == 0) | 405 if (strcmp(serverName, "some.domain.com") == 0) |
396 return errSecDuplicateItem; | 406 return errSecDuplicateItem; |
397 | 407 |
398 // If the account already exists in the keychain, we don't add it. | 408 // If the account already exists in the keychain, we don't add it. |
399 if (AlreadyContainsInternetPassword(serverNameLength, serverName, | 409 if (AlreadyContainsInternetPassword(serverNameLength, serverName, |
400 securityDomainLength, securityDomain, | 410 securityDomainLength, securityDomain, |
401 accountNameLength, accountName, | 411 accountNameLength, accountName, |
402 pathLength, path, | 412 pathLength, path, |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
500 SetTestDataPort(key, item_data.port); | 510 SetTestDataPort(key, item_data.port); |
501 SetTestDataString(key, kSecSecurityDomainItemAttr, | 511 SetTestDataString(key, kSecSecurityDomainItemAttr, |
502 item_data.security_domain); | 512 item_data.security_domain); |
503 SetTestDataString(key, kSecCreationDateItemAttr, item_data.creation_date); | 513 SetTestDataString(key, kSecCreationDateItemAttr, item_data.creation_date); |
504 SetTestDataString(key, kSecAccountItemAttr, item_data.username); | 514 SetTestDataString(key, kSecAccountItemAttr, item_data.username); |
505 SetTestDataPasswordString(key, item_data.password); | 515 SetTestDataPasswordString(key, item_data.password); |
506 SetTestDataNegativeItem(key, item_data.negative_item); | 516 SetTestDataNegativeItem(key, item_data.negative_item); |
507 } | 517 } |
508 | 518 |
509 } // namespace crypto | 519 } // namespace crypto |
OLD | NEW |