]> git.saurik.com Git - apple/security.git/blob - SecurityTests/regressions/kc/kc-15-item-add-generic.c
Security-57031.1.35.tar.gz
[apple/security.git] / SecurityTests / regressions / kc / kc-15-item-add-generic.c
1 #include <Security/SecKeychain.h>
2 #include <Security/SecKeychainItem.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5
6 #include "testmore.h"
7 #include "testenv.h"
8 #include "testleaks.h"
9
10 void tests(void)
11 {
12 SecKeychainRef keychain;
13 ok_status(SecKeychainCreate("test", 4, "test", FALSE, NULL, &keychain),
14 "create keychain");
15 SecKeychainItemRef item = NULL;
16 ok_status(SecKeychainAddGenericPassword(keychain, 7, "service", 7,
17 "account", 4, "test", &item), "add generic password");
18 ok(item, "is item non NULL");
19 SecKeychainItemRef oldItem = item;
20 is_status(SecKeychainAddGenericPassword(keychain, 7, "service", 7,
21 "account", 4, "test", &oldItem),
22 errSecDuplicateItem, "add generic password again");
23 is((intptr_t)item, (intptr_t)oldItem, "item is unchanged");
24
25 SecItemClass itemClass = 0;
26 SecKeychainAttribute attrs[] =
27 {
28 { kSecAccountItemAttr },
29 { kSecServiceItemAttr }
30 };
31 SecKeychainAttributeList attrList = { sizeof(attrs) / sizeof(*attrs), attrs };
32 UInt32 length = 0;
33 void *data = NULL;
34 ok_status(SecKeychainItemCopyContent(item, &itemClass, &attrList, &length, &data), "SecKeychainItemCopyContent");
35 ok_status(SecKeychainItemFreeContent(&attrList, data), "SecKeychainItemCopyContent");
36
37 is(CFGetRetainCount(item), 1, "item retaincount is 1");
38 is(CFGetRetainCount(keychain), 2, "keychain retaincount is 2");
39 CFRelease(item);
40 is(CFGetRetainCount(keychain), 1, "keychain retaincount is 1");
41 ok_status(SecKeychainDelete(keychain), "delete keychain");
42 CFRelease(keychain);
43 }
44
45 int main(int argc, char *const *argv)
46 {
47 plan_tests(13);
48
49 if (!tests_begin(argc, argv))
50 BAIL_OUT("tests_begin failed");
51
52 tests();
53 ok(tests_end(1), "cleanup");
54 ok_leaks("no leaks");
55
56 return 0;
57 }