source: mainline/uspace/app/netstart/self_test.c@ 0ee648a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0ee648a was 49d819b4, checked in by Jakub Jermar <jakub@…>, 15 years ago

Merge from lp:~jakub/helenos/net.

  • Property mode set to 100644
File size: 10.5 KB
Line 
1/*
2 * Copyright (c) 2009 Lukas Mejdrech
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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.
16 *
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.
27 */
28
29/** @addtogroup net
30 * @{
31 */
32
33/** @file
34 * Networking self-tests implementation.
35 *
36 */
37
38#include <errno.h>
39#include <malloc.h>
40#include <stdio.h>
41
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>
48
49#include "self_test.h"
50
51/** Test the statement, compare the result and evaluate.
52 *
53 * @param[in] statement The statement to test.
54 * @param[in] result The expected result.
55 *
56 */
57#define TEST(statement, result) \
58 do { \
59 printf("\n\t%s == %s", #statement, #result); \
60 if ((statement) != (result)) { \
61 printf("\tfailed\n"); \
62 fprintf(stderr, "\nNetwork self-test failed\n"); \
63 return EINVAL; \
64 } else \
65 printf("\tOK"); \
66 } while (0)
67
68#define XMALLOC(var, type) \
69 do { \
70 (var) = (type *) malloc(sizeof(type)); \
71 if ((var) == NULL) { \
72 fprintf(stderr, "\nMemory allocation error\n"); \
73 return ENOMEM; \
74 } \
75 } while (0)
76
77GENERIC_CHAR_MAP_DECLARE(int_char_map, int);
78GENERIC_CHAR_MAP_IMPLEMENT(int_char_map, int);
79
80GENERIC_FIELD_DECLARE(int_field, int);
81GENERIC_FIELD_IMPLEMENT(int_field, int);
82
83INT_MAP_DECLARE(int_map, int);
84INT_MAP_IMPLEMENT(int_map, int);
85
86/** Self-test start function.
87 *
88 * Run all self-tests.
89 *
90 * @returns EOK on success.
91 * @returns The first error occurred.
92 *
93 */
94int self_test(void)
95{
96 printf("Running networking self-tests\n");
97
98 printf("\nChar map test");
99 char_map_t cm;
100
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);
135
136 TEST((char_map_destroy(&cm), EOK), EOK);
137 TEST(char_map_update(&cm, "ucho", 0, 3), EINVAL);
138
139 printf("\nCRC computation test");
140 uint32_t value;
141
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);
149
150 printf("\nDynamic fifo test");
151 dyn_fifo_t fifo;
152
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);
189
190 printf("\nGeneric char map test");
191
192 int *x;
193 int *y;
194 int *z;
195 int *u;
196 int *v;
197 int *w;
198
199 XMALLOC(x, int);
200 XMALLOC(y, int);
201 XMALLOC(z, int);
202 XMALLOC(u, int);
203 XMALLOC(v, int);
204 XMALLOC(w, int);
205
206 int_char_map_t icm;
207 icm.magic = 0;
208
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);
229
230 printf("\nGeneric field test");
231
232 XMALLOC(x, int);
233 XMALLOC(y, int);
234 XMALLOC(z, int);
235 XMALLOC(u, int);
236 XMALLOC(v, int);
237 XMALLOC(w, int);
238
239 int_field_t gf;
240 gf.magic = 0;
241
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);
274
275 printf("\nInt map test");
276
277 XMALLOC(x, int);
278 XMALLOC(y, int);
279 XMALLOC(z, int);
280 XMALLOC(u, int);
281 XMALLOC(v, int);
282 XMALLOC(w, int);
283
284 int_map_t im;
285 im.magic = 0;
286
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);
322
323 printf("\nMeasured strings test");
324
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,
328 string->length);
329
330 return EOK;
331}
332
333/** @}
334 */
Note: See TracBrowser for help on using the repository browser.