File: db.c

package info (click to toggle)
proftpd-mod-proxy 0.9.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,476 kB
  • sloc: ansic: 43,512; perl: 43,487; sh: 3,479; makefile: 248
file content (634 lines) | stat: -rw-r--r-- 22,097 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/*
 * ProFTPD - mod_proxy testsuite
 * Copyright (c) 2015-2022 TJ Saunders <tj@castaglia.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
 *
 * As a special exemption, TJ Saunders and other respective copyright holders
 * give permission to link this program with OpenSSL, and distribute the
 * resulting executable, without including the source code for OpenSSL in the
 * source distribution.
 */

/* Database API tests. */

#include "tests.h"

static pool *p = NULL;

static const char *db_test_table = "/tmp/prt-mod_proxy-db.dat";

static void set_up(void) {
  (void) unlink(db_test_table);

  if (p == NULL) {
    p = permanent_pool = make_sub_pool(NULL);
    session.c = NULL;
    session.notes = NULL;
  }

  if (getenv("TEST_VERBOSE") != NULL) {
    pr_trace_set_levels("proxy.db", 1, 20);
  }

  mark_point();
  proxy_db_init(p);
}

static void tear_down(void) {
  proxy_db_free();

  if (getenv("TEST_VERBOSE") != NULL) {
    pr_trace_set_levels("proxy.db", 0, 0);
  }

  if (p) {
    destroy_pool(p);
    p = permanent_pool = NULL;
    session.c = NULL;
    session.notes = NULL;
  }

  (void) unlink(db_test_table);
}

START_TEST (db_close_test) {
  int res;

  res = proxy_db_close(NULL, NULL);
  ck_assert_msg(res < 0, "Failed to handle null pool");
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
    strerror(errno), errno);

  res = proxy_db_close(p, NULL);
  ck_assert_msg(res < 0, "Failed to handle null dbh");
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
    strerror(errno), errno);
}
END_TEST

START_TEST (db_open_test) {
  int res;
  const char *table_path, *schema_name;
  struct proxy_dbh *dbh;

  dbh = proxy_db_open(NULL, NULL, NULL);
  ck_assert_msg(dbh == NULL, "Failed to handle null pool");
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
    strerror(errno), errno);

  dbh = proxy_db_open(p, NULL, NULL);
  ck_assert_msg(dbh == NULL, "Failed to handle null table path");
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
    strerror(errno), errno);

  (void) unlink(db_test_table);
  table_path = db_test_table;
  schema_name = "proxy_test";

  dbh = proxy_db_open(p, table_path, NULL);
  ck_assert_msg(dbh == NULL, "Failed to handle null schema name");
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
    strerror(errno), errno);

  dbh = proxy_db_open(p, table_path, schema_name);
  ck_assert_msg(dbh != NULL, "Failed to open table '%s': %s", table_path,
    strerror(errno));

  res = proxy_db_close(p, dbh);
  ck_assert_msg(res == 0, "Failed to close table '%s': %s", table_path,
    strerror(errno));

  (void) unlink(db_test_table);
}
END_TEST

