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 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 break; | 85 break; |
86 } | 86 } |
87 } | 87 } |
88 if (attribute_index == -1) { | 88 if (attribute_index == -1) { |
89 NOTREACHED() << "Unsupported attribute: " << tag; | 89 NOTREACHED() << "Unsupported attribute: " << tag; |
90 return NULL; | 90 return NULL; |
91 } | 91 } |
92 return &(attribute_list.attr[attribute_index]); | 92 return &(attribute_list.attr[attribute_index]); |
93 } | 93 } |
94 | 94 |
95 void MockKeychain::SetTestDataBytes(int item, | 95 void MockKeychain::SetTestDataBytes(unsigned int item, |
96 UInt32 tag, | 96 UInt32 tag, |
97 const void* data, | 97 const void* data, |
98 size_t length) { | 98 size_t length) { |
99 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], | 99 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], |
100 tag); | 100 tag); |
101 attribute->length = length; | 101 attribute->length = length; |
102 if (length > 0) { | 102 if (length > 0) { |
103 if (attribute->data) | 103 if (attribute->data) |
104 free(attribute->data); | 104 free(attribute->data); |
105 attribute->data = malloc(length); | 105 attribute->data = malloc(length); |
106 CHECK(attribute->data); | 106 CHECK(attribute->data); |
107 memcpy(attribute->data, data, length); | 107 memcpy(attribute->data, data, length); |
108 } else { | 108 } else { |
109 attribute->data = NULL; | 109 attribute->data = NULL; |
110 } | 110 } |
111 } | 111 } |
112 | 112 |
113 void MockKeychain::SetTestDataString(int item, UInt32 tag, const char* value) { | 113 void MockKeychain::SetTestDataString(unsigned int item, |
| 114 UInt32 tag, |
| 115 const char* value) { |
114 SetTestDataBytes(item, tag, value, value ? strlen(value) : 0); | 116 SetTestDataBytes(item, tag, value, value ? strlen(value) : 0); |
115 } | 117 } |
116 | 118 |
117 void MockKeychain::SetTestDataPort(int item, UInt32 value) { | 119 void MockKeychain::SetTestDataPort(unsigned int item, UInt32 value) { |
118 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], | 120 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], |
119 kSecPortItemAttr); | 121 kSecPortItemAttr); |
120 UInt32* data = static_cast<UInt32*>(attribute->data); | 122 UInt32* data = static_cast<UInt32*>(attribute->data); |
121 *data = value; | 123 *data = value; |
122 } | 124 } |
123 | 125 |
124 void MockKeychain::SetTestDataProtocol(int item, SecProtocolType value) { | 126 void MockKeychain::SetTestDataProtocol(unsigned int item, SecProtocolType value)
{ |
125 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], | 127 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], |
126 kSecProtocolItemAttr); | 128 kSecProtocolItemAttr); |
127 SecProtocolType* data = static_cast<SecProtocolType*>(attribute->data); | 129 SecProtocolType* data = static_cast<SecProtocolType*>(attribute->data); |
128 *data = value; | 130 *data = value; |
129 } | 131 } |
130 | 132 |
131 void MockKeychain::SetTestDataAuthType(int item, SecAuthenticationType value) { | 133 void MockKeychain::SetTestDataAuthType(unsigned int item, SecAuthenticationType
value) { |
132 SecKeychainAttribute* attribute = AttributeWithTag( | 134 SecKeychainAttribute* attribute = AttributeWithTag( |
133 keychain_attr_list_[item], kSecAuthenticationTypeItemAttr); | 135 keychain_attr_list_[item], kSecAuthenticationTypeItemAttr); |
134 SecAuthenticationType* data = static_cast<SecAuthenticationType*>( | 136 SecAuthenticationType* data = static_cast<SecAuthenticationType*>( |
135 attribute->data); | 137 attribute->data); |
136 *data = value; | 138 *data = value; |
137 } | 139 } |
138 | 140 |
139 void MockKeychain::SetTestDataNegativeItem(int item, Boolean value) { | 141 void MockKeychain::SetTestDataNegativeItem(unsigned int item, Boolean value) { |
140 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], | 142 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], |
141 kSecNegativeItemAttr); | 143 kSecNegativeItemAttr); |
142 Boolean* data = static_cast<Boolean*>(attribute->data); | 144 Boolean* data = static_cast<Boolean*>(attribute->data); |
143 *data = value; | 145 *data = value; |
144 } | 146 } |
145 | 147 |
146 void MockKeychain::SetTestDataCreator(int item, OSType value) { | 148 void MockKeychain::SetTestDataCreator(unsigned int item, OSType value) { |
147 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], | 149 SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], |
148 kSecCreatorItemAttr); | 150 kSecCreatorItemAttr); |
149 OSType* data = static_cast<OSType*>(attribute->data); | 151 OSType* data = static_cast<OSType*>(attribute->data); |
150 *data = value; | 152 *data = value; |
151 } | 153 } |
152 | 154 |
153 void MockKeychain::SetTestDataPasswordBytes(int item, const void* data, | 155 void MockKeychain::SetTestDataPasswordBytes(unsigned int item, const void* data, |
154 size_t length) { | 156 size_t length) { |
155 keychain_data_[item].length = length; | 157 keychain_data_[item].length = length; |
156 if (length > 0) { | 158 if (length > 0) { |
157 if (keychain_data_[item].data) | 159 if (keychain_data_[item].data) |
158 free(keychain_data_[item].data); | 160 free(keychain_data_[item].data); |
159 keychain_data_[item].data = malloc(length); | 161 keychain_data_[item].data = malloc(length); |
160 memcpy(keychain_data_[item].data, data, length); | 162 memcpy(keychain_data_[item].data, data, length); |
161 } else { | 163 } else { |
162 keychain_data_[item].data = NULL; | 164 keychain_data_[item].data = NULL; |
163 } | 165 } |
164 } | 166 } |
165 | 167 |
166 void MockKeychain::SetTestDataPasswordString(int item, const char* value) { | 168 void MockKeychain::SetTestDataPasswordString(unsigned int item, |
| 169 const char* value) { |
167 SetTestDataPasswordBytes(item, value, value ? strlen(value) : 0); | 170 SetTestDataPasswordBytes(item, value, value ? strlen(value) : 0); |
168 } | 171 } |
169 | 172 |
170 OSStatus MockKeychain::ItemCopyAttributesAndData( | 173 OSStatus MockKeychain::ItemCopyAttributesAndData( |
171 SecKeychainItemRef itemRef, | 174 SecKeychainItemRef itemRef, |
172 SecKeychainAttributeInfo* info, | 175 SecKeychainAttributeInfo* info, |
173 SecItemClass* itemClass, | 176 SecItemClass* itemClass, |
174 SecKeychainAttributeList** attrList, | 177 SecKeychainAttributeList** attrList, |
175 UInt32* length, | 178 UInt32* length, |
176 void** outData) const { | 179 void** outData) const { |
177 DCHECK(itemRef); | 180 DCHECK(itemRef); |
178 unsigned int key = reinterpret_cast<unsigned int>(itemRef) - 1; | 181 unsigned int key = static_cast<unsigned int>(reinterpret_cast<uintptr_t>(itemR
ef)) - 1; |
179 if (keychain_attr_list_.find(key) == keychain_attr_list_.end()) | 182 if (keychain_attr_list_.find(key) == keychain_attr_list_.end()) |
180 return errSecInvalidItemRef; | 183 return errSecInvalidItemRef; |
181 | 184 |
182 DCHECK(!itemClass); // itemClass not implemented in the Mock. | 185 DCHECK(!itemClass); // itemClass not implemented in the Mock. |
183 if (attrList) | 186 if (attrList) |
184 *attrList = &(keychain_attr_list_[key]); | 187 *attrList = &(keychain_attr_list_[key]); |
185 if (outData) { | 188 if (outData) { |
186 *outData = keychain_data_[key].data; | 189 *outData = keychain_data_[key].data; |
187 DCHECK(length); | 190 DCHECK(length); |
188 *length = keychain_data_[key].length; | 191 *length = keychain_data_[key].length; |
189 } | 192 } |
190 | 193 |
191 ++attribute_data_copy_count_; | 194 ++attribute_data_copy_count_; |
192 return noErr; | 195 return noErr; |
193 } | 196 } |
194 | 197 |
195 OSStatus MockKeychain::ItemModifyAttributesAndData( | 198 OSStatus MockKeychain::ItemModifyAttributesAndData( |
196 SecKeychainItemRef itemRef, | 199 SecKeychainItemRef itemRef, |
197 const SecKeychainAttributeList* attrList, | 200 const SecKeychainAttributeList* attrList, |
198 UInt32 length, | 201 UInt32 length, |
199 const void* data) const { | 202 const void* data) const { |
200 DCHECK(itemRef); | 203 DCHECK(itemRef); |
201 const char* fail_trigger = "fail_me"; | 204 const char* fail_trigger = "fail_me"; |
202 if (length == strlen(fail_trigger) && | 205 if (length == strlen(fail_trigger) && |
203 memcmp(data, fail_trigger, length) == 0) { | 206 memcmp(data, fail_trigger, length) == 0) { |
204 return errSecAuthFailed; | 207 return errSecAuthFailed; |
205 } | 208 } |
206 | 209 |
207 unsigned int key = reinterpret_cast<unsigned int>(itemRef) - 1; | 210 unsigned int key = static_cast<unsigned int>(reinterpret_cast<uintptr_t>(itemR
ef)) - 1; |
208 if (keychain_attr_list_.find(key) == keychain_attr_list_.end()) | 211 if (keychain_attr_list_.find(key) == keychain_attr_list_.end()) |
209 return errSecInvalidItemRef; | 212 return errSecInvalidItemRef; |
210 | 213 |
211 MockKeychain* mutable_this = const_cast<MockKeychain*>(this); | 214 MockKeychain* mutable_this = const_cast<MockKeychain*>(this); |
212 if (attrList) { | 215 if (attrList) { |
213 for (UInt32 change_attr = 0; change_attr < attrList->count; ++change_attr) { | 216 for (UInt32 change_attr = 0; change_attr < attrList->count; ++change_attr) { |
214 if (attrList->attr[change_attr].tag == kSecCreatorItemAttr) { | 217 if (attrList->attr[change_attr].tag == kSecCreatorItemAttr) { |
215 void* data = attrList->attr[change_attr].data; | 218 void* data = attrList->attr[change_attr].data; |
216 mutable_this->SetTestDataCreator(key, *(static_cast<OSType*>(data))); | 219 mutable_this->SetTestDataCreator(key, *(static_cast<OSType*>(data))); |
217 } else { | 220 } else { |
218 NOTIMPLEMENTED(); | 221 NOTIMPLEMENTED(); |
219 } | 222 } |
220 } | 223 } |
221 } | 224 } |
222 if (data) | 225 if (data) |
223 mutable_this->SetTestDataPasswordBytes(key, data, length); | 226 mutable_this->SetTestDataPasswordBytes(key, data, length); |
224 return noErr; | 227 return noErr; |
225 } | 228 } |
226 | 229 |
227 OSStatus MockKeychain::ItemFreeAttributesAndData( | 230 OSStatus MockKeychain::ItemFreeAttributesAndData( |
228 SecKeychainAttributeList* attrList, | 231 SecKeychainAttributeList* attrList, |
229 void* data) const { | 232 void* data) const { |
230 --attribute_data_copy_count_; | 233 --attribute_data_copy_count_; |
231 return noErr; | 234 return noErr; |
232 } | 235 } |
233 | 236 |
234 OSStatus MockKeychain::ItemDelete(SecKeychainItemRef itemRef) const { | 237 OSStatus MockKeychain::ItemDelete(SecKeychainItemRef itemRef) const { |
235 unsigned int key = reinterpret_cast<unsigned int>(itemRef) - 1; | 238 unsigned int key = static_cast<unsigned int>(reinterpret_cast<uintptr_t>(itemR
ef)) - 1; |
236 | 239 |
237 for (unsigned int i = 0; i < keychain_attr_list_[key].count; ++i) { | 240 for (unsigned int i = 0; i < keychain_attr_list_[key].count; ++i) { |
238 if (keychain_attr_list_[key].attr[i].data) | 241 if (keychain_attr_list_[key].attr[i].data) |
239 free(keychain_attr_list_[key].attr[i].data); | 242 free(keychain_attr_list_[key].attr[i].data); |
240 } | 243 } |
241 free(keychain_attr_list_[key].attr); | 244 free(keychain_attr_list_[key].attr); |
242 if (keychain_data_[key].data) | 245 if (keychain_data_[key].data) |
243 free(keychain_data_[key].data); | 246 free(keychain_data_[key].data); |
244 | 247 |
245 keychain_attr_list_.erase(key); | 248 keychain_attr_list_.erase(key); |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
476 add_generic_password_ = | 479 add_generic_password_ = |
477 std::string(const_cast<char*>(static_cast<const char*>(passwordData)), | 480 std::string(const_cast<char*>(static_cast<const char*>(passwordData)), |
478 passwordLength); | 481 passwordLength); |
479 return noErr; | 482 return noErr; |
480 } | 483 } |
481 | 484 |
482 void MockKeychain::Free(CFTypeRef ref) const { | 485 void MockKeychain::Free(CFTypeRef ref) const { |
483 if (!ref) | 486 if (!ref) |
484 return; | 487 return; |
485 | 488 |
486 if (reinterpret_cast<int>(ref) == kDummySearchRef) { | 489 if (static_cast<unsigned int>(reinterpret_cast<uintptr_t>(ref)) == kDummySearc
hRef) { |
487 --search_copy_count_; | 490 --search_copy_count_; |
488 } else { | 491 } else { |
489 --keychain_item_copy_count_; | 492 --keychain_item_copy_count_; |
490 } | 493 } |
491 } | 494 } |
492 | 495 |
493 int MockKeychain::UnfreedSearchCount() const { | 496 int MockKeychain::UnfreedSearchCount() const { |
494 return search_copy_count_; | 497 return search_copy_count_; |
495 } | 498 } |
496 | 499 |
(...skipping 28 matching lines...) Expand all Loading... |
525 SetTestDataPort(key, item_data.port); | 528 SetTestDataPort(key, item_data.port); |
526 SetTestDataString(key, kSecSecurityDomainItemAttr, | 529 SetTestDataString(key, kSecSecurityDomainItemAttr, |
527 item_data.security_domain); | 530 item_data.security_domain); |
528 SetTestDataString(key, kSecCreationDateItemAttr, item_data.creation_date); | 531 SetTestDataString(key, kSecCreationDateItemAttr, item_data.creation_date); |
529 SetTestDataString(key, kSecAccountItemAttr, item_data.username); | 532 SetTestDataString(key, kSecAccountItemAttr, item_data.username); |
530 SetTestDataPasswordString(key, item_data.password); | 533 SetTestDataPasswordString(key, item_data.password); |
531 SetTestDataNegativeItem(key, item_data.negative_item); | 534 SetTestDataNegativeItem(key, item_data.negative_item); |
532 } | 535 } |
533 | 536 |
534 } // namespace crypto | 537 } // namespace crypto |
OLD | NEW |