2 * Copyright (c) 2016 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #import <Security/Security.h>
26 #include "keychain_regressions.h"
27 #include "kc-helpers.h"
28 #include "kc-item-helpers.h"
30 #include <dispatch/private.h>
31 #include <Security/Security.h>
37 static OSStatus
callbackFunction(SecKeychainEvent keychainEvent
,
38 SecKeychainCallbackInfo
*info
, void *context
)
40 CFRetainSafe(info
->item
);
41 //printf("received a callback: %d %p\n", keychainEvent, info->item);
42 CFReleaseNull(info
->item
);
48 SecKeychainRef kc
= getEmptyTestKeychain();
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
);
55 dispatch_queue_set_width(process_queue
, 40);
57 dispatch_group_t g
= dispatch_group_create();
60 // Run the CFRunLoop to clear out existing notifications
61 CFRunLoopRunInMode(kCFRunLoopDefaultMode
, 1.0, false);
63 UInt32 didGetNotification
= 0;
64 ok_status(SecKeychainAddCallback(callbackFunction
, kSecAddEventMask
| kSecDeleteEventMask
, &didGetNotification
), "add callback");
66 // Run the CFRunLoop to mark this run loop as "pumped"
67 CFRunLoopRunInMode(kCFRunLoopDefaultMode
, 1.0, false);
69 for(int i
= 0; i
< BLOCKS
; i
++) {
70 dispatch_group_async(g
, process_queue
, ^() {
71 SecKeychainItemRef blockItem
= NULL
;
72 CFStringRef itemclass
= kSecClassInternetPassword
;
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
);
78 asprintf(&name
, "%s (item %d)", testName
, i
);
81 blockItem
= createCustomItem(name
, kc
, createAddCustomItemDictionaryWithService(kc
, itemclass
, label
, account
, service
));
83 ok_status(SecKeychainItemDelete(blockItem
), "%s: SecKeychainItemDelete", name
);
84 usleep(100 * arc4random_uniform(10000));
85 CFReleaseNull(blockItem
);
89 CFReleaseNull(service
);
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);
99 CFRunLoopRunInMode(kCFRunLoopDefaultMode
, 1.0, false);
101 ok_status(SecKeychainDelete(kc
), "%s: SecKeychainDelete", testName
);
105 int kc_20_item_delete_stress(int argc
, char *const *argv
)
107 plan_tests(getEmptyTestKeychainTests
+ 1 + (createCustomItemTests
+ 1)*BLOCKS
+ 1);
108 initializeKeychainTests(__FUNCTION__
);