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