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