Code

t/utils/channel_test: Added some more write/read tests.
[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 <limits.h>
34 #include <stdint.h>
36 static struct {
37         int data;
38         int expected_write;
39         int expected_read;
40 } golden_data_int[] = {
41         { 5,       0, 0 },
42         { 15,      0, 0 },
43         { -3,      0, 0 },
44         { INT_MAX, 0, 0 },
45         { 27,      0, 0 },
46         { 42,      0, 0 },
47         { 6,       0, 0 },
48         { 2854,    0, 0 },
49         { 10562,   0, 0 },
50         { 0,       0, 0 },
52         /* exceeding buffer size */
53         { 20, -1, -1 },
54         { 42, -1, -1 },
55 };
57 static struct {
58         char *data;
59         int   expected_write;
60         int   expected_read;
61 } golden_data_string[] = {
62         { "c",      0, 0 },
63         { "",       0, 0 },
64         { "abc",    0, 0 },
65         { "foobar", 0, 0 },
66         { "qux",    0, 0 },
67         { "a b c",  0, 0 },
68         { "123",    0, 0 },
69         { "xyz",    0, 0 },
70         { "b",      0, 0 },
71         { "a",      0, 0 },
73         /* exceeding buffer size */
74         { "err1", -1, -1 },
75         { "err2", -1, -1 },
76 };
78 static sdb_channel_t *chan;
80 static void
81 setup_int(void)
82 {
83         chan = sdb_channel_create(10, sizeof(int));
84         fail_unless(chan != NULL,
85                         "sdb_channel_create(10, sizeof(int)) = NULL; "
86                         "expected list object");
87 } /* setup_int */
89 static void
90 setup_string(void)
91 {
92         chan = sdb_channel_create(10, sizeof(char *));
93         fail_unless(chan != NULL,
94                         "sdb_chan_create(10, sizeof(char *))) = NULL; "
95                         "expected channel object");
96 } /* setup_string */
98 static void
99 teardown(void)
101         sdb_channel_destroy(chan);
102         chan = NULL;
103 } /* teardown */
105 START_TEST(test_create)
107         chan = sdb_channel_create(0, 0);
108         fail_unless(chan == NULL,
109                         "sdb_channel_create(0, 0) = %p; expected: NULL", chan);
111         chan = sdb_channel_create(0, 1);
112         fail_unless(chan != NULL,
113                         "sdb_channel_create(0, 1) = NULL; expected: channel object");
114         sdb_channel_destroy(chan);
116         chan = sdb_channel_create(42, 23);
117         fail_unless(chan != NULL,
118                         "sdb_channel_create(32, 23) = NULL; expected: channel object");
119         sdb_channel_destroy(chan);
121 END_TEST
123 START_TEST(test_write_read)
125         uint32_t data;
126         int check;
128         chan = sdb_channel_create(0, 1);
129         fail_unless(chan != NULL,
130                         "sdb_channel_create(0, 0) = NULL; expected: channel object");
132         data = 0x00ffff00;
133         check = sdb_channel_write(chan, &data);
134         fail_unless(!check, "sdb_channel_write() = %i; expected: 0", check);
135         check = sdb_channel_write(chan, &data);
136         fail_unless(check, "sdb_channel_write() = 0; expected: <0");
138         data = 0xffffffff;
139         check = sdb_channel_read(chan, &data);
140         /* result depends on endianess */
141         fail_unless((data == 0xffffff00) || (data == 0x00ffffff),
142                         "sdb_channel_read() returned data %x; "
143                         "expected: 0xffffff00 || 0x00ffffff", data);
145         sdb_channel_destroy(chan);
147 END_TEST
149 START_TEST(test_write_int)
151         size_t i;
152         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_int); ++i) {
153                 int data = golden_data_int[i].data;
154                 int expected = golden_data_int[i].expected_write;
156                 int check = sdb_channel_write(chan, &data);
157                 fail_unless(check == expected,
158                                 "sdb_channel_write(chan, %i) = %i; expected: %i",
159                                 data, check, expected);
160         }
162 END_TEST
164 START_TEST(test_read_int)
166         size_t i;
168         /* populate */
169         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_int); ++i) {
170                 int data = golden_data_int[i].data;
171                 int expected = golden_data_int[i].expected_write;
173                 int check = sdb_channel_write(chan, &data);
174                 fail_unless(check == expected,
175                                 "sdb_channel_write(chan, %i) = %i; expected: %i",
176                                 data, check, expected);
177         }
179         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_int); ++i) {
180                 int data = (int)i;
181                 int expected = golden_data_int[i].expected_read;
183                 int check = sdb_channel_read(chan, &data);
184                 fail_unless(check == expected,
185                                 "sdb_channel_read(chan, %i) = %i; expected: %i",
186                                 data, check, expected);
187                 if (check) {
188                         fail_unless(data == (int)i,
189                                         "sdb_channel_read() modified data to '%i'; "
190                                         "expected: no modification", data);
191                 }
192                 else {
193                         fail_unless(data == golden_data_int[i].data,
194                                         "sdb_channel_read() returned data %i; expected: %i",
195                                         data, golden_data_int[i].data);
196                 }
197         }
199 END_TEST
201 START_TEST(test_write_read_int)
203         size_t i;
204         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_int); ++i) {
205                 int data = golden_data_int[i].data;
206                 int check = sdb_channel_write(chan, &data);
207                 fail_unless(check == 0,
208                                 "sdb_channel_write(chan, %i) = %i; expected: 0",
209                                 data, check);
211                 data = (int)i;
212                 check = sdb_channel_read(chan, &data);
213                 fail_unless(check == 0,
214                                 "sdb_channel_read(chan, %i) = %i; expected: 0",
215                                 data, check);
216                 if (check) {
217                         fail_unless(data == (int)i,
218                                         "sdb_channel_read() modified data to '%i'; "
219                                         "expected: no modification", data);
220                 }
221                 else {
222                         fail_unless(data == golden_data_int[i].data,
223                                         "sdb_channel_read() returned data %i; expected: %i",
224                                         data, golden_data_int[i].data);
225                 }
226         }
228 END_TEST
230 START_TEST(test_write_string)
232         size_t i;
233         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_string); ++i) {
234                 char *data = golden_data_string[i].data;
235                 int expected = golden_data_string[i].expected_write;
237                 int check = sdb_channel_write(chan, &data);
238                 fail_unless(check == expected,
239                                 "sdb_channel_write(chan, '%s') = %i; expected: %i",
240                                 data, check, expected);
241         }
243 END_TEST
245 START_TEST(test_read_string)
247         size_t i;
249         /* populate */
250         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_string); ++i) {
251                 char *data = golden_data_string[i].data;
252                 int expected = golden_data_string[i].expected_write;
254                 int check = sdb_channel_write(chan, &data);
255                 fail_unless(check == expected,
256                                 "sdb_channel_write(chan, '%s') = %i; expected: %i",
257                                 data, check, expected);
258         }
260         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_string); ++i) {
261                 char *data = NULL;
262                 int expected = golden_data_string[i].expected_read;
264                 int check = sdb_channel_read(chan, &data);
265                 fail_unless(check == expected,
266                                 "sdb_channel_read(chan, '') = %i; expected: %i",
267                                 check, expected);
268                 if (check) {
269                         fail_unless(data == NULL,
270                                         "sdb_channel_read() modified data to '%s'; "
271                                         "expected: no modification", data);
272                 }
273                 else {
274                         fail_unless(data != NULL,
275                                         "sdb_channel_read() did not return any data");
276                         fail_unless(!strcmp(data, golden_data_string[i].data),
277                                         "sdb_channel_read() returned data '%s'; expected: '%s'",
278                                         data, golden_data_string[i].data);
279                 }
280         }
282 END_TEST
284 START_TEST(test_write_read_string)
286         size_t i;
287         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_string); ++i) {
288                 char *data = golden_data_string[i].data;
289                 int check = sdb_channel_write(chan, &data);
290                 fail_unless(check == 0,
291                                 "sdb_channel_write(chan, '%s') = %i; expected: 0",
292                                 data, check);
294                 data = NULL;
295                 check = sdb_channel_read(chan, &data);
296                 fail_unless(check == 0,
297                                 "sdb_channel_read(chan, '') = %i; expected: 0", check);
298                 if (check) {
299                         fail_unless(data == NULL,
300                                         "sdb_channel_read() modified data to '%s'; "
301                                         "expected: no modifications", data);
302                 }
303                 else {
304                         fail_unless(data != NULL,
305                                         "sdb_channel_read() did not return any data");
306                         fail_unless(!strcmp(data, golden_data_string[i].data),
307                                         "sdb_channel_read() returned data '%s'; expected: '%s'",
308                                         data, golden_data_string[i].data);
309                 }
310         }
312 END_TEST
314 Suite *
315 util_channel_suite(void)
317         Suite *s = suite_create("utils::channel");
318         TCase *tc;
320         tc = tcase_create("core");
321         tcase_add_test(tc, test_create);
322         tcase_add_test(tc, test_write_read);
323         suite_add_tcase(s, tc);
325         tc = tcase_create("integer");
326         tcase_add_checked_fixture(tc, setup_int, teardown);
327         tcase_add_test(tc, test_write_int);
328         tcase_add_test(tc, test_read_int);
329         tcase_add_test(tc, test_write_read_int);
330         suite_add_tcase(s, tc);
332         tc = tcase_create("string");
333         tcase_add_checked_fixture(tc, setup_string, teardown);
334         tcase_add_test(tc, test_write_string);
335         tcase_add_test(tc, test_read_string);
336         tcase_add_test(tc, test_write_read_string);
337         suite_add_tcase(s, tc);
339         return s;
340 } /* util_llist_suite */
342 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */