2 * Copyright (c) 2012-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 #include <utilities/SecCFRelease.h>
26 #include <utilities/SecDb.h>
28 #include <CoreFoundation/CoreFoundation.h>
30 #include "utilities_regressions.h"
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;
39 dispatch_queue_t queue
= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0);
40 dispatch_group_t group
= dispatch_group_create();
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
;
49 CFErrorRef error
= NULL
;
50 if (!perform(db
, &error
, ^void (SecDbConnectionRef dbconn
) {
52 if (count
> max_count
) {
55 struct timespec ts
= { .tv_nsec
= 200000 };
59 fail("perform %s %@", name
, error
);
64 dispatch_group_wait(group
, DISPATCH_TIME_FOREVER
);
65 dispatch_release(group
);
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
);
76 todo("can't guarantee all threads used");
77 is(count_func(db
, "writers", &max_conn_count
, SecDbPerformWrite
), kSecDbMaxWriters
, "max writers is %d", kSecDbMaxWriters
);
80 dispatch_group_async(group
, queue
, ^{
81 cmp_ok(count_func(db
, "readers", &max_conn_count
, SecDbPerformRead
), <=, kSecDbMaxReaders
, "max readers is %d", kSecDbMaxReaders
);
83 todo("can't guarantee all threads used");
84 is(count_func(db
, "readers", &max_conn_count
, SecDbPerformRead
), kSecDbMaxReaders
, "max readers is %d", kSecDbMaxReaders
);
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
);
91 todo("can't guarantee all threads idle");
92 is(max_conn_count
, kSecDbMaxIdleHandles
, "max idle connection count is %d", kSecDbMaxIdleHandles
);
97 static void tests(void)
99 CFTypeID typeID
= SecDbGetTypeID();
100 CFStringRef tid
= CFCopyTypeIDDescription(typeID
);
101 ok(CFEqual(CFSTR("SecDb"), tid
), "tid matches");
104 typeID
= SecDbConnectionGetTypeID();
105 tid
= CFCopyTypeIDDescription(typeID
);
106 ok(CFEqual(CFSTR("SecDbConnection"), tid
), "tid matches");
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
: "");
112 SecDbRef db
= SecDbCreate(dbName
, NULL
);
113 CFReleaseNull(dbName
);
114 ok(db
, "SecDbCreate");
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
),
120 ok(SecDbExec(dbconn
, CFSTR("INSERT INTO tablea(key,value)VALUES(1,2);"), &error
),
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
);
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
139 }), "SecDbStep: %@", error
);
140 CFReleaseNull(error
);
141 }), "SecDbPrepare: %@", error
);
142 CFReleaseNull(error
);
144 ok(SecDbTransaction(dbconn
, kSecDbExclusiveTransactionType
, &error
, ^(bool *commit
) {
145 ok(SecDbExec(dbconn
, CFSTR("INSERT INTO tablea (key,value)VALUES(13,21);"), &error
),
147 ok(SecDbExec(dbconn
, CFSTR("INSERT INTO tablea (key,value)VALUES(2,5);"), &error
),
149 }), "SecDbTransaction: %@", error
);
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
);
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
);
162 }), "SecDbStep: %@", error
);
163 CFReleaseNull(error
);
165 }), "SecDbPrepare: %@", error
);
167 ok(SecDbExec(dbconn
, CFSTR("DROP TABLE tablea;"), &error
),
169 }), "SecDbPerformWrite: %@", error
);
170 CFReleaseNull(error
);
172 count_connections(db
);
177 int su_40_secdb(int argc
, char *const *argv
)
179 plan_tests(kTestCount
);
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
);