3 * Copyright (C) 2013 Olaf Bergmann <bergmann@tzi.org>
5 * This file is part of the CoAP library libcoap. Please see
6 * README for terms of use.
14 #include "test_error_response.h"
16 coap_pdu_t *pdu; /* Holds the request PDU for most tests */
17 coap_opt_filter_t opts; /* option filter used for generating responses */
19 /************************************************************************
21 ************************************************************************/
23 /* FIXME: handle COAP_ERROR_PHRASE_LENGTH == 0 */
25 void t_error_response1(void)
28 { 0x60, 0x80, 0x12, 0x34, 0xff, 'B', 'a', 'd', ' ', 'R', 'e', 'q', 'u', 'e', 's', 't' };
31 coap_pdu_clear(pdu, pdu->max_size);
32 pdu->hdr->type = COAP_MESSAGE_CON;
33 pdu->hdr->id = htons(0x1234);
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);
39 CU_ASSERT_PTR_NOT_NULL(response);
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));
48 CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
51 void t_error_response2(void)
54 { 0x55, 0x84, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', 0xff, 'N', 'o', 't', ' ', 'F', 'o', 'u', 'n',
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");
64 coap_option_filter_clear(opts);
65 response = coap_new_error_response(pdu, COAP_RESPONSE_CODE(404), opts);
67 CU_ASSERT_PTR_NOT_NULL(response);
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);
75 CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
78 void t_error_response3(void)
80 const unsigned char code = COAP_RESPONSE_CODE(402);
82 { 0x65, code, 0x00, 0x00, 't', 'o', 'k', 'e', 'n', 0x90, 0xff, 'B', 'a', 'd', ' ', 'O', 'p',
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"); */
91 /* unknown critical option 9 */
92 coap_add_option(pdu, 9, 0, NULL);
94 coap_option_filter_clear(opts);
95 coap_option_setb(opts, 9);
96 response = coap_new_error_response(pdu, code, opts);
98 CU_ASSERT_PTR_NOT_NULL(response);
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);
106 CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
109 void t_error_response4(void)
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 };
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',
118 coap_pdu_t *response;
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"); */
125 /* unknown critical option 9 */
126 coap_add_option(pdu, 9, sizeof(optval), optval);
128 coap_option_filter_clear(opts);
129 coap_option_setb(opts, 9);
130 response = coap_new_error_response(pdu, code, opts);
132 CU_ASSERT_PTR_NOT_NULL(response);
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);
140 CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
143 void t_error_response5(void)
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 };
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;
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"); */
160 /* unknown critical option 9 */
161 coap_add_option(pdu, 9, sizeof(optval), optval);
163 coap_option_filter_clear(opts);
164 coap_option_setb(opts, 9);
165 response = coap_new_error_response(pdu, code, opts);
167 CU_ASSERT_PTR_NOT_NULL(response);
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);
175 CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
178 void t_error_response6(void)
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 };
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;
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"); */
195 /* unknown critical option 23 */
196 coap_add_option(pdu, 23, sizeof(optval), optval);
198 coap_option_filter_clear(opts);
199 coap_option_setb(opts, 23);
200 response = coap_new_error_response(pdu, code, opts);
202 CU_ASSERT_PTR_NOT_NULL(response);
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);
210 CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
213 void t_error_response7(void)
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 };
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;
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");
231 /* unknown critical option 23 */
232 coap_add_option(pdu, 23, sizeof(optval), optval);
234 coap_option_filter_clear(opts);
235 coap_option_setb(opts, 23);
236 response = coap_new_error_response(pdu, code, opts);
238 CU_ASSERT_PTR_NOT_NULL(response);
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);
246 CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
249 void t_error_response8(void)
251 const unsigned char code = COAP_RESPONSE_CODE(503);
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;
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);
263 /* unknown options 1001 and 1014 */
264 coap_add_option(pdu, 1001, 0, NULL);
265 coap_add_option(pdu, 1014, 0, NULL);
267 /* known option 2000 */
268 coap_add_option(pdu, 2000, 0, NULL);
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);
275 CU_ASSERT_PTR_NOT_NULL(response);
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);
283 CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
286 int t_error_response_tests_create(void)
288 pdu = coap_pdu_init(0, 0, 0, COAP_MAX_PDU_SIZE);
293 int t_error_response_tests_remove(void)
295 coap_delete_pdu(pdu);
299 CU_pSuite t_init_error_response_tests(void)
303 suite[0] = CU_add_suite("error response generator", t_error_response_tests_create,
304 t_error_response_tests_remove);
307 fprintf(stderr, "W: cannot add error response generator test suite (%s)\n",
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()); \
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);