]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_keychain/regressions/kc-20-item-delete-stress.c
Security-59306.140.5.tar.gz
[apple/security.git] / OSX / libsecurity_keychain / regressions / kc-20-item-delete-stress.c
1 /*
2 * Copyright (c) 2016 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the xLicense.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #import <Security/Security.h>
25
26 #include "keychain_regressions.h"
27 #include "kc-helpers.h"
28 #include "kc-item-helpers.h"
29
30 #include <dispatch/private.h>
31 #include <Security/Security.h>
32
33 #include <stdlib.h>
34
35 #define BLOCKS 30
36
37 static OSStatus callbackFunction(SecKeychainEvent keychainEvent,
38 SecKeychainCallbackInfo *info, void *context)
39 {
40 CFRetainSafe(info->item);
41 //printf("received a callback: %d %p\n", keychainEvent, info->item);
42 CFReleaseNull(info->item);
43
44 return 0;
45 }
46
47 static void tests() {
48 SecKeychainRef kc = getEmptyTestKeychain();
49
50 static dispatch_once_t onceToken = 0;
51 static dispatch_queue_t process_queue = NULL;
52 dispatch_once(&onceToken, ^{
53 process_queue = dispatch_queue_create("com.apple.security.item-add-queue", DISPATCH_QUEUE_CONCURRENT);
54
55 dispatch_queue_set_width(process_queue, 40);
56 });
57 dispatch_group_t g = dispatch_group_create();
58
59
60 // Run the CFRunLoop to clear out existing notifications
61 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0, false);
62
63 UInt32 didGetNotification = 0;
64 ok_status(SecKeychainAddCallback(callbackFunction, kSecAddEventMask | kSecDeleteEventMask, &didGetNotification), "add callback");
65
66 // Run the CFRunLoop to mark this run loop as "pumped"
67 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0, false);
68
69 for(int i = 0; i < BLOCKS; i++) {
70 dispatch_group_async(g, process_queue, ^() {
71 SecKeychainItemRef blockItem = NULL;
72 CFStringRef itemclass = kSecClassInternetPassword;
73
74 CFStringRef label = CFStringCreateWithFormat(NULL, NULL, CFSTR("testItem%05d"), i);
75 CFStringRef account = CFSTR("testAccount");
76 CFStringRef service = CFStringCreateWithFormat(NULL, NULL, CFSTR("testService%05d"), i);
77 char * name;
78 asprintf(&name, "%s (item %d)", testName, i);
79
80 // add the item
81 blockItem = createCustomItem(name, kc, createAddCustomItemDictionaryWithService(kc, itemclass, label, account, service));
82
83 ok_status(SecKeychainItemDelete(blockItem), "%s: SecKeychainItemDelete", name);
84 usleep(100 * arc4random_uniform(10000));
85 CFReleaseNull(blockItem);
86
87 free(name);
88 CFReleaseNull(label);
89 CFReleaseNull(service);
90 });
91 }
92
93 // Process run loop until every block has run
94 while(dispatch_group_wait(g, DISPATCH_TIME_NOW) != 0) {
95 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0, false);
96 }
97
98 // One last hurrah
99 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0, false);
100
101 ok_status(SecKeychainDelete(kc), "%s: SecKeychainDelete", testName);
102 CFReleaseNull(kc);
103 }
104
105 int kc_20_item_delete_stress(int argc, char *const *argv)
106 {
107 plan_tests(getEmptyTestKeychainTests + 1 + (createCustomItemTests + 1)*BLOCKS + 1);
108 initializeKeychainTests(__FUNCTION__);
109
110 tests();
111
112 deleteTestFiles();
113 return 0;
114 }