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