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 static struct {
35 int data;
36 int expected_write;
37 int expected_read;
38 } golden_data_int[] = {
39 { 5, 0, 0 },
40 { 15, 0, 0 },
41 { -3, 0, 0 },
42 { INT_MAX, 0, 0 },
43 { 27, 0, 0 },
44 { 42, 0, 0 },
45 { 6, 0, 0 },
46 { 2854, 0, 0 },
47 { 10562, 0, 0 },
48 { 0, 0, 0 },
50 /* exceeding buffer size */
51 { 20, -1, -1 },
52 { 42, -1, -1 },
53 };
55 static struct {
56 char *data;
57 int expected_write;
58 int expected_read;
59 } golden_data_string[] = {
60 { "c", 0, 0 },
61 { "", 0, 0 },
62 { "abc", 0, 0 },
63 { "foobar", 0, 0 },
64 { "qux", 0, 0 },
65 { "a b c", 0, 0 },
66 { "123", 0, 0 },
67 { "xyz", 0, 0 },
68 { "b", 0, 0 },
69 { "a", 0, 0 },
71 /* exceeding buffer size */
72 { "err1", -1, -1 },
73 { "err2", -1, -1 },
74 };
76 static sdb_channel_t *chan;
78 static void
79 setup_int(void)
80 {
81 chan = sdb_channel_create(10, sizeof(int));
82 fail_unless(chan != NULL,
83 "sdb_channel_create(10, sizeof(int)) = NULL; "
84 "expected list object");
85 } /* setup_int */
87 static void
88 setup_string(void)
89 {
90 chan = sdb_channel_create(10, sizeof(char *));
91 fail_unless(chan != NULL,
92 "sdb_chan_create(10, sizeof(char *))) = NULL; "
93 "expected channel object");
94 } /* setup_string */
96 static void
97 teardown(void)
98 {
99 sdb_channel_destroy(chan);
100 chan = NULL;
101 } /* teardown */
103 START_TEST(test_create)
104 {
105 chan = sdb_channel_create(0, 0);
106 fail_unless(chan == NULL,
107 "sdb_channel_create(0, 0) = %p; expected: NULL", chan);
109 chan = sdb_channel_create(0, 1);
110 fail_unless(chan != NULL,
111 "sdb_channel_create(0, 1) = NULL; expected: channel object");
112 sdb_channel_destroy(chan);
114 chan = sdb_channel_create(42, 23);
115 fail_unless(chan != NULL,
116 "sdb_channel_create(32, 23) = NULL; expected: channel object");
117 sdb_channel_destroy(chan);
118 }
119 END_TEST
121 START_TEST(test_write_int)
122 {
123 size_t i;
124 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_int); ++i) {
125 int data = golden_data_int[i].data;
126 int expected = golden_data_int[i].expected_write;
128 int check = sdb_channel_write(chan, &data);
129 fail_unless(check == expected,
130 "sdb_channel_write(chan, %i) = %i; expected: %i",
131 data, check, expected);
132 }
133 }
134 END_TEST
136 START_TEST(test_read_int)
137 {
138 size_t i;
140 /* populate */
141 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_int); ++i) {
142 int data = golden_data_int[i].data;
143 int expected = golden_data_int[i].expected_write;
145 int check = sdb_channel_write(chan, &data);
146 fail_unless(check == expected,
147 "sdb_channel_write(chan, %i) = %i; expected: %i",
148 data, check, expected);
149 }
151 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_int); ++i) {
152 int data = (int)i;
153 int expected = golden_data_int[i].expected_read;
155 int check = sdb_channel_read(chan, &data);
156 fail_unless(check == expected,
157 "sdb_channel_read(chan, %i) = %i; expected: %i",
158 data, check, expected);
159 if (! check) {
160 fail_unless(data == golden_data_int[i].data,
161 "sdb_channel_read() returned data %i; expected: %i",
162 data, golden_data_int[i].data);
163 }
164 }
165 }
166 END_TEST
168 START_TEST(test_write_string)
169 {
170 size_t i;
171 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_string); ++i) {
172 char *data = golden_data_string[i].data;
173 int expected = golden_data_string[i].expected_write;
175 int check = sdb_channel_write(chan, &data);
176 fail_unless(check == expected,
177 "sdb_channel_write(chan, '%s') = %i; expected: %i",
178 data, check, expected);
179 }
180 }
181 END_TEST
183 START_TEST(test_read_string)
184 {
185 size_t i;
187 /* populate */
188 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_string); ++i) {
189 char *data = golden_data_string[i].data;
190 int expected = golden_data_string[i].expected_write;
192 int check = sdb_channel_write(chan, &data);
193 fail_unless(check == expected,
194 "sdb_channel_write(chan, '%s') = %i; expected: %i",
195 data, check, expected);
196 }
198 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data_string); ++i) {
199 char *data = NULL;
200 int expected = golden_data_string[i].expected_read;
202 int check = sdb_channel_read(chan, &data);
203 fail_unless(check == expected,
204 "sdb_channel_read(chan, '') = %i; expected: %i",
205 check, expected);
206 if (check) {
207 fail_unless(data == NULL,
208 "sdb_channel_read() returned data '%s'; expected: none",
209 data);
210 }
211 else {
212 fail_unless(data != NULL,
213 "sdb_channel_read() did not return any data");
214 fail_unless(!strcmp(data, golden_data_string[i].data),
215 "sdb_channel_read() returned data '%s'; expected: '%s'",
216 data, golden_data_string[i].data);
217 }
218 }
219 }
220 END_TEST
222 Suite *
223 util_channel_suite(void)
224 {
225 Suite *s = suite_create("utils::channel");
226 TCase *tc;
228 tc = tcase_create("core");
229 tcase_add_test(tc, test_create);
230 suite_add_tcase(s, tc);
232 tc = tcase_create("integer");
233 tcase_add_checked_fixture(tc, setup_int, teardown);
234 tcase_add_test(tc, test_write_int);
235 tcase_add_test(tc, test_read_int);
236 suite_add_tcase(s, tc);
238 tc = tcase_create("string");
239 tcase_add_checked_fixture(tc, setup_string, teardown);
240 tcase_add_test(tc, test_write_string);
241 tcase_add_test(tc, test_read_string);
242 suite_add_tcase(s, tc);
244 return s;
245 } /* util_llist_suite */
247 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */