iotivity 0.9.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / lib / libcoap-4.1.1 / tests / test_error_response.c
1 /* libcoap unit tests
2  *
3  * Copyright (C) 2013 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_error_response.h"
15
16 coap_pdu_t *pdu; /* Holds the request PDU for most tests */
17 coap_opt_filter_t opts; /* option filter used for generating responses */
18
19 /************************************************************************
20  ** PDU decoder
21  ************************************************************************/
22
23 /* FIXME: handle COAP_ERROR_PHRASE_LENGTH == 0 */
24
25 void t_error_response1(void)
26 {
27     char teststr[] =
28     { 0x60, 0x80, 0x12, 0x34, 0xff, 'B', 'a', 'd', ' ', 'R', 'e', 'q', 'u', 'e', 's', 't' };
29     coap_pdu_t *response;
30
31     coap_pdu_clear(pdu, pdu->max_size);
32     pdu->hdr->type = COAP_MESSAGE_CON;
33     pdu->hdr->id = htons(0x1234);
34
35     /* result = coap_add_token(pdu, 5, (unsigned char *)"token"); */
36     coap_option_filter_clear(opts);
37     response = coap_new_error_response(pdu, COAP_RESPONSE_CODE(400), opts);
38
39     CU_ASSERT_PTR_NOT_NULL(response);
40
41     CU_ASSERT(response->length == sizeof(teststr));
42     CU_ASSERT(response->hdr->version == 1);
43     CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK);
44     CU_ASSERT(response->hdr->token_length == 0);
45     CU_ASSERT(response->hdr->code == 0x80);
46     CU_ASSERT(pdu->hdr->id == htons(0x1234));
47
48     CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
49 }
50
51 void t_error_response2(void)
52 {
53     char teststr[] =
54     { 0x55, 0x84, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', 0xff, 'N', 'o', 't', ' ', 'F', 'o', 'u', 'n',
55             'd' };
56     coap_pdu_t *response;
57
58     coap_pdu_clear(pdu, pdu->max_size);
59     pdu->hdr->type = COAP_MESSAGE_NON;
60     pdu->hdr->id = htons(0x1234);
61     coap_add_token(pdu, 5, (unsigned char *) "token");
62     coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (unsigned char *) "time");
63
64     coap_option_filter_clear(opts);
65     response = coap_new_error_response(pdu, COAP_RESPONSE_CODE(404), opts);
66
67     CU_ASSERT_PTR_NOT_NULL(response);
68
69     CU_ASSERT(response->length == sizeof(teststr));
70     CU_ASSERT(response->hdr->version == 1);
71     CU_ASSERT(response->hdr->type == COAP_MESSAGE_NON);
72     CU_ASSERT(response->hdr->token_length == 5);
73     CU_ASSERT(response->hdr->code == 0x84);
74
75     CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
76 }
77
78 void t_error_response3(void)
79 {
80     const unsigned char code = COAP_RESPONSE_CODE(402);
81     char teststr[] =
82     { 0x65, code, 0x00, 0x00, 't', 'o', 'k', 'e', 'n', 0x90, 0xff, 'B', 'a', 'd', ' ', 'O', 'p',
83             't', 'i', 'o', 'n' };
84     coap_pdu_t *response;
85
86     coap_pdu_clear(pdu, pdu->max_size);
87     pdu->hdr->type = COAP_MESSAGE_CON;
88     coap_add_token(pdu, 5, (unsigned char *) "token");
89     /* coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (unsigned char *)"time"); */
90
91     /* unknown critical option 9 */
92     coap_add_option(pdu, 9, 0, NULL);
93
94     coap_option_filter_clear(opts);
95     coap_option_setb(opts, 9);
96     response = coap_new_error_response(pdu, code, opts);
97
98     CU_ASSERT_PTR_NOT_NULL(response);
99
100     CU_ASSERT(response->length == sizeof(teststr));
101     CU_ASSERT(response->hdr->version == 1);
102     CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK);
103     CU_ASSERT(response->hdr->token_length == 5);
104     CU_ASSERT(response->hdr->code == code);
105
106     CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
107 }
108
109 void t_error_response4(void)
110 {
111     const unsigned char code = COAP_RESPONSE_CODE(402);
112     unsigned char optval[] =
113     { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b };
114     char teststr[] =
115     { 0x65, code, 0x00, 0x00, 't', 'o', 'k', 'e', 'n', 0x9c, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
116             0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0xff, 'B', 'a', 'd', ' ', 'O', 'p', 't', 'i', 'o',
117             'n' };
118     coap_pdu_t *response;
119
120     coap_pdu_clear(pdu, pdu->max_size);
121     pdu->hdr->type = COAP_MESSAGE_CON;
122     coap_add_token(pdu, 5, (unsigned char *) "token");
123     /* coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (unsigned char *)"time"); */
124
125     /* unknown critical option 9 */
126     coap_add_option(pdu, 9, sizeof(optval), optval);
127
128     coap_option_filter_clear(opts);
129     coap_option_setb(opts, 9);
130     response = coap_new_error_response(pdu, code, opts);
131
132     CU_ASSERT_PTR_NOT_NULL(response);
133
134     CU_ASSERT(response->length == sizeof(teststr));
135     CU_ASSERT(response->hdr->version == 1);
136     CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK);
137     CU_ASSERT(response->hdr->token_length == 5);
138     CU_ASSERT(response->hdr->code == code);
139
140     CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
141 }
142
143 void t_error_response5(void)
144 {
145     const unsigned char code = COAP_RESPONSE_CODE(402);
146     unsigned char optval[] =
147     { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
148             0x0f, 0x10, 0x11, 0x12 };
149     char teststr[] =
150     { 0x65, code, 0x00, 0x00, 't', 'o', 'k', 'e', 'n', 0x9d, 0x06, 0x00, 0x01, 0x02, 0x03, 0x04,
151             0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
152             0xff, 'B', 'a', 'd', ' ', 'O', 'p', 't', 'i', 'o', 'n' };
153     coap_pdu_t *response;
154
155     coap_pdu_clear(pdu, pdu->max_size);
156     pdu->hdr->type = COAP_MESSAGE_CON;
157     coap_add_token(pdu, 5, (unsigned char *) "token");
158     /* coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (unsigned char *)"time"); */
159
160     /* unknown critical option 9 */
161     coap_add_option(pdu, 9, sizeof(optval), optval);
162
163     coap_option_filter_clear(opts);
164     coap_option_setb(opts, 9);
165     response = coap_new_error_response(pdu, code, opts);
166
167     CU_ASSERT_PTR_NOT_NULL(response);
168
169     CU_ASSERT(response->length == sizeof(teststr));
170     CU_ASSERT(response->hdr->version == 1);
171     CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK);
172     CU_ASSERT(response->hdr->token_length == 5);
173     CU_ASSERT(response->hdr->code == code);
174
175     CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
176 }
177
178 void t_error_response6(void)
179 {
180     const unsigned char code = COAP_RESPONSE_CODE(402);
181     unsigned char optval[] =
182     { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
183             0x0f, 0x10, 0x11, 0x12 };
184     char teststr[] =
185     { 0x65, code, 0x00, 0x00, 't', 'o', 'k', 'e', 'n', 0xdd, 0x0a, 0x06, 0x00, 0x01, 0x02, 0x03,
186             0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
187             0x12, 0xff, 'B', 'a', 'd', ' ', 'O', 'p', 't', 'i', 'o', 'n' };
188     coap_pdu_t *response;
189
190     coap_pdu_clear(pdu, pdu->max_size);
191     pdu->hdr->type = COAP_MESSAGE_CON;
192     coap_add_token(pdu, 5, (unsigned char *) "token");
193     /* coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (unsigned char *)"time"); */
194
195     /* unknown critical option 23 */
196     coap_add_option(pdu, 23, sizeof(optval), optval);
197
198     coap_option_filter_clear(opts);
199     coap_option_setb(opts, 23);
200     response = coap_new_error_response(pdu, code, opts);
201
202     CU_ASSERT_PTR_NOT_NULL(response);
203
204     CU_ASSERT(response->length == sizeof(teststr));
205     CU_ASSERT(response->hdr->version == 1);
206     CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK);
207     CU_ASSERT(response->hdr->token_length == 5);
208     CU_ASSERT(response->hdr->code == code);
209
210     CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
211 }
212
213 void t_error_response7(void)
214 {
215     const unsigned char code = COAP_RESPONSE_CODE(402);
216     unsigned char optval[] =
217     { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
218             0x0f, 0x10, 0x11, 0x12 };
219     char teststr[] =
220     { 0x65, code, 0x00, 0x00, 't', 'o', 'k', 'e', 'n', 0xdd, 0x0a, 0x06, 0x00, 0x01, 0x02, 0x03,
221             0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
222             0x12, 0xff, 'B', 'a', 'd', ' ', 'O', 'p', 't', 'i', 'o', 'n' };
223     coap_pdu_t *response;
224
225     coap_pdu_clear(pdu, pdu->max_size);
226     pdu->hdr->type = COAP_MESSAGE_CON;
227     coap_add_token(pdu, 5, (unsigned char *) "token");
228     /* known option 11 */
229     coap_add_option(pdu, 11, 4, (unsigned char *) "time");
230
231     /* unknown critical option 23 */
232     coap_add_option(pdu, 23, sizeof(optval), optval);
233
234     coap_option_filter_clear(opts);
235     coap_option_setb(opts, 23);
236     response = coap_new_error_response(pdu, code, opts);
237
238     CU_ASSERT_PTR_NOT_NULL(response);
239
240     CU_ASSERT(response->length == sizeof(teststr));
241     CU_ASSERT(response->hdr->version == 1);
242     CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK);
243     CU_ASSERT(response->hdr->token_length == 5);
244     CU_ASSERT(response->hdr->code == code);
245
246     CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
247 }
248
249 void t_error_response8(void)
250 {
251     const unsigned char code = COAP_RESPONSE_CODE(503);
252     char teststr[] =
253     { 0x65, code, 0x00, 0x00, 't', 'o', 'k', 'e', 'n', 0xe0, 0x02, 0xdc, 0xd0, 0x00, 0xff, 'S', 'e',
254             'r', 'v', 'i', 'c', 'e', ' ', 'U', 'n', 'a', 'v', 'a', 'i', 'l', 'a', 'b', 'l', 'e' };
255     coap_pdu_t *response;
256
257     coap_pdu_clear(pdu, pdu->max_size);
258     pdu->hdr->type = COAP_MESSAGE_CON;
259     coap_add_token(pdu, 5, (unsigned char *) "token");
260     /* known option 1000 */
261     coap_add_option(pdu, 1000, 0, NULL);
262
263     /* unknown options 1001 and 1014 */
264     coap_add_option(pdu, 1001, 0, NULL);
265     coap_add_option(pdu, 1014, 0, NULL);
266
267     /* known option 2000 */
268     coap_add_option(pdu, 2000, 0, NULL);
269
270     coap_option_filter_clear(opts);
271     coap_option_setb(opts, 1001);
272     coap_option_setb(opts, 1014);
273     response = coap_new_error_response(pdu, code, opts);
274
275     CU_ASSERT_PTR_NOT_NULL(response);
276
277     CU_ASSERT(response->length == sizeof(teststr));
278     CU_ASSERT(response->hdr->version == 1);
279     CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK);
280     CU_ASSERT(response->hdr->token_length == 5);
281     CU_ASSERT(response->hdr->code == code);
282
283     CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
284 }
285
286 int t_error_response_tests_create(void)
287 {
288     pdu = coap_pdu_init(0, 0, 0, COAP_MAX_PDU_SIZE);
289
290     return pdu == NULL;
291 }
292
293 int t_error_response_tests_remove(void)
294 {
295     coap_delete_pdu(pdu);
296     return 0;
297 }
298
299 CU_pSuite t_init_error_response_tests(void)
300 {
301     CU_pSuite suite[1];
302
303     suite[0] = CU_add_suite("error response generator", t_error_response_tests_create,
304             t_error_response_tests_remove);
305     if (!suite[0])
306     { /* signal error */
307         fprintf(stderr, "W: cannot add error response generator test suite (%s)\n",
308                 CU_get_error_msg());
309
310         return NULL;
311     }
312
313 #define ERROR_RESPONSE_TEST(s,t)                                        \
314   if (!CU_ADD_TEST(s,t)) {                                              \
315     fprintf(stderr, "W: cannot add error response generator test (%s)\n", \
316             CU_get_error_msg());                                        \
317   }
318
319     ERROR_RESPONSE_TEST(suite[0], t_error_response1);
320     ERROR_RESPONSE_TEST(suite[0], t_error_response2);
321     ERROR_RESPONSE_TEST(suite[0], t_error_response3);
322     ERROR_RESPONSE_TEST(suite[0], t_error_response4);
323     ERROR_RESPONSE_TEST(suite[0], t_error_response5);
324     ERROR_RESPONSE_TEST(suite[0], t_error_response6);
325     ERROR_RESPONSE_TEST(suite[0], t_error_response7);
326     ERROR_RESPONSE_TEST(suite[0], t_error_response8);
327
328     return suite[0];
329 }
330