]> git.saurik.com Git - apple/security.git/blob - SecurityTests/regressions/kc/kc-21-item-use-callback.c
Security-57031.1.35.tar.gz
[apple/security.git] / SecurityTests / regressions / kc / kc-21-item-use-callback.c
1 #include <Security/SecKeychainItem.h>
2 #include <Security/SecKeychain.h>
3 #include <CoreFoundation/CFRunLoop.h>
4 #include <assert.h>
5 #include <unistd.h>
6
7 #include "testmore.h"
8 #include "testenv.h"
9 #include "testleaks.h"
10
11 static char account[] = "account";
12 static char service[] = "service";
13 static char password[] = "password";
14
15 static void checkContent(SecKeychainItemRef itemRef)
16 {
17 SecItemClass itemClass;
18
19 SecKeychainAttribute attrs[] =
20 {
21 { kSecLabelItemAttr, 0, NULL },
22 { kSecAccountItemAttr, 0, NULL },
23 { kSecServiceItemAttr, 0, NULL }
24 };
25
26 SecKeychainAttributeList attrList =
27 { sizeof(attrs) / sizeof(*attrs), attrs };
28 UInt32 length;
29 void *data;
30
31 #if 1
32 ok_status(SecKeychainItemCopyContent(itemRef, &itemClass, &attrList,
33 &length, &data), "get item data in callback");
34 SKIP: {
35 skip("length mismatch", 1,
36 is(length, sizeof(password), "<rdar://problem/3867900> "
37 "SecKeychainItemCopyContent() returns bad data on items "
38 "from notifications"));
39
40 ok(!memcmp(password, data, length), "password data matches.");
41 }
42 #else
43 if (length != sizeof(password) || memcmp(password, data, length))
44 {
45 fprintf(stderr, "password '%.*s' not same as '%.*s'\n",
46 (int)sizeof(password), password,
47 (int)length, (char *)data);
48 }
49 #endif
50
51 ok_status(SecKeychainItemFreeContent(&attrList, data),
52 "free item data in callback");
53 }
54
55 static OSStatus callbackFunction(SecKeychainEvent keychainEvent,
56 SecKeychainCallbackInfo *info, void *context)
57 {
58 assert(keychainEvent == kSecAddEvent && context != NULL);
59 assert(info != NULL);
60 assert(info->item != NULL);
61
62 checkContent(info->item);
63 *((UInt32 *)context) = 1;
64
65 ok_status(SecKeychainItemDelete(info->item), "delete item");
66 return 0;
67 }
68
69 int
70 main(int argc, char *const *argv)
71 {
72 plan_tests(6);
73
74 ok(tests_begin(argc, argv), "setup");
75
76 UInt32 didGetNotification = 0;
77 ok_status(SecKeychainAddCallback(callbackFunction, kSecAddEventMask,
78 &didGetNotification), "add callback");
79
80 SecKeychainRef keychain;
81 ok_status(SecKeychainCreate("test", 4, "test", FALSE, NULL, &keychain),
82 "create keychain");
83 SecKeychainItemRef itemRef;
84 ok_status(SecKeychainAddGenericPassword(keychain,
85 sizeof(account), account,
86 sizeof(service), service,
87 sizeof(password), password,
88 &itemRef),
89 "add generic password, release and wait for callback");
90 //checkContent(itemRef);
91 CFRelease(itemRef);
92 CFRelease(keychain);
93
94 if (argc > 1 && !strcmp(argv[1], "-l")) {
95 printf("pid: %d\n", getpid());
96 sleep(100);
97 }
98 ok(tests_end(1), "cleanup");
99 ok_leaks("leaks");
100
101 return 0;
102 }