START_TEST (db_open_with_version_test) {
  int res, flags = 0;
  struct proxy_dbh *dbh;
  const char *table_path, *schema_name;
  unsigned int schema_version;

  dbh = proxy_db_open_with_version(NULL, NULL, NULL, 0, 0);
  ck_assert_msg(dbh == NULL, "Failed to handle null pool");
  ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
    strerror(errno), errno);

  (void) unlink(db_test_table);
  table_path = db_test_table;
  schema_name = "proxy_test";
  schema_version = 0;

  mark_point();
  dbh = proxy_db_open_with_version(p, table_path, schema_name, schema_version,
    flags);
  ck_assert_msg(dbh != NULL,
    "Failed to open table '%s', schema '%s', version %u: %s", table_path,
    schema_name, schema_version, strerror(errno));

  res = proxy_db_close(p, dbh);
  ck_assert_msg(res == 0, "Failed to close database: %s", strerror(errno));

  flags |= PROXY_DB_OPEN_FL_INTEGRITY_CHECK;

  mark_point();
  dbh = proxy_db_open_with_version(p, table_path, schema_name, schema_version,
    flags);
  ck_assert_msg(dbh != NULL,
    "Failed to open table '%s', schema '%s', version %u: %s", table_path,
    schema_name, schema_version, strerror(errno));

  res = proxy_db_close(p, dbh);
  ck_assert_msg(res == 0, "Failed to close database: %s", strerror(errno));

  if (getenv("CI") == NULL &&
      getenv("TRAVIS") == NULL) {
    /* Enable the vacuuming for these tests. */
    flags |= PROXY_DB_OPEN_FL_VACUUM;

    mark_point();
    dbh = proxy_db_open_with_version(p, table_path, schema_name, schema_version,
      flags);
    ck_assert_msg(dbh != NULL,
      "Failed to open table '%s', schema '%s', version %u: %s", table_path,
      schema_name, schema_version, strerror(errno));

    res = proxy_db_close(p, dbh);
    ck_assert_msg(res == 0, "Failed to close database: %s", strerror(errno));

    flags &= ~PROXY_DB_OPEN_FL_VACUUM;
  }

  flags &= ~PROXY_DB_OPEN_FL_INTEGRITY_CHECK;

  mark_point();
  schema_version = 76;
  flags |= PROXY_DB_OPEN_FL_SCHEMA_VERSION_CHECK|PROXY_DB_OPEN_FL_ERROR_ON_SCHEMA_VERSION_SKEW;
  dbh = proxy_db_open_with_version(p, table_path, schema_name, schema_version,
    flags);
  ck_assert_msg(dbh == NULL, "Opened table with version skew unexpectedly");
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got '%s' (%d)", EPERM,
    strerror(errno), errno);

  mark_point();
  flags &= ~PROXY_DB_OPEN_FL_ERROR_ON_SCHEMA_VERSION_SKEW;
  dbh = proxy_db_open_with_version(p, table_path, schema_name, schema_version,
    flags);
  ck_assert_msg(dbh != NULL,
    "Failed to open table '%s', schema '%s', version %u: %s", table_path,
    schema_name, schema_version, strerror(errno));

  res = proxy_db_close(p, dbh);
  ck_assert_msg(res == 0, "Failed to close database: %s", strerror(errno));

  mark_point();
  schema_version = 76;
  dbh = proxy_db_open_with_version(p, table_path, schema_name, schema_version,
    flags);
  ck_assert_msg(dbh != NULL,
    "Failed to open table '%s', schema '%s', version %u: %s", table_path,
    schema_name, schema_version, strerror(errno));

  res = proxy_db_close(p, dbh);
  ck_assert_msg(res == 0, "Failed to close databas: %s", strerror(errno));

  mark_point();
  schema_version = 99;
  dbh = proxy_db_open_with_version(p, table_path, schema_name, schema_version,
    flags);
  ck_assert_msg(dbh != NULL,
    "Failed to open table '%s', schema '%s', version %u: %s", table_path,
    schema_name, schema_version, strerror(errno));

  res = proxy_db_close(p, dbh);
  ck_assert_msg(res == 0, "Failed to close database: %s", strerror(errno));

  (void) unlink(db_test_table);
}
END_TEST

START_TEST (db_exec_stmt_test) {
  int res;
  const char *table_path, *schema_name, *stmt, *errstr;
  struct proxy_dbh *dbh;

  res = proxy_db_exec_stmt(NULL, NULL, NULL, NULL);
  ck_assert_msg(res < 0, "Failed to handle null pool");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  res = proxy_db_exec_stmt(p, NULL, NULL, NULL);
  ck_assert_msg(res < 0, "Failed to handle null dbh");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  (void) unlink(db_test_table);
  table_path = db_test_table;
  schema_name = "proxy_test";

  dbh = proxy_db_open(p, table_path, schema_name);
  ck_assert_msg(dbh != NULL, "Failed to open table '%s': %s", table_path,
    strerror(errno));

  res = proxy_db_exec_stmt(p, dbh, NULL, NULL);
  ck_assert_msg(res < 0, "Failed to handle null statement");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  stmt = "SELECT COUNT(*) FROM foo;";
  errstr = NULL;
  res = proxy_db_exec_stmt(p, dbh, stmt, &errstr);
  ck_assert_msg(res < 0, "Failed to execute statement '%s'", stmt);
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  res = proxy_db_close(p, dbh);
  ck_assert_msg(res == 0, "Failed to close database: %s", strerror(errno));

  (void) unlink(db_test_table);
}
END_TEST

static int create_table(pool *stmt_pool, struct proxy_dbh *dbh,
    const char *table_name) {
  int res;
  const char *stmt, *errstr = NULL;

  stmt = pstrcat(stmt_pool, "CREATE TABLE ", table_name,
    " (id INTEGER, name TEXT);", NULL);
  res = proxy_db_exec_stmt(stmt_pool, dbh, stmt, &errstr);
  return res;
}

