]> git.ipfire.org Git - thirdparty/lldpd.git/blame - tests/check_marshal.c
Don't mix declarations and statements.
[thirdparty/lldpd.git] / tests / check_marshal.c
CommitLineData
6bf5e749
VB
1#include <stdlib.h>
2#include <unistd.h>
db323555 3#include <check.h>
6bf5e749 4#include <sys/queue.h>
db323555 5
6bf5e749
VB
6#define MARSHAL_EXPORT
7#include "../src/marshal.h"
db323555
VB
8
9/* This suite can be run in valgrind for memory leaks:
10 CK_FORK=no valgrind -v --leak-check=yes ./tests/check_marshal
11*/
12
6bf5e749 13
db323555
VB
14struct struct_simple {
15 int a1;
16 long a2;
17 char a3;
18 time_t a4;
19 char a5[7];
20};
5e73393b 21MARSHAL(struct_simple);
db323555
VB
22
23START_TEST(test_simple_structure) {
24 struct struct_simple source = {
25 .a1 = 78452,
26 .a2 = 48751424,
27 .a3 = 'h',
28 .a4 = 784254,
29 .a5 = { 'A', 'B', 'C', 'D', 'E', 'F', 'G'},
30 };
31 struct struct_simple *destination;
32 void *buffer;
33 size_t len, len2;
34
35 len = marshal_serialize(struct_simple, &source, &buffer);
36 fail_unless(len > 0, "Unable to serialize");
37 memset(&source, 0, sizeof(struct struct_simple));
38 len2 = marshal_unserialize(struct_simple, buffer, len, &destination);
39 fail_unless(len2 > 0, "Unable to deserialize");
40 free(buffer);
41 ck_assert_int_eq(len, len2);
42 ck_assert_int_eq(destination->a1, 78452);
43 ck_assert_int_eq(destination->a2, 48751424);
44 ck_assert_int_eq(destination->a3, 'h');
45 ck_assert_int_eq(destination->a4, 784254);
46 ck_assert_int_eq(destination->a5[0], 'A');
47 ck_assert_int_eq(destination->a5[1], 'B');
48 ck_assert_int_eq(destination->a5[2], 'C');
49 ck_assert_int_eq(destination->a5[3], 'D');
50 ck_assert_int_eq(destination->a5[4], 'E');
51 ck_assert_int_eq(destination->a5[5], 'F');
52 ck_assert_int_eq(destination->a5[6], 'G');
53 free(destination);
54}
55END_TEST
56
57struct struct_sub {
58 int e1;
59 struct struct_simple e2;
60 char e3;
61};
5e73393b
VB
62MARSHAL_BEGIN(struct_sub)
63MARSHAL_SUBSTRUCT(struct_sub, struct_simple, e2)
64MARSHAL_END;
db323555
VB
65
66START_TEST(test_substruct_structure) {
67 struct struct_sub source = {
68 .e1 = -5122,
69 .e2 = {
70 .a1 = 78452,
71 .a2 = 48751424,
72 .a3 = 'h',
73 .a4 = 784254,
74 .a5 = { 'A', 'B', 'C', 'D', 'E', 'F', 'G'},
75 },
76 .e3 = 'a',
77 };
78
79 struct struct_sub *destination;
80 void *buffer;
81 size_t len, len2;
82
83 len = marshal_serialize(struct_sub, &source, &buffer);
84 fail_unless(len > 0, "Unable to serialize");
85 memset(&source, 0, sizeof(struct struct_sub));
86 len2 = marshal_unserialize(struct_sub, buffer, len, &destination);
87 fail_unless(len2 > 0, "Unable to deserialize");
88 free(buffer);
89 ck_assert_int_eq(len, len2);
90 ck_assert_int_eq(destination->e1, -5122);
91 ck_assert_int_eq(destination->e2.a1, 78452);
92 ck_assert_int_eq(destination->e2.a2, 48751424);
93 ck_assert_int_eq(destination->e2.a3, 'h');
94 ck_assert_int_eq(destination->e2.a4, 784254);
95 ck_assert_int_eq(destination->e2.a5[0], 'A');
96 ck_assert_int_eq(destination->e2.a5[1], 'B');
97 ck_assert_int_eq(destination->e2.a5[2], 'C');
98 ck_assert_int_eq(destination->e2.a5[3], 'D');
99 ck_assert_int_eq(destination->e2.a5[4], 'E');
100 ck_assert_int_eq(destination->e2.a5[5], 'F');
101 ck_assert_int_eq(destination->e2.a5[6], 'G');
102 ck_assert_int_eq(destination->e3, 'a');
103 free(destination);
104}
105END_TEST
106
107struct struct_onepointer {
108 int b1;
109 long b2;
110 char b3;
111 struct struct_simple *b4;
112 int b5;
113};
5e73393b
VB
114MARSHAL_BEGIN(struct_onepointer)
115MARSHAL_POINTER(struct_onepointer, struct_simple, b4)
116MARSHAL_END;
db323555
VB
117
118START_TEST(test_pointer_structure) {
119 struct struct_simple source_simple = {
120 .a1 = 78452,
121 .a2 = 48751424,
122 .a3 = 'h',
123 .a4 = 784254,
124 .a5 = { 'A', 'B', 'C', 'D', 'E', 'F', 'G'},
125 };
126 struct struct_onepointer source = {
127 .b1 = 18,
128 .b2 = 15454,
129 .b3 = 'o',
130 .b4 = &source_simple,
131 .b5 = 333333,
132 };
133
134 struct struct_onepointer *destination;
135 void *buffer;
136 size_t len, len2;
137
138 len = marshal_serialize(struct_onepointer, &source, &buffer);
139 fail_unless(len > 0, "Unable to serialize");
140 memset(&source_simple, 0, sizeof(struct struct_simple));
141 memset(&source, 0, sizeof(struct struct_onepointer));
142 len2 = marshal_unserialize(struct_onepointer, buffer, len, &destination);
143 fail_unless(len2 > 0, "Unable to deserialize");
144 free(buffer);
145 ck_assert_int_eq(len, len2);
146 ck_assert_int_eq(destination->b1, 18);
147 ck_assert_int_eq(destination->b2, 15454);
148 ck_assert_int_eq(destination->b3, 'o');
149 ck_assert_int_eq(destination->b4->a1, 78452);
150 ck_assert_int_eq(destination->b4->a2, 48751424);
151 ck_assert_int_eq(destination->b4->a3, 'h');
152 ck_assert_int_eq(destination->b4->a4, 784254);
153 ck_assert_int_eq(destination->b4->a5[0], 'A');
154 ck_assert_int_eq(destination->b4->a5[1], 'B');
155 ck_assert_int_eq(destination->b4->a5[2], 'C');
156 ck_assert_int_eq(destination->b4->a5[3], 'D');
157 ck_assert_int_eq(destination->b4->a5[4], 'E');
158 ck_assert_int_eq(destination->b4->a5[5], 'F');
159 ck_assert_int_eq(destination->b4->a5[6], 'G');
160 ck_assert_int_eq(destination->b5, 333333);
161 free(destination->b4); free(destination);
162}
163END_TEST
164
165struct struct_nestedpointers {
166 int c1;
167 long c2;
168 struct struct_simple *c3;
169 struct struct_onepointer *c4;
170 int c5;
171};
5e73393b
VB
172MARSHAL_BEGIN(struct_nestedpointers)
173MARSHAL_POINTER(struct_nestedpointers, struct_simple, c3)
174MARSHAL_POINTER(struct_nestedpointers, struct_onepointer, c4)
175MARSHAL_END;
db323555
VB
176
177START_TEST(test_several_pointers_structure) {
178 struct struct_simple source_simple1 = {
179 .a1 = 78452,
180 .a2 = 48751424,
181 .a3 = 'h',
182 .a4 = 784254,
183 .a5 = { 'A', 'B', 'C', 'D', 'E', 'F', 'G'},
184 };
185 struct struct_simple source_simple2 = {
186 .a1 = 451,
187 .a2 = 451424,
188 .a3 = 'o',
189 .a4 = 74,
190 .a5 = { 'a', 'b', 'c', 'd', 'e', 'f', 'g'},
191 };
192 struct struct_onepointer source_onepointer = {
193 .b1 = 18,
194 .b2 = 15454,
195 .b3 = 'o',
196 .b4 = &source_simple1,
197 .b5 = 333333,
198 };
199 struct struct_nestedpointers source = {
200 .c1 = 4542,
201 .c2 = 5665454,
202 .c3 = &source_simple2,
203 .c4 = &source_onepointer,
204 .c5 = -545424,
205 };
206
207 struct struct_nestedpointers *destination;
208 void *buffer;
209 size_t len, len2;
210
211 len = marshal_serialize(struct_nestedpointers, &source, &buffer);
212 fail_unless(len > 0, "Unable to serialize");
213 memset(&source_simple1, 0, sizeof(struct struct_simple));
214 memset(&source_simple2, 0, sizeof(struct struct_simple));
215 memset(&source_onepointer, 0, sizeof(struct struct_onepointer));
216 memset(&source, 0, sizeof(struct struct_nestedpointers));
217 len2 = marshal_unserialize(struct_nestedpointers, buffer, len, &destination);
218 fail_unless(len2 > 0, "Unable to deserialize");
219 free(buffer);
220 ck_assert_int_eq(len, len2);
221 ck_assert_int_eq(destination->c1, 4542);
222 ck_assert_int_eq(destination->c2, 5665454);
223 ck_assert_int_eq(destination->c3->a1, 451);
224 ck_assert_int_eq(destination->c3->a2, 451424);
225 ck_assert_int_eq(destination->c3->a3, 'o');
226 ck_assert_int_eq(destination->c3->a4, 74);
227 ck_assert_int_eq(destination->c3->a5[3], 'd');
228 ck_assert_int_eq(destination->c3->a5[4], 'e');
229 ck_assert_int_eq(destination->c3->a5[6], 'g');
230 ck_assert_int_eq(destination->c4->b1, 18);
231 ck_assert_int_eq(destination->c4->b2, 15454);
232 ck_assert_int_eq(destination->c4->b3, 'o');
233 ck_assert_int_eq(destination->c4->b4->a1, 78452);
234 ck_assert_int_eq(destination->c4->b4->a2, 48751424);
235 ck_assert_int_eq(destination->c4->b4->a3, 'h');
236 ck_assert_int_eq(destination->c4->b4->a4, 784254);
237 ck_assert_int_eq(destination->c4->b4->a5[0], 'A');
238 ck_assert_int_eq(destination->c4->b4->a5[1], 'B');
239 ck_assert_int_eq(destination->c4->b4->a5[2], 'C');
240 ck_assert_int_eq(destination->c4->b4->a5[3], 'D');
241 ck_assert_int_eq(destination->c4->b4->a5[4], 'E');
242 ck_assert_int_eq(destination->c4->b4->a5[5], 'F');
243 ck_assert_int_eq(destination->c4->b4->a5[6], 'G');
244 ck_assert_int_eq(destination->c4->b5, 333333);
245 free(destination->c3); free(destination->c4->b4);
246 free(destination->c4); free(destination);
247}
248END_TEST
249
250START_TEST(test_null_pointers) {
251 struct struct_simple source_simple2 = {
252 .a1 = 451,
253 .a2 = 451424,
254 .a3 = 'o',
255 .a4 = 74,
256 .a5 = { 'a', 'b', 'c', 'd', 'e', 'f', 'g'},
257 };
258 struct struct_nestedpointers source = {
259 .c1 = 4542,
260 .c2 = 5665454,
261 .c3 = &source_simple2,
262 .c4 = NULL,
263 .c5 = -545424,
264 };
265
266 struct struct_nestedpointers *destination;
267 void *buffer;
268 size_t len, len2;
269
270 len = marshal_serialize(struct_nestedpointers, &source, &buffer);
271 fail_unless(len > 0, "Unable to serialize");
272 memset(&source_simple2, 0, sizeof(struct struct_simple));
273 memset(&source, 0, sizeof(struct struct_nestedpointers));
274 len2 = marshal_unserialize(struct_nestedpointers, buffer, len, &destination);
275 fail_unless(len2 > 0, "Unable to deserialize");
276 free(buffer);
277 ck_assert_int_eq(len, len2);
278 ck_assert_int_eq(destination->c1, 4542);
279 ck_assert_int_eq(destination->c2, 5665454);
280 ck_assert_int_eq(destination->c3->a1, 451);
281 ck_assert_int_eq(destination->c3->a2, 451424);
282 ck_assert_int_eq(destination->c3->a3, 'o');
283 ck_assert_int_eq(destination->c3->a4, 74);
284 ck_assert_int_eq(destination->c3->a5[3], 'd');
285 ck_assert_int_eq(destination->c3->a5[4], 'e');
286 ck_assert_int_eq(destination->c3->a5[6], 'g');
287 ck_assert_int_eq(destination->c4, NULL);
288 free(destination->c3); free(destination);
289}
290END_TEST
291
292struct struct_multipleref {
293 int f1;
294 struct struct_simple* f2;
295 struct struct_simple* f3;
296 struct struct_nestedpointers* f4;
297};
5e73393b
VB
298MARSHAL_BEGIN(struct_multipleref)
299MARSHAL_POINTER(struct_multipleref, struct_simple, f2)
300MARSHAL_POINTER(struct_multipleref, struct_simple, f3)
301MARSHAL_POINTER(struct_multipleref, struct_nestedpointers, f4)
302MARSHAL_END;
db323555
VB
303
304START_TEST(test_multiple_references) {
305 struct struct_simple source_simple = {
306 .a1 = 451,
307 .a2 = 451424,
308 .a3 = 'o',
309 .a4 = 74,
310 .a5 = { 'a', 'b', 'c', 'd', 'e', 'f', 'g'},
311 };
312 struct struct_nestedpointers source_nested = {
313 .c3 = &source_simple,
314 .c4 = NULL,
315 };
316 struct struct_multipleref source = {
317 .f1 = 15,
318 .f2 = &source_simple,
319 .f3 = &source_simple,
320 .f4 = &source_nested,
321 };
322
323 struct struct_multipleref *destination;
324 void *buffer = NULL;
325 size_t len, len2;
326
327 len = marshal_serialize(struct_multipleref, &source, &buffer);
328 fail_unless(buffer != NULL, "Buffer is empty");
329 fail_unless(len > 0, "Unable to serialize");
330 memset(&source_simple, 0, sizeof(struct struct_simple));
331 memset(&source_nested, 0, sizeof(struct struct_nestedpointers));
332 memset(&source, 0, sizeof(struct struct_multipleref));
333 len2 = marshal_unserialize(struct_multipleref, buffer, len, &destination);
334 fail_unless(len2 > 0, "Unable to deserialize");
335 free(buffer);
336 ck_assert_int_eq(len, len2);
337 ck_assert_int_eq(destination->f1, 15);
338 ck_assert_int_eq(destination->f2, destination->f3);
339 ck_assert_int_eq(destination->f2, destination->f4->c3);
340 ck_assert_int_eq(destination->f2->a1, 451);
341 ck_assert_int_eq(destination->f2->a2, 451424);
342 ck_assert_int_eq(destination->f2->a3, 'o');
343 ck_assert_int_eq(destination->f2->a4, 74);
344 ck_assert_int_eq(destination->f4->c4, NULL);
345 free(destination->f2); free(destination->f4); free(destination);
346}
347END_TEST
348
349struct struct_circularref {
350 int g1;
351 struct struct_circularref* g2;
352};
5e73393b
VB
353MARSHAL_BEGIN(struct_circularref)
354MARSHAL_POINTER(struct_circularref, struct_circularref, g2)
355MARSHAL_END;
db323555
VB
356
357START_TEST(test_circular_references) {
358 struct struct_circularref source = {
359 .g1 = 42,
360 .g2 = &source,
361 };
362
363 struct struct_circularref *destination;
364 void *buffer = NULL;
365 size_t len, len2;
366
367 len = marshal_serialize(struct_circularref, &source, &buffer);
368 fail_unless(len > 0, "Unable to serialize");
369 memset(&source, 0, sizeof(struct struct_circularref));
370 len2 = marshal_unserialize(struct_circularref, buffer, len, &destination);
371 fail_unless(len2 > 0, "Unable to deserialize");
372 free(buffer);
373 ck_assert_int_eq(len, len2);
374 ck_assert_int_eq(destination->g1, 42);
375 ck_assert_int_eq(destination->g2->g1, 42);
376 ck_assert_int_eq(destination->g2, destination->g2->g2);
377 free(destination);
378}
379END_TEST
380
381START_TEST(test_too_small_unmarshal) {
382 struct struct_simple source_simple1;
383 struct struct_onepointer source_onepointer = {
384 .b4 = &source_simple1,
385 };
386 struct struct_nestedpointers source = {
387 .c3 = &source_simple1,
388 .c4 = &source_onepointer,
389 };
390
391 struct struct_nestedpointers *destination;
392 void *buffer;
393 size_t len, len2;
5fd6695c 394 int i, j;
db323555
VB
395
396 len = marshal_serialize(struct_nestedpointers, &source, &buffer);
397 fail_unless(len > 0, "Unable to serialize");
398 memset(&source_simple1, 0, sizeof(struct struct_simple));
399 memset(&source_onepointer, 0, sizeof(struct struct_onepointer));
400 memset(&source, 0, sizeof(struct struct_nestedpointers));
db323555
VB
401 /* Loop 30 times to ease debugging leaks with valgrind */
402 for (j = 0; j < 30; j++) {
403 for (i = 0; i < len; i++) {
404 len2 = marshal_unserialize(struct_nestedpointers, buffer, 1, &destination);
405 fail_unless(len2 == 0,
406 "Should not be able to deserialize, too small (%d<%d)",
407 i, len);
408 }
409 }
410 len2 = marshal_unserialize(struct_nestedpointers, buffer, len + 5, &destination);
411 fail_unless(len2 == len, "Deserialized too much");
412 free(destination->c3);
413 free(destination->c4); free(destination); free(buffer);
414}
415END_TEST
416
417struct struct_simpleentry {
418 TAILQ_ENTRY(struct_simpleentry) s_entries;
419 int g1;
420 struct struct_simple *g2;
421};
5e73393b
VB
422MARSHAL_BEGIN(struct_simpleentry)
423MARSHAL_TQE(struct_simpleentry, s_entries)
424MARSHAL_POINTER(struct_simpleentry, struct_simple, g2)
425MARSHAL_END;
db323555
VB
426
427TAILQ_HEAD(list_simple, struct_simpleentry);
5e73393b 428MARSHAL_TQ(list_simple, struct_simpleentry);
db323555
VB
429
430START_TEST(test_simple_list) {
431 struct struct_simple source_simple = {
432 .a1 = 451,
433 .a2 = 451424,
434 .a3 = 'o',
435 .a4 = 74,
436 .a5 = { 'a', 'b', 'c', 'd', 'e', 'f', 'g'},
437 };
438 struct list_simple source;
439 struct struct_simpleentry entry1 = {
440 .g1 = 47,
441 .g2 = &source_simple,
442 };
443 struct struct_simpleentry entry2 = {
444 .g1 = 49,
445 .g2 = &source_simple,
446 };
447 struct struct_simpleentry entry3 = {
448 .g1 = 4700,
449 .g2 = NULL,
450 };
451 struct struct_simpleentry entry4 = {
452 .g1 = -47,
453 .g2 = &source_simple,
454 };
5fd6695c
VB
455 struct list_simple *destination;
456 void *buffer;
457 size_t len, len2;
458 struct struct_simpleentry *e1, *e2;
459 struct struct_simple *s;
460
db323555
VB
461 TAILQ_INIT(&source);
462 TAILQ_INSERT_TAIL(&source, &entry1, s_entries);
463 TAILQ_INSERT_TAIL(&source, &entry2, s_entries);
464 TAILQ_INSERT_TAIL(&source, &entry3, s_entries);
465 TAILQ_INSERT_TAIL(&source, &entry4, s_entries);
466
db323555
VB
467 len = marshal_serialize(list_simple, &source, &buffer);
468 fail_unless(len > 0, "Unable to serialize");
469 memset(&source, 0, sizeof(struct list_simple));
470 memset(&entry1, 0, sizeof(struct struct_simpleentry));
471 memset(&entry2, 0, sizeof(struct struct_simpleentry));
472 memset(&entry3, 0, sizeof(struct struct_simpleentry));
473 memset(&entry4, 0, sizeof(struct struct_simpleentry));
474 len2 = marshal_unserialize(list_simple, buffer, len, &destination);
475 fail_unless(len2 > 0, "Unable to deserialize");
476 free(buffer);
477
db323555
VB
478 e1 = TAILQ_FIRST(destination);
479 ck_assert_int_eq(e1->g1, 47);
480 s = e1->g2;
481 e2 = TAILQ_NEXT(e1, s_entries);
482 free(e1);
483 ck_assert_int_eq(e2->g1, 49);
484 ck_assert_int_eq(e2->g2, s);
485 e1 = TAILQ_NEXT(e2, s_entries);
486 free(e2);
487 ck_assert_int_eq(e1->g1, 4700);
488 ck_assert_int_eq(e1->g2, NULL);
489 e2 = TAILQ_NEXT(e1, s_entries);
490 free(e1);
491 ck_assert_int_eq(e2->g1, -47);
492 ck_assert_int_eq(e2->g2, s);
493 e1 = TAILQ_NEXT(e2, s_entries);
494 free(e2);
495 ck_assert_int_eq(e1, NULL);
496 free(s);
497 free(destination);
498}
499END_TEST
500
501struct struct_withlist {
502 int i1;
503 TAILQ_HEAD(, struct_simpleentry) i2;
504 int i3;
505};
5e73393b
VB
506MARSHAL_BEGIN(struct_withlist)
507MARSHAL_SUBTQ(struct_withlist, struct_simpleentry, i2)
508MARSHAL_END;
db323555
VB
509
510START_TEST(test_embedded_list) {
511 struct struct_withlist source = {
512 .i1 = 45424,
513 .i3 = 4542,
514 };
515 struct struct_simple source_simple = {
516 .a1 = 451,
517 .a2 = 451424,
518 .a3 = 'o',
519 .a4 = 74,
520 .a5 = { 'a', 'b', 'c', 'd', 'e', 'f', 'g'},
521 };
522 struct struct_simpleentry entry1 = {
523 .g1 = 47,
524 .g2 = &source_simple,
525 };
526 struct struct_simpleentry entry2 = {
527 .g1 = 49,
528 .g2 = &source_simple,
529 };
530 struct struct_simpleentry entry3 = {
531 .g1 = 4700,
532 .g2 = NULL,
533 };
534 struct struct_simpleentry entry4 = {
535 .g1 = -47,
536 .g2 = &source_simple,
537 };
5fd6695c
VB
538 struct struct_withlist *destination;
539 void *buffer;
540 size_t len, len2;
541 struct struct_simpleentry *e1, *e2;
542 struct struct_simple *s;
543
db323555
VB
544 TAILQ_INIT(&source.i2);
545 TAILQ_INSERT_TAIL(&source.i2, &entry1, s_entries);
546 TAILQ_INSERT_TAIL(&source.i2, &entry2, s_entries);
547 TAILQ_INSERT_TAIL(&source.i2, &entry3, s_entries);
548 TAILQ_INSERT_TAIL(&source.i2, &entry4, s_entries);
549
db323555
VB
550 len = marshal_serialize(struct_withlist, &source, &buffer);
551 fail_unless(len > 0, "Unable to serialize");
552 memset(&source, 0, sizeof(struct list_simple));
553 memset(&entry1, 0, sizeof(struct struct_simpleentry));
554 memset(&entry2, 0, sizeof(struct struct_simpleentry));
555 memset(&entry3, 0, sizeof(struct struct_simpleentry));
556 memset(&entry4, 0, sizeof(struct struct_simpleentry));
557 len2 = marshal_unserialize(struct_withlist, buffer, len, &destination);
558 fail_unless(len2 > 0, "Unable to deserialize");
559 free(buffer);
560
561 ck_assert_int_eq(destination->i1, 45424);
562 ck_assert_int_eq(destination->i3, 4542);
db323555
VB
563 e1 = TAILQ_FIRST(&destination->i2);
564 ck_assert_int_eq(e1->g1, 47);
565 ck_assert_int_eq(e1->g2->a4, 74);
566 s = e1->g2;
567 e2 = TAILQ_NEXT(e1, s_entries);
568 free(e1);
569 ck_assert_int_eq(e2->g1, 49);
570 ck_assert_int_eq(e2->g2, s);
571 e1 = TAILQ_NEXT(e2, s_entries);
572 free(e2);
573 ck_assert_int_eq(e1->g1, 4700);
574 ck_assert_int_eq(e1->g2, NULL);
575 e2 = TAILQ_NEXT(e1, s_entries);
576 free(e1);
577 ck_assert_int_eq(e2->g1, -47);
578 ck_assert_int_eq(e2->g2, s);
579 e1 = TAILQ_NEXT(e2, s_entries);
580 free(e2);
581 ck_assert_int_eq(e1, NULL);
582 free(s);
583 free(destination);
584}
585END_TEST
586
da781141
VB
587struct struct_string {
588 int s1;
589 char *s2;
590 char *s3;
591};
592MARSHAL_BEGIN(struct_string)
593MARSHAL_STR(struct_string, s2)
594MARSHAL_STR(struct_string, s3)
595MARSHAL_END;
596
597START_TEST(test_string) {
598 struct struct_string source = {
599 .s1 = 44444,
600 .s2 = "String 2",
601 .s3 = "String 3",
602 };
603 struct struct_string *destination;
604 void *buffer;
605 size_t len, len2;
606
607 len = marshal_serialize(struct_string, &source, &buffer);
608 fail_unless(len > 0, "Unable to serialize");
609 memset(&source, 0, sizeof(struct struct_string));
610 len2 = marshal_unserialize(struct_string, buffer, len, &destination);
611 fail_unless(len2 > 0, "Unable to deserialize");
612 free(buffer);
613 ck_assert_int_eq(len, len2);
614 ck_assert_int_eq(destination->s1, 44444);
615 ck_assert_str_eq(destination->s2, "String 2");
616 ck_assert_str_eq(destination->s3, "String 3");
617 free(destination->s2); free(destination->s3);
618 free(destination);
619}
620END_TEST
305e061c 621
ca4ed9da
VB
622struct struct_fixedstring {
623 int s1;
624 char *s2;
625 int s2_len;
626 char *s3;
627};
628MARSHAL_BEGIN(struct_fixedstring)
629MARSHAL_FSTR(struct_fixedstring, s2, s2_len)
630MARSHAL_STR(struct_fixedstring, s3)
631MARSHAL_END;
632
633START_TEST(test_fixed_string) {
634 struct struct_fixedstring source = {
635 .s1 = 44444,
636 .s2 = "String 2 Bla",
637 .s2_len = 8,
638 .s3 = "String 3",
639 };
640 struct struct_fixedstring *destination;
641 void *buffer;
642 size_t len, len2;
643
644 len = marshal_serialize(struct_fixedstring, &source, &buffer);
645 fail_unless(len > 0, "Unable to serialize");
305e061c 646 memset(&source, 0, sizeof(struct struct_fixedstring));
ca4ed9da
VB
647 len2 = marshal_unserialize(struct_fixedstring, buffer, len, &destination);
648 fail_unless(len2 > 0, "Unable to deserialize");
649 free(buffer);
650 ck_assert_int_eq(len, len2);
651 ck_assert_int_eq(destination->s1, 44444);
652 ck_assert_int_eq(destination->s2_len, 8);
653 ck_assert_int_eq(destination->s2[0], 'S');
654 ck_assert_int_eq(destination->s2[2], 'r');
655 ck_assert_int_eq(destination->s2[4], 'n');
656 ck_assert_int_eq(destination->s2[5], 'g');
657 ck_assert_int_eq(destination->s2[6], ' ');
658 ck_assert_int_eq(destination->s2[7], '2');
659 ck_assert_str_eq(destination->s3, "String 3");
660 free(destination->s2); free(destination->s3);
661 free(destination);
662}
663END_TEST
da781141 664
305e061c
VB
665struct struct_ignore {
666 int t1;
667 void *t2;
668 int t3;
669};
670MARSHAL_BEGIN(struct_ignore)
671MARSHAL_IGNORE(struct_ignore, t2)
672MARSHAL_END;
673
674START_TEST(test_ignore) {
675 struct struct_ignore source = {
676 .t1 = 4544,
677 .t2 = (void *)"String 2 Bla",
678 .t3 = 11111,
679 };
680 struct struct_ignore *destination;
681 void *buffer;
682 size_t len, len2;
683
684 len = marshal_serialize(struct_ignore, &source, &buffer);
685 fail_unless(len > 0, "Unable to serialize");
686 memset(&source, 0, sizeof(struct struct_ignore));
687 len2 = marshal_unserialize(struct_ignore, buffer, len, &destination);
688 fail_unless(len2 > 0, "Unable to deserialize");
689 free(buffer);
690 ck_assert_int_eq(len, len2);
691 ck_assert_int_eq(destination->t1, 4544);
692 ck_assert_int_eq(destination->t2, NULL);
693 ck_assert_int_eq(destination->t3, 11111);
694 free(destination);
695}
696END_TEST
697
db323555
VB
698Suite *
699marshal_suite(void)
700{
701 Suite *s = suite_create("Marshalling");
702
703 TCase *tc_marshal = tcase_create("Marshalling");
704 tcase_add_test(tc_marshal, test_simple_structure);
705 tcase_add_test(tc_marshal, test_substruct_structure);
706 tcase_add_test(tc_marshal, test_pointer_structure);
707 tcase_add_test(tc_marshal, test_several_pointers_structure);
708 tcase_add_test(tc_marshal, test_null_pointers);
709 tcase_add_test(tc_marshal, test_multiple_references);
710 tcase_add_test(tc_marshal, test_circular_references);
711 tcase_add_test(tc_marshal, test_too_small_unmarshal);
712 tcase_add_test(tc_marshal, test_simple_list);
713 tcase_add_test(tc_marshal, test_embedded_list);
da781141 714 tcase_add_test(tc_marshal, test_string);
ca4ed9da 715 tcase_add_test(tc_marshal, test_fixed_string);
305e061c 716 tcase_add_test(tc_marshal, test_ignore);
db323555
VB
717 suite_add_tcase(s, tc_marshal);
718
719 return s;
720}
721
722int
723main()
724{
725 int number_failed;
726 Suite *s = marshal_suite();
727 SRunner *sr = srunner_create(s);
728 srunner_run_all(sr, CK_ENV);
729 number_failed = srunner_ntests_failed(sr);
730 srunner_free(sr);
731 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
732}