Code

channel_test: Updated timeout check according to latest changes.
[sysdb.git] / t / utils / channel_test.c
1 /*
2  * SysDB - t/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 #include "utils/channel.h"
29 #include "libsysdb_test.h"
31 #include <check.h>
32 #include <errno.h>
33 #include <limits.h>
35 #include <stdint.h>
37 static struct {
38         int data;
39         int expected_write;
40         int expected_read;
41 } golden_data_int[] = {
42         { 5,       0, 0 },
43         { 15,      0, 0 },
44         { -3,      0, 0 },
45         { INT_MAX, 0, 0 },
46         { 27,      0, 0 },
47         { 42,      0, 0 },
48         { 6,       0, 0 },
49         { 2854,    0, 0 },
50         { 10562,   0, 0 },
51         { 0,       0, 0 },
53         /* exceeding buffer size */
54         { 20, -1, -1 },
55         { 42, -1, -1 },
56 };
58 static struct {
59         char *data;
60         int   expected_write;
61         int   expected_read;
62 } golden_data_string[] = {
63         { "c",      0, 0 },
64         { "",       0, 0 },
65         { "abc",    0, 0 },
66         { "foobar", 0, 0 },
67         { "qux",    0, 0 },
68         { "a b c",  0, 0 },
69         { "123",    0, 0 },
70         { "xyz",    0, 0 },
71         { "b",      0, 0 },
72         { "a",      0, 0 },
74         /* exceeding buffer size */
75         { "err1", -1, -1 },
76         { "err2", -1, -1 },
77 };
79 static sdb_channel_t *chan;
81 static void
82 setup_int(void)
83 {
84         chan = sdb_channel_create(10, sizeof(int));
85         fail_unless(chan != NULL,
86                         "sdb_channel_create(10, sizeof(int)) = NULL; "
87                         "expected list object");
88 } /* setup_int */
90 static void
91 setup_string(void)
92 {
93         chan = sdb_channel_create(10, sizeof(char *));
94         fail_unless(chan != NULL,
95                         "sdb_chan_create(10, sizeof(char *))) = NULL; "
96                         "expected channel object");
97 } /* setup_string */
99 static void
100 teardown(void)
102         sdb_channel_destroy(chan);
103         chan = NULL;
104 } /* teardown */
106 START_TEST(test_create)
108         chan = sdb_channel_create(0, 0);
109         fail_unless(chan == NULL,
110                         "sdb_channel_create(0, 0) = %p; expected: NULL", chan);
112         chan = sdb_channel_create(0, 1);
113         fail_unless(chan != NULL,
114                         "sdb_channel_create(0, 1) = NULL; expected: channel object");
115         sdb_channel_destroy(chan);
117         chan = sdb_channel_create(42, 23);
118         fail_unless(chan != NULL,
119                         "sdb_channel_create(32, 23) = NULL; expected: channel object");
120         sdb_channel_destroy(chan);
122 END_TEST
124 START_TEST(test_write_read)
126         uint32_t data;
127         int check;
129         chan = sdb_channel_create(0, 1);
130         fail_unless(chan != NULL,
131                         "sdb_channel_create(0, 0) = NULL; expected: channel object");
133         data = 0x00ffff00;
134         check = sdb_channel_write(chan, &data);
135         fail_unless(!check, "sdb_channel_write() = %i; expected: 0", check);
136         check = sdb_channel_write(chan, &data);
137         fail_unless(check, "sdb_channel_write() = 0; expected: <0");
139         data = 0xffffffff;
140         check = sdb_channel_read(chan, &data);
141         /* result depends on endianess */
142         fail_unless((data == 0xffffff00) || (data == 0x00ffffff),
143                         "sdb_channel_read() returned data %x; "
144                         "expected: 0xffffff00 || 0x00ffffff", data);
146         sdb_channel_destroy(chan);
148 END_TEST
150 START_TEST(test_select)
152         struct timespec ts = { 0, 10 };
153         int check;
154         int data;
156         chan = sdb_channel_create(0, 1);
158         errno = 0;
159         check = sdb_channel_select(chan, &data, NULL, NULL, NULL, &ts);
160         fail_unless(check < ETIMEDOUT,
161                         "sdb_channel_select() = %i; expected: <0", check);
162         fail_unless(errno == ETIMEDOUT,
163                         "sdb_channel_select() set errno to %i; expected: %i (ETIMEDOUT)",
164                         errno, ETIMEDOUT);
166         check = sdb_channel_select(chan, NULL, NULL, &data, NULL, NULL);
167         fail_unless(! check, "sdb_channel_select() = %i; expected: 0", check);
168         check = sdb_channel_select(chan, NULL, NULL, &data, NULL, &ts);
169         fail_unless(! check, "sdb_channel_select() = %i; expected: 0", check);
171         sdb_channel_destroy(chan);
173 END_TEST
175 START_TEST(test_write_int)
177         size_t i;
178         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_int); ++i) {
179                 int data = golden_data_int[i].data;
180                 int expected = golden_data_int[i].expected_write;
182                 int check = sdb_channel_write(chan, &data);
183                 fail_unless(check == expected,
184                                 "sdb_channel_write(chan, %i) = %i; expected: %i",
185                                 data, check, expected);
186         }
188 END_TEST
190 START_TEST(test_read_int)
192         size_t i;
194         /* populate */
195         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_int); ++i) {
196                 int data = golden_data_int[i].data;
197                 int expected = golden_data_int[i].expected_write;
199                 int check = sdb_channel_write(chan, &data);
200                 fail_unless(check == expected,
201                                 "sdb_channel_write(chan, %i) = %i; expected: %i",
202                                 data, check, expected);
203         }
205         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_int); ++i) {
206                 int data = (int)i;
207                 int expected = golden_data_int[i].expected_read;
209                 int check = sdb_channel_read(chan, &data);
210                 fail_unless(check == expected,
211                                 "sdb_channel_read(chan, %i) = %i; expected: %i",
212                                 data, check, expected);
213                 if (check) {
214                         fail_unless(data == (int)i,
215                                         "sdb_channel_read() modified data to '%i'; "
216                                         "expected: no modification", data);
217                 }
218                 else {
219                         fail_unless(data == golden_data_int[i].data,
220                                         "sdb_channel_read() returned data %i; expected: %i",
221                                         data, golden_data_int[i].data);
222                 }
223         }
225 END_TEST
227 START_TEST(test_write_read_int)
229         size_t i;
230         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_int); ++i) {
231                 int data = golden_data_int[i].data;
232                 int check = sdb_channel_write(chan, &data);
233                 fail_unless(check == 0,
234                                 "sdb_channel_write(chan, %i) = %i; expected: 0",
235                                 data, check);
237                 data = (int)i;
238                 check = sdb_channel_read(chan, &data);
239                 fail_unless(check == 0,
240                                 "sdb_channel_read(chan, %i) = %i; expected: 0",
241                                 data, check);
242                 if (check) {
243                         fail_unless(data == (int)i,
244                                         "sdb_channel_read() modified data to '%i'; "
245                                         "expected: no modification", data);
246                 }
247                 else {
248                         fail_unless(data == golden_data_int[i].data,
249                                         "sdb_channel_read() returned data %i; expected: %i",
250                                         data, golden_data_int[i].data);
251                 }
252         }
254 END_TEST
256 START_TEST(test_write_string)
258         size_t i;
259         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_string); ++i) {
260                 char *data = golden_data_string[i].data;
261                 int expected = golden_data_string[i].expected_write;
263                 int check = sdb_channel_write(chan, &data);
264                 fail_unless(check == expected,
265                                 "sdb_channel_write(chan, '%s') = %i; expected: %i",
266                                 data, check, expected);
267         }
269 END_TEST
271 START_TEST(test_read_string)
273         size_t i;
275         /* populate */
276         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_string); ++i) {
277                 char *data = golden_data_string[i].data;
278                 int expected = golden_data_string[i].expected_write;
280                 int check = sdb_channel_write(chan, &data);
281                 fail_unless(check == expected,
282                                 "sdb_channel_write(chan, '%s') = %i; expected: %i",
283                                 data, check, expected);
284         }
286         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_string); ++i) {
287                 char *data = NULL;
288                 int expected = golden_data_string[i].expected_read;
290                 int check = sdb_channel_read(chan, &data);
291                 fail_unless(check == expected,
292                                 "sdb_channel_read(chan, '') = %i; expected: %i",
293                                 check, expected);
294                 if (check) {
295                         fail_unless(data == NULL,
296                                         "sdb_channel_read() modified data to '%s'; "
297                                         "expected: no modification", data);
298                 }
299                 else {
300                         fail_unless(data != NULL,
301                                         "sdb_channel_read() did not return any data");
302                         fail_unless(!strcmp(data, golden_data_string[i].data),
303                                         "sdb_channel_read() returned data '%s'; expected: '%s'",
304                                         data, golden_data_string[i].data);
305                 }
306         }
308 END_TEST
310 START_TEST(test_write_read_string)
312         size_t i;
313         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_string); ++i) {
314                 char *data = golden_data_string[i].data;
315                 int check = sdb_channel_write(chan, &data);
316                 fail_unless(check == 0,
317                                 "sdb_channel_write(chan, '%s') = %i; expected: 0",
318                                 data, check);
320                 data = NULL;
321                 check = sdb_channel_read(chan, &data);
322                 fail_unless(check == 0,
323                                 "sdb_channel_read(chan, '') = %i; expected: 0", check);
324                 if (check) {
325                         fail_unless(data == NULL,
326                                         "sdb_channel_read() modified data to '%s'; "
327                                         "expected: no modifications", data);
328                 }
329                 else {
330                         fail_unless(data != NULL,
331                                         "sdb_channel_read() did not return any data");
332                         fail_unless(!strcmp(data, golden_data_string[i].data),
333                                         "sdb_channel_read() returned data '%s'; expected: '%s'",
334                                         data, golden_data_string[i].data);
335                 }
336         }
338 END_TEST
340 Suite *
341 util_channel_suite(void)
343         Suite *s = suite_create("utils::channel");
344         TCase *tc;
346         tc = tcase_create("core");
347         tcase_add_test(tc, test_create);
348         tcase_add_test(tc, test_write_read);
349         tcase_add_test(tc, test_select);
350         suite_add_tcase(s, tc);
352         tc = tcase_create("integer");
353         tcase_add_checked_fixture(tc, setup_int, teardown);
354         tcase_add_test(tc, test_write_int);
355         tcase_add_test(tc, test_read_int);
356         tcase_add_test(tc, test_write_read_int);
357         suite_add_tcase(s, tc);
359         tc = tcase_create("string");
360         tcase_add_checked_fixture(tc, setup_string, teardown);
361         tcase_add_test(tc, test_write_string);
362         tcase_add_test(tc, test_read_string);
363         tcase_add_test(tc, test_write_read_string);
364         suite_add_tcase(s, tc);
366         return s;
367 } /* util_llist_suite */
369 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */