| 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 "chrome/browser/mock_keychain_mac.h" | 7 #include "crypto/mock_keychain_mac.h" |
| 8 |
| 9 namespace crypto { |
| 8 | 10 |
| 9 MockKeychain::MockKeychain() | 11 MockKeychain::MockKeychain() |
| 10 : next_item_key_(0), | 12 : next_item_key_(0), |
| 11 search_copy_count_(0), | 13 search_copy_count_(0), |
| 12 keychain_item_copy_count_(0), | 14 keychain_item_copy_count_(0), |
| 13 attribute_data_copy_count_(0), | 15 attribute_data_copy_count_(0), |
| 14 find_generic_result_(noErr), | 16 find_generic_result_(noErr), |
| 15 called_add_generic_(false), | 17 called_add_generic_(false), |
| 16 password_data_count_(0) {} | 18 password_data_count_(0) {} |
| 17 | 19 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 keychain_attr_list_[key].attr[i].length = data_size; | 57 keychain_attr_list_[key].attr[i].length = data_size; |
| 56 keychain_attr_list_[key].attr[i].data = calloc(1, data_size); | 58 keychain_attr_list_[key].attr[i].data = calloc(1, data_size); |
| 57 } | 59 } |
| 58 } | 60 } |
| 59 } | 61 } |
| 60 | 62 |
| 61 MockKeychain::~MockKeychain() { | 63 MockKeychain::~MockKeychain() { |
| 62 for (std::map<unsigned int, SecKeychainAttributeList>::iterator it = | 64 for (std::map<unsigned int, SecKeychainAttributeList>::iterator it = |
| 63 keychain_attr_list_.begin(); it != keychain_attr_list_.end(); ++it) { | 65 keychain_attr_list_.begin(); it != keychain_attr_list_.end(); ++it) { |
| 64 for (unsigned int i = 0; i < it->second.count; ++i) { | 66 for (unsigned int i = 0; i < it->second.count; ++i) { |
| 65 if (it->second.attr[i].data) { | 67 if (it->second.attr[i].data) |
| 66 free(it->second.attr[i].data); | 68 free(it->second.attr[i].data); |
| 67 } | |
| 68 } | 69 } |
| 69 free(it->second.attr); | 70 free(it->second.attr); |
| 70 if (keychain_data_[it->first].data) { | 71 if (keychain_data_[it->first].data) |
| 71 free(keychain_data_[it->first].data); | 72 free(keychain_data_[it->first].data); |
| 72 } | |
| 73 } | 73 } |
| 74 keychain_attr_list_.clear(); | 74 keychain_attr_list_.clear(); |
| 75 keychain_data_.clear(); | 75 keychain_data_.clear(); |
| 76 } | 76 } |
| 77 | 77 |
| 78 SecKeychainAttribute* MockKeychain::AttributeWithTag( | 78 SecKeychainAttribute* MockKeychain::AttributeWithTag( |
| 79 const SecKeychainAttributeList& attribute_list, UInt32 tag) { | 79 const SecKeychainAttributeList& attribute_list, |
| 80 UInt32 tag) { |
| 80 int attribute_index = -1; | 81 int attribute_index = -1; |
| 81 for (unsigned int i = 0; i < attribute_list.count; ++i) { | 82 for (unsigned int i = 0; i < attribute_list.count; ++i) { |
| 82 if (attribute_list.attr[i].tag == tag) { | 83 if (attribute_list.attr[i].tag == tag) { |
| 83 attribute_index = i; | 84 attribute_index = i; |
| 84 break; | 85 break; |
| 85 } | 86 } |
| 86 } | 87 } |
| 87 if (attribute_index == -1) { | 88 if (attribute_index == -1) { |
| 88 NOTREACHED() << "Unsupported attribute: " << tag; | 89 NOTREACHED() << "Unsupported attribute: " << tag; |
| 89 return NULL; | 90 return NULL; |
| 90 } | 91 } |
| 91 return &(attribute_list.attr[attribute_index]); | 92 return &(attribute_list.attr[attribute_index]); |
| 92 } | 93 } |
| 93 | 94 |
| 94 void MockKeychain::SetTestDataBytes(int item, UInt32 tag, const void* data, | 95 void MockKeychain::SetTestDataBytes(int item, |
| 96 UInt32 tag, |
| 97 const void* data, |
| 95 size_t length) { | 98 size_t length) { |
| 96 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], | 99 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], |
| 97 tag); | 100 tag); |
| 98 attribute->length = length; | 101 attribute->length = length; |
| 99 if (length > 0) { | 102 if (length > 0) { |
| 100 if (attribute->data) { | 103 if (attribute->data) |
| 101 free(attribute->data); | 104 free(attribute->data); |
| 102 } | |
| 103 attribute->data = malloc(length); | 105 attribute->data = malloc(length); |
| 104 CHECK(attribute->data); | 106 CHECK(attribute->data); |
| 105 memcpy(attribute->data, data, length); | 107 memcpy(attribute->data, data, length); |
| 106 } else { | 108 } else { |
| 107 attribute->data = NULL; | 109 attribute->data = NULL; |
| 108 } | 110 } |
| 109 } | 111 } |
| 110 | 112 |
| 111 void MockKeychain::SetTestDataString(int item, UInt32 tag, const char* value) { | 113 void MockKeychain::SetTestDataString(int item, UInt32 tag, const char* value) { |
| 112 SetTestDataBytes(item, tag, value, value ? strlen(value) : 0); | 114 SetTestDataBytes(item, tag, value, value ? strlen(value) : 0); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], | 147 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], |
| 146 kSecCreatorItemAttr); | 148 kSecCreatorItemAttr); |
| 147 OSType* data = static_cast<OSType*>(attribute->data); | 149 OSType* data = static_cast<OSType*>(attribute->data); |
| 148 *data = value; | 150 *data = value; |
| 149 } | 151 } |
| 150 | 152 |
| 151 void MockKeychain::SetTestDataPasswordBytes(int item, const void* data, | 153 void MockKeychain::SetTestDataPasswordBytes(int item, const void* data, |
| 152 size_t length) { | 154 size_t length) { |
| 153 keychain_data_[item].length = length; | 155 keychain_data_[item].length = length; |
| 154 if (length > 0) { | 156 if (length > 0) { |
| 155 if (keychain_data_[item].data) { | 157 if (keychain_data_[item].data) |
| 156 free(keychain_data_[item].data); | 158 free(keychain_data_[item].data); |
| 157 } | |
| 158 keychain_data_[item].data = malloc(length); | 159 keychain_data_[item].data = malloc(length); |
| 159 memcpy(keychain_data_[item].data, data, length); | 160 memcpy(keychain_data_[item].data, data, length); |
| 160 } else { | 161 } else { |
| 161 keychain_data_[item].data = NULL; | 162 keychain_data_[item].data = NULL; |
| 162 } | 163 } |
| 163 } | 164 } |
| 164 | 165 |
| 165 void MockKeychain::SetTestDataPasswordString(int item, const char* value) { | 166 void MockKeychain::SetTestDataPasswordString(int item, const char* value) { |
| 166 SetTestDataPasswordBytes(item, value, value ? strlen(value) : 0); | 167 SetTestDataPasswordBytes(item, value, value ? strlen(value) : 0); |
| 167 } | 168 } |
| 168 | 169 |
| 169 OSStatus MockKeychain::ItemCopyAttributesAndData( | 170 OSStatus MockKeychain::ItemCopyAttributesAndData( |
| 170 SecKeychainItemRef itemRef, SecKeychainAttributeInfo *info, | 171 SecKeychainItemRef itemRef, |
| 171 SecItemClass *itemClass, SecKeychainAttributeList **attrList, | 172 SecKeychainAttributeInfo* info, |
| 172 UInt32 *length, void **outData) const { | 173 SecItemClass* itemClass, |
| 174 SecKeychainAttributeList** attrList, |
| 175 UInt32* length, |
| 176 void** outData) const { |
| 173 DCHECK(itemRef); | 177 DCHECK(itemRef); |
| 174 unsigned int key = reinterpret_cast<unsigned int>(itemRef) - 1; | 178 unsigned int key = reinterpret_cast<unsigned int>(itemRef) - 1; |
| 175 if (keychain_attr_list_.find(key) == keychain_attr_list_.end()) { | 179 if (keychain_attr_list_.find(key) == keychain_attr_list_.end()) |
| 176 return errSecInvalidItemRef; | 180 return errSecInvalidItemRef; |
| 177 } | |
| 178 | 181 |
| 179 DCHECK(!itemClass); // itemClass not implemented in the Mock. | 182 DCHECK(!itemClass); // itemClass not implemented in the Mock. |
| 180 if (attrList) { | 183 if (attrList) |
| 181 *attrList = &(keychain_attr_list_[key]); | 184 *attrList = &(keychain_attr_list_[key]); |
| 182 } | |
| 183 if (outData) { | 185 if (outData) { |
| 184 *outData = keychain_data_[key].data; | 186 *outData = keychain_data_[key].data; |
| 185 DCHECK(length); | 187 DCHECK(length); |
| 186 *length = keychain_data_[key].length; | 188 *length = keychain_data_[key].length; |
| 187 } | 189 } |
| 188 | 190 |
| 189 ++attribute_data_copy_count_; | 191 ++attribute_data_copy_count_; |
| 190 return noErr; | 192 return noErr; |
| 191 } | 193 } |
| 192 | 194 |
| 193 OSStatus MockKeychain::ItemModifyAttributesAndData( | 195 OSStatus MockKeychain::ItemModifyAttributesAndData( |
| 194 SecKeychainItemRef itemRef, const SecKeychainAttributeList *attrList, | 196 SecKeychainItemRef itemRef, |
| 195 UInt32 length, const void *data) const { | 197 const SecKeychainAttributeList* attrList, |
| 198 UInt32 length, |
| 199 const void* data) const { |
| 196 DCHECK(itemRef); | 200 DCHECK(itemRef); |
| 197 const char* fail_trigger = "fail_me"; | 201 const char* fail_trigger = "fail_me"; |
| 198 if (length == strlen(fail_trigger) && | 202 if (length == strlen(fail_trigger) && |
| 199 memcmp(data, fail_trigger, length) == 0) { | 203 memcmp(data, fail_trigger, length) == 0) { |
| 200 return errSecAuthFailed; | 204 return errSecAuthFailed; |
| 201 } | 205 } |
| 202 | 206 |
| 203 unsigned int key = reinterpret_cast<unsigned int>(itemRef) - 1; | 207 unsigned int key = reinterpret_cast<unsigned int>(itemRef) - 1; |
| 204 if (keychain_attr_list_.find(key) == keychain_attr_list_.end()) { | 208 if (keychain_attr_list_.find(key) == keychain_attr_list_.end()) |
| 205 return errSecInvalidItemRef; | 209 return errSecInvalidItemRef; |
| 206 } | |
| 207 | 210 |
| 208 MockKeychain* mutable_this = const_cast<MockKeychain*>(this); | 211 MockKeychain* mutable_this = const_cast<MockKeychain*>(this); |
| 209 if (attrList) { | 212 if (attrList) { |
| 210 for (UInt32 change_attr = 0; change_attr < attrList->count; ++change_attr) { | 213 for (UInt32 change_attr = 0; change_attr < attrList->count; ++change_attr) { |
| 211 if (attrList->attr[change_attr].tag == kSecCreatorItemAttr) { | 214 if (attrList->attr[change_attr].tag == kSecCreatorItemAttr) { |
| 212 void* data = attrList->attr[change_attr].data; | 215 void* data = attrList->attr[change_attr].data; |
| 213 mutable_this->SetTestDataCreator(key, *(static_cast<OSType*>(data))); | 216 mutable_this->SetTestDataCreator(key, *(static_cast<OSType*>(data))); |
| 214 } else { | 217 } else { |
| 215 NOTIMPLEMENTED(); | 218 NOTIMPLEMENTED(); |
| 216 } | 219 } |
| 217 } | 220 } |
| 218 } | 221 } |
| 219 if (data) { | 222 if (data) |
| 220 mutable_this->SetTestDataPasswordBytes(key, data, length); | 223 mutable_this->SetTestDataPasswordBytes(key, data, length); |
| 221 } | |
| 222 return noErr; | 224 return noErr; |
| 223 } | 225 } |
| 224 | 226 |
| 225 OSStatus MockKeychain::ItemFreeAttributesAndData( | 227 OSStatus MockKeychain::ItemFreeAttributesAndData( |
| 226 SecKeychainAttributeList *attrList, | 228 SecKeychainAttributeList* attrList, |
| 227 void *data) const { | 229 void* data) const { |
| 228 --attribute_data_copy_count_; | 230 --attribute_data_copy_count_; |
| 229 return noErr; | 231 return noErr; |
| 230 } | 232 } |
| 231 | 233 |
| 232 OSStatus MockKeychain::ItemDelete(SecKeychainItemRef itemRef) const { | 234 OSStatus MockKeychain::ItemDelete(SecKeychainItemRef itemRef) const { |
| 233 unsigned int key = reinterpret_cast<unsigned int>(itemRef) - 1; | 235 unsigned int key = reinterpret_cast<unsigned int>(itemRef) - 1; |
| 234 | 236 |
| 235 for (unsigned int i = 0; i < keychain_attr_list_[key].count; ++i) { | 237 for (unsigned int i = 0; i < keychain_attr_list_[key].count; ++i) { |
| 236 if (keychain_attr_list_[key].attr[i].data) { | 238 if (keychain_attr_list_[key].attr[i].data) |
| 237 free(keychain_attr_list_[key].attr[i].data); | 239 free(keychain_attr_list_[key].attr[i].data); |
| 238 } | |
| 239 } | 240 } |
| 240 free(keychain_attr_list_[key].attr); | 241 free(keychain_attr_list_[key].attr); |
| 241 if (keychain_data_[key].data) { | 242 if (keychain_data_[key].data) |
| 242 free(keychain_data_[key].data); | 243 free(keychain_data_[key].data); |
| 243 } | |
| 244 | 244 |
| 245 keychain_attr_list_.erase(key); | 245 keychain_attr_list_.erase(key); |
| 246 keychain_data_.erase(key); | 246 keychain_data_.erase(key); |
| 247 added_via_api_.erase(key); | 247 added_via_api_.erase(key); |
| 248 return noErr; | 248 return noErr; |
| 249 } | 249 } |
| 250 | 250 |
| 251 OSStatus MockKeychain::SearchCreateFromAttributes( | 251 OSStatus MockKeychain::SearchCreateFromAttributes( |
| 252 CFTypeRef keychainOrArray, SecItemClass itemClass, | 252 CFTypeRef keychainOrArray, |
| 253 const SecKeychainAttributeList *attrList, | 253 SecItemClass itemClass, |
| 254 SecKeychainSearchRef *searchRef) const { | 254 const SecKeychainAttributeList* attrList, |
| 255 SecKeychainSearchRef* searchRef) const { |
| 255 // Figure out which of our mock items matches, and set up the array we'll use | 256 // Figure out which of our mock items matches, and set up the array we'll use |
| 256 // to generate results out of SearchCopyNext. | 257 // to generate results out of SearchCopyNext. |
| 257 remaining_search_results_.clear(); | 258 remaining_search_results_.clear(); |
| 258 for (std::map<unsigned int, SecKeychainAttributeList>::const_iterator it = | 259 for (std::map<unsigned int, SecKeychainAttributeList>::const_iterator it = |
| 259 keychain_attr_list_.begin(); it != keychain_attr_list_.end(); ++it) { | 260 keychain_attr_list_.begin(); it != keychain_attr_list_.end(); ++it) { |
| 260 bool mock_item_matches = true; | 261 bool mock_item_matches = true; |
| 261 for (UInt32 search_attr = 0; search_attr < attrList->count; ++search_attr) { | 262 for (UInt32 search_attr = 0; search_attr < attrList->count; ++search_attr) { |
| 262 SecKeychainAttribute* mock_attribute = | 263 SecKeychainAttribute* mock_attribute = |
| 263 AttributeWithTag(it->second, attrList->attr[search_attr].tag); | 264 AttributeWithTag(it->second, attrList->attr[search_attr].tag); |
| 264 if (mock_attribute->length != attrList->attr[search_attr].length || | 265 if (mock_attribute->length != attrList->attr[search_attr].length || |
| 265 memcmp(mock_attribute->data, attrList->attr[search_attr].data, | 266 memcmp(mock_attribute->data, attrList->attr[search_attr].data, |
| 266 attrList->attr[search_attr].length) != 0) { | 267 attrList->attr[search_attr].length) != 0) { |
| 267 mock_item_matches = false; | 268 mock_item_matches = false; |
| 268 break; | 269 break; |
| 269 } | 270 } |
| 270 } | 271 } |
| 271 if (mock_item_matches) { | 272 if (mock_item_matches) |
| 272 remaining_search_results_.push_back(it->first); | 273 remaining_search_results_.push_back(it->first); |
| 273 } | |
| 274 } | 274 } |
| 275 | 275 |
| 276 DCHECK(searchRef); | 276 DCHECK(searchRef); |
| 277 *searchRef = reinterpret_cast<SecKeychainSearchRef>(kDummySearchRef); | 277 *searchRef = reinterpret_cast<SecKeychainSearchRef>(kDummySearchRef); |
| 278 ++search_copy_count_; | 278 ++search_copy_count_; |
| 279 return noErr; | 279 return noErr; |
| 280 } | 280 } |
| 281 | 281 |
| 282 bool MockKeychain::AlreadyContainsInternetPassword( | 282 bool MockKeychain::AlreadyContainsInternetPassword( |
| 283 UInt32 serverNameLength, const char *serverName, | 283 UInt32 serverNameLength, |
| 284 UInt32 securityDomainLength, const char *securityDomain, | 284 const char* serverName, |
| 285 UInt32 accountNameLength, const char *accountName, | 285 UInt32 securityDomainLength, |
| 286 UInt32 pathLength, const char *path, | 286 const char* securityDomain, |
| 287 UInt16 port, SecProtocolType protocol, | 287 UInt32 accountNameLength, |
| 288 const char* accountName, |
| 289 UInt32 pathLength, |
| 290 const char* path, |
| 291 UInt16 port, |
| 292 SecProtocolType protocol, |
| 288 SecAuthenticationType authenticationType) const { | 293 SecAuthenticationType authenticationType) const { |
| 289 for (std::map<unsigned int, SecKeychainAttributeList>::const_iterator it = | 294 for (std::map<unsigned int, SecKeychainAttributeList>::const_iterator it = |
| 290 keychain_attr_list_.begin(); it != keychain_attr_list_.end(); ++it) { | 295 keychain_attr_list_.begin(); it != keychain_attr_list_.end(); ++it) { |
| 291 SecKeychainAttribute* attribute; | 296 SecKeychainAttribute* attribute; |
| 292 attribute = AttributeWithTag(it->second, kSecServerItemAttr); | 297 attribute = AttributeWithTag(it->second, kSecServerItemAttr); |
| 293 if ((attribute->length != serverNameLength) || | 298 if ((attribute->length != serverNameLength) || |
| 294 (attribute->data == NULL && *serverName != '\0') || | 299 (attribute->data == NULL && *serverName != '\0') || |
| 295 (attribute->data != NULL && *serverName == '\0') || | 300 (attribute->data != NULL && *serverName == '\0') || |
| 296 strncmp(serverName, | 301 strncmp(serverName, |
| 297 (const char*) attribute->data, | 302 (const char*) attribute->data, |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 } | 348 } |
| 344 // The keychain already has this item, since all fields other than the | 349 // The keychain already has this item, since all fields other than the |
| 345 // password match. | 350 // password match. |
| 346 return true; | 351 return true; |
| 347 } | 352 } |
| 348 return false; | 353 return false; |
| 349 } | 354 } |
| 350 | 355 |
| 351 OSStatus MockKeychain::AddInternetPassword( | 356 OSStatus MockKeychain::AddInternetPassword( |
| 352 SecKeychainRef keychain, | 357 SecKeychainRef keychain, |
| 353 UInt32 serverNameLength, const char *serverName, | 358 UInt32 serverNameLength, |
| 354 UInt32 securityDomainLength, const char *securityDomain, | 359 const char* serverName, |
| 355 UInt32 accountNameLength, const char *accountName, | 360 UInt32 securityDomainLength, |
| 356 UInt32 pathLength, const char *path, | 361 const char* securityDomain, |
| 357 UInt16 port, SecProtocolType protocol, | 362 UInt32 accountNameLength, |
| 363 const char* accountName, |
| 364 UInt32 pathLength, |
| 365 const char* path, |
| 366 UInt16 port, |
| 367 SecProtocolType protocol, |
| 358 SecAuthenticationType authenticationType, | 368 SecAuthenticationType authenticationType, |
| 359 UInt32 passwordLength, const void *passwordData, | 369 UInt32 passwordLength, |
| 360 SecKeychainItemRef *itemRef) const { | 370 const void* passwordData, |
| 371 SecKeychainItemRef* itemRef) const { |
| 361 | 372 |
| 362 // Check for the magic duplicate item trigger. | 373 // Check for the magic duplicate item trigger. |
| 363 if (strcmp(serverName, "some.domain.com") == 0) { | 374 if (strcmp(serverName, "some.domain.com") == 0) |
| 364 return errSecDuplicateItem; | 375 return errSecDuplicateItem; |
| 365 } | |
| 366 | 376 |
| 367 // If the account already exists in the keychain, we don't add it. | 377 // If the account already exists in the keychain, we don't add it. |
| 368 if (AlreadyContainsInternetPassword(serverNameLength, serverName, | 378 if (AlreadyContainsInternetPassword(serverNameLength, serverName, |
| 369 securityDomainLength, securityDomain, | 379 securityDomainLength, securityDomain, |
| 370 accountNameLength, accountName, | 380 accountNameLength, accountName, |
| 371 pathLength, path, | 381 pathLength, path, |
| 372 port, protocol, | 382 port, protocol, |
| 373 authenticationType)) { | 383 authenticationType)) { |
| 374 return errSecDuplicateItem; | 384 return errSecDuplicateItem; |
| 375 } | 385 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 404 added_via_api_.insert(key); | 414 added_via_api_.insert(key); |
| 405 | 415 |
| 406 if (itemRef) { | 416 if (itemRef) { |
| 407 *itemRef = reinterpret_cast<SecKeychainItemRef>(key + 1); | 417 *itemRef = reinterpret_cast<SecKeychainItemRef>(key + 1); |
| 408 ++keychain_item_copy_count_; | 418 ++keychain_item_copy_count_; |
| 409 } | 419 } |
| 410 return noErr; | 420 return noErr; |
| 411 } | 421 } |
| 412 | 422 |
| 413 OSStatus MockKeychain::SearchCopyNext(SecKeychainSearchRef searchRef, | 423 OSStatus MockKeychain::SearchCopyNext(SecKeychainSearchRef searchRef, |
| 414 SecKeychainItemRef *itemRef) const { | 424 SecKeychainItemRef* itemRef) const { |
| 415 if (remaining_search_results_.empty()) { | 425 if (remaining_search_results_.empty()) |
| 416 return errSecItemNotFound; | 426 return errSecItemNotFound; |
| 417 } | |
| 418 unsigned int key = remaining_search_results_.front(); | 427 unsigned int key = remaining_search_results_.front(); |
| 419 remaining_search_results_.erase(remaining_search_results_.begin()); | 428 remaining_search_results_.erase(remaining_search_results_.begin()); |
| 420 *itemRef = reinterpret_cast<SecKeychainItemRef>(key + 1); | 429 *itemRef = reinterpret_cast<SecKeychainItemRef>(key + 1); |
| 421 ++keychain_item_copy_count_; | 430 ++keychain_item_copy_count_; |
| 422 return noErr; | 431 return noErr; |
| 423 } | 432 } |
| 424 | 433 |
| 425 OSStatus MockKeychain::FindGenericPassword(CFTypeRef keychainOrArray, | 434 OSStatus MockKeychain::FindGenericPassword(CFTypeRef keychainOrArray, |
| 426 UInt32 serviceNameLength, | 435 UInt32 serviceNameLength, |
| 427 const char *serviceName, | 436 const char* serviceName, |
| 428 UInt32 accountNameLength, | 437 UInt32 accountNameLength, |
| 429 const char *accountName, | 438 const char* accountName, |
| 430 UInt32 *passwordLength, | 439 UInt32* passwordLength, |
| 431 void **passwordData, | 440 void** passwordData, |
| 432 SecKeychainItemRef *itemRef) const { | 441 SecKeychainItemRef* itemRef) const { |
| 433 // When simulating |noErr| we return canned |passwordData| and | 442 // When simulating |noErr| we return canned |passwordData| and |
| 434 // |passwordLenght|. Otherwise, just return given code. | 443 // |passwordLenght|. Otherwise, just return given code. |
| 435 if (find_generic_result_ == noErr) { | 444 if (find_generic_result_ == noErr) { |
| 436 static char password[] = "my_password"; | 445 static char password[] = "my_password"; |
| 437 | 446 |
| 438 DCHECK(passwordData); | 447 DCHECK(passwordData); |
| 439 *passwordData = static_cast<void*>(password); | 448 *passwordData = static_cast<void*>(password); |
| 440 DCHECK(passwordLength); | 449 DCHECK(passwordLength); |
| 441 *passwordLength = strlen(password); | 450 *passwordLength = strlen(password); |
| 442 password_data_count_++; | 451 password_data_count_++; |
| 443 } | 452 } |
| 444 | 453 |
| 445 return find_generic_result_; | 454 return find_generic_result_; |
| 446 } | 455 } |
| 447 | 456 |
| 448 OSStatus MockKeychain::ItemFreeContent(SecKeychainAttributeList *attrList, | 457 OSStatus MockKeychain::ItemFreeContent(SecKeychainAttributeList* attrList, |
| 449 void *data) const { | 458 void* data) const { |
| 450 // No-op. | 459 // No-op. |
| 451 password_data_count_--; | 460 password_data_count_--; |
| 452 return noErr; | 461 return noErr; |
| 453 } | 462 } |
| 454 | 463 |
| 455 OSStatus MockKeychain::AddGenericPassword(SecKeychainRef keychain, | 464 OSStatus MockKeychain::AddGenericPassword(SecKeychainRef keychain, |
| 456 UInt32 serviceNameLength, | 465 UInt32 serviceNameLength, |
| 457 const char *serviceName, | 466 const char* serviceName, |
| 458 UInt32 accountNameLength, | 467 UInt32 accountNameLength, |
| 459 const char *accountName, | 468 const char* accountName, |
| 460 UInt32 passwordLength, | 469 UInt32 passwordLength, |
| 461 const void *passwordData, | 470 const void* passwordData, |
| 462 SecKeychainItemRef *itemRef) const { | 471 SecKeychainItemRef* itemRef) const { |
| 463 called_add_generic_ = true; | 472 called_add_generic_ = true; |
| 464 | 473 |
| 465 DCHECK(passwordLength > 0); | 474 DCHECK(passwordLength > 0); |
| 466 DCHECK(passwordData); | 475 DCHECK(passwordData); |
| 467 add_generic_password_ = | 476 add_generic_password_ = |
| 468 std::string(const_cast<char*>(static_cast<const char*>(passwordData)), | 477 std::string(const_cast<char*>(static_cast<const char*>(passwordData)), |
| 469 passwordLength); | 478 passwordLength); |
| 470 return noErr; | 479 return noErr; |
| 471 } | 480 } |
| 472 | 481 |
| 473 void MockKeychain::Free(CFTypeRef ref) const { | 482 void MockKeychain::Free(CFTypeRef ref) const { |
| 474 if (!ref) { | 483 if (!ref) |
| 475 return; | 484 return; |
| 476 } | |
| 477 | 485 |
| 478 if (reinterpret_cast<int>(ref) == kDummySearchRef) { | 486 if (reinterpret_cast<int>(ref) == kDummySearchRef) { |
| 479 --search_copy_count_; | 487 --search_copy_count_; |
| 480 } else { | 488 } else { |
| 481 --keychain_item_copy_count_; | 489 --keychain_item_copy_count_; |
| 482 } | 490 } |
| 483 } | 491 } |
| 484 | 492 |
| 485 int MockKeychain::UnfreedSearchCount() const { | 493 int MockKeychain::UnfreedSearchCount() const { |
| 486 return search_copy_count_; | 494 return search_copy_count_; |
| 487 } | 495 } |
| 488 | 496 |
| 489 int MockKeychain::UnfreedKeychainItemCount() const { | 497 int MockKeychain::UnfreedKeychainItemCount() const { |
| 490 return keychain_item_copy_count_; | 498 return keychain_item_copy_count_; |
| 491 } | 499 } |
| 492 | 500 |
| 493 int MockKeychain::UnfreedAttributeDataCount() const { | 501 int MockKeychain::UnfreedAttributeDataCount() const { |
| 494 return attribute_data_copy_count_; | 502 return attribute_data_copy_count_; |
| 495 } | 503 } |
| 496 | 504 |
| 497 bool MockKeychain::CreatorCodesSetForAddedItems() const { | 505 bool MockKeychain::CreatorCodesSetForAddedItems() const { |
| 498 for (std::set<unsigned int>::const_iterator i = added_via_api_.begin(); | 506 for (std::set<unsigned int>::const_iterator i = added_via_api_.begin(); |
| 499 i != added_via_api_.end(); ++i) { | 507 i != added_via_api_.end(); ++i) { |
| 500 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[*i], | 508 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[*i], |
| 501 kSecCreatorItemAttr); | 509 kSecCreatorItemAttr); |
| 502 OSType* data = static_cast<OSType*>(attribute->data); | 510 OSType* data = static_cast<OSType*>(attribute->data); |
| 503 if (*data == 0) { | 511 if (*data == 0) |
| 504 return false; | 512 return false; |
| 505 } | |
| 506 } | 513 } |
| 507 return true; | 514 return true; |
| 508 } | 515 } |
| 509 | 516 |
| 510 void MockKeychain::AddTestItem(const KeychainTestData& item_data) { | 517 void MockKeychain::AddTestItem(const KeychainTestData& item_data) { |
| 511 unsigned int key = next_item_key_++; | 518 unsigned int key = next_item_key_++; |
| 512 | 519 |
| 513 InitializeKeychainData(key); | 520 InitializeKeychainData(key); |
| 514 SetTestDataAuthType(key, item_data.auth_type); | 521 SetTestDataAuthType(key, item_data.auth_type); |
| 515 SetTestDataString(key, kSecServerItemAttr, item_data.server); | 522 SetTestDataString(key, kSecServerItemAttr, item_data.server); |
| 516 SetTestDataProtocol(key, item_data.protocol); | 523 SetTestDataProtocol(key, item_data.protocol); |
| 517 SetTestDataString(key, kSecPathItemAttr, item_data.path); | 524 SetTestDataString(key, kSecPathItemAttr, item_data.path); |
| 518 SetTestDataPort(key, item_data.port); | 525 SetTestDataPort(key, item_data.port); |
| 519 SetTestDataString(key, kSecSecurityDomainItemAttr, | 526 SetTestDataString(key, kSecSecurityDomainItemAttr, |
| 520 item_data.security_domain); | 527 item_data.security_domain); |
| 521 SetTestDataString(key, kSecCreationDateItemAttr, item_data.creation_date); | 528 SetTestDataString(key, kSecCreationDateItemAttr, item_data.creation_date); |
| 522 SetTestDataString(key, kSecAccountItemAttr, item_data.username); | 529 SetTestDataString(key, kSecAccountItemAttr, item_data.username); |
| 523 SetTestDataPasswordString(key, item_data.password); | 530 SetTestDataPasswordString(key, item_data.password); |
| 524 SetTestDataNegativeItem(key, item_data.negative_item); | 531 SetTestDataNegativeItem(key, item_data.negative_item); |
| 525 } | 532 } |
| 533 |
| 534 } // namespace crypto |
| OLD | NEW |