START_TEST (db_prepare_stmt_test) {
  int res;
  const char *table_path, *schema_name, *stmt;
  struct proxy_dbh *dbh;

  res = proxy_db_prepare_stmt(NULL, NULL, NULL);
  ck_assert_msg(res < 0, "Failed to handle null pool");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  res = proxy_db_prepare_stmt(p, NULL, NULL);
  ck_assert_msg(res < 0, "Failed to handle null dbh");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  (void) unlink(db_test_table);
  table_path = db_test_table;
  schema_name = "proxy_test";

  dbh = proxy_db_open(p, table_path, schema_name);
  ck_assert_msg(dbh != NULL, "Failed to open table '%s': %s", table_path,
    strerror(errno));

  res = proxy_db_prepare_stmt(p, dbh, NULL);
  ck_assert_msg(res < 0, "Failed to handle null statement");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  stmt = "foo bar baz?";
  res = proxy_db_prepare_stmt(p, dbh, stmt);
  ck_assert_msg(res < 0, "Prepared invalid statement '%s' unexpectedly", stmt);
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  res = create_table(p, dbh, "foo");
  ck_assert_msg(res == 0, "Failed to create table 'foo': %s", strerror(errno));

  stmt = "SELECT COUNT(*) FROM foo;";
  res = proxy_db_prepare_stmt(p, dbh, stmt);
  ck_assert_msg(res == 0, "Failed to prepare statement '%s': %s", stmt,
    strerror(errno));

  res = proxy_db_prepare_stmt(p, dbh, stmt);
  ck_assert_msg(res == 0, "Failed to prepare statement '%s': %s", stmt,
    strerror(errno));

  res = create_table(p, dbh, "bar");
  ck_assert_msg(res == 0, "Failed to create table 'bar': %s", strerror(errno));

  stmt = "SELECT COUNT(*) FROM bar;";
  res = proxy_db_prepare_stmt(p, dbh, stmt);
  ck_assert_msg(res == 0, "Failed to prepare statement '%s': %s", stmt,
    strerror(errno));

  res = proxy_db_close(p, dbh);
  ck_assert_msg(res == 0, "Failed to close database: %s", strerror(errno));

  (void) unlink(db_test_table);
}
END_TEST

START_TEST (db_finish_stmt_test) {
  int res;
  const char *table_path, *schema_name, *stmt;
  struct proxy_dbh *dbh;

  res = proxy_db_finish_stmt(NULL, NULL, NULL);
  ck_assert_msg(res < 0, "Failed to handle null arguments");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  res = proxy_db_finish_stmt(p, NULL, NULL);
  ck_assert_msg(res < 0, "Failed to handle null dbh");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  (void) unlink(db_test_table);
  table_path = db_test_table;
  schema_name = "proxy_test";

  dbh = proxy_db_open(p, table_path, schema_name);
  ck_assert_msg(dbh != NULL, "Failed to open table '%s': %s", table_path,
    strerror(errno));

  res = proxy_db_finish_stmt(p, dbh, NULL);
  ck_assert_msg(res < 0, "Failed to handle null statement");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  stmt = "SELECT COUNT(*) FROM foo";
  res = proxy_db_finish_stmt(p, dbh, stmt);
  ck_assert_msg(res < 0, "Failed to handle unprepared statement");
  ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got '%s' (%d)", ENOENT,
    strerror(errno), errno);

  res = create_table(p, dbh, "foo");
  ck_assert_msg(res == 0, "Failed to create table 'foo': %s", strerror(errno));

  res = proxy_db_prepare_stmt(p, dbh, stmt);
  ck_assert_msg(res == 0, "Failed to prepare statement '%s': %s", stmt,
    strerror(errno));

  res = proxy_db_finish_stmt(p, dbh, stmt);
  ck_assert_msg(res == 0, "Failed to finish statement '%s': %s", stmt,
    strerror(errno));

  res = proxy_db_finish_stmt(p, dbh, stmt);
  ck_assert_msg(res < 0, "Failed to handle unprepared statement");
  ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got '%s' (%d)", ENOENT,
    strerror(errno), errno);

  res = proxy_db_close(p, dbh);
  ck_assert_msg(res == 0, "Failed to close database: %s", strerror(errno));

  (void) unlink(db_test_table);
}
END_TEST

START_TEST (db_bind_stmt_test) {
  int res;
  const char *table_path, *schema_name, *stmt;
  struct proxy_dbh *dbh;
  int idx, int_val;
  long long_val;
  char *text_val;
  void *blob_val;

  res = proxy_db_bind_stmt(NULL, NULL, NULL, -1, -1, NULL, -1);
  ck_assert_msg(res < 0, "Failed to handle null pool");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  res = proxy_db_bind_stmt(p, NULL, NULL, -1, -1, NULL, -1);
  ck_assert_msg(res < 0, "Failed to handle null dbh");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  (void) unlink(db_test_table);
  table_path = db_test_table;
  schema_name = "proxy_test";

  dbh = proxy_db_open(p, table_path, schema_name);
  ck_assert_msg(dbh != NULL, "Failed to open table '%s': %s", table_path,
    strerror(errno));

  res = proxy_db_bind_stmt(p, dbh, NULL, -1, -1, NULL, -1);
  ck_assert_msg(res < 0, "Failed to handle null statement");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  stmt = "SELECT COUNT(*) FROM table";
  idx = -1;
  res = proxy_db_bind_stmt(p, dbh, stmt, idx, PROXY_DB_BIND_TYPE_INT, NULL, -1);
  ck_assert_msg(res < 0, "Failed to handle invalid index %d", idx);
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  idx = 1;
  res = proxy_db_bind_stmt(p, dbh, stmt, idx, PROXY_DB_BIND_TYPE_INT, NULL, -1);
  ck_assert_msg(res < 0, "Failed to handle unprepared statement");
  ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got '%s' (%d)", ENOENT,
    strerror(errno), errno);

  res = create_table(p, dbh, "foo");
  ck_assert_msg(res == 0, "Failed to create table 'foo': %s", strerror(errno));

  stmt = "SELECT COUNT(*) FROM foo;";
  res = proxy_db_prepare_stmt(p, dbh, stmt);
  ck_assert_msg(res == 0, "Failed to prepare statement '%s': %s", stmt,
    strerror(errno));

  res = proxy_db_bind_stmt(p, dbh, stmt, idx, PROXY_DB_BIND_TYPE_INT, NULL, -1);
  ck_assert_msg(res < 0, "Failed to handle missing INT value");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  int_val = 7;
  res = proxy_db_bind_stmt(p, dbh, stmt, idx, PROXY_DB_BIND_TYPE_INT, &int_val,
    -1);
  ck_assert_msg(res < 0, "Failed to handle invalid index value");
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got '%s' (%d)", EPERM,
    strerror(errno), errno);

  res = proxy_db_bind_stmt(p, dbh, stmt, idx, PROXY_DB_BIND_TYPE_LONG, NULL,
    -1);
  ck_assert_msg(res < 0, "Failed to handle missing LONG value");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  long_val = 7;
  res = proxy_db_bind_stmt(p, dbh, stmt, idx, PROXY_DB_BIND_TYPE_LONG,
    &long_val, -1);
  ck_assert_msg(res < 0, "Failed to handle invalid index value");
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got '%s' (%d)", EPERM,
    strerror(errno), errno);

  res = proxy_db_bind_stmt(p, dbh, stmt, idx, PROXY_DB_BIND_TYPE_TEXT, NULL, 0);
  ck_assert_msg(res < 0, "Failed to handle missing TEXT value");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  text_val = "testing";
  res = proxy_db_bind_stmt(p, dbh, stmt, idx, PROXY_DB_BIND_TYPE_TEXT,
    text_val, 0);
  ck_assert_msg(res < 0, "Failed to handle invalid index value");
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got '%s' (%d)", EPERM,
    strerror(errno), errno);

  res = proxy_db_bind_stmt(p, dbh, stmt, idx, PROXY_DB_BIND_TYPE_BLOB, NULL,
    -1);
  ck_assert_msg(res < 0, "Failed to handle missing BLOB value");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  blob_val = "testing";
  res = proxy_db_bind_stmt(p, dbh, stmt, idx, PROXY_DB_BIND_TYPE_BLOB,
    blob_val, strlen(blob_val));
  ck_assert_msg(res < 0, "Failed to handle invalid index value");
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got '%s' (%d)", EPERM,
    strerror(errno), errno);

  res = proxy_db_bind_stmt(p, dbh, stmt, idx, PROXY_DB_BIND_TYPE_NULL, NULL, 0);
  ck_assert_msg(res < 0, "Failed to handle invalid NULL value");
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got '%s' (%d)", EPERM,
    strerror(errno), errno);

  stmt = "SELECT COUNT(*) FROM foo WHERE id = ?;";
  res = proxy_db_prepare_stmt(p, dbh, stmt);
  ck_assert_msg(res == 0, "Failed to prepare statement '%s': %s", stmt,
    strerror(errno));

  int_val = 7;
  res = proxy_db_bind_stmt(p, dbh, stmt, idx, PROXY_DB_BIND_TYPE_INT, &int_val,     -1);
  ck_assert_msg(res == 0, "Failed to bind INT value: %s", strerror(errno));

  res = proxy_db_close(p, dbh);
  ck_assert_msg(res == 0, "Failed to close database: %s", strerror(errno));

  (void) unlink(db_test_table);
}
END_TEST

