Code

b8a5ea988aee0ad710ba9909835baf1e5359f45a
[sysdb.git] / t / unit / utils / dbi_test.c
1 /*
2  * SysDB - t/unit/utils/dbi_test.c
3  * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "libsysdb_test.h"
33 #include "utils/dbi.h"
35 #include <check.h>
36 #include <dbi/dbi.h>
38 #define TEST_MAGIC ((void *)0x1337)
40 /*
41  * private data-types
42  */
43 typedef union {
44         long long   integer;
45         double      decimal;
46         const char *string;
47         time_t      datetime;
48         struct {
49                 size_t length;
50                 const unsigned char *datum;
51         } binary;
52 } mock_data_t;
54 typedef struct {
55         const char *name;
56         unsigned long long nrows;
57         unsigned long long current_row;
58         unsigned int    nfields;
59         unsigned short *field_types;
60         char          **field_names;
61 } mock_query_t;
63 /*
64  * private variables
65  */
67 static sdb_dbi_client_t *client;
69 /*
70  * mock queries
71  */
73 /* field definitions */
74 static unsigned short field_types[] = {
75         DBI_TYPE_INTEGER,
76         DBI_TYPE_DECIMAL,
77         DBI_TYPE_STRING,
78         DBI_TYPE_DATETIME,
79         DBI_TYPE_BINARY,
80 };
81 static char *field_names[] = {
82         "field0",
83         "field1",
84         "field2",
85         "field3",
86         "field4",
87 };
89 #define DATUM(p) ((const unsigned char *)(p))
90 static mock_data_t golden_data[][5] = {
91         {
92                 { .integer  = 1234   },
93                 { .decimal  = 1.234  },
94                 { .string   = "abcd" },
95                 { .datetime = 0      },
96                 { .binary   = { 1, DATUM("a") } },
97         },
98         {
99                 { .integer  = 2345   },
100                 { .decimal  = 23.45  },
101                 { .string   = "bcde" },
102                 { .datetime = 1      },
103                 { .binary   = { 4, DATUM("bcde") } },
104         },
105         {
106                 { .integer  = 3456   },
107                 { .decimal  = 345.6  },
108                 { .string   = "cd"   },
109                 { .datetime = 2      },
110                 { .binary   = { 0, DATUM(NULL) } },
111         },
112         {
113                 { .integer  = 4567   },
114                 { .decimal  = 4567   },
115                 { .string   = "d"    },
116                 { .datetime = 3      },
117                 { .binary   = { 13, DATUM("defghijklmnop") } },
118         },
119         {
120                 { .integer  = 5678   },
121                 { .decimal  = 56.78  },
122                 { .string   = "efgh" },
123                 { .datetime = 4      },
124                 { .binary   = { 5, DATUM("efghi") } },
125         },
126 };
128 /* query definitions */
129 static mock_query_t mock_queries[] = {
130         { "mockquery0", 5, 1, 0, NULL, NULL },
131         { "mockquery1", 0, 0, 1, field_types, field_names },
132         { "mockquery2", 1, 1, 1, field_types, field_names },
133         { "mockquery3", 2, 1, 1, field_types, field_names },
134         { "mockquery4", 5, 1, 1, field_types, field_names },
135         { "mockquery5", 5, 1, 2, field_types, field_names },
136         { "mockquery6", 5, 1, 3, field_types, field_names },
137         { "mockquery7", 5, 1, 4, field_types, field_names },
138         { "mockquery8", 5, 1, 5, field_types, field_names },
139 };
141 static mock_query_t *current_query = NULL;
143 /*
144  * mocked functions
145  */
147 /* dbi_driver, dbi_conn, dbi_result are void pointers */
149 dbi_driver
150 dbi_driver_open(const char *name)
152         if (strcmp(name, "mockdriver"))
153                 return NULL;
154         return (dbi_driver)strdup(name);
155 } /* dbi_driver_open */
157 dbi_driver
158 dbi_driver_list(dbi_driver curr)
160         if (!curr)
161                 return "mockdriver";
162         return NULL;
163 } /* dbi_driver_list */
165 const char *
166 dbi_driver_get_name(dbi_driver driver)
168         return (const char *)driver;
169 } /* dbi_driver_get_name */
171 int
172 dbi_conn_set_option(dbi_conn __attribute__((unused)) conn,
173                 const char __attribute__((unused)) *key,
174                 const char __attribute__((unused)) *value)
176         return 0;
177 } /* dbi_conn_set_option */
179 const char *
180 dbi_conn_get_option_list(dbi_conn __attribute__((unused)) conn,
181                 const char __attribute__((unused)) *key)
183         return NULL;
184 } /* dbi_conn_get_option_list */
186 dbi_conn
187 dbi_conn_open(dbi_driver driver)
189         if (strcmp((const char *)driver, "mockdriver"))
190                 return NULL;
191         return (dbi_conn)"mockconnection";
192 } /* dbi_conn_open */
194 static unsigned long long dbi_conn_connect_called = 0;
195 int
196 dbi_conn_connect(dbi_conn conn)
198         ++dbi_conn_connect_called;
199         if (strcmp((const char *)conn, "mockconnection"))
200                 return DBI_ERROR_NOCONN;
201         return 0;
202 } /* dbi_conn_connect */
204 int
205 dbi_conn_ping(dbi_conn conn)
207         if (strcmp((const char *)conn, "mockconnection"))
208                 return 0;
209         return 1;
210 } /* dbi_conn_connect */
212 void
213 dbi_conn_close(dbi_conn __attribute__((unused)) conn)
215         return;
216 } /* dbi_conn_close */
218 int
219 dbi_conn_error(dbi_conn conn, const char **errmsg)
221         if ((! conn) || (strcmp((const char *)conn, "mockconnection")))
222                 return DBI_ERROR_BADOBJECT;
223         if (errmsg)
224                 *errmsg = "mockerror";
225         return DBI_ERROR_UNSUPPORTED;
226 } /* dbi_conn_error */
228 static unsigned long long dbi_conn_query_called = 0;
229 dbi_result
230 dbi_conn_query(dbi_conn conn, const char __attribute__((unused)) *statement)
232         size_t i;
234         ++dbi_conn_query_called;
235         if (strcmp((const char *)conn, "mockconnection"))
236                 return NULL;
238         for (i = 0; i < SDB_STATIC_ARRAY_LEN(mock_queries); ++i) {
239                 if (!strcmp(mock_queries[i].name, statement)) {
240                         current_query = &mock_queries[i];
241                         return (dbi_result)current_query;
242                 }
243         }
244         return NULL;
245 } /* dbi_conn_query */
247 unsigned long long
248 dbi_result_get_numrows(dbi_result res)
250         mock_query_t *q = res;
251         if (! q)
252                 return DBI_ROW_ERROR;
253         return q->nrows;
254 } /* dbi_result_get_numrows */
256 unsigned int
257 dbi_result_get_numfields(dbi_result res)
259         mock_query_t *q = res;
260         if (! q)
261                 return DBI_FIELD_ERROR;
262         return q->nfields;
263 } /* dbi_result_get_numfields */
265 unsigned short
266 dbi_result_get_field_type_idx(dbi_result res, unsigned int i)
268         mock_query_t *q = res;
269         if ((! q) || (i > q->nfields))
270                 return DBI_TYPE_ERROR;
271         return q->field_types[i - 1];
272 } /* dbi_result_get_field_type_idx */
274 const char *
275 dbi_result_get_field_name(dbi_result res, unsigned int i)
277         mock_query_t *q = res;
278         if ((! q) || (i > q->nfields))
279                 return NULL;
280         return q->field_names[i - 1];
281 } /* dbi_result_get_field_name */
283 int
284 dbi_result_seek_row(dbi_result res, unsigned long long n)
286         mock_query_t *q = res;
287         if ((! q) || (n > q->nrows))
288                 return 0;
290         q->current_row = n;
291         return 1;
292 } /* dbi_result_seek_row */
294 static mock_data_t
295 get_golden_data(dbi_result res, unsigned int i) {
296         mock_query_t *q = res;
297         fail_unless(q != NULL, "dbi_result_get_*_idx() called with "
298                         "NULL result data; expected valid result object");
300         /* this information is managed by seek_row and, thus,
301          * should never be invalid */
302         fail_unless(q->current_row && q->current_row <= q->nrows,
303                         "INTERNAL ERROR: current row out of range");
305         fail_unless(i && i <= q->nfields,
306                         "dbi_result_get_*_idx() called with index out of range; "
307                         "got: %u; expected [1, %u]", i, q->nfields);
308         return golden_data[q->current_row - 1][i - 1];
309 } /* get_golden_data */
311 long long
312 dbi_result_get_longlong_idx(dbi_result res, unsigned int i)
314         fail_unless(current_query->field_types[i - 1] == DBI_TYPE_INTEGER,
315                         "dbi_result_get_longlong_idx() called for non-integer "
316                         "column type %u", current_query->field_types[i - 1]);
317         return get_golden_data(res, i).integer;
318 } /* dbi_result_get_longlong_idx */
320 double
321 dbi_result_get_double_idx(dbi_result res, unsigned int i)
323         fail_unless(current_query->field_types[i - 1] == DBI_TYPE_DECIMAL,
324                         "dbi_result_get_double_idx() called for non-decimal "
325                         "column type %u", current_query->field_types[i - 1]);
326         return get_golden_data(res, i).decimal;
327 } /* dbi_result_get_double_idx */
329 const char *
330 dbi_result_get_string_idx(dbi_result res, unsigned int i)
332         fail_unless(current_query->field_types[i - 1] == DBI_TYPE_STRING,
333                         "dbi_result_get_string_idx() called for non-string "
334                         "column type %u", current_query->field_types[i - 1]);
335         return get_golden_data(res, i).string;
336 } /* dbi_result_get_string_idx */
338 char *
339 dbi_result_get_string_copy_idx(dbi_result res, unsigned int i)
341         fail_unless(current_query->field_types[i - 1] == DBI_TYPE_STRING,
342                         "dbi_result_get_string_copy_idx() called for non-string "
343                         "column type %u", current_query->field_types[i - 1]);
344         if (! get_golden_data(res, i).string)
345                 return NULL;
346         return strdup(get_golden_data(res, i).string);
347 } /* dbi_result_get_string_copy_idx */
349 time_t
350 dbi_result_get_datetime_idx(dbi_result res, unsigned int i)
352         fail_unless(current_query->field_types[i - 1] == DBI_TYPE_DATETIME,
353                         "dbi_result_get_datetime_idx() called for non-datetime "
354                         "column type %u", current_query->field_types[i - 1]);
355         return get_golden_data(res, i).datetime;
356 } /* dbi_result_get_datetime_idx */
358 size_t
359 dbi_result_get_field_length_idx(dbi_result res, unsigned int i)
361         /* this will check if the parameters are valid */
362         get_golden_data(res, i);
364         switch (current_query->field_types[i - 1]) {
365                 case DBI_TYPE_INTEGER:
366                         return sizeof(long long);
367                         break;
368                 case DBI_TYPE_DECIMAL:
369                         return sizeof(double);
370                         break;
371                 case DBI_TYPE_STRING:
372                         return strlen(get_golden_data(res, i).string) + 1;
373                         break;
374                 case DBI_TYPE_DATETIME:
375                         return sizeof(time_t);
376                         break;
377                 case DBI_TYPE_BINARY:
378                         return get_golden_data(res, i).binary.length;
379                         break;
380         }
382         fail("INTERNAL ERROR: dbi_result_get_field_length_idx() "
383                         "called for unexpected field type %u",
384                         current_query->field_types[i - 1]);
385         return 0;
386 } /* dbi_result_get_field_length_idx */
388 const unsigned char *
389 dbi_result_get_binary_idx(dbi_result res, unsigned int i)
391         fail_unless(current_query->field_types[i - 1] == DBI_TYPE_BINARY,
392                         "dbi_result_get_binary_idx() called for non-binary "
393                         "column type %u", current_query->field_types[i - 1]);
394         return get_golden_data(res, i).binary.datum;
395 } /* dbi_result_get_binary_idx */
397 unsigned char *
398 dbi_result_get_binary_copy_idx(dbi_result res, unsigned int i)
400         const char *data;
401         fail_unless(current_query->field_types[i - 1] == DBI_TYPE_BINARY,
402                         "dbi_result_get_binary_copy_idx() called for non-binary "
403                         "column type %u", current_query->field_types[i - 1]);
404         data = (const char *)get_golden_data(res, i).binary.datum;
405         if (! data)
406                 return NULL;
407         return (unsigned char *)strdup(data);
408 } /* dbi_result_get_binary_copy_idx */
410 static unsigned long long dbi_result_free_called = 0;
411 int
412 dbi_result_free(dbi_result res)
414         mock_query_t *q = res;
416         ++dbi_result_free_called;
417         if (! q)
418                 return -1;
420         current_query = NULL;
421         return 0;
422 } /* dbi_result_free */
424 /*
425  * private helper functions
426  */
428 static void
429 setup(void)
431         client = sdb_dbi_client_create("mockdriver", "mockdatabase");
432         fail_unless(client != NULL,
433                         "sdb_dbi_client_create() = NULL; expected client object");
435         dbi_conn_connect_called = 0;
436 } /* setup */
438 static void
439 connect(void)
441         int check = sdb_dbi_client_connect(client);
442         fail_unless(check == 0,
443                         "sdb_dbi_client_connect() = %i; expected: 0", check);
444 } /* connect */
446 static void
447 teardown(void)
449         sdb_dbi_client_destroy(client);
450         client = NULL;
451 } /* teardown */
453 static unsigned long long query_callback_called = 0;
454 static int
455 query_callback(sdb_dbi_client_t *c,
456                 size_t n, sdb_data_t *data, sdb_object_t *user_data)
458         size_t i;
460         ++query_callback_called;
462         fail_unless(c == client,
463                         "query callback received unexpected client = %p; "
464                         "expected: %p", c, client);
465         fail_unless(n == current_query->nfields,
466                         "query callback received n = %i; expected: %i",
467                         n, current_query->nfields);
468         fail_unless(data != NULL,
469                         "query callback received data = NULL; expected: valid data");
470         fail_unless(user_data == TEST_MAGIC,
471                         "query callback received user_data = %p; expected: %p",
472                         user_data, TEST_MAGIC);
474         for (i = 0; i < n; ++i) {
475                 int expected_type = DBI_TYPE_TO_SC(current_query->field_types[i]);
476                 mock_data_t expected_data;
478                 fail_unless((int)data[i].type == expected_type,
479                                 "query callback received unexpected type %i for "
480                                 "column %zu; expected: %i", data[i].type, i,
481                                 expected_type);
483                 expected_data = golden_data[current_query->current_row - 1][i];
484                 switch (expected_type) {
485                         case SDB_TYPE_INTEGER:
486                                 fail_unless(data[i].data.integer == expected_data.integer,
487                                                 "query callback received unexpected data %lli "
488                                                 "for column %zu; expected: %lli",
489                                                 data[i].data.integer, i, expected_data.integer);
490                                 break;
491                         case SDB_TYPE_DECIMAL:
492                                 fail_unless(data[i].data.decimal == expected_data.decimal,
493                                                 "query callback received unexpected data %g "
494                                                 "for column %zu; expected: %g",
495                                                 data[i].data.decimal, i, expected_data.decimal);
496                                 break;
497                         case SDB_TYPE_STRING:
498                                 fail_unless(!strcmp(data[i].data.string, expected_data.string),
499                                                 "query callback received unexpected data %s "
500                                                 "for column %zu; expected: %s",
501                                                 data[i].data.string, i, expected_data.string);
502                                 break;
503                         case SDB_TYPE_DATETIME:
504                                 fail_unless(data[i].data.datetime
505                                                         == SECS_TO_SDB_TIME(expected_data.datetime),
506                                                 "query callback received unexpected data "PRIscTIME
507                                                 " for column %zu; expected: "PRIscTIME,
508                                                 data[i].data.integer, i,
509                                                 SECS_TO_SDB_TIME(expected_data.integer));
510                                 break;
511                         case SDB_TYPE_BINARY:
512                                 fail_unless(data[i].data.binary.length ==
513                                                         expected_data.binary.length,
514                                                 "query callback received unexpected "
515                                                 "binary data length %zu for column %zu; "
516                                                 "expected: %lli", data[i].data.binary.length, i,
517                                                 expected_data.binary.length);
518                                 fail_unless(!memcmp(data[i].data.binary.datum,
519                                                         expected_data.binary.datum,
520                                                         expected_data.binary.length),
521                                                 "query callback received unexpected binary data %p "
522                                                 "for column %zu; expected: %p",
523                                                 data[i].data.binary.datum, i,
524                                                 expected_data.binary.datum);
525                                 break;
526                         default:
527                                 fail("INTERNAL ERROR: query callback received "
528                                                 "unknown type %i for column %zu",
529                                                 expected_type, i);
530                 }
531         }
532         return 0;
533 } /* query_callback */
535 /*
536  * tests
537  */
539 START_TEST(test_sdb_dbi_client_connect)
541         int check = sdb_dbi_client_connect(client);
542         fail_unless(check == 0,
543                         "sdb_dbi_client_connect() = %i; expected: 0", check);
545         fail_unless(dbi_conn_connect_called == 1,
546                         "sdb_dbi_client_create() called dbi_conn_connect %i times; "
547                         "expected: 1", dbi_conn_connect_called);
549 END_TEST
551 START_TEST(test_sdb_dbi_client_check_conn)
553         int check = sdb_dbi_client_check_conn(client);
554         fail_unless(check == 0,
555                         "sdb_dbi_client_check_conn() = %i; expected: 0", check);
557         /* the first call will actually connect to the database */
558         fail_unless(dbi_conn_connect_called == 1,
559                         "sdb_dbi_client_check_conn() called dbi_conn_connect %i times; "
560                         "expected: 1", dbi_conn_connect_called);
562         dbi_conn_connect_called = 0;
563         check = sdb_dbi_client_check_conn(client);
564         fail_unless(check == 0,
565                         "sdb_dbi_client_check_conn() = %i; expected: 0", check);
567         /* should not reconnect */
568         fail_unless(dbi_conn_connect_called == 0,
569                         "sdb_dbi_client_check_conn() called dbi_conn_connect %i time(s); "
570                         "expected: 0", dbi_conn_connect_called);
572 END_TEST
574 START_TEST(test_sdb_dbi_exec_query)
576         size_t i;
578         int check = sdb_dbi_exec_query(client, "mockquery0", query_callback,
579                         /* user_data = */ TEST_MAGIC, /* n = */ 0);
580         /* not connected yet */
581         fail_unless(check < 0,
582                         "sdb_dbi_exec_query() = %i; expected: < 0", check);
584         connect();
586         for (i = 0; i < SDB_STATIC_ARRAY_LEN(mock_queries); ++i) {
587                 mock_query_t *q = &mock_queries[i];
589                 unsigned long long expected_callback_calls = 0;
591                 dbi_conn_query_called = 0;
592                 query_callback_called = 0;
593                 dbi_result_free_called = 0;
595                 /* sdb_dbi_exec_query will only use as many type arguments are needed,
596                  * so we can safely pass in the maximum number of arguments required
597                  * on each call */
598                 check = sdb_dbi_exec_query(client, q->name, query_callback,
599                                 /* user_data = */ TEST_MAGIC, /* n = */ (int)q->nfields,
600                                 SDB_TYPE_INTEGER, SDB_TYPE_DECIMAL, SDB_TYPE_STRING,
601                                 SDB_TYPE_DATETIME, SDB_TYPE_BINARY);
602                 fail_unless(check == 0,
603                                 "sdb_dbi_exec_query() = %i; expected: 0", check);
605                 fail_unless(dbi_conn_query_called == 1,
606                                 "sdb_dbi_exec_query() called dbi_conn_query %i times; "
607                                 "expected: 1", dbi_conn_query_called);
609                 if (q->nfields)
610                         expected_callback_calls = q->nrows;
612                 fail_unless(query_callback_called == expected_callback_calls,
613                                 "sdb_dbi_exec_query() did not call the registered callback "
614                                 "for each result row; got %i call%s; expected: 0",
615                                 query_callback_called,
616                                 (query_callback_called == 1) ? "" : "s");
618                 fail_unless(dbi_result_free_called == 1,
619                                 "sdb_dbi_exec_query() did not free the query result object");
620         }
622 END_TEST
624 /*
625  * test API
626  */
628 Suite *
629 util_dbi_suite(void)
631         Suite *s = suite_create("utils::dbi");
632         TCase *tc;
634         tc = tcase_create("core");
635         tcase_add_checked_fixture(tc, setup, teardown);
636         tcase_add_test(tc, test_sdb_dbi_client_connect);
637         tcase_add_test(tc, test_sdb_dbi_client_check_conn);
638         tcase_add_test(tc, test_sdb_dbi_exec_query);
639         suite_add_tcase(s, tc);
641         return s;
642 } /* util_llist_suite */
644 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */