]> git.saurik.com Git - apple/security.git/blob - RegressionTests/secitemnotifications/secitemnotifications.m
Security-57740.1.18.tar.gz
[apple/security.git] / RegressionTests / secitemnotifications / secitemnotifications.m
1 //
2 // Copyright 2016 Apple. All rights reserved.
3 //
4
5 /*
6 * This is to fool os services to not provide the Keychain manager
7 * interface tht doens't work since we don't have unified headers
8 * between iOS and OS X. rdar://23405418/
9 */
10 #define __KEYCHAINCORE__ 1
11
12 #include <Foundation/Foundation.h>
13 #include <Security/Security.h>
14 #include <Security/SecItemPriv.h>
15 #include <notify.h>
16 #include <err.h>
17
18 int
19 main(int argc, const char ** argv)
20 {
21 dispatch_queue_t queue = dispatch_queue_create("notifications-queue", NULL);
22 __block int got_notification = false;
23 OSStatus status;
24 int token;
25
26 NSDictionary *query = @{
27 (id)kSecClass : (id)kSecClassGenericPassword,
28 (id)kSecAttrAccessGroup : @"keychain-test1",
29 (id)kSecAttrSyncViewHint : @"PCS-Master",
30 (id)kSecAttrAccount : @"account-delete-me",
31 (id)kSecAttrSynchronizable : (id)kCFBooleanTrue,
32 (id)kSecAttrAccessible : (id)kSecAttrAccessibleAfterFirstUnlock,
33 };
34 status = SecItemDelete((__bridge CFDictionaryRef)query);
35 if (status != errSecSuccess && status != errSecItemNotFound)
36 errx(1, "cleanup item: %d", (int)status);
37
38 notify_register_dispatch("com.apple.security.view-change.PCS", &token, queue, ^(int __unused token2) {
39 printf("got notification\n");
40 got_notification = true;
41 });
42
43 /*
44 * now check add notification
45 */
46
47 status = SecItemAdd((__bridge CFDictionaryRef)query, NULL);
48 if (status != errSecSuccess)
49 errx(1, "add item: %d", (int)status);
50
51 sleep(3);
52
53 if (!got_notification)
54 errx(1, "failed to get notification on add");
55 got_notification = false;
56
57 /*
58 * clean up and check delete notification too
59 */
60
61 status = SecItemDelete((__bridge CFDictionaryRef)query);
62 if (status != errSecSuccess)
63 errx(1, "cleanup2 item: %d", (int)status);
64
65 sleep(3);
66
67 if (!got_notification)
68 errx(1, "failed to get notification on delete");
69
70 return 0;
71 }