Code

frontend: Connection-specific functions now use the sdb_conn_ prefix.
[sysdb.git] / t / unit / utils / channel_test.c
1 /*
2  * SysDB - t/unit/utils/channel_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
32 #include "utils/channel.h"
33 #include "testutils.h"
35 #include <check.h>
36 #include <errno.h>
37 #include <limits.h>
39 #include <stdint.h>
41 static struct {
42         int data;
43         int expected_write;
44         int expected_read;
45 } golden_data_int[] = {
46         { 5,       0, 0 },
47         { 15,      0, 0 },
48         { -3,      0, 0 },
49         { INT_MAX, 0, 0 },
50         { 27,      0, 0 },
51         { 42,      0, 0 },
52         { 6,       0, 0 },
53         { 2854,    0, 0 },
54         { 10562,   0, 0 },
55         { 0,       0, 0 },
57         /* exceeding buffer size */
58         { 20, -1, -1 },
59         { 42, -1, -1 },
60 };
62 static struct {
63         char *data;
64         int   expected_write;
65         int   expected_read;
66 } golden_data_string[] = {
67         { "c",      0, 0 },
68         { "",       0, 0 },
69         { "abc",    0, 0 },
70         { "foobar", 0, 0 },
71         { "qux",    0, 0 },
72         { "a b c",  0, 0 },
73         { "123",    0, 0 },
74         { "xyz",    0, 0 },
75         { "b",      0, 0 },
76         { "a",      0, 0 },
78         /* exceeding buffer size */
79         { "err1", -1, -1 },
80         { "err2", -1, -1 },
81 };
83 static sdb_channel_t *chan;
85 static void
86 setup_int(void)
87 {
88         chan = sdb_channel_create(10, sizeof(int));
89         fail_unless(chan != NULL,
90                         "sdb_channel_create(10, sizeof(int)) = NULL; "
91                         "expected list object");
92 } /* setup_int */
94 static void
95 setup_string(void)
96 {
97         chan = sdb_channel_create(10, sizeof(char *));
98         fail_unless(chan != NULL,
99                         "sdb_chan_create(10, sizeof(char *))) = NULL; "
100                         "expected channel object");
101 } /* setup_string */
103 static void
104 teardown(void)
106         sdb_channel_destroy(chan);
107         chan = NULL;
108 } /* teardown */
110 START_TEST(test_create)
112         chan = sdb_channel_create(0, 0);
113         fail_unless(chan == NULL,
114                         "sdb_channel_create(0, 0) = %p; expected: NULL", chan);
116         chan = sdb_channel_create(0, 1);
117         fail_unless(chan != NULL,
118                         "sdb_channel_create(0, 1) = NULL; expected: channel object");
119         sdb_channel_destroy(chan);
121         chan = sdb_channel_create(42, 23);
122         fail_unless(chan != NULL,
123                         "sdb_channel_create(32, 23) = NULL; expected: channel object");
124         sdb_channel_destroy(chan);
126 END_TEST
128 START_TEST(test_write_read)
130         uint32_t data;
131         int check;
133         chan = sdb_channel_create(0, 1);
134         fail_unless(chan != NULL,
135                         "sdb_channel_create(0, 0) = NULL; expected: channel object");
137         data = 0x00ffff00;
138         check = sdb_channel_write(chan, &data);
139         fail_unless(!check, "sdb_channel_write() = %i; expected: 0", check);
140         check = sdb_channel_write(chan, &data);
141         fail_unless(check, "sdb_channel_write() = 0; expected: <0");
143         data = 0xffffffff;
144         check = sdb_channel_read(chan, &data);
145         fail_unless(check == 0,
146                         "sdb_channel_read() = %d; expected: 0", check);
147         /* result depends on endianess */
148         fail_unless((data == 0xffffff00) || (data == 0x00ffffff),
149                         "sdb_channel_read() returned data %x; "
150                         "expected: 0xffffff00 || 0x00ffffff", data);
152         sdb_channel_destroy(chan);
154 END_TEST
156 START_TEST(test_select)
158         struct timespec ts = { 0, 10 };
159         int check;
160         int data;
162         chan = sdb_channel_create(0, 1);
164         errno = 0;
165         check = sdb_channel_select(chan, &data, NULL, NULL, NULL, &ts);
166         fail_unless(check < ETIMEDOUT,
167                         "sdb_channel_select() = %i; expected: <0", check);
168         fail_unless(errno == ETIMEDOUT,
169                         "sdb_channel_select() set errno to %i; expected: %i (ETIMEDOUT)",
170                         errno, ETIMEDOUT);
172         check = sdb_channel_select(chan, NULL, NULL, &data, NULL, NULL);
173         fail_unless(! check, "sdb_channel_select() = %i; expected: 0", check);
174         check = sdb_channel_select(chan, NULL, NULL, &data, NULL, &ts);
175         fail_unless(! check, "sdb_channel_select() = %i; expected: 0", check);
177         sdb_channel_destroy(chan);
179 END_TEST
181 START_TEST(test_write_int)
183         size_t i;
184         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_int); ++i) {
185                 int data = golden_data_int[i].data;
186                 int expected = golden_data_int[i].expected_write;
188                 int check = sdb_channel_write(chan, &data);
189                 fail_unless(check == expected,
190                                 "sdb_channel_write(chan, %i) = %i; expected: %i",
191                                 data, check, expected);
192         }
194 END_TEST
196 START_TEST(test_read_int)
198         size_t i;
200         /* populate */
201         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_int); ++i) {
202                 int data = golden_data_int[i].data;
203                 int expected = golden_data_int[i].expected_write;
205                 int check = sdb_channel_write(chan, &data);
206                 fail_unless(check == expected,
207                                 "sdb_channel_write(chan, %i) = %i; expected: %i",
208                                 data, check, expected);
209         }
211         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_int); ++i) {
212                 int data = (int)i;
213                 int expected = golden_data_int[i].expected_read;
215                 int check = sdb_channel_read(chan, &data);
216                 fail_unless(check == expected,
217                                 "sdb_channel_read(chan, %i) = %i; expected: %i",
218                                 data, check, expected);
219                 if (check) {
220                         fail_unless(data == (int)i,
221                                         "sdb_channel_read() modified data to '%i'; "
222                                         "expected: no modification", data);
223                 }
224                 else {
225                         fail_unless(data == golden_data_int[i].data,
226                                         "sdb_channel_read() returned data %i; expected: %i",
227                                         data, golden_data_int[i].data);
228                 }
229         }
231 END_TEST
233 START_TEST(test_write_read_int)
235         size_t i;
236         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_int); ++i) {
237                 int data = golden_data_int[i].data;
238                 int check = sdb_channel_write(chan, &data);
239                 fail_unless(check == 0,
240                                 "sdb_channel_write(chan, %i) = %i; expected: 0",
241                                 data, check);
243                 data = (int)i;
244                 check = sdb_channel_read(chan, &data);
245                 fail_unless(check == 0,
246                                 "sdb_channel_read(chan, %i) = %i; expected: 0",
247                                 data, check);
248                 if (check) {
249                         fail_unless(data == (int)i,
250                                         "sdb_channel_read() modified data to '%i'; "
251                                         "expected: no modification", data);
252                 }
253                 else {
254                         fail_unless(data == golden_data_int[i].data,
255                                         "sdb_channel_read() returned data %i; expected: %i",
256                                         data, golden_data_int[i].data);
257                 }
258         }
260 END_TEST
262 START_TEST(test_write_string)
264         size_t i;
265         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_string); ++i) {
266                 char *data = golden_data_string[i].data;
267                 int expected = golden_data_string[i].expected_write;
269                 int check = sdb_channel_write(chan, &data);
270                 fail_unless(check == expected,
271                                 "sdb_channel_write(chan, '%s') = %i; expected: %i",
272                                 data, check, expected);
273         }
275 END_TEST
277 START_TEST(test_read_string)
279         size_t i;
281         /* populate */
282         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_string); ++i) {
283                 char *data = golden_data_string[i].data;
284                 int expected = golden_data_string[i].expected_write;
286                 int check = sdb_channel_write(chan, &data);
287                 fail_unless(check == expected,
288                                 "sdb_channel_write(chan, '%s') = %i; expected: %i",
289                                 data, check, expected);
290         }
292         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_string); ++i) {
293                 char *data = NULL;
294                 int expected = golden_data_string[i].expected_read;
296                 int check = sdb_channel_read(chan, &data);
297                 fail_unless(check == expected,
298                                 "sdb_channel_read(chan, '') = %i; expected: %i",
299                                 check, expected);
300                 if (check) {
301                         fail_unless(data == NULL,
302                                         "sdb_channel_read() modified data to '%s'; "
303                                         "expected: no modification", data);
304                 }
305                 else {
306                         fail_unless(data != NULL,
307                                         "sdb_channel_read() did not return any data");
308                         fail_unless(!strcmp(data, golden_data_string[i].data),
309                                         "sdb_channel_read() returned data '%s'; expected: '%s'",
310                                         data, golden_data_string[i].data);
311                 }
312         }
314 END_TEST
316 START_TEST(test_write_read_string)
318         size_t i;
319         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_string); ++i) {
320                 char *data = golden_data_string[i].data;
321                 int check = sdb_channel_write(chan, &data);
322                 fail_unless(check == 0,
323                                 "sdb_channel_write(chan, '%s') = %i; expected: 0",
324                                 data, check);
326                 data = NULL;
327                 check = sdb_channel_read(chan, &data);
328                 fail_unless(check == 0,
329                                 "sdb_channel_read(chan, '') = %i; expected: 0", check);
330                 if (check) {
331                         fail_unless(data == NULL,
332                                         "sdb_channel_read() modified data to '%s'; "
333                                         "expected: no modifications", data);
334                 }
335                 else {
336                         fail_unless(data != NULL,
337                                         "sdb_channel_read() did not return any data");
338                         fail_unless(!strcmp(data, golden_data_string[i].data),
339                                         "sdb_channel_read() returned data '%s'; expected: '%s'",
340                                         data, golden_data_string[i].data);
341                 }
342         }
344 END_TEST
346 TEST_MAIN("utils::channel")
348         TCase *tc = tcase_create("core");
349         tcase_add_test(tc, test_create);
350         tcase_add_test(tc, test_write_read);
351         tcase_add_test(tc, test_select);
352         ADD_TCASE(tc);
354         tc = tcase_create("integer");
355         tcase_add_checked_fixture(tc, setup_int, teardown);
356         tcase_add_test(tc, test_write_int);
357         tcase_add_test(tc, test_read_int);
358         tcase_add_test(tc, test_write_read_int);
359         ADD_TCASE(tc);
361         tc = tcase_create("string");
362         tcase_add_checked_fixture(tc, setup_string, teardown);
363         tcase_add_test(tc, test_write_string);
364         tcase_add_test(tc, test_read_string);
365         tcase_add_test(tc, test_write_read_string);
366         ADD_TCASE(tc);
368 TEST_MAIN_END
370 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */