2 * Copyright (c) 2006-2010,2012-2013 Apple Inc. All Rights Reserved.
5 #include <CoreFoundation/CoreFoundation.h>
6 #include <Security/SecItem.h>
7 #include <Security/SecItemPriv.h>
8 #include <Security/SecBase.h>
9 #include <utilities/array_size.h>
10 #include <utilities/SecCFWrappers.h>
14 #include "Security_regressions.h"
16 // TODO: Test whether limit's works.
18 /* Test basic add delete update copy matching stuff. */
19 static void tests(void)
22 CFNumberRef eighty
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, &v_eighty
);
23 const char *v_data
= "test";
24 CFDataRef pwdata
= CFDataCreate(NULL
, (UInt8
*)v_data
, strlen(v_data
));
25 const void *keys
[] = {
31 kSecAttrAuthenticationType
,
34 const void *values
[] = {
35 kSecClassInternetPassword
,
36 CFSTR("members.spamcop.net"),
43 CFDictionaryRef item
= CFDictionaryCreate(NULL
, keys
, values
,
44 array_size(keys
), NULL
, NULL
);
45 ok_status(SecItemAdd(item
, NULL
), "add internet password");
46 is_status(SecItemAdd(item
, NULL
), errSecDuplicateItem
,
47 "add internet password again");
49 CFTypeRef results
= NULL
;
50 /* Create a dict with all attrs except the data. */
51 CFDictionaryRef query
= CFDictionaryCreate(NULL
, keys
, values
,
52 (array_size(keys
)) - 1, NULL
, NULL
);
53 ok_status(SecItemCopyMatching(query
, &results
), "find internet password");
54 CFReleaseNull(results
);
56 CFMutableDictionaryRef query2
= CFDictionaryCreateMutableCopy(kCFAllocatorDefault
, 0, query
);
57 CFDictionarySetValue(query2
, kSecReturnAttributes
, kCFBooleanTrue
);
58 CFDictionarySetValue(query2
, kSecMatchLimit
, kSecMatchLimitOne
);
59 ok_status(SecItemCopyMatching(query2
, &results
), "find internet password, return attributes");
60 CFReleaseNull(query2
);
61 query2
= CFDictionaryCreateMutableCopy(kCFAllocatorDefault
, 0, results
);
62 CFReleaseNull(results
);
63 CFDictionaryRemoveValue(query2
, kSecAttrSHA1
);
64 CFDictionarySetValue(query2
, kSecClass
, kSecClassInternetPassword
);
65 CFDictionarySetValue(query2
, kSecReturnData
, kCFBooleanTrue
);
66 ok_status(SecItemCopyMatching(query2
, &results
), "find internet password using returned attributes");
67 CFReleaseSafe(query2
);
68 ok(isData(results
) && CFEqual(results
, pwdata
), "retrieved data correctly");
69 CFReleaseNull(results
);
71 /* Modify the server attr of the item. */
72 const void *ch_keys
[] = {
76 const void *ch_values
[] = {
77 CFSTR("www.spamcop.net"),
78 kSecClassInternetPassword
,
80 CFDictionaryRef changes
= CFDictionaryCreate(NULL
, ch_keys
, ch_values
,
82 ok_status(SecItemUpdate(query
, changes
), "update internet password");
84 is_status(SecItemUpdate(query
, changes
), errSecItemNotFound
,
85 "update non-exisiting internet password again");
87 /* Delete the original item (which should fail since we modified it). */
88 is_status(SecItemDelete(query
), errSecItemNotFound
,
89 "delete non existing internet password");
90 ok_status(SecItemAdd(item
, NULL
), "add unmodified password");
91 ok_status(SecItemDelete(query
), "delete internet password");
93 ok_status(SecItemAdd(item
, NULL
), "add unmodified password");
94 is_status(SecItemUpdate(query
, changes
), errSecDuplicateItem
,
95 "update internet password causing dupe");
96 ok_status(SecItemDelete(query
), "delete internet password");
98 CFDictionaryRef changed_item
= CFDictionaryCreate(NULL
, ch_keys
, ch_values
,
99 array_size(ch_keys
), NULL
, NULL
);
100 ok_status(SecItemDelete(changed_item
), "delete changed internet password");
103 CFRelease(changed_item
);
132 int si_10_find_internet(int argc
, char *const *argv
)