]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_keychain/regressions/kc-10-item-add-generic.c
Security-59754.80.3.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 is(length, strlen("test"), "item data is right length");
33 eq_stringn(data, length, "test", strlen("test"), "Item data is right");
34 ok_status(SecKeychainItemFreeContent(&attrList, data), "SecKeychainItemCopyContent");
35
36 is(CFGetRetainCount(item), 1, "item retaincount is 1");
37 cmp_ok(CFGetRetainCount(keychain), >=, 2, "keychain retaincount is at least 2");
38 CFRelease(item);
39 cmp_ok(CFGetRetainCount(keychain), >=, 1, "keychain retaincount is at least 1");
40 ok_status(SecKeychainDelete(keychain), "delete keychain");
41 CFRelease(keychain);
42 }
43
44 int kc_10_item_add_generic(int argc, char *const *argv)
45 {
46 initializeKeychainTests("kc-10-item-add-generic");
47 plan_tests(14);
48
49 tests();
50
51 deleteTestFiles();
52 return 0;
53 }