Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / resource / csdk / connectivity / lib / libcoap-4.1.1 / tests / test_pdu.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 <assert.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <coap.h>
14 #include "test_pdu.h"
15
16 coap_pdu_t *pdu; /* Holds the parsed PDU for most tests */
17
18 /************************************************************************
19  ** PDU decoder
20  ************************************************************************/
21
22 void t_parse_pdu1(void)
23 {
24     char teststr[] =
25     { 0x40, 0x01, 0x93, 0x34 };
26     int result;
27
28     result = coap_pdu_parse((unsigned char *) teststr, sizeof(teststr), pdu);
29     CU_ASSERT(result > 0);
30
31     CU_ASSERT(pdu->length == sizeof(teststr));
32     CU_ASSERT(pdu->hdr->version == 1);
33     CU_ASSERT(pdu->hdr->type == COAP_MESSAGE_CON);
34     CU_ASSERT(pdu->hdr->token_length == 0);
35     CU_ASSERT(pdu->hdr->code == COAP_REQUEST_GET);
36     CU_ASSERT(memcmp(&pdu->hdr->id, teststr + 2, 2) == 0);
37     CU_ASSERT_PTR_NULL(pdu->data);
38 }
39
40 void t_parse_pdu2(void)
41 {
42     char teststr[] =
43     { 0x55, 0x69, 0x12, 0x34, 't', 'o', 'k', 'e', 'n' };
44     int result;
45
46     result = coap_pdu_parse((unsigned char *) teststr, sizeof(teststr), pdu);
47     CU_ASSERT(result > 0);
48
49     CU_ASSERT(pdu->length == sizeof(teststr));
50     CU_ASSERT(pdu->hdr->version == 1);
51     CU_ASSERT(pdu->hdr->type == COAP_MESSAGE_NON);
52     CU_ASSERT(pdu->hdr->token_length == 5);
53     CU_ASSERT(pdu->hdr->code == 0x69);
54     CU_ASSERT(memcmp(&pdu->hdr->id, teststr + 2, 2) == 0);
55     CU_ASSERT(memcmp(pdu->hdr->token, teststr + 4, 5) == 0);
56     CU_ASSERT_PTR_NULL(pdu->data);
57 }
58
59 void t_parse_pdu3(void)
60 {
61     char teststr[] =
62     { 0x53, 0x69, 0x12, 0x34, 't', 'o', 'k', 'e', 'n' };
63     int result;
64
65     result = coap_pdu_parse((unsigned char *) teststr, sizeof(teststr), pdu);
66     CU_ASSERT(result == 0);
67 }
68
69 void t_parse_pdu4(void)
70 {
71     /* illegal token length */
72     char teststr[] =
73     { 0x59, 0x69, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', '1', '2', '3', '4' };
74     int result;
75
76     result = coap_pdu_parse((unsigned char *) teststr, sizeof(teststr), pdu);
77     CU_ASSERT(result == 0);
78
79     teststr[0] = 0x5f;
80
81     result = coap_pdu_parse((unsigned char *) teststr, sizeof(teststr), pdu);
82     CU_ASSERT(result == 0);
83 }
84
85 void t_parse_pdu5(void)
86 {
87     /* PDU with options */
88     char teststr[] =
89     { 0x55, 0x73, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', 0x00, 0xc1, 0x00 };
90     int result;
91
92     result = coap_pdu_parse((unsigned char *) teststr, sizeof(teststr), pdu);
93     CU_ASSERT(result > 0);
94
95     CU_ASSERT(pdu->length == sizeof(teststr));
96     CU_ASSERT(pdu->hdr->version == 1);
97     CU_ASSERT(pdu->hdr->type == COAP_MESSAGE_NON);
98     CU_ASSERT(pdu->hdr->token_length == 5);
99     CU_ASSERT(pdu->hdr->code == 0x73);
100     CU_ASSERT(memcmp(&pdu->hdr->id, teststr + 2, 2) == 0);
101     CU_ASSERT(memcmp(pdu->hdr->token, teststr + 4, 5) == 0);
102     CU_ASSERT_PTR_NULL(pdu->data);
103
104     /* FIXME: check options */
105 }
106
107 void t_parse_pdu6(void)
108 {
109     /* PDU with options that exceed the PDU */
110     char teststr[] =
111     { 0x55, 0x73, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', 0x00, 0xc1, 0x00, 0xae, 0xf0, 0x03 };
112     int result;
113
114     result = coap_pdu_parse((unsigned char *) teststr, sizeof(teststr), pdu);
115     CU_ASSERT(result == 0);
116 }
117
118 void t_parse_pdu7(void)
119 {
120     /* PDU with options and payload */
121     char teststr[] =
122     { 0x55, 0x73, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', 0x00, 0xc1, 0x00, 0xff, 'p', 'a', 'y', 'l',
123             'o', 'a', 'd' };
124     int result;
125
126     result = coap_pdu_parse((unsigned char *) teststr, sizeof(teststr), pdu);
127     CU_ASSERT(result > 0);
128
129     CU_ASSERT(pdu->length == sizeof(teststr));
130     CU_ASSERT(pdu->hdr->version == 1);
131     CU_ASSERT(pdu->hdr->type == COAP_MESSAGE_NON);
132     CU_ASSERT(pdu->hdr->token_length == 5);
133     CU_ASSERT(pdu->hdr->code == 0x73);
134     CU_ASSERT(memcmp(&pdu->hdr->id, teststr + 2, 2) == 0);
135     CU_ASSERT(memcmp(pdu->hdr->token, teststr + 4, 5) == 0);
136
137     /* FIXME: check options */
138
139     CU_ASSERT(pdu->data == (unsigned char *) pdu->hdr + 13);
140     CU_ASSERT(memcmp(pdu->data, teststr + 13, 7) == 0);
141 }
142
143 void t_parse_pdu8(void)
144 {
145     /* PDU without options but with payload */
146     char teststr[] =
147     { 0x50, 0x73, 0x12, 0x34, 0xff, 'p', 'a', 'y', 'l', 'o', 'a', 'd' };
148     int result;
149
150     result = coap_pdu_parse((unsigned char *) teststr, sizeof(teststr), pdu);
151     CU_ASSERT(result > 0);
152
153     CU_ASSERT(pdu->length == sizeof(teststr));
154     CU_ASSERT(pdu->hdr->version == 1);
155     CU_ASSERT(pdu->hdr->type == COAP_MESSAGE_NON);
156     CU_ASSERT(pdu->hdr->token_length == 0);
157     CU_ASSERT(pdu->hdr->code == 0x73);
158     CU_ASSERT(memcmp(&pdu->hdr->id, teststr + 2, 2) == 0);
159
160     /* FIXME: check options */
161
162     CU_ASSERT(pdu->data == (unsigned char *) pdu->hdr + 5);
163     CU_ASSERT(memcmp(pdu->data, teststr + 5, 7) == 0);
164 }
165
166 void t_parse_pdu9(void)
167 {
168     /* PDU without options and payload but with payload start marker */
169     char teststr[] =
170     { 0x70, 0x00, 0x12, 0x34, 0xff };
171     int result;
172
173     result = coap_pdu_parse((unsigned char *) teststr, sizeof(teststr), pdu);
174     CU_ASSERT(result == 0);
175 }
176
177 void t_parse_pdu10(void)
178 {
179     /* PDU without payload but with options and payload start marker */
180     char teststr[] =
181     { 0x53, 0x73, 0x12, 0x34, 't', 'o', 'k', 0x30, 0xc1, 0x00, 0xff };
182     int result;
183
184     result = coap_pdu_parse((unsigned char *) teststr, sizeof(teststr), pdu);
185     CU_ASSERT(result == 0);
186 }
187
188 void t_parse_pdu11(void)
189 {
190     char teststr[] =
191     { 0x60, 0x00, 0x12, 0x34 };
192     int result;
193
194     result = coap_pdu_parse((unsigned char *) teststr, sizeof(teststr), pdu);
195     CU_ASSERT(result > 0);
196
197     CU_ASSERT(pdu->length == sizeof(teststr));
198     CU_ASSERT(pdu->hdr->version == 1);
199     CU_ASSERT(pdu->hdr->type == COAP_MESSAGE_ACK);
200     CU_ASSERT(pdu->hdr->token_length == 0);
201     CU_ASSERT(pdu->hdr->code == 0);
202     CU_ASSERT(memcmp(&pdu->hdr->id, teststr + 2, 2) == 0);
203 }
204
205 void t_parse_pdu12(void)
206 {
207     /* RST */
208     char teststr[] =
209     { 0x70, 0x00, 0x12, 0x34 };
210     int result;
211
212     result = coap_pdu_parse((unsigned char *) teststr, sizeof(teststr), pdu);
213     CU_ASSERT(result > 0);
214
215     CU_ASSERT(pdu->length == sizeof(teststr));
216     CU_ASSERT(pdu->hdr->version == 1);
217     CU_ASSERT(pdu->hdr->type == COAP_MESSAGE_RST);
218     CU_ASSERT(pdu->hdr->token_length == 0);
219     CU_ASSERT(pdu->hdr->code == 0);
220     CU_ASSERT(memcmp(&pdu->hdr->id, teststr + 2, 2) == 0);
221 }
222
223 void t_parse_pdu13(void)
224 {
225     /* RST with content */
226     char teststr[] =
227     { 0x70, 0x00, 0x12, 0x34, 0xff, 'c', 'o', 'n', 't', 'e', 'n', 't' };
228     int result;
229
230     result = coap_pdu_parse((unsigned char *) teststr, sizeof(teststr), pdu);
231     CU_ASSERT(result == 0);
232 }
233
234 void t_parse_pdu14(void)
235 {
236     /* ACK with content */
237     char teststr[] =
238     { 0x60, 0x00, 0x12, 0x34, 0xff, 'c', 'o', 'n', 't', 'e', 'n', 't' };
239     int result;
240
241     result = coap_pdu_parse((unsigned char *) teststr, sizeof(teststr), pdu);
242     CU_ASSERT(result == 0);
243 }
244
245 /************************************************************************
246  ** PDU encoder
247  ************************************************************************/
248
249 void t_encode_pdu1(void)
250 {
251     char teststr[] =
252     { 0x45, 0x01, 0x12, 0x34, 't', 'o', 'k', 'e', 'n' };
253     int result;
254
255     coap_pdu_clear(pdu, pdu->max_size);
256     pdu->hdr->type = COAP_MESSAGE_CON;
257     pdu->hdr->code = COAP_REQUEST_GET;
258     pdu->hdr->id = htons(0x1234);
259
260     result = coap_add_token(pdu, 5, (unsigned char *) "token");
261
262     CU_ASSERT(result == 1);
263     CU_ASSERT(pdu->length = sizeof(teststr));
264     CU_ASSERT_PTR_NULL(pdu->data);
265     CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0);
266 }
267
268 void t_encode_pdu2(void)
269 {
270     size_t old_max = pdu->max_size;
271     int result;
272
273     coap_pdu_clear(pdu, 7); /* set very small PDU size */
274
275     pdu->hdr->type = COAP_MESSAGE_CON;
276     pdu->hdr->code = COAP_REQUEST_GET;
277     pdu->hdr->id = htons(0x1234);
278
279     result = coap_add_token(pdu, 5, (unsigned char *) "token");
280
281     CU_ASSERT(result == 0);
282
283     coap_pdu_clear(pdu, old_max); /* restore PDU size */
284 }
285
286 void t_encode_pdu3(void)
287 {
288     int result;
289
290     result = coap_add_token(pdu, 9, (unsigned char *) "123456789");
291
292     CU_ASSERT(result == 0);
293 }
294
295 void t_encode_pdu4(void)
296 {
297     /* PDU with options */
298     char teststr[] =
299     { 0x60, 0x99, 0x12, 0x34, 0x3d, 0x05, 0x66, 0x61, 0x6e, 0x63, 0x79, 0x70, 0x72, 0x6f, 0x78,
300             0x79, 0x2e, 0x63, 0x6f, 0x61, 0x70, 0x2e, 0x6d, 0x65, 0x84, 0x70, 0x61, 0x74, 0x68,
301             0x00, 0xe8, 0x1e, 0x28, 0x66, 0x61, 0x6e, 0x63, 0x79, 0x6f, 0x70, 0x74 };
302     int result;
303
304     coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */
305
306     pdu->hdr->type = COAP_MESSAGE_ACK;
307     pdu->hdr->code = 0x99;
308     pdu->hdr->id = htons(0x1234);
309
310     CU_ASSERT(pdu->length == 4);
311
312     result = coap_add_option(pdu, COAP_OPTION_URI_HOST, 18, (unsigned char *) "fancyproxy.coap.me");
313
314     CU_ASSERT(result == 20);
315     CU_ASSERT(pdu->max_delta == 3);
316     CU_ASSERT(pdu->length == 24);
317     CU_ASSERT_PTR_NULL(pdu->data);
318
319     result = coap_add_option(pdu, COAP_OPTION_URI_PATH, 4, (unsigned char *) "path");
320
321     CU_ASSERT(result == 5);
322     CU_ASSERT(pdu->max_delta == 11);
323     CU_ASSERT(pdu->length == 29);
324     CU_ASSERT_PTR_NULL(pdu->data);
325
326     result = coap_add_option(pdu, COAP_OPTION_URI_PATH, 0, NULL);
327
328     CU_ASSERT(result == 1);
329     CU_ASSERT(pdu->max_delta == 11);
330     CU_ASSERT(pdu->length == 30);
331     CU_ASSERT_PTR_NULL(pdu->data);
332
333     result = coap_add_option(pdu, 8000, 8, (unsigned char *) "fancyopt");
334
335     CU_ASSERT(result == 11);
336     CU_ASSERT(pdu->max_delta == 8000);
337     CU_ASSERT(pdu->length == 41);
338     CU_ASSERT_PTR_NULL(pdu->data);
339
340     CU_ASSERT(pdu->length == sizeof(teststr));
341     CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0);
342 }
343
344 void t_encode_pdu5(void)
345 {
346     /* PDU with token and options */
347     char teststr[] =
348     { 0x68, 0x84, 0x12, 0x34, '1', '2', '3', '4', '5', '6', '7', '8', 0x18, 0x41, 0x42, 0x43, 0x44,
349             0x45, 0x46, 0x47, 0x48, 0xd1, 0x03, 0x12 };
350     int result;
351
352     coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */
353
354     pdu->hdr->type = COAP_MESSAGE_ACK;
355     pdu->hdr->code = COAP_RESPONSE_CODE(404);
356     pdu->hdr->id = htons(0x1234);
357
358     CU_ASSERT(pdu->length == 4);
359
360     result = coap_add_token(pdu, 8, (unsigned char *) "12345678");
361
362     CU_ASSERT(pdu->length == 12);
363
364     result = coap_add_option(pdu, COAP_OPTION_IF_MATCH, 8, (unsigned char *) "ABCDEFGH");
365
366     CU_ASSERT(result == 9);
367     CU_ASSERT(pdu->max_delta == 1);
368     CU_ASSERT(pdu->length == 21);
369     CU_ASSERT_PTR_NULL(pdu->data);
370
371     result = coap_add_option(pdu, COAP_OPTION_ACCEPT, 1, (unsigned char *) "\x12");
372
373     CU_ASSERT(result == 3);
374     CU_ASSERT(pdu->max_delta == 17);
375     CU_ASSERT(pdu->length == 24);
376     CU_ASSERT_PTR_NULL(pdu->data);
377
378     CU_ASSERT(pdu->length == sizeof(teststr));
379     CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0);
380 }
381
382 void t_encode_pdu6(void)
383 {
384     /* PDU with data */
385     char teststr[] =
386     { 0x50, 0x02, 0x12, 0x34, 0xff, '1', '2', '3', '4', '5', '6', '7', '8' };
387     coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */
388
389     pdu->hdr->type = COAP_MESSAGE_NON;
390     pdu->hdr->code = COAP_REQUEST_POST;
391     pdu->hdr->id = htons(0x1234);
392
393     CU_ASSERT(pdu->length == 4);
394     CU_ASSERT_PTR_NULL(pdu->data);
395
396     coap_add_data(pdu, 8, (unsigned char *) "12345678");
397
398     CU_ASSERT(pdu->length == sizeof(teststr));
399     CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0);
400 }
401
402 void t_encode_pdu7(void)
403 {
404     /* PDU with empty data */
405     char teststr[] =
406     { 0x40, 0x43, 0x12, 0x34 };
407     int result;
408     coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */
409
410     pdu->hdr->type = COAP_MESSAGE_CON;
411     pdu->hdr->code = COAP_RESPONSE_CODE(203);
412     pdu->hdr->id = htons(0x1234);
413
414     CU_ASSERT(pdu->length == 4);
415
416     result = coap_add_data(pdu, 0, NULL);
417
418     CU_ASSERT(result > 0);
419     CU_ASSERT(pdu->length == 4);
420     CU_ASSERT_PTR_NULL(pdu->data);
421
422     CU_ASSERT(pdu->length == sizeof(teststr));
423     CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0);
424 }
425
426 void t_encode_pdu8(void)
427 {
428     /* PDU with token and data */
429     char teststr[] =
430     { 0x42, 0x43, 0x12, 0x34, 0x00, 0x01, 0xff, 0x00 };
431     int result;
432     coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */
433
434     pdu->hdr->type = COAP_MESSAGE_CON;
435     pdu->hdr->code = COAP_RESPONSE_CODE(203);
436     pdu->hdr->id = htons(0x1234);
437
438     CU_ASSERT(pdu->length == 4);
439
440     result = coap_add_token(pdu, 2, (unsigned char *) "\x00\x01");
441
442     CU_ASSERT(result > 0);
443
444     result = coap_add_data(pdu, 1, (unsigned char *) "\0");
445
446     CU_ASSERT(result > 0);
447     CU_ASSERT(pdu->length == 8);
448     CU_ASSERT(pdu->data == (unsigned char *) pdu->hdr + 7);
449
450     CU_ASSERT(pdu->length == sizeof(teststr));
451     CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0);
452 }
453
454 void t_encode_pdu9(void)
455 {
456     /* PDU with options and data */
457     char teststr[] =
458     { 0x60, 0x44, 0x12, 0x34, 0x48, 's', 'o', 'm', 'e', 'e', 't', 'a', 'g', 0x10, 0xdd, 0x11, 0x04,
459             's', 'o', 'm', 'e', 'r', 'a', 't', 'h', 'e', 'r', 'l', 'o', 'n', 'g', 'u', 'r', 'i',
460             0xff, 'd', 'a', 't', 'a' };
461     int result;
462
463     coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */
464
465     pdu->hdr->type = COAP_MESSAGE_ACK;
466     pdu->hdr->code = COAP_RESPONSE_CODE(204);
467     pdu->hdr->id = htons(0x1234);
468
469     CU_ASSERT(pdu->length == 4);
470
471     result = coap_add_option(pdu, COAP_OPTION_ETAG, 8, (unsigned char *) "someetag");
472
473     CU_ASSERT(result == 9);
474     CU_ASSERT(pdu->max_delta == 4);
475     CU_ASSERT(pdu->length == 13);
476     CU_ASSERT_PTR_NULL(pdu->data);
477
478     result = coap_add_option(pdu, COAP_OPTION_IF_NONE_MATCH, 0, NULL);
479
480     CU_ASSERT(result == 1);
481     CU_ASSERT(pdu->max_delta == 5);
482     CU_ASSERT(pdu->length == 14);
483     CU_ASSERT_PTR_NULL(pdu->data);
484
485     result = coap_add_option(pdu, COAP_OPTION_PROXY_URI, 17, (unsigned char *) "someratherlonguri");
486
487     CU_ASSERT(result == 20);
488     CU_ASSERT(pdu->max_delta == 35);
489     CU_ASSERT(pdu->length == 34);
490     CU_ASSERT_PTR_NULL(pdu->data);
491
492     result = coap_add_data(pdu, 4, (unsigned char *) "data");
493
494     CU_ASSERT(result > 0);
495     CU_ASSERT(pdu->length == 39);
496     CU_ASSERT(pdu->data == (unsigned char *) pdu->hdr + 35);
497
498     CU_ASSERT(pdu->length == sizeof(teststr));
499     CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0);
500 }
501
502 void t_encode_pdu10(void)
503 {
504     /* PDU with token, options and data */
505     char teststr[] =
506     { 0x62, 0x44, 0x12, 0x34, 0x00, 0x00, 0x8d, 0xf2, 'c', 'o', 'a', 'p', ':', '/', '/', 'e', 'x',
507             'a', 'm', 'p', 'l', 'e', '.', 'c', 'o', 'm', '/', '1', '2', '3', '4', '5', '/', '%',
508             '3', 'F', 'x', 'y', 'z', '/', '3', '0', '4', '8', '2', '3', '4', '2', '3', '4', '/',
509             '2', '3', '4', '0', '2', '3', '4', '8', '2', '3', '4', '/', '2', '3', '9', '0', '8',
510             '4', '2', '3', '4', '-', '2', '3', '/', '%', 'A', 'B', '%', '3', '0', '%', 'a', 'f',
511             '/', '+', '1', '2', '3', '/', 'h', 'f', 'k', 's', 'd', 'h', '/', '2', '3', '4', '8',
512             '0', '-', '2', '3', '4', '-', '9', '8', '2', '3', '5', '/', '1', '2', '0', '4', '/',
513             '2', '4', '3', '5', '4', '6', '3', '4', '5', '3', '4', '5', '2', '4', '3', '/', '0',
514             '1', '9', '8', 's', 'd', 'n', '3', '-', 'a', '-', '3', '/', '/', '/', 'a', 'f', 'f',
515             '0', '9', '3', '4', '/', '9', '7', 'u', '2', '1', '4', '1', '/', '0', '0', '0', '2',
516             '/', '3', '9', '3', '2', '4', '2', '3', '5', '3', '2', '/', '5', '6', '2', '3', '4',
517             '0', '2', '3', '/', '-', '-', '-', '-', '/', '=', '1', '2', '3', '4', '=', '/', '0',
518             '9', '8', '1', '4', '1', '-', '9', '5', '6', '4', '6', '4', '3', '/', '2', '1', '9',
519             '7', '0', '-', '-', '-', '-', '-', '/', '8', '2', '3', '6', '4', '9', '2', '3', '4',
520             '7', '2', 'w', 'e', 'r', 'e', 'r', 'e', 'w', 'r', '0', '-', '9', '2', '1', '-', '3',
521             '9', '1', '2', '3', '-', '3', '4', '/', 0x0d, 0x01, '/', '/', '4', '9', '2', '4', '0',
522             '3', '-', '-', '0', '9', '8', '/', 0xc1, '*', 0xff, 'd', 'a', 't', 'a' };
523     int result;
524
525     coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */
526
527     pdu->hdr->type = COAP_MESSAGE_ACK;
528     pdu->hdr->code = COAP_RESPONSE_CODE(204);
529     pdu->hdr->id = htons(0x1234);
530
531     CU_ASSERT(pdu->length == 4);
532
533     result = coap_add_token(pdu, 2, (unsigned char *) "\0\0");
534
535     CU_ASSERT(result > 0);
536     result =
537             coap_add_option(pdu, COAP_OPTION_LOCATION_PATH, 255,
538                     (unsigned char *) "coap://example.com/12345/%3Fxyz/3048234234/23402348234/239084234-23/%AB%30%af/+123/hfksdh/23480-234-98235/1204/243546345345243/0198sdn3-a-3///aff0934/97u2141/0002/3932423532/56234023/----/=1234=/098141-9564643/21970-----/82364923472wererewr0-921-39123-34/");
539
540     CU_ASSERT(result == 257);
541     CU_ASSERT(pdu->max_delta == 8);
542     CU_ASSERT(pdu->length == 263);
543     CU_ASSERT_PTR_NULL(pdu->data);
544
545     result = coap_add_option(pdu, COAP_OPTION_LOCATION_PATH, 14,
546             (unsigned char *) "//492403--098/");
547
548     CU_ASSERT(result == 16);
549     CU_ASSERT(pdu->max_delta == 8);
550     CU_ASSERT(pdu->length == 279);
551     CU_ASSERT_PTR_NULL(pdu->data);
552
553     result = coap_add_option(pdu, COAP_OPTION_LOCATION_QUERY, 1, (unsigned char *) "*");
554
555     CU_ASSERT(result == 2);
556     CU_ASSERT(pdu->max_delta == 20);
557     CU_ASSERT(pdu->length == 281);
558     CU_ASSERT_PTR_NULL(pdu->data);
559
560     result = coap_add_data(pdu, 4, (unsigned char *) "data");
561
562     CU_ASSERT(result > 0);
563     CU_ASSERT(pdu->length == 286);
564     CU_ASSERT(pdu->data == (unsigned char *) pdu->hdr + 282);
565
566     CU_ASSERT(pdu->length == sizeof(teststr));
567     CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0);
568 }
569
570 void t_encode_pdu11(void)
571 {
572     /* data too long for PDU */
573     size_t old_max = pdu->max_size;
574     int result;
575
576     coap_pdu_clear(pdu, 8); /* clear PDU, with small maximum */
577
578     CU_ASSERT(pdu->data == NULL);
579     result = coap_add_data(pdu, 10, (unsigned char *) "0123456789");
580
581     CU_ASSERT(result == 0);
582     CU_ASSERT(pdu->data == NULL);
583
584     pdu->max_size = old_max;
585 }
586
587 int t_pdu_tests_create(void)
588 {
589     pdu = coap_pdu_init(0, 0, 0, COAP_MAX_PDU_SIZE);
590
591     return pdu == NULL;
592 }
593
594 int t_pdu_tests_remove(void)
595 {
596     coap_delete_pdu(pdu);
597     return 0;
598 }
599
600 CU_pSuite t_init_pdu_tests(void)
601 {
602     CU_pSuite suite[2];
603
604     suite[0] = CU_add_suite("pdu parser", t_pdu_tests_create, t_pdu_tests_remove);
605     if (!suite[0])
606     { /* signal error */
607         fprintf(stderr, "W: cannot add pdu parser test suite (%s)\n", CU_get_error_msg());
608
609         return NULL;
610     }
611
612 #define PDU_TEST(s,t)                             \
613   if (!CU_ADD_TEST(s,t)) {                        \
614     fprintf(stderr, "W: cannot add pdu parser test (%s)\n",       \
615         CU_get_error_msg());                      \
616   }
617
618     PDU_TEST(suite[0], t_parse_pdu1);
619     PDU_TEST(suite[0], t_parse_pdu2);
620     PDU_TEST(suite[0], t_parse_pdu3);
621     PDU_TEST(suite[0], t_parse_pdu4);
622     PDU_TEST(suite[0], t_parse_pdu5);
623     PDU_TEST(suite[0], t_parse_pdu6);
624     PDU_TEST(suite[0], t_parse_pdu7);
625     PDU_TEST(suite[0], t_parse_pdu8);
626     PDU_TEST(suite[0], t_parse_pdu9);
627     PDU_TEST(suite[0], t_parse_pdu10);
628     PDU_TEST(suite[0], t_parse_pdu11);
629     PDU_TEST(suite[0], t_parse_pdu12);
630     PDU_TEST(suite[0], t_parse_pdu13);
631     PDU_TEST(suite[0], t_parse_pdu14);
632
633     suite[1] = CU_add_suite("pdu encoder", t_pdu_tests_create, t_pdu_tests_remove);
634     if (suite[1])
635     {
636 #define PDU_ENCODER_TEST(s,t)                             \
637   if (!CU_ADD_TEST(s,t)) {                        \
638     fprintf(stderr, "W: cannot add pdu encoder test (%s)\n",          \
639         CU_get_error_msg());                      \
640   }
641         PDU_ENCODER_TEST(suite[1], t_encode_pdu1);
642         PDU_ENCODER_TEST(suite[1], t_encode_pdu2);
643         PDU_ENCODER_TEST(suite[1], t_encode_pdu3);
644         PDU_ENCODER_TEST(suite[1], t_encode_pdu4);
645         PDU_ENCODER_TEST(suite[1], t_encode_pdu5);
646         PDU_ENCODER_TEST(suite[1], t_encode_pdu6);
647         PDU_ENCODER_TEST(suite[1], t_encode_pdu7);
648         PDU_ENCODER_TEST(suite[1], t_encode_pdu8);
649         PDU_ENCODER_TEST(suite[1], t_encode_pdu9);
650         PDU_ENCODER_TEST(suite[1], t_encode_pdu10);
651         PDU_ENCODER_TEST(suite[1], t_encode_pdu11);
652
653     }
654     else
655         /* signal error */
656         fprintf(stderr, "W: cannot add pdu parser test suite (%s)\n", CU_get_error_msg());
657
658     return suite[0];
659 }
660