START_TEST (db_exec_prepared_stmt_test) {
  int res;
  array_header *results;
  const char *table_path, *schema_name, *stmt, *errstr = NULL;
  struct proxy_dbh *dbh;

  results = proxy_db_exec_prepared_stmt(NULL, NULL, NULL, NULL);
  ck_assert_msg(results == NULL, "Failed to handle null pool");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  results = proxy_db_exec_prepared_stmt(p, NULL, NULL, NULL);
  ck_assert_msg(results == NULL, "Failed to handle null dbh");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  (void) unlink(db_test_table);
  table_path = db_test_table;
  schema_name = "proxy_test";

  dbh = proxy_db_open(p, table_path, schema_name);
  ck_assert_msg(dbh != NULL, "Failed to open table '%s': %s", table_path,
    strerror(errno));

  results = proxy_db_exec_prepared_stmt(p, dbh, NULL, NULL);
  ck_assert_msg(results == NULL, "Failed to handle null statement");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  stmt = "SELECT COUNT(*) FROM foo;";
  results = proxy_db_exec_prepared_stmt(p, dbh, stmt, &errstr);
  ck_assert_msg(results == NULL, "Failed to handle unprepared statement");
  ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got '%s' (%d)", ENOENT,
    strerror(errno), errno);

  res = create_table(p, dbh, "foo");
  ck_assert_msg(res == 0, "Failed to create table 'foo': %s", strerror(errno));

  res = proxy_db_prepare_stmt(p, dbh, stmt);
  ck_assert_msg(res == 0, "Failed to prepare statement '%s': %s", stmt,
    strerror(errno));

  results = proxy_db_exec_prepared_stmt(p, dbh, stmt, &errstr);
  ck_assert_msg(results != NULL,
    "Failed to execute prepared statement '%s': %s (%s)", stmt, errstr,
    strerror(errno));

  res = proxy_db_close(p, dbh);
  ck_assert_msg(res == 0, "Failed to close database: %s", strerror(errno));

  (void) unlink(db_test_table);
}
END_TEST

START_TEST (db_reindex_test) {
  int res;
  const char *table_path, *schema_name, *index_name, *errstr = NULL;
  struct proxy_dbh *dbh;

  res = proxy_db_reindex(NULL, NULL, NULL, NULL);
  ck_assert_msg(res < 0, "Failed to handle null pool");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  res = proxy_db_reindex(p, NULL, NULL, NULL);
  ck_assert_msg(res < 0, "Failed to handle null dbh");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  (void) unlink(db_test_table);
  table_path = db_test_table;
  schema_name = "proxy_test";

  dbh = proxy_db_open(p, table_path, schema_name);
  ck_assert_msg(dbh != NULL, "Failed to open table '%s': %s", table_path,
    strerror(errno));

  res = proxy_db_reindex(p, dbh, NULL, NULL);
  ck_assert_msg(res < 0, "Failed to handle null index name");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);

  index_name = "test_idx";
  res = proxy_db_reindex(p, dbh, index_name, &errstr);
  ck_assert_msg(res < 0, "Failed to handle invalid index");
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got '%s' (%d)", EINVAL,
    strerror(errno), errno);
  ck_assert_msg(errstr != NULL, "Failed to provide error string");

  res = proxy_db_close(p, dbh);
  ck_assert_msg(res == 0, "Failed to close database: %s", strerror(errno));

  (void) unlink(db_test_table);
}
END_TEST

Suite *tests_get_db_suite(void) {
  Suite *suite;
  TCase *testcase;

  suite = suite_create("db");
  testcase = tcase_create("base");

  tcase_add_checked_fixture(testcase, set_up, tear_down);

  tcase_add_test(testcase, db_close_test);
  tcase_add_test(testcase, db_open_test);
  tcase_add_test(testcase, db_open_with_version_test);
  tcase_add_test(testcase, db_exec_stmt_test);
  tcase_add_test(testcase, db_prepare_stmt_test);
  tcase_add_test(testcase, db_finish_stmt_test);
  tcase_add_test(testcase, db_bind_stmt_test);
  tcase_add_test(testcase, db_exec_prepared_stmt_test);
  tcase_add_test(testcase, db_reindex_test);

  suite_add_tcase(suite, testcase);
  return suite;
}