2
* Copyright (c) 2009 Lukas Mejdrech
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* - Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* - 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
* - The name of the author may not be used to endorse or promote products
15
* derived from this software without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
* Networking self-tests implementation.
42
#include <net_checksum.h>
43
#include <adt/int_map.h>
44
#include <adt/char_map.h>
45
#include <adt/generic_char_map.h>
46
#include <adt/measured_strings.h>
47
#include <adt/dynamic_fifo.h>
49
#include "self_test.h"
51
/** Test the statement, compare the result and evaluate.
53
* @param[in] statement The statement to test.
54
* @param[in] result The expected result.
57
#define TEST(statement, result) \
59
printf("\n\t%s == %s", #statement, #result); \
60
if ((statement) != (result)) { \
61
printf("\tfailed\n"); \
62
fprintf(stderr, "\nNetwork self-test failed\n"); \
68
#define XMALLOC(var, type) \
70
(var) = (type *) malloc(sizeof(type)); \
71
if ((var) == NULL) { \
72
fprintf(stderr, "\nMemory allocation error\n"); \
77
GENERIC_CHAR_MAP_DECLARE(int_char_map, int);
78
GENERIC_CHAR_MAP_IMPLEMENT(int_char_map, int);
80
GENERIC_FIELD_DECLARE(int_field, int);
81
GENERIC_FIELD_IMPLEMENT(int_field, int);
83
INT_MAP_DECLARE(int_map, int);
84
INT_MAP_IMPLEMENT(int_map, int);
86
/** Self-test start function.
90
* @returns EOK on success.
91
* @returns The first error occurred.
96
printf("Running networking self-tests\n");
98
printf("\nChar map test");
101
TEST(char_map_update(&cm, "ucho", 0, 3), EINVAL);
102
TEST(char_map_initialize(&cm), EOK);
103
TEST(char_map_exclude(&cm, "bla", 0), CHAR_MAP_NULL);
104
TEST(char_map_find(&cm, "bla", 0), CHAR_MAP_NULL);
105
TEST(char_map_add(&cm, "bla", 0, 1), EOK);
106
TEST(char_map_find(&cm, "bla", 0), 1);
107
TEST(char_map_add(&cm, "bla", 0, 10), EEXISTS);
108
TEST(char_map_update(&cm, "bla", 0, 2), EOK);
109
TEST(char_map_find(&cm, "bla", 0), 2);
110
TEST(char_map_update(&cm, "ucho", 0, 2), EOK);
111
TEST(char_map_exclude(&cm, "bla", 0), 2);
112
TEST(char_map_exclude(&cm, "bla", 0), CHAR_MAP_NULL);
113
TEST(char_map_find(&cm, "ucho", 0), 2);
114
TEST(char_map_update(&cm, "ucho", 0, 3), EOK);
115
TEST(char_map_find(&cm, "ucho", 0), 3);
116
TEST(char_map_add(&cm, "blabla", 0, 5), EOK);
117
TEST(char_map_find(&cm, "blabla", 0), 5);
118
TEST(char_map_add(&cm, "bla", 0, 6), EOK);
119
TEST(char_map_find(&cm, "bla", 0), 6);
120
TEST(char_map_exclude(&cm, "bla", 0), 6);
121
TEST(char_map_find(&cm, "bla", 0), CHAR_MAP_NULL);
122
TEST(char_map_find(&cm, "blabla", 0), 5);
123
TEST(char_map_add(&cm, "auto", 0, 7), EOK);
124
TEST(char_map_find(&cm, "auto", 0), 7);
125
TEST(char_map_add(&cm, "kara", 0, 8), EOK);
126
TEST(char_map_find(&cm, "kara", 0), 8);
127
TEST(char_map_add(&cm, "nic", 0, 9), EOK);
128
TEST(char_map_find(&cm, "nic", 0), 9);
129
TEST(char_map_find(&cm, "blabla", 0), 5);
130
TEST(char_map_add(&cm, "micnicnic", 5, 9), EOK);
131
TEST(char_map_find(&cm, "micni", 0), 9);
132
TEST(char_map_find(&cm, "micnicn", 5), 9);
133
TEST(char_map_add(&cm, "\x10\x0\x2\x2", 4, 15), EOK);
134
TEST(char_map_find(&cm, "\x10\x0\x2\x2", 4), 15);
136
TEST((char_map_destroy(&cm), EOK), EOK);
137
TEST(char_map_update(&cm, "ucho", 0, 3), EINVAL);
139
printf("\nCRC computation test");
142
TEST(value = ~compute_crc32(~0, "123456789", 8 * 9), 0xcbf43926);
143
TEST(value = ~compute_crc32(~0, "1", 8), 0x83dcefb7);
144
TEST(value = ~compute_crc32(~0, "12", 8 * 2), 0x4f5344cd);
145
TEST(value = ~compute_crc32(~0, "123", 8 * 3), 0x884863d2);
146
TEST(value = ~compute_crc32(~0, "1234", 8 * 4), 0x9be3e0a3);
147
TEST(value = ~compute_crc32(~0, "12345678", 8 * 8), 0x9ae0daaf);
148
TEST(value = ~compute_crc32(~0, "ahoj pane", 8 * 9), 0x5fc3d706);
150
printf("\nDynamic fifo test");
153
TEST(dyn_fifo_push(&fifo, 1, 0), EINVAL);
154
TEST(dyn_fifo_initialize(&fifo, 1), EOK);
155
TEST(dyn_fifo_push(&fifo, 1, 0), EOK);
156
TEST(dyn_fifo_pop(&fifo), 1);
157
TEST(dyn_fifo_pop(&fifo), ENOENT);
158
TEST(dyn_fifo_push(&fifo, 2, 1), EOK);
159
TEST(dyn_fifo_push(&fifo, 3, 1), ENOMEM);
160
TEST(dyn_fifo_push(&fifo, 3, 0), EOK);
161
TEST(dyn_fifo_pop(&fifo), 2);
162
TEST(dyn_fifo_pop(&fifo), 3);
163
TEST(dyn_fifo_push(&fifo, 4, 2), EOK);
164
TEST(dyn_fifo_push(&fifo, 5, 2), EOK);
165
TEST(dyn_fifo_push(&fifo, 6, 2), ENOMEM);
166
TEST(dyn_fifo_push(&fifo, 6, 5), EOK);
167
TEST(dyn_fifo_push(&fifo, 7, 5), EOK);
168
TEST(dyn_fifo_pop(&fifo), 4);
169
TEST(dyn_fifo_pop(&fifo), 5);
170
TEST(dyn_fifo_push(&fifo, 8, 5), EOK);
171
TEST(dyn_fifo_push(&fifo, 9, 5), EOK);
172
TEST(dyn_fifo_push(&fifo, 10, 6), EOK);
173
TEST(dyn_fifo_push(&fifo, 11, 6), EOK);
174
TEST(dyn_fifo_pop(&fifo), 6);
175
TEST(dyn_fifo_pop(&fifo), 7);
176
TEST(dyn_fifo_push(&fifo, 12, 6), EOK);
177
TEST(dyn_fifo_push(&fifo, 13, 6), EOK);
178
TEST(dyn_fifo_push(&fifo, 14, 6), ENOMEM);
179
TEST(dyn_fifo_push(&fifo, 14, 8), EOK);
180
TEST(dyn_fifo_pop(&fifo), 8);
181
TEST(dyn_fifo_pop(&fifo), 9);
182
TEST(dyn_fifo_pop(&fifo), 10);
183
TEST(dyn_fifo_pop(&fifo), 11);
184
TEST(dyn_fifo_pop(&fifo), 12);
185
TEST(dyn_fifo_pop(&fifo), 13);
186
TEST(dyn_fifo_pop(&fifo), 14);
187
TEST(dyn_fifo_destroy(&fifo), EOK);
188
TEST(dyn_fifo_push(&fifo, 1, 0), EINVAL);
190
printf("\nGeneric char map test");
209
TEST(int_char_map_add(&icm, "ucho", 0, z), EINVAL);
210
TEST(int_char_map_initialize(&icm), EOK);
211
TEST((int_char_map_exclude(&icm, "bla", 0), EOK), EOK);
212
TEST(int_char_map_find(&icm, "bla", 0), NULL);
213
TEST(int_char_map_add(&icm, "bla", 0, x), EOK);
214
TEST(int_char_map_find(&icm, "bla", 0), x);
215
TEST(int_char_map_add(&icm, "bla", 0, y), EEXISTS);
216
TEST((int_char_map_exclude(&icm, "bla", 0), EOK), EOK);
217
TEST((int_char_map_exclude(&icm, "bla", 0), EOK), EOK);
218
TEST(int_char_map_add(&icm, "blabla", 0, v), EOK);
219
TEST(int_char_map_find(&icm, "blabla", 0), v);
220
TEST(int_char_map_add(&icm, "bla", 0, w), EOK);
221
TEST(int_char_map_find(&icm, "bla", 0), w);
222
TEST((int_char_map_exclude(&icm, "bla", 0), EOK), EOK);
223
TEST(int_char_map_find(&icm, "bla", 0), NULL);
224
TEST(int_char_map_find(&icm, "blabla", 0), v);
225
TEST(int_char_map_add(&icm, "auto", 0, u), EOK);
226
TEST(int_char_map_find(&icm, "auto", 0), u);
227
TEST((int_char_map_destroy(&icm), EOK), EOK);
228
TEST(int_char_map_add(&icm, "ucho", 0, z), EINVAL);
230
printf("\nGeneric field test");
242
TEST(int_field_add(&gf, x), EINVAL);
243
TEST(int_field_count(&gf), -1);
244
TEST(int_field_initialize(&gf), EOK);
245
TEST(int_field_count(&gf), 0);
246
TEST(int_field_get_index(&gf, 1), NULL);
247
TEST(int_field_add(&gf, x), 0);
248
TEST(int_field_get_index(&gf, 0), x);
249
TEST((int_field_exclude_index(&gf, 0), EOK), EOK);
250
TEST(int_field_get_index(&gf, 0), NULL);
251
TEST(int_field_add(&gf, y), 1);
252
TEST(int_field_get_index(&gf, 1), y);
253
TEST(int_field_add(&gf, z), 2);
254
TEST(int_field_get_index(&gf, 2), z);
255
TEST(int_field_get_index(&gf, 1), y);
256
TEST(int_field_count(&gf), 3);
257
TEST(int_field_add(&gf, u), 3);
258
TEST(int_field_get_index(&gf, 3), u);
259
TEST(int_field_add(&gf, v), 4);
260
TEST(int_field_get_index(&gf, 4), v);
261
TEST(int_field_add(&gf, w), 5);
262
TEST(int_field_get_index(&gf, 5), w);
263
TEST(int_field_count(&gf), 6);
264
TEST((int_field_exclude_index(&gf, 1), EOK), EOK);
265
TEST(int_field_get_index(&gf, 1), NULL);
266
TEST(int_field_get_index(&gf, 3), u);
267
TEST((int_field_exclude_index(&gf, 7), EOK), EOK);
268
TEST(int_field_get_index(&gf, 3), u);
269
TEST(int_field_get_index(&gf, 5), w);
270
TEST((int_field_exclude_index(&gf, 4), EOK), EOK);
271
TEST(int_field_get_index(&gf, 4), NULL);
272
TEST((int_field_destroy(&gf), EOK), EOK);
273
TEST(int_field_count(&gf), -1);
275
printf("\nInt map test");
287
TEST(int_map_add(&im, 1, x), EINVAL);
288
TEST(int_map_count(&im), -1);
289
TEST(int_map_initialize(&im), EOK);
290
TEST(int_map_count(&im), 0);
291
TEST(int_map_find(&im, 1), NULL);
292
TEST(int_map_add(&im, 1, x), 0);
293
TEST(int_map_find(&im, 1), x);
294
TEST((int_map_exclude(&im, 1), EOK), EOK);
295
TEST(int_map_find(&im, 1), NULL);
296
TEST(int_map_add(&im, 1, y), 1);
297
TEST(int_map_find(&im, 1), y);
298
TEST(int_map_add(&im, 4, z), 2);
299
TEST(int_map_get_index(&im, 2), z);
300
TEST(int_map_find(&im, 4), z);
301
TEST(int_map_find(&im, 1), y);
302
TEST(int_map_count(&im), 3);
303
TEST(int_map_add(&im, 2, u), 3);
304
TEST(int_map_find(&im, 2), u);
305
TEST(int_map_add(&im, 3, v), 4);
306
TEST(int_map_find(&im, 3), v);
307
TEST(int_map_get_index(&im, 4), v);
308
TEST(int_map_add(&im, 6, w), 5);
309
TEST(int_map_find(&im, 6), w);
310
TEST(int_map_count(&im), 6);
311
TEST((int_map_exclude(&im, 1), EOK), EOK);
312
TEST(int_map_find(&im, 1), NULL);
313
TEST(int_map_find(&im, 2), u);
314
TEST((int_map_exclude(&im, 7), EOK), EOK);
315
TEST(int_map_find(&im, 2), u);
316
TEST(int_map_find(&im, 6), w);
317
TEST((int_map_exclude_index(&im, 4), EOK), EOK);
318
TEST(int_map_get_index(&im, 4), NULL);
319
TEST(int_map_find(&im, 3), NULL);
320
TEST((int_map_destroy(&im), EOK), EOK);
321
TEST(int_map_count(&im), -1);
323
printf("\nMeasured strings test");
325
measured_string_ref string =
326
measured_string_create_bulk("I am a measured string!", 0);
327
printf("\n%x, %s at %x of %d\n", string, string->value, string->value,