Code

t/utils/channel_test: Added a test-case for sdb_channel_select().
[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         check = sdb_channel_select(chan, &data, NULL, NULL, NULL, &ts);
159         fail_unless(check == ETIMEDOUT,
160                         "sdb_channel_select() = %i; expected: %i (ETIMEDOUT)",
161                         check, ETIMEDOUT);
163         check = sdb_channel_select(chan, NULL, NULL, &data, NULL, NULL);
164         fail_unless(! check, "sdb_channel_select() = %i; expected: 0", check);
165         check = sdb_channel_select(chan, NULL, NULL, &data, NULL, &ts);
166         fail_unless(! check, "sdb_channel_select() = %i; expected: 0", check);
168         sdb_channel_destroy(chan);
170 END_TEST
172 START_TEST(test_write_int)
174         size_t i;
175         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_int); ++i) {
176                 int data = golden_data_int[i].data;
177                 int expected = golden_data_int[i].expected_write;
179                 int check = sdb_channel_write(chan, &data);
180                 fail_unless(check == expected,
181                                 "sdb_channel_write(chan, %i) = %i; expected: %i",
182                                 data, check, expected);
183         }
185 END_TEST
187 START_TEST(test_read_int)
189         size_t i;
191         /* populate */
192         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_int); ++i) {
193                 int data = golden_data_int[i].data;
194                 int expected = golden_data_int[i].expected_write;
196                 int check = sdb_channel_write(chan, &data);
197                 fail_unless(check == expected,
198                                 "sdb_channel_write(chan, %i) = %i; expected: %i",
199                                 data, check, expected);
200         }
202         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_int); ++i) {
203                 int data = (int)i;
204                 int expected = golden_data_int[i].expected_read;
206                 int check = sdb_channel_read(chan, &data);
207                 fail_unless(check == expected,
208                                 "sdb_channel_read(chan, %i) = %i; expected: %i",
209                                 data, check, expected);
210                 if (check) {
211                         fail_unless(data == (int)i,
212                                         "sdb_channel_read() modified data to '%i'; "
213                                         "expected: no modification", data);
214                 }
215                 else {
216                         fail_unless(data == golden_data_int[i].data,
217                                         "sdb_channel_read() returned data %i; expected: %i",
218                                         data, golden_data_int[i].data);
219                 }
220         }
222 END_TEST
224 START_TEST(test_write_read_int)
226         size_t i;
227         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_int); ++i) {
228                 int data = golden_data_int[i].data;
229                 int check = sdb_channel_write(chan, &data);
230                 fail_unless(check == 0,
231                                 "sdb_channel_write(chan, %i) = %i; expected: 0",
232                                 data, check);
234                 data = (int)i;
235                 check = sdb_channel_read(chan, &data);
236                 fail_unless(check == 0,
237                                 "sdb_channel_read(chan, %i) = %i; expected: 0",
238                                 data, check);
239                 if (check) {
240                         fail_unless(data == (int)i,
241                                         "sdb_channel_read() modified data to '%i'; "
242                                         "expected: no modification", data);
243                 }
244                 else {
245                         fail_unless(data == golden_data_int[i].data,
246                                         "sdb_channel_read() returned data %i; expected: %i",
247                                         data, golden_data_int[i].data);
248                 }
249         }
251 END_TEST
253 START_TEST(test_write_string)
255         size_t i;
256         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_string); ++i) {
257                 char *data = golden_data_string[i].data;
258                 int expected = golden_data_string[i].expected_write;
260                 int check = sdb_channel_write(chan, &data);
261                 fail_unless(check == expected,
262                                 "sdb_channel_write(chan, '%s') = %i; expected: %i",
263                                 data, check, expected);
264         }
266 END_TEST
268 START_TEST(test_read_string)
270         size_t i;
272         /* populate */
273         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_string); ++i) {
274                 char *data = golden_data_string[i].data;
275                 int expected = golden_data_string[i].expected_write;
277                 int check = sdb_channel_write(chan, &data);
278                 fail_unless(check == expected,
279                                 "sdb_channel_write(chan, '%s') = %i; expected: %i",
280                                 data, check, expected);
281         }
283         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_string); ++i) {
284                 char *data = NULL;
285                 int expected = golden_data_string[i].expected_read;
287                 int check = sdb_channel_read(chan, &data);
288                 fail_unless(check == expected,
289                                 "sdb_channel_read(chan, '') = %i; expected: %i",
290                                 check, expected);
291                 if (check) {
292                         fail_unless(data == NULL,
293                                         "sdb_channel_read() modified data to '%s'; "
294                                         "expected: no modification", data);
295                 }
296                 else {
297                         fail_unless(data != NULL,
298                                         "sdb_channel_read() did not return any data");
299                         fail_unless(!strcmp(data, golden_data_string[i].data),
300                                         "sdb_channel_read() returned data '%s'; expected: '%s'",
301                                         data, golden_data_string[i].data);
302                 }
303         }
305 END_TEST
307 START_TEST(test_write_read_string)
309         size_t i;
310         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_string); ++i) {
311                 char *data = golden_data_string[i].data;
312                 int check = sdb_channel_write(chan, &data);
313                 fail_unless(check == 0,
314                                 "sdb_channel_write(chan, '%s') = %i; expected: 0",
315                                 data, check);
317                 data = NULL;
318                 check = sdb_channel_read(chan, &data);
319                 fail_unless(check == 0,
320                                 "sdb_channel_read(chan, '') = %i; expected: 0", check);
321                 if (check) {
322                         fail_unless(data == NULL,
323                                         "sdb_channel_read() modified data to '%s'; "
324                                         "expected: no modifications", data);
325                 }
326                 else {
327                         fail_unless(data != NULL,
328                                         "sdb_channel_read() did not return any data");
329                         fail_unless(!strcmp(data, golden_data_string[i].data),
330                                         "sdb_channel_read() returned data '%s'; expected: '%s'",
331                                         data, golden_data_string[i].data);
332                 }
333         }
335 END_TEST
337 Suite *
338 util_channel_suite(void)
340         Suite *s = suite_create("utils::channel");
341         TCase *tc;
343         tc = tcase_create("core");
344         tcase_add_test(tc, test_create);
345         tcase_add_test(tc, test_write_read);
346         tcase_add_test(tc, test_select);
347         suite_add_tcase(s, tc);
349         tc = tcase_create("integer");
350         tcase_add_checked_fixture(tc, setup_int, teardown);
351         tcase_add_test(tc, test_write_int);
352         tcase_add_test(tc, test_read_int);
353         tcase_add_test(tc, test_write_read_int);
354         suite_add_tcase(s, tc);
356         tc = tcase_create("string");
357         tcase_add_checked_fixture(tc, setup_string, teardown);
358         tcase_add_test(tc, test_write_string);
359         tcase_add_test(tc, test_read_string);
360         tcase_add_test(tc, test_write_read_string);
361         suite_add_tcase(s, tc);
363         return s;
364 } /* util_llist_suite */
366 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */