]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_keychain/regressions/kc-10-item-add-generic.c
Security-57740.31.2.tar.gz
[apple/security.git] / OSX / libsecurity_keychain / regressions / kc-10-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 "keychain_regressions.h"
7 #include "kc-helpers.h"
8
9 static void tests(void)
10 {
11 SecKeychainRef keychain = getPopulatedTestKeychain();
12 SecKeychainItemRef item = NULL;
13 ok_status(SecKeychainAddGenericPassword(keychain, 7, "service", 7,
14 "account", 4, "test", &item), "add generic password");
15 ok(item, "is item non NULL");
16 SecKeychainItemRef oldItem = item;
17 is_status(SecKeychainAddGenericPassword(keychain, 7, "service", 7,
18 "account", 4, "test", &oldItem),
19 errSecDuplicateItem, "add generic password again");
20 is((intptr_t)item, (intptr_t)oldItem, "item is unchanged");
21
22 SecItemClass itemClass = 0;
23 SecKeychainAttribute attrs[] =
24 {
25 { kSecAccountItemAttr },
26 { kSecServiceItemAttr }
27 };
28 SecKeychainAttributeList attrList = { sizeof(attrs) / sizeof(*attrs), attrs };
29 UInt32 length = 0;
30 void *data = NULL;
31 ok_status(SecKeychainItemCopyContent(item, &itemClass, &attrList, &length, &data), "SecKeychainItemCopyContent");
32 eq_string(data, "test", "Item data is wrong");
33 ok_status(SecKeychainItemFreeContent(&attrList, data), "SecKeychainItemCopyContent");
34
35 is(CFGetRetainCount(item), 1, "item retaincount is 1");
36 cmp_ok(CFGetRetainCount(keychain), >=, 2, "keychain retaincount is at least 2");
37 CFRelease(item);
38 cmp_ok(CFGetRetainCount(keychain), >=, 1, "keychain retaincount is at least 1");
39 ok_status(SecKeychainDelete(keychain), "delete keychain");
40 CFRelease(keychain);
41 }
42
43 int kc_10_item_add_generic(int argc, char *const *argv)
44 {
45 initializeKeychainTests("kc-10-item-add-generic");
46 plan_tests(13);
47
48 tests();
49
50 deleteTestFiles();
51 return 0;
52 }