]> git.saurik.com Git - apple/security.git/blob - OSX/utilities/Regressions/su-40-secdb.c
Security-57337.20.44.tar.gz
[apple/security.git] / OSX / utilities / Regressions / su-40-secdb.c
1 /*
2 * Copyright (c) 2012-2014 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 License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 #include <utilities/SecCFRelease.h>
26 #include <utilities/SecDb.h>
27
28 #include <CoreFoundation/CoreFoundation.h>
29
30 #include "utilities_regressions.h"
31 #include <time.h>
32
33 #define kTestCount 31
34
35 static int count_func(SecDbRef db, const char *name, CFIndex *max_conn_count, bool (*perform)(SecDbRef db, CFErrorRef *error, void (^perform)(SecDbConnectionRef dbconn))) {
36 __block int count = 0;
37 __block int max_count = 0;
38 *max_conn_count = 0;
39 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
40 dispatch_group_t group = dispatch_group_create();
41
42 for (int i = 0; i < 100; ++i) {
43 dispatch_group_async(group, queue, ^{
44 CFIndex conn_count = SecDbIdleConnectionCount(db);
45 if (conn_count > *max_conn_count) {
46 *max_conn_count = conn_count;
47 }
48
49 CFErrorRef error = NULL;
50 if (!perform(db, &error, ^void (SecDbConnectionRef dbconn) {
51 count++;
52 if (count > max_count) {
53 max_count = count;
54 }
55 struct timespec ts = { .tv_nsec = 200000 };
56 nanosleep(&ts, NULL);
57 count--;
58 })) {
59 fail("perform %s %@", name, error);
60 CFReleaseNull(error);
61 }
62 });
63 }
64 dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
65 dispatch_release(group);
66 return max_count;
67 }
68
69 static void count_connections(SecDbRef db) {
70 __block CFIndex max_conn_count = 0;
71 dispatch_group_t group = dispatch_group_create();
72 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
73 dispatch_group_async(group, queue, ^{
74 cmp_ok(count_func(db, "writers", &max_conn_count, SecDbPerformWrite), <=, kSecDbMaxWriters, "max writers is %d", kSecDbMaxWriters);
75 TODO: {
76 todo("can't guarantee all threads used");
77 is(count_func(db, "writers", &max_conn_count, SecDbPerformWrite), kSecDbMaxWriters, "max writers is %d", kSecDbMaxWriters);
78 }
79 });
80 dispatch_group_async(group, queue, ^{
81 cmp_ok(count_func(db, "readers", &max_conn_count, SecDbPerformRead), <=, kSecDbMaxReaders, "max readers is %d", kSecDbMaxReaders);
82 TODO: {
83 todo("can't guarantee all threads used");
84 is(count_func(db, "readers", &max_conn_count, SecDbPerformRead), kSecDbMaxReaders, "max readers is %d", kSecDbMaxReaders);
85 }
86 });
87 dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
88 dispatch_release(group);
89 cmp_ok(max_conn_count, <=, kSecDbMaxIdleHandles, "max idle connection count is %d", kSecDbMaxIdleHandles);
90 TODO: {
91 todo("can't guarantee all threads idle");
92 is(max_conn_count, kSecDbMaxIdleHandles, "max idle connection count is %d", kSecDbMaxIdleHandles);
93 }
94
95 }
96
97 static void tests(void)
98 {
99 CFTypeID typeID = SecDbGetTypeID();
100 CFStringRef tid = CFCopyTypeIDDescription(typeID);
101 ok(CFEqual(CFSTR("SecDb"), tid), "tid matches");
102 CFReleaseNull(tid);
103
104 typeID = SecDbConnectionGetTypeID();
105 tid = CFCopyTypeIDDescription(typeID);
106 ok(CFEqual(CFSTR("SecDbConnection"), tid), "tid matches");
107 CFReleaseNull(tid);
108
109 const char *home_var = getenv("HOME");
110 CFStringRef dbName = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%s/Library/Keychains/su-40-sqldb.db"), home_var ? home_var : "");
111
112 SecDbRef db = SecDbCreate(dbName, NULL);
113 CFReleaseNull(dbName);
114 ok(db, "SecDbCreate");
115
116 __block CFErrorRef error = NULL;
117 ok(SecDbPerformWrite(db, &error, ^void (SecDbConnectionRef dbconn) {
118 ok(SecDbExec(dbconn, CFSTR("CREATE TABLE tablea(key TEXT,value BLOB);"), &error),
119 "exec: %@", error);
120 ok(SecDbExec(dbconn, CFSTR("INSERT INTO tablea(key,value)VALUES(1,2);"), &error),
121 "exec: %@", error);
122
123 CFStringRef sql = CFSTR("INSERT INTO tablea(key,value)VALUES(?,?);");
124 ok(SecDbPrepare(dbconn, sql, &error, ^void (sqlite3_stmt *stmt) {
125 ok_status(sqlite3_bind_text(stmt, 1, "key1", 4, NULL), "bind_text[1]");
126 ok_status(sqlite3_bind_blob(stmt, 2, "value1", 6, NULL), "bind_blob[2]");
127 ok(SecDbStep(dbconn, stmt, &error, NULL), "SecDbStep: %@", error);
128 CFReleaseNull(error);
129 }), "SecDbPrepare: %@", error);
130 CFReleaseNull(error);
131
132 sql = CFSTR("SELECT key,value FROM tablea;");
133 ok(SecDbPrepare(dbconn, sql, &error, ^void (sqlite3_stmt *stmt) {
134 ok(SecDbStep(dbconn, stmt, &error, ^(bool *stop) {
135 const unsigned char *key = sqlite3_column_text(stmt, 1);
136 pass("got a row key: %s", key);
137 // A row happened, we're done
138 *stop = true;
139 }), "SecDbStep: %@", error);
140 CFReleaseNull(error);
141 }), "SecDbPrepare: %@", error);
142 CFReleaseNull(error);
143
144 ok(SecDbTransaction(dbconn, kSecDbExclusiveTransactionType, &error, ^(bool *commit) {
145 ok(SecDbExec(dbconn, CFSTR("INSERT INTO tablea (key,value)VALUES(13,21);"), &error),
146 "exec: %@", error);
147 ok(SecDbExec(dbconn, CFSTR("INSERT INTO tablea (key,value)VALUES(2,5);"), &error),
148 "exec: %@", error);
149 }), "SecDbTransaction: %@", error);
150
151 ok(SecDbPrepare(dbconn, sql, &error, ^void (sqlite3_stmt *stmt) {
152 ok(SecDbStep(dbconn, stmt, &error, ^(bool *stop) {
153 const unsigned char *key = sqlite3_column_text(stmt, 1);
154 pass("got a row key: %s", key);
155 }), "SecDbStep: %@", error);
156 CFReleaseNull(error);
157 sqlite3_reset(stmt);
158 ok(SecDbStep(dbconn, stmt, &error, ^(bool *stop) {
159 const unsigned char *key = sqlite3_column_text(stmt, 1);
160 pass("got a row key: %s", key);
161 *stop = true;
162 }), "SecDbStep: %@", error);
163 CFReleaseNull(error);
164
165 }), "SecDbPrepare: %@", error);
166
167 ok(SecDbExec(dbconn, CFSTR("DROP TABLE tablea;"), &error),
168 "exec: %@", error);
169 }), "SecDbPerformWrite: %@", error);
170 CFReleaseNull(error);
171
172 count_connections(db);
173
174 CFReleaseNull(db);
175 }
176
177 int su_40_secdb(int argc, char *const *argv)
178 {
179 plan_tests(kTestCount);
180 tests();
181
182 return 0;
183 }
184
185 #if 0
186 // The following still need tests.
187 ok(SecDbTransaction(dbconn, kSecDbNoneTransactionType, &error, ^bool {}), "");
188 ok(SecDbTransaction(dbconn, kSecDbImmediateTransactionType, &error, ^bool {}), "");
189 ok(SecDbTransaction(dbconn, kSecDbNormalTransactionType, &error, ^bool {}), "");
190 ok(SecDbPerformRead(SecDbRef db, CFErrorRef *error, void ^(SecDbConnectionRef dbconn){}), "");
191 SecDbCheckpoint(SecDbConnectionRef dbconn);
192 #endif