1 #include <Security/SecKeychainItem.h>
2 #include <Security/SecKeychain.h>
3 #include <CoreFoundation/CFRunLoop.h>
11 static char account
[] = "account";
12 static char service
[] = "service";
13 static char password
[] = "password";
15 static void checkContent(SecKeychainItemRef itemRef
)
17 SecItemClass itemClass
;
19 SecKeychainAttribute attrs
[] =
21 { kSecLabelItemAttr
, 0, NULL
},
22 { kSecAccountItemAttr
, 0, NULL
},
23 { kSecServiceItemAttr
, 0, NULL
}
26 SecKeychainAttributeList attrList
=
27 { sizeof(attrs
) / sizeof(*attrs
), attrs
};
32 ok_status(SecKeychainItemCopyContent(itemRef
, &itemClass
, &attrList
,
33 &length
, &data
), "get item data in callback");
35 skip("length mismatch", 1,
36 is(length
, sizeof(password
), "<rdar://problem/3867900> "
37 "SecKeychainItemCopyContent() returns bad data on items "
38 "from notifications"));
40 ok(!memcmp(password
, data
, length
), "password data matches.");
43 if (length
!= sizeof(password
) || memcmp(password
, data
, length
))
45 fprintf(stderr
, "password '%.*s' not same as '%.*s'\n",
46 (int)sizeof(password
), password
,
47 (int)length
, (char *)data
);
51 ok_status(SecKeychainItemFreeContent(&attrList
, data
),
52 "free item data in callback");
55 static OSStatus
callbackFunction(SecKeychainEvent keychainEvent
,
56 SecKeychainCallbackInfo
*info
, void *context
)
58 assert(keychainEvent
== kSecAddEvent
&& context
!= NULL
);
60 assert(info
->item
!= NULL
);
62 checkContent(info
->item
);
63 *((UInt32
*)context
) = 1;
65 ok_status(SecKeychainItemDelete(info
->item
), "delete item");
70 main(int argc
, char *const *argv
)
74 ok(tests_begin(argc
, argv
), "setup");
76 UInt32 didGetNotification
= 0;
77 ok_status(SecKeychainAddCallback(callbackFunction
, kSecAddEventMask
,
78 &didGetNotification
), "add callback");
80 SecKeychainRef keychain
;
81 ok_status(SecKeychainCreate("test", 4, "test", FALSE
, NULL
, &keychain
),
83 SecKeychainItemRef itemRef
;
84 ok_status(SecKeychainAddGenericPassword(keychain
,
85 sizeof(account
), account
,
86 sizeof(service
), service
,
87 sizeof(password
), password
,
89 "add generic password, release and wait for callback");
90 //checkContent(itemRef);
94 if (argc
> 1 && !strcmp(argv
[1], "-l")) {
95 printf("pid: %d\n", getpid());
98 ok(tests_end(1), "cleanup");