2 // secd-03-corrupted-item.c
5 // Created by Fabrice Gautier on 06/19/13.
9 #include "secd_regressions.h"
11 #include <securityd/SecDbItem.h>
12 #include <utilities/array_size.h>
13 #include <utilities/SecCFWrappers.h>
14 #include <utilities/SecFileLocations.h>
15 #include <utilities/fileIo.h>
17 #include <securityd/SOSCloudCircleServer.h>
18 #include <securityd/SecItemServer.h>
20 #include <Security/SecBasePriv.h>
22 #include <AssertMacros.h>
29 #include "SecdTestKeychainUtilities.h"
31 static OSStatus
do_query(void)
33 /* querying a password */
34 const void *keys
[] = {
39 const void *values
[] = {
40 kSecClassInternetPassword
,
41 CFSTR("corrupt.spamcop.net"),
44 CFDictionaryRef query
= CFDictionaryCreate(NULL
, keys
, values
,
45 array_size(keys
), NULL
, NULL
);
46 CFTypeRef results
= NULL
;
48 OSStatus err
= SecItemCopyMatching(query
, &results
);
53 static void *do_add(void *arg
)
57 for(int i
=0;i
<20;i
++) {
58 /* Creating a password */
59 SInt32 v_eighty
= (tid
+1)*1000+i
;
60 CFNumberRef eighty
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, &v_eighty
);
61 const char *v_data
= "test";
62 CFDataRef pwdata
= CFDataCreate(NULL
, (UInt8
*)v_data
, strlen(v_data
));
63 const void *keys
[] = {
69 kSecAttrAuthenticationType
,
72 const void *values
[] = {
73 kSecClassInternetPassword
,
74 CFSTR("members.spamcop.net"),
82 CFDictionaryRef item
= CFDictionaryCreate(NULL
, keys
, values
,
83 array_size(keys
), NULL
, NULL
);
85 ok_status(SecItemAdd(item
, NULL
), "add internet password");
94 static const char *corrupt_item_sql
= "UPDATE inet SET data=X'12345678' WHERE rowid=1";
97 int secd_03_corrupted_items(int argc
, char *const *argv
)
99 plan_tests(4 + N_THREADS
*21 + kSecdTestSetupTestCount
);
101 /* custom keychain dir */
102 secd_test_setup_temp_keychain("secd_03_corrupted_items", NULL
);
106 CFNumberRef eighty
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, &v_eighty
);
107 const char *v_data
= "test";
108 CFDataRef pwdata
= CFDataCreate(NULL
, (UInt8
*)v_data
, strlen(v_data
));
109 CFMutableDictionaryRef query
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
110 CFDictionaryAddValue(query
, kSecClass
, kSecClassInternetPassword
);
111 CFDictionaryAddValue(query
, kSecAttrServer
, CFSTR("corrupt.spamcop.net"));
112 CFDictionaryAddValue(query
, kSecAttrAccount
, CFSTR("smith"));
113 CFDictionaryAddValue(query
, kSecAttrPort
, eighty
);
114 CFDictionaryAddValue(query
, kSecAttrProtocol
, kSecAttrProtocolHTTP
);
115 CFDictionaryAddValue(query
, kSecAttrAuthenticationType
, kSecAttrAuthenticationTypeDefault
);
116 CFDictionaryAddValue(query
, kSecValueData
, pwdata
);
117 ok_status(SecItemAdd(query
, NULL
), "add internet password");
119 /* corrupt the password */
120 CFStringRef keychain_path_cf
= __SecKeychainCopyPath();
122 CFStringPerformWithCString(keychain_path_cf
, ^(const char *keychain_path
) {
123 /* Create a new keychain sqlite db */
126 is(sqlite3_open(keychain_path
, &db
), SQLITE_OK
, "create keychain");
127 is(sqlite3_exec(db
, corrupt_item_sql
, NULL
, NULL
, NULL
), SQLITE_OK
,
128 "corrupting keychain item1");
132 pthread_t add_thread
[N_THREADS
];
133 void *add_err
[N_THREADS
] = {NULL
,};
135 for(int i
=0; i
<N_THREADS
; i
++)
136 pthread_create(&add_thread
[i
], NULL
, do_add
, (void*)(intptr_t)i
);
138 is_status(do_query(), errSecItemNotFound
, "query");
140 for(int i
=0; i
<N_THREADS
; i
++)
141 pthread_join(add_thread
[i
], &add_err
[i
]);
143 for(int i
=0; i
<N_THREADS
; i
++)
144 ok(add_err
[i
]==NULL
, "add thread");