27c12ad0aa44461f188c3ed452d89eee045e6c85
[platform/upstream/iotivity.git] / resource / csdk / connectivity / lib / libcoap-4.1.1 / tests / test_options.c
1 /* libcoap unit tests
2  *
3  * Copyright (C) 2012 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <coap.h>
13 #include "test_options.h"
14
15 /************************************************************************
16  ** decoder tests
17  ************************************************************************/
18
19 void t_parse_option1(void)
20 {
21     /* delta == 0, length == 0, value == 0 */
22     str teststr =
23     { 1, (unsigned char *) "" };
24
25     size_t result;
26     coap_option_t option;
27
28     /* result = coap_opt_parse(teststr.s, teststr.s + teststr.length, &option); */
29     result = coap_opt_parse(teststr.s, teststr.length, &option);
30     CU_ASSERT(result == 1);
31     CU_ASSERT(option.delta == 0);
32     CU_ASSERT(option.length == 0);
33     /* FIXME: value? */
34 }
35
36 void t_parse_option2(void)
37 {
38     /* delta == 12, length == 1, value == 0 */
39     str teststr =
40     { 2, (unsigned char *) "\xc1" };
41
42     size_t result;
43     coap_option_t option;
44
45     result = coap_opt_parse(teststr.s, teststr.length, &option);
46     CU_ASSERT(result == 2);
47     CU_ASSERT(option.delta == 12);
48     CU_ASSERT(option.length == 1);
49     CU_ASSERT(option.value == teststr.s + 1);
50 }
51
52 void t_parse_option3(void)
53 {
54     /* delta == 3, length == 12, value == 0 */
55     str teststr =
56     { 13, (unsigned char *) "\x3c\x00\x01\x02\x03\x04"
57             "\x05\x06\x07\x08\x09\x0a\x0b" };
58
59     size_t result;
60     coap_option_t option;
61
62     result = coap_opt_parse(teststr.s, teststr.length, &option);
63     CU_ASSERT(result == 13);
64     CU_ASSERT(option.delta == 3);
65     CU_ASSERT(option.length == 12);
66     CU_ASSERT(option.value == teststr.s + 1);
67     /* CU_ASSERT(memcmp(option.value, teststr.s + 1, 12) == 0); */
68 }
69
70 void t_parse_option4(void)
71 {
72     /* delta == 15, length == 3, value == 0 */
73     str teststr =
74     { 2, (unsigned char *) "\xf3" };
75
76     size_t result;
77     coap_option_t option;
78
79     result = coap_opt_parse(teststr.s, teststr.length, &option);
80     CU_ASSERT(result == 0);
81 }
82
83 void t_parse_option5(void)
84 {
85     /* delta == 3, length == 15, value == 0 */
86     str teststr =
87     { 2, (unsigned char *) "\x3f" };
88
89     size_t result;
90     coap_option_t option;
91
92     result = coap_opt_parse(teststr.s, teststr.length, &option);
93     CU_ASSERT(result == 0);
94 }
95
96 void t_parse_option6(void)
97 {
98     /* delta == 15, length == 15 */
99     str teststr =
100     { 1, (unsigned char *) "\xff" };
101
102     size_t result;
103     coap_option_t option;
104
105     result = coap_opt_parse(teststr.s, teststr.length, &option);
106     CU_ASSERT(result == 0);
107 }
108
109 void t_parse_option7(void)
110 {
111     /* delta == 20, length == 0 */
112     str teststr =
113     { 2, (unsigned char *) "\xd0\x07" };
114
115     size_t result;
116     coap_option_t option;
117
118     result = coap_opt_parse(teststr.s, teststr.length, &option);
119     CU_ASSERT(result == 2);
120     CU_ASSERT(option.delta == 20);
121     CU_ASSERT(option.length == 0);
122 }
123
124 void t_parse_option8(void)
125 {
126     /* delta == 780, length == 0 */
127     str teststr =
128     { 3, (unsigned char *) "\xe0\x01\xff" };
129
130     size_t result;
131     coap_option_t option;
132
133     result = coap_opt_parse(teststr.s, teststr.length, &option);
134     CU_ASSERT(result == 3);
135     CU_ASSERT(option.delta == 780);
136     CU_ASSERT(option.length == 0);
137 }
138
139 void t_parse_option9(void)
140 {
141     /* delta == 65535, length == 0 */
142     str teststr =
143     { 3, (unsigned char *) "\xe0\xfe\xf2" };
144
145     size_t result;
146     coap_option_t option;
147
148     result = coap_opt_parse(teststr.s, teststr.length, &option);
149     CU_ASSERT(result == 3);
150     CU_ASSERT(option.delta == 65535);
151 }
152
153 void t_parse_option10(void)
154 {
155     /* delta > 65535 (illegal), length == 0 */
156     str teststr =
157     { 3, (unsigned char *) "\xe0\xff\xff" };
158
159     size_t result;
160     coap_option_t option;
161
162     result = coap_opt_parse(teststr.s, teststr.length, &option);
163     CU_ASSERT(result == 0);
164 }
165
166 void t_parse_option11(void)
167 {
168     /* illegal delta value (option too short) */
169     str teststr =
170     { 1, (unsigned char *) "\xd0" };
171
172     size_t result;
173     coap_option_t option;
174
175     result = coap_opt_parse(teststr.s, teststr.length, &option);
176     CU_ASSERT(result == 0);
177 }
178
179 void t_parse_option12(void)
180 {
181     /* delta == 280, length == 500 */
182     str teststr =
183     { 3, (unsigned char *) "\xee\xff\x0b" };
184
185     size_t result;
186     coap_option_t option;
187
188     result = coap_opt_parse(teststr.s, teststr.length, &option);
189     CU_ASSERT(result == 0);
190 }
191
192 void t_parse_option13(void)
193 {
194     /* delta == 280, length == 500 */
195     unsigned char _data[505];
196     str teststr =
197     { sizeof(_data), _data };
198     teststr.s[0] = 0xee;
199     teststr.s[1] = 0x00;
200     teststr.s[2] = 0x0b;
201     teststr.s[3] = 0x00;
202     teststr.s[4] = 0xe7;
203
204     size_t result;
205     coap_option_t option;
206
207     result = coap_opt_parse(teststr.s, teststr.length, &option);
208     CU_ASSERT(result == sizeof(_data));
209     CU_ASSERT(option.delta == 280);
210     CU_ASSERT(option.length == 500);
211     CU_ASSERT(option.value == &_data[5]);
212 }
213
214 void t_parse_option14(void)
215 {
216     /* delta == 268, length == 65535 */
217     unsigned char *data;
218     unsigned int length = 4 + 65535;
219
220     data = (unsigned char *) malloc(length);
221     if (!data)
222     {
223         CU_FAIL("internal error in test framework -- insufficient memory\n");
224         return;
225     }
226
227     data[0] = 0xde;
228     data[1] = 0xff;
229     data[2] = 0xfe;
230     data[3] = 0xf2;
231
232     size_t result;
233     coap_option_t option;
234
235     result = coap_opt_parse(data, length, &option);
236     CU_ASSERT(result == length);
237     CU_ASSERT(option.delta == 268);
238     CU_ASSERT(option.length == 65535);
239     CU_ASSERT(option.value == &data[4]);
240 }
241
242 /************************************************************************
243  ** encoder tests
244  ************************************************************************/
245
246 void t_encode_option1(void)
247 {
248     char teststr[] =
249     { 0x00 };
250     unsigned char buf[40];
251     size_t result;
252
253     result = coap_opt_setheader((coap_opt_t *) buf, sizeof(buf), 0, 0);
254     CU_ASSERT(result == sizeof(teststr));
255
256     CU_ASSERT(memcmp(buf, teststr, result) == 0);
257 }
258
259 void t_encode_option2(void)
260 {
261     char teststr[] =
262     { 0x5d, 0xff };
263     unsigned char buf[40];
264     size_t result;
265
266     result = coap_opt_setheader((coap_opt_t *) buf, sizeof(buf), 5, 268);
267     CU_ASSERT(result == sizeof(teststr));
268
269     CU_ASSERT(memcmp(buf, teststr, result) == 0);
270 }
271
272 void t_encode_option3(void)
273 {
274     char teststr[] =
275     { 0xd1, 0x01 };
276     unsigned char buf[40];
277     size_t result;
278
279     result = coap_opt_setheader((coap_opt_t *) buf, sizeof(buf), 14, 1);
280     CU_ASSERT(result == sizeof(teststr));
281
282     CU_ASSERT(memcmp(buf, teststr, result) == 0);
283 }
284
285 void t_encode_option4(void)
286 {
287     char teststr[] =
288     { 0xdd, 0xff, 0xab };
289     unsigned char buf[40];
290     size_t result;
291
292     result = coap_opt_setheader((coap_opt_t *) buf, sizeof(buf), 268, 184);
293     CU_ASSERT(result == sizeof(teststr));
294
295     CU_ASSERT(memcmp(buf, teststr, result) == 0);
296 }
297
298 void t_encode_option5(void)
299 {
300     char teststr[] =
301     { 0xed, 0x13, 0x00, 0xff };
302     unsigned char buf[40];
303     size_t result;
304
305     result = coap_opt_setheader((coap_opt_t *) buf, sizeof(buf), 5133, 268);
306     CU_ASSERT(result == sizeof(teststr));
307
308     CU_ASSERT(memcmp(buf, teststr, result) == 0);
309 }
310
311 void t_encode_option6(void)
312 {
313     char teststr[] =
314     { 0xee, 0xfe, 0xf2, 0xfe, 0xf2 };
315     unsigned char buf[40];
316     size_t result;
317
318     result = coap_opt_setheader((coap_opt_t *) buf, sizeof(buf), 65535, 65535);
319     CU_ASSERT(result == sizeof(teststr));
320
321     CU_ASSERT(memcmp(buf, teststr, result) == 0);
322 }
323
324 void t_encode_option7(void)
325 {
326     char teststr[] =
327     { 0x35, 'v', 'a', 'l', 'u', 'e' };
328     const size_t valoff = 1;
329     unsigned char buf[40];
330     size_t result;
331
332     result = coap_opt_encode((coap_opt_t *) buf, sizeof(buf), 3, (unsigned char *) teststr + valoff,
333             sizeof(teststr) - valoff);
334
335     CU_ASSERT(result == sizeof(teststr));
336
337     CU_ASSERT(memcmp(buf, teststr, result) == 0);
338 }
339
340 void t_encode_option8(void)
341 {
342     /* value does not fit in message buffer */
343     unsigned char buf[40];
344     size_t result;
345
346     result = coap_opt_encode((coap_opt_t *) buf, 8, 15, (unsigned char *) "something", 9);
347
348     CU_ASSERT(result == 0);
349
350     result = coap_opt_encode((coap_opt_t *) buf, 1, 15, (unsigned char *) "something", 9);
351
352     CU_ASSERT(result == 0);
353 }
354
355 /************************************************************************
356  ** accessor tests
357  ************************************************************************/
358
359 void t_access_option1(void)
360 {
361     const char teststr[] =
362     { 0x12, 'a', 'b' };
363
364     CU_ASSERT(coap_opt_delta((coap_opt_t *) teststr) == 1);
365     CU_ASSERT(coap_opt_length((coap_opt_t *) teststr) == 2);
366     CU_ASSERT_PTR_EQUAL(coap_opt_value((coap_opt_t *) teststr), teststr + 1);
367     CU_ASSERT(coap_opt_size((coap_opt_t *) teststr) == sizeof(teststr));
368 }
369
370 void t_access_option2(void)
371 {
372     const char teststr[] =
373     { 0xe2, 0x18, 0xfd, 'a', 'b' };
374
375     CU_ASSERT(coap_opt_delta((coap_opt_t *) teststr) == 6666);
376     CU_ASSERT(coap_opt_length((coap_opt_t *) teststr) == 2);
377     CU_ASSERT_PTR_EQUAL(coap_opt_value((coap_opt_t *) teststr), teststr + 3);
378     CU_ASSERT(coap_opt_size((coap_opt_t *) teststr) == sizeof(teststr));
379 }
380
381 void t_access_option3(void)
382 {
383     const char teststr[] =
384     { 0xed, 0x18, 0x0a, 0x00, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm' };
385
386     CU_ASSERT(coap_opt_delta((coap_opt_t *) teststr) == 6423);
387     CU_ASSERT(coap_opt_length((coap_opt_t *) teststr) == 13);
388     CU_ASSERT_PTR_EQUAL(coap_opt_value((coap_opt_t *) teststr), teststr + 4);
389     CU_ASSERT(coap_opt_size((coap_opt_t *) teststr) == sizeof(teststr));
390 }
391
392 void t_access_option4(void)
393 {
394     const char teststr[] =
395     { 0xde, 0xff, 0xfe, 0xf2, 'a', 'b', 'c' };
396
397     CU_ASSERT(coap_opt_delta((coap_opt_t *) teststr) == 268);
398     CU_ASSERT(coap_opt_length((coap_opt_t *) teststr) == 65535);
399     CU_ASSERT_PTR_EQUAL(coap_opt_value((coap_opt_t *) teststr), teststr + 4);
400     CU_ASSERT(coap_opt_size((coap_opt_t *) teststr) == 65535 + 4);
401 }
402
403 void t_access_option5(void)
404 {
405     const char teststr[] =
406     { 0xee, 0xfe, 0xf2, 0x00, 0xdd, 'a', 'b', 'c' };
407
408     CU_ASSERT(coap_opt_delta((coap_opt_t *) teststr) == 65535);
409     CU_ASSERT(coap_opt_length((coap_opt_t *) teststr) == 490);
410     CU_ASSERT_PTR_EQUAL(coap_opt_value((coap_opt_t *) teststr), teststr + 5);
411     CU_ASSERT(coap_opt_size((coap_opt_t *) teststr) == 495);
412 }
413
414 void t_access_option6(void)
415 {
416     const char teststr[] =
417     { 0xf2, 'a', 'b' };
418
419     CU_ASSERT(coap_opt_delta((coap_opt_t *) teststr) == 0);
420     CU_ASSERT(coap_opt_length((coap_opt_t *) teststr) == 0);
421     CU_ASSERT_PTR_EQUAL(coap_opt_value((coap_opt_t *) teststr), NULL);
422     CU_ASSERT(coap_opt_size((coap_opt_t *) teststr) == 0);
423 }
424
425 void t_access_option7(void)
426 {
427     const char teststr[] =
428     { 0x2f, 'a', 'b' };
429
430     CU_ASSERT(coap_opt_delta((coap_opt_t *) teststr) == 2);
431     CU_ASSERT(coap_opt_length((coap_opt_t *) teststr) == 0);
432     CU_ASSERT_PTR_EQUAL(coap_opt_value((coap_opt_t *) teststr), NULL);
433     CU_ASSERT(coap_opt_size((coap_opt_t *) teststr) == 0);
434 }
435
436 /************************************************************************
437  ** accessor tests
438  ************************************************************************/
439
440 #define TEST_MAX_SIZE 1000
441
442 void t_iterate_option1(void)
443 {
444     /* CoAP PDU without token, options, or data */
445     char teststr[] __attribute__ ((aligned (8))) =
446     { 0x00, 0x00, 0x00, 0x00 };
447
448     coap_pdu_t pdu =
449     { .max_size = TEST_MAX_SIZE, .hdr = (coap_hdr_t *) teststr, .length = sizeof(teststr) };
450     coap_opt_iterator_t oi, *result;
451     coap_opt_t *option;
452
453     result = coap_option_iterator_init(&pdu, &oi, COAP_OPT_ALL);
454
455     CU_ASSERT(result == NULL);
456     CU_ASSERT(oi.bad == 1);
457
458     option = coap_option_next(&oi);
459     CU_ASSERT(oi.bad == 1);
460     CU_ASSERT(option == NULL);
461 }
462
463 void t_iterate_option2(void)
464 {
465     /* CoAP PDU with token but without options and data */
466     char teststr[] __attribute__ ((aligned (8))) =
467     { 0x03, 0x00, 0x00, 0x00, 't', 'o', 'k' };
468
469     coap_pdu_t pdu =
470     { .max_size = TEST_MAX_SIZE, .hdr = (coap_hdr_t *) teststr, .length = sizeof(teststr) };
471     coap_opt_iterator_t oi, *result;
472     coap_opt_t *option;
473
474     result = coap_option_iterator_init(&pdu, &oi, COAP_OPT_ALL);
475
476     CU_ASSERT(result == NULL);
477     CU_ASSERT(oi.bad == 1);
478
479     option = coap_option_next(&oi);
480     CU_ASSERT(oi.bad == 1);
481     CU_ASSERT(option == NULL);
482 }
483
484 void t_iterate_option3(void)
485 {
486     /* CoAP PDU with token and options */
487     char teststr[] __attribute__ ((aligned (8))) =
488     { 0x03, 0x00, 0x00, 0x00, 't', 'o', 'k', 0x13, 'o', 'p', 't', 0x00, 0xd1, 0x10, 'x' };
489
490     coap_pdu_t pdu =
491     { .max_size = TEST_MAX_SIZE, .hdr = (coap_hdr_t *) teststr, .length = sizeof(teststr) };
492     coap_opt_iterator_t oi, *result;
493     coap_opt_t *option;
494
495     result = coap_option_iterator_init(&pdu, &oi, COAP_OPT_ALL);
496
497     CU_ASSERT_PTR_EQUAL(result, &oi);
498     CU_ASSERT(oi.bad == 0);
499
500     option = coap_option_next(&oi);
501     CU_ASSERT(oi.bad == 0);
502     CU_ASSERT(oi.type == 1);
503     CU_ASSERT_PTR_EQUAL(option, teststr + 7);
504
505     option = coap_option_next(&oi);
506     CU_ASSERT(oi.bad == 0);
507     CU_ASSERT(oi.type == 1);
508     CU_ASSERT_PTR_EQUAL(option, teststr + 11);
509
510     option = coap_option_next(&oi);
511     CU_ASSERT(oi.bad == 0);
512     CU_ASSERT(oi.type == 30);
513     CU_ASSERT_PTR_EQUAL(option, teststr + 12);
514
515     option = coap_option_next(&oi);
516     CU_ASSERT(oi.bad == 1);
517     CU_ASSERT_PTR_EQUAL(option, NULL);
518 }
519
520 void t_iterate_option4(void)
521 {
522     /* CoAP PDU with token, options, and data */
523     char teststr[] __attribute__ ((aligned (8))) =
524     { 0x03, 0x00, 0x00, 0x00, 't', 'o', 'k', 0x13, 'o', 'p', 't', 0x00, 0xd1, 0x10, 'x', 0xff, 'd',
525             'a', 't', 'a' };
526
527     coap_pdu_t pdu =
528     { .max_size = TEST_MAX_SIZE, .hdr = (coap_hdr_t *) teststr, .length = sizeof(teststr) };
529     coap_opt_iterator_t oi, *result;
530     coap_opt_t *option;
531
532     result = coap_option_iterator_init(&pdu, &oi, COAP_OPT_ALL);
533
534     CU_ASSERT_PTR_EQUAL(result, &oi);
535     CU_ASSERT(oi.bad == 0);
536
537     option = coap_option_next(&oi);
538     CU_ASSERT(oi.bad == 0);
539     CU_ASSERT(oi.type == 1);
540     CU_ASSERT_PTR_EQUAL(option, teststr + 7);
541
542     option = coap_option_next(&oi);
543     CU_ASSERT(oi.bad == 0);
544     CU_ASSERT(oi.type == 1);
545     CU_ASSERT_PTR_EQUAL(option, teststr + 11);
546
547     option = coap_option_next(&oi);
548     CU_ASSERT(oi.bad == 0);
549     CU_ASSERT(oi.type == 30);
550     CU_ASSERT_PTR_EQUAL(option, teststr + 12);
551
552     option = coap_option_next(&oi);
553     CU_ASSERT(oi.bad == 1);
554     CU_ASSERT_PTR_EQUAL(option, NULL);
555 }
556
557 void t_iterate_option5(void)
558 {
559     /* CoAP PDU with malformed option */
560     char teststr[] __attribute__ ((aligned (8))) =
561     { 0x00, 0x00, 0x00, 0x00, 0x52, 'o', 'p', 0xee, 0x12, 0x03, 0x00 };
562
563     coap_pdu_t pdu =
564     { .max_size = TEST_MAX_SIZE, .hdr = (coap_hdr_t *) teststr, .length = sizeof(teststr) };
565     coap_opt_iterator_t oi, *result;
566     coap_opt_t *option;
567
568     result = coap_option_iterator_init(&pdu, &oi, COAP_OPT_ALL);
569
570     CU_ASSERT_PTR_EQUAL(result, &oi);
571     CU_ASSERT(oi.bad == 0);
572
573     option = coap_option_next(&oi);
574     CU_ASSERT(oi.bad == 0);
575     CU_ASSERT(oi.type == 5);
576     CU_ASSERT_PTR_EQUAL(option, teststr + 4);
577
578     option = coap_option_next(&oi);
579     CU_ASSERT(oi.bad == 1);
580     CU_ASSERT_PTR_EQUAL(option, NULL);
581 }
582
583 void t_iterate_option6(void)
584 {
585     /* option filter */
586     /* CoAP PDU with token, options, and data */
587     char teststr[] __attribute__ ((aligned (8))) =
588     { 0x00, 0x00, 0x00, 0x00, 0x80, 0x20, 0x00, 0x00, 0xc0, 0x00 };
589
590     coap_pdu_t pdu =
591     { .max_size = TEST_MAX_SIZE, .hdr = (coap_hdr_t *) teststr, .length = sizeof(teststr) };
592     coap_opt_iterator_t oi, *result;
593     coap_opt_t *option;
594     coap_opt_filter_t filter;
595
596     coap_option_filter_clear(filter);
597     coap_option_setb(filter, 10); /* option nr 10 only */
598     result = coap_option_iterator_init(&pdu, &oi, filter);
599
600     CU_ASSERT_PTR_EQUAL(result, &oi);
601     CU_ASSERT(oi.bad == 0);
602
603     option = coap_option_next(&oi);
604     CU_ASSERT(oi.bad == 0);
605     CU_ASSERT(oi.type == 10);
606     CU_ASSERT_PTR_EQUAL(option, teststr + 5);
607
608     option = coap_option_next(&oi);
609     CU_ASSERT(oi.bad == 0);
610     CU_ASSERT(oi.type == 10);
611     CU_ASSERT_PTR_EQUAL(option, teststr + 6);
612
613     option = coap_option_next(&oi);
614     CU_ASSERT(oi.bad == 0);
615     CU_ASSERT(oi.type == 10);
616     CU_ASSERT_PTR_EQUAL(option, teststr + 7);
617
618     option = coap_option_next(&oi);
619     CU_ASSERT(oi.bad == 1);
620     CU_ASSERT_PTR_EQUAL(option, NULL);
621 }
622
623 void t_iterate_option7(void)
624 {
625     /* option filter */
626     char teststr[] __attribute__ ((aligned (8))) =
627     { 0x00, 0x00, 0x00, 0x00, 0x80, 0x20, 0x00, 0x00, 0xc0, 0x00, 0x10, 0x10, 0x00 };
628
629     coap_pdu_t pdu =
630     { .max_size = TEST_MAX_SIZE, .hdr = (coap_hdr_t *) teststr, .length = sizeof(teststr) };
631     coap_opt_iterator_t oi, *result;
632     coap_opt_t *option;
633     coap_opt_filter_t filter;
634
635     /* search options nr 8 and 22 */
636     coap_option_filter_clear(filter);
637     coap_option_setb(filter, 8);
638     coap_option_setb(filter, 22);
639     result = coap_option_iterator_init(&pdu, &oi, filter);
640
641     CU_ASSERT_PTR_EQUAL(result, &oi);
642     CU_ASSERT(oi.bad == 0);
643
644     option = coap_option_next(&oi);
645     CU_ASSERT(oi.bad == 0);
646     CU_ASSERT(oi.type == 8);
647     CU_ASSERT_PTR_EQUAL(option, teststr + 4);
648
649     option = coap_option_next(&oi);
650     CU_ASSERT(oi.bad == 0);
651     CU_ASSERT(oi.type == 22);
652     CU_ASSERT_PTR_EQUAL(option, teststr + 8);
653
654     option = coap_option_next(&oi);
655     CU_ASSERT(oi.bad == 0);
656     CU_ASSERT(oi.type == 22);
657     CU_ASSERT_PTR_EQUAL(option, teststr + 9);
658
659     option = coap_option_next(&oi);
660     CU_ASSERT(oi.bad == 1);
661     CU_ASSERT_PTR_EQUAL(option, NULL);
662 }
663
664 void t_iterate_option8(void)
665 {
666     /* option filter */
667     char teststr[] __attribute__ ((aligned (8))) =
668     { 0x00, 0x00, 0x00, 0x00, 0x80, 0x20, 0x00, 0x00, 0xc0, 0x00, 0x10, 0x10, 0x00 };
669
670     coap_pdu_t pdu =
671     { .max_size = TEST_MAX_SIZE, .hdr = (coap_hdr_t *) teststr, .length = sizeof(teststr) };
672     coap_opt_iterator_t oi, *result;
673     coap_opt_t *option;
674     coap_opt_filter_t filter;
675
676     /* search option nr 36 */
677     coap_option_filter_clear(filter);
678     coap_option_setb(filter, 36);
679     result = coap_option_iterator_init(&pdu, &oi, filter);
680
681     CU_ASSERT_PTR_EQUAL(result, &oi);
682     CU_ASSERT(oi.bad == 0);
683
684     option = coap_option_next(&oi);
685     CU_ASSERT(oi.bad == 1);
686     CU_ASSERT_PTR_EQUAL(option, NULL);
687 }
688
689 void t_iterate_option9(void)
690 {
691     /* options filter: option number too large for filter */
692     char teststr[] __attribute__ ((aligned (8))) =
693     { 0x00, 0x00, 0x00, 0x00, 0x80, 0x20, 0x00, 0x00, 0xc0, 0x00, 0x10, 0x10, 0x00 };
694
695     coap_pdu_t pdu =
696     { .max_size = TEST_MAX_SIZE, .hdr = (coap_hdr_t *) teststr, .length = sizeof(teststr) };
697     coap_opt_iterator_t oi, *result;
698     coap_opt_t *option;
699     coap_opt_filter_t filter;
700
701     /* search option nr 100 */
702     coap_option_filter_clear(filter);
703     coap_option_setb(filter, 100);
704     result = coap_option_iterator_init(&pdu, &oi, filter);
705
706     CU_ASSERT_PTR_EQUAL(result, &oi);
707     CU_ASSERT(oi.bad == 0);
708
709     option = coap_option_next(&oi);
710     CU_ASSERT(oi.bad == 1);
711     CU_ASSERT_PTR_EQUAL(option, NULL);
712 }
713
714 void t_iterate_option10(void)
715 {
716     /* options filter: option numbers in PDU exceed filter size */
717     char teststr[] __attribute__ ((aligned (8))) =
718     { 0x00, 0x00, 0x00, 0x00, 0x80, 0x20, 0x00, 0x00, 0xd0, 0x26, 0xe0, 0x10, 0x00 };
719
720     coap_pdu_t pdu =
721     { .max_size = TEST_MAX_SIZE, .hdr = (coap_hdr_t *) teststr, .length = sizeof(teststr) };
722     coap_opt_iterator_t oi, *result;
723     coap_opt_t *option;
724     coap_opt_filter_t filter;
725
726     /* search option nr 61 */
727     coap_option_filter_clear(filter);
728     coap_option_setb(filter, 61);
729     result = coap_option_iterator_init(&pdu, &oi, filter);
730
731     CU_ASSERT_PTR_EQUAL(result, &oi);
732     CU_ASSERT(oi.bad == 0);
733
734     option = coap_option_next(&oi);
735     CU_ASSERT(oi.bad == 0);
736     CU_ASSERT_PTR_EQUAL(option, teststr + 8);
737
738     option = coap_option_next(&oi);
739     CU_ASSERT(oi.bad == 1);
740     CU_ASSERT_PTR_EQUAL(option, NULL);
741 }
742
743 /************************************************************************
744  ** initialization 
745  ************************************************************************/
746
747 CU_pSuite t_init_option_tests(void)
748 {
749     CU_pSuite suite[4];
750
751     suite[0] = CU_add_suite("option parser", NULL, NULL);
752     if (!suite[0])
753     { /* signal error */
754         fprintf(stderr, "W: cannot add option parser test suite (%s)\n", CU_get_error_msg());
755
756         return NULL;
757     }
758
759 #define OPTION_TEST(n,s)                              \
760   if (!CU_add_test(suite[0], s, t_parse_option##n)) {         \
761     fprintf(stderr, "W: cannot add option parser test (%s)\n",        \
762         CU_get_error_msg());                      \
763   }
764
765     OPTION_TEST(1, "parse option #1");
766     OPTION_TEST(2, "parse option #2");
767     OPTION_TEST(3, "parse option #3");
768     OPTION_TEST(4, "parse option #4");
769     OPTION_TEST(5, "parse option #5");
770     OPTION_TEST(6, "parse option #6");
771     OPTION_TEST(7, "parse option #7");
772     OPTION_TEST(8, "parse option #8");
773     OPTION_TEST(9, "parse option #9");
774     OPTION_TEST(10, "parse option #10");
775     OPTION_TEST(11, "parse option #11");
776     OPTION_TEST(12, "parse option #12");
777     OPTION_TEST(13, "parse option #13");
778     OPTION_TEST(14, "parse option #14");
779
780     if ((suite[1] = CU_add_suite("option encoder", NULL, NULL)))
781     {
782 #define OPTION_ENCODER_TEST(n,s)                  \
783     if (!CU_add_test(suite[1], s, t_encode_option##n)) {              \
784       fprintf(stderr, "W: cannot add option encoder test (%s)\n",     \
785           CU_get_error_msg());                    \
786     }
787
788         OPTION_ENCODER_TEST(1, "encode option #1");
789         OPTION_ENCODER_TEST(2, "encode option #2");
790         OPTION_ENCODER_TEST(3, "encode option #3");
791         OPTION_ENCODER_TEST(4, "encode option #4");
792         OPTION_ENCODER_TEST(5, "encode option #5");
793         OPTION_ENCODER_TEST(6, "encode option #6");
794         OPTION_ENCODER_TEST(7, "encode option #7");
795         OPTION_ENCODER_TEST(8, "encode option #8");
796
797     }
798     else
799     {
800         fprintf(stderr, "W: cannot add option encoder test suite (%s)\n", CU_get_error_msg());
801     }
802
803     if ((suite[2] = CU_add_suite("option accessors", NULL, NULL)))
804     {
805 #define OPTION_ACCESSOR_TEST(n,s)                 \
806     if (!CU_add_test(suite[2], s, t_access_option##n)) {              \
807       fprintf(stderr, "W: cannot add option accessor function test (%s)\n",     \
808           CU_get_error_msg());                    \
809     }
810
811         OPTION_ACCESSOR_TEST(1, "access option #1");
812         OPTION_ACCESSOR_TEST(2, "access option #2");
813         OPTION_ACCESSOR_TEST(3, "access option #3");
814         OPTION_ACCESSOR_TEST(4, "access option #4");
815         OPTION_ACCESSOR_TEST(5, "access option #5");
816         OPTION_ACCESSOR_TEST(6, "access option #6");
817         OPTION_ACCESSOR_TEST(7, "access option #7");
818
819     }
820     else
821     {
822         fprintf(stderr, "W: cannot add option acessor function test suite (%s)\n",
823                 CU_get_error_msg());
824     }
825
826     if ((suite[3] = CU_add_suite("option iterator", NULL, NULL)))
827     {
828 #define OPTION_ITERATOR_TEST(n,s)                 \
829     if (!CU_add_test(suite[3], s, t_iterate_option##n)) {             \
830       fprintf(stderr, "W: cannot add option iterator test (%s)\n",     \
831           CU_get_error_msg());                    \
832     }
833
834         OPTION_ITERATOR_TEST(1, "option iterator #1");
835         OPTION_ITERATOR_TEST(2, "option iterator #2");
836         OPTION_ITERATOR_TEST(3, "option iterator #3");
837         OPTION_ITERATOR_TEST(4, "option iterator #4");
838         OPTION_ITERATOR_TEST(5, "option iterator #5");
839         OPTION_ITERATOR_TEST(6, "option iterator #6");
840         OPTION_ITERATOR_TEST(7, "option iterator #7");
841         OPTION_ITERATOR_TEST(8, "option iterator #8");
842         OPTION_ITERATOR_TEST(9, "option iterator #9");
843         OPTION_ITERATOR_TEST(10, "option iterator #10");
844
845     }
846     else
847     {
848         fprintf(stderr, "W: cannot add option iterator test suite (%s)\n", CU_get_error_msg());
849     }
850
851     return suite[0];
852 }
853