2 * Copyright (c) 2013-2014 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 License.
21 * @APPLE_LICENSE_HEADER_END@
25 * This is to fool os services to not provide the Keychain manager
26 * interface tht doens't work since we don't have unified headers
27 * between iOS and OS X. rdar://23405418/
29 #define __KEYCHAINCORE__ 1
32 #import "secd_regressions.h"
34 #import <Foundation/Foundation.h>
35 #import "keychain/securityd/SecDbItem.h"
36 #import <utilities/array_size.h>
37 #import <utilities/SecCFWrappers.h>
38 #import <utilities/SecFileLocations.h>
39 #import <utilities/fileIo.h>
41 #import "keychain/securityd/SecItemServer.h"
43 #import <Security/SecBasePriv.h>
45 #import <AssertMacros.h>
52 #import "SecdTestKeychainUtilities.h"
55 #define N_THREADS (10)
58 static void *do_add(void *arg)
62 for(int i=0;i<N_ADDS;i++) {
63 /* Creating a password */
64 SInt32 v_eighty = (tid+1)*1000+i;
65 CFNumberRef eighty = CFNumberCreate(NULL, kCFNumberSInt32Type, &v_eighty);
66 const char *v_data = "test";
67 CFDataRef pwdata = CFDataCreate(NULL, (UInt8 *)v_data, strlen(v_data));
68 const void *keys[] = {
74 kSecAttrAuthenticationType,
77 const void *values[] = {
78 kSecClassInternetPassword,
79 CFSTR("members.spamcop.net"),
87 CFDictionaryRef item = CFDictionaryCreate(NULL, keys, values,
88 array_size(keys), NULL, NULL);
90 ok_status(SecItemAdd(item, NULL), "add internet password");
91 CFReleaseNull(eighty);
92 CFReleaseNull(pwdata);
100 int secd_05_corrupted_items(int argc, char *const *argv)
102 plan_tests(1 + N_THREADS*(N_ADDS+1) + N_ITEMS*4 + kSecdTestSetupTestCount);
104 /* custom keychain dir */
105 secd_test_setup_temp_keychain("secd_05_corrupted_items", NULL);
108 const char *v_data = "test";
109 CFDataRef pwdata = CFDataCreate(NULL, (UInt8 *)v_data, strlen(v_data));
110 CFMutableDictionaryRef query = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
111 CFDictionaryAddValue(query, kSecClass, kSecClassInternetPassword);
112 CFDictionaryAddValue(query, kSecAttrServer, CFSTR("corrupt.spamcop.net"));
113 CFDictionaryAddValue(query, kSecAttrAccount, CFSTR("smith"));
114 CFDictionaryAddValue(query, kSecAttrProtocol, kSecAttrProtocolHTTP);
115 CFDictionaryAddValue(query, kSecAttrAuthenticationType, kSecAttrAuthenticationTypeDefault);
116 CFDictionaryAddValue(query, kSecValueData, pwdata);
119 for(i=1; i<=N_ITEMS; i++) {
120 CFNumberRef port = CFNumberCreate(NULL, kCFNumberSInt32Type, &i);
121 CFDictionarySetValue(query, kSecAttrPort, port);
122 ok_status(SecItemAdd(query, NULL), "add internet password");
128 SecKeychainDbReset(^{
129 /* corrupt all the password */
130 NSString *keychain_path = CFBridgingRelease(__SecKeychainCopyPath());
131 char corrupt_item_sql[80];
134 is(sqlite3_open([keychain_path UTF8String], &db), SQLITE_OK, "open keychain");
136 for(int i=1;i<=N_ITEMS;i++) {
137 ok_unix(snprintf(corrupt_item_sql, sizeof(corrupt_item_sql), "UPDATE inet SET data=X'12345678' WHERE rowid=%d", i));
138 is(sqlite3_exec(db, corrupt_item_sql, NULL, NULL, NULL), SQLITE_OK, "corrupting keychain item");
142 /* start the adder threads */
143 pthread_t add_thread[N_THREADS];
144 void *add_err[N_THREADS] = {NULL,};
146 for(int i=0; i<N_THREADS; i++)
147 pthread_create(&add_thread[i], NULL, do_add, (void*)(intptr_t)i);
149 /* query the corrupted items */
150 CFDictionaryAddValue(query, kSecReturnPersistentRef, kCFBooleanTrue);
151 for(int i=1;i<=N_ITEMS;i++) {
152 CFTypeRef ref = NULL;
153 CFNumberRef port = CFNumberCreate(NULL, kCFNumberSInt32Type, &i);
154 CFDictionarySetValue(query, kSecAttrPort, port);
155 is_status(SecItemCopyMatching(query, &ref), errSecItemNotFound, "Item not found");
160 /* collect the adder threads */
161 for(int i=0; i<N_THREADS; i++)
162 pthread_join(add_thread[i], &add_err[i]);
164 for(int i=0; i<N_THREADS; i++)
165 ok(add_err[i]==NULL, "add thread");
167 CFReleaseNull(pwdata);
168 CFReleaseNull(query);