]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_keychain/regressions/kc-20-item-add-stress.c
Security-59306.140.5.tar.gz
[apple/security.git] / OSX / libsecurity_keychain / regressions / kc-20-item-add-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 <Security/Security.h>
31
32 #define BLOCKS 300
33
34 static void tests() {
35 SecKeychainRef kc = getPopulatedTestKeychain();
36
37 static dispatch_once_t onceToken = 0;
38 static dispatch_queue_t process_queue = NULL;
39 dispatch_once(&onceToken, ^{
40 process_queue = dispatch_queue_create("com.apple.security.item-add-queue", DISPATCH_QUEUE_CONCURRENT);
41 });
42 dispatch_group_t g = dispatch_group_create();
43
44 for(int i = 0; i < BLOCKS; i++) {
45 dispatch_group_async(g, process_queue, ^() {
46 SecKeychainItemRef blockItem = NULL;
47 CFStringRef itemclass = kSecClassInternetPassword;
48
49 CFStringRef label = CFStringCreateWithFormat(NULL, NULL, CFSTR("testItem%05d"), i);
50 CFStringRef account = CFSTR("testAccount");
51 CFStringRef service = CFStringCreateWithFormat(NULL, NULL, CFSTR("testService%05d"), i);
52 char * name;
53 asprintf(&name, "%s (item %d)", testName, i);
54
55 // add the item
56 blockItem = createCustomItem(name, kc, createAddCustomItemDictionaryWithService(kc, itemclass, label, account, service));
57 CFReleaseNull(blockItem);
58
59 // find the item
60 blockItem = checkNCopyFirst(name, createQueryCustomItemDictionaryWithService(kc, itemclass, label, service), 1);
61 readPasswordContents(blockItem, CFSTR("data"));
62
63 // update the item
64 CFMutableDictionaryRef newData = CFDictionaryCreateMutable(NULL, 0,
65 &kCFTypeDictionaryKeyCallBacks,
66 &kCFTypeDictionaryValueCallBacks);
67 CFDataRef differentdata = CFDataCreate(NULL, (void*)"differentdata", strlen("differentdata"));
68 CFDictionarySetValue(newData, kSecValueData, differentdata);
69 CFReleaseNull(differentdata);
70
71 CFDictionaryRef query = createQueryCustomItemDictionaryWithService(kc, itemclass, label, service);
72 ok_status(SecItemUpdate(query, newData), "%s: SecItemUpdate", name);
73 CFReleaseNull(query);
74 readPasswordContents(blockItem, CFSTR("differentdata"));
75
76 // delete the item
77 ok_status(SecKeychainItemDelete(blockItem), "%s: SecKeychainItemDelete", name);
78 CFReleaseNull(blockItem);
79 blockItem = checkNCopyFirst(name, createQueryCustomItemDictionaryWithService(kc, itemclass, label, service), 0);
80
81 free(name);
82 CFReleaseNull(label);
83 CFReleaseNull(service);
84 CFReleaseNull(blockItem);
85 });
86 }
87
88 dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
89
90 ok_status(SecKeychainDelete(kc), "%s: SecKeychainDelete", testName);
91 CFReleaseNull(kc);
92 }
93
94 int kc_20_item_add_stress(int argc, char *const *argv)
95 {
96 plan_tests(( makeItemTests + checkNTests + readPasswordContentsTests + 1 + readPasswordContentsTests + 1 + checkNTests )*BLOCKS + getPopulatedTestKeychainTests + 1);
97 initializeKeychainTests(__FUNCTION__);
98
99 tests();
100
101 deleteTestFiles();
102 return 0;
103 }