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/SecBase.h>
8 #include <utilities/array_size.h>
9 #include <utilities/SecCFWrappers.h>
13 #include "Security_regressions.h"
15 // TODO: Test whether limit's works.
17 /* Test basic add delete update copy matching stuff. */
18 static void tests(void)
21 CFNumberRef eighty
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, &v_eighty
);
22 const char *v_data
= "test";
23 CFDataRef pwdata
= CFDataCreate(NULL
, (UInt8
*)v_data
, strlen(v_data
));
24 const void *keys
[] = {
30 kSecAttrAuthenticationType
,
33 const void *values
[] = {
34 kSecClassInternetPassword
,
35 CFSTR("members.spamcop.net"),
42 CFDictionaryRef item
= CFDictionaryCreate(NULL
, keys
, values
,
43 array_size(keys
), NULL
, NULL
);
44 ok_status(SecItemAdd(item
, NULL
), "add internet password");
45 is_status(SecItemAdd(item
, NULL
), errSecDuplicateItem
,
46 "add internet password again");
48 CFTypeRef results
= NULL
;
49 /* Create a dict with all attrs except the data. */
50 CFDictionaryRef query
= CFDictionaryCreate(NULL
, keys
, values
,
51 (array_size(keys
)) - 1, NULL
, NULL
);
52 ok_status(SecItemCopyMatching(query
, &results
), "find internet password");
53 CFReleaseNull(results
);
55 CFMutableDictionaryRef query2
= CFDictionaryCreateMutableCopy(kCFAllocatorDefault
, 0, query
);
56 CFDictionarySetValue(query2
, kSecReturnAttributes
, kCFBooleanTrue
);
57 CFDictionarySetValue(query2
, kSecMatchLimit
, kSecMatchLimitOne
);
58 ok_status(SecItemCopyMatching(query2
, &results
), "find internet password, return attributes");
59 CFReleaseNull(query2
);
60 query2
= CFDictionaryCreateMutableCopy(kCFAllocatorDefault
, 0, results
);
61 CFDictionarySetValue(query2
, kSecClass
, kSecClassInternetPassword
);
62 CFDictionarySetValue(query2
, kSecReturnData
, kCFBooleanTrue
);
63 ok_status(SecItemCopyMatching(query2
, &results
), "find internet password using returned attributes");
64 CFReleaseSafe(query2
);
65 ok(isData(results
) && CFEqual(results
, pwdata
), "retrieved data correctly");
66 CFReleaseNull(results
);
68 /* Modify the server attr of the item. */
69 const void *ch_keys
[] = {
73 const void *ch_values
[] = {
74 CFSTR("www.spamcop.net"),
75 kSecClassInternetPassword
,
77 CFDictionaryRef changes
= CFDictionaryCreate(NULL
, ch_keys
, ch_values
,
79 ok_status(SecItemUpdate(query
, changes
), "update internet password");
81 is_status(SecItemUpdate(query
, changes
), errSecItemNotFound
,
82 "update non-exisiting internet password again");
84 /* Delete the original item (which should fail since we modified it). */
85 is_status(SecItemDelete(query
), errSecItemNotFound
,
86 "delete non existing internet password");
87 ok_status(SecItemAdd(item
, NULL
), "add unmodified password");
88 ok_status(SecItemDelete(query
), "delete internet password");
90 ok_status(SecItemAdd(item
, NULL
), "add unmodified password");
91 is_status(SecItemUpdate(query
, changes
), errSecDuplicateItem
,
92 "update internet password causing dupe");
93 ok_status(SecItemDelete(query
), "delete internet password");
95 CFDictionaryRef changed_item
= CFDictionaryCreate(NULL
, ch_keys
, ch_values
,
96 array_size(ch_keys
), NULL
, NULL
);
97 ok_status(SecItemDelete(changed_item
), "delete changed internet password");
100 CFRelease(changed_item
);
129 int si_10_find_internet(int argc
, char *const *argv
)