Example #1
0
void
t_error_response3(void) {
  const unsigned char code = COAP_RESPONSE_CODE(402);
  unsigned char 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->hdr->type = COAP_MESSAGE_CON;
  coap_add_token(pdu, 5, (unsigned char *)"token");
  /* coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (unsigned char *)"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->length == sizeof(teststr));
  CU_ASSERT(response->hdr->version == 1);
  CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK);
  CU_ASSERT(response->hdr->token_length == 5);
  CU_ASSERT(response->hdr->code == code);
  
  CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
}
Example #2
0
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);
}
Example #3
0
void
t_error_response2(void) {
  unsigned char 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->hdr->type = COAP_MESSAGE_NON;
  pdu->hdr->id = htons(0x1234);
  coap_add_token(pdu, 5, (unsigned char *)"token");
  coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (unsigned char *)"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->length == sizeof(teststr));
  CU_ASSERT(response->hdr->version == 1);
  CU_ASSERT(response->hdr->type == COAP_MESSAGE_NON);
  CU_ASSERT(response->hdr->token_length == 5);
  CU_ASSERT(response->hdr->code == 0x84);
  
  CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
}
Example #4
0
void
t_error_response1(void) {
  unsigned char 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->hdr->type = COAP_MESSAGE_CON;
  pdu->hdr->id = htons(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->length == sizeof(teststr));
  CU_ASSERT(response->hdr->version == 1);
  CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK);
  CU_ASSERT(response->hdr->token_length == 0);
  CU_ASSERT(response->hdr->code == 0x80);
  CU_ASSERT(pdu->hdr->id == htons(0x1234));
  
  CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
}
Example #5
0
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);
}
Example #6
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);
}
Example #7
0
void hnd_get_async(coap_context_t  *ctx, struct coap_resource_t *resource, 
              coap_address_t *peer, coap_pdu_t *request, str *token,
              coap_pdu_t *response) {
    coap_opt_iterator_t opt_iter;
    coap_opt_t *option;
    unsigned long delay = 5;
    size_t size;

    if (async) {
        if (async->id != request->hdr->id) {
            coap_opt_filter_t f;
            coap_option_filter_clear(f);
            response->hdr->code = COAP_RESPONSE_CODE(503);
        }
        return;
    }

    option = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter);
    if (option) {
        unsigned char *p = COAP_OPT_VALUE(option);

        delay = 0;
        for (size = COAP_OPT_LENGTH(option); size; --size, ++p)
            delay = delay * 10 + (*p - '0');
    }

    async = coap_register_async(ctx, peer, request, 
                                COAP_ASYNC_SEPARATE | COAP_ASYNC_CONFIRM,
                                (void *)(COAP_TICKS_PER_SECOND * delay));
}
Example #8
0
/* handler for TD_COAP_CORE_16 */
void 
hnd_get_separate(coap_context_t  *ctx, struct coap_resource_t *resource, 
		 coap_address_t *peer, coap_pdu_t *request, str *token,
		 coap_pdu_t *response) {
  coap_opt_iterator_t opt_iter;
  coap_opt_t *option;
  coap_opt_filter_t f;
  unsigned long delay = 5;

  (void)resource;
  (void)token;

  if (async) {
    if (async->id != request->hdr->id) {
      coap_opt_filter_t f;
      coap_option_filter_clear(f);
      response->hdr->code = COAP_RESPONSE_CODE(503);
    }
    return;
  }

  /* search for option delay in query list */
  coap_option_filter_clear(f);
  coap_option_setb(f, COAP_OPTION_URI_QUERY);
  
  coap_option_iterator_init(request, &opt_iter, f);
  
  while ((option = coap_option_next(&opt_iter))) {
    if (strncmp("delay=", (char *)COAP_OPT_VALUE(option), 6) == 0) {
      int i;
      unsigned long d = 0;
      
      for (i = 6; i < COAP_OPT_LENGTH(option); ++i)
	d = d * 10 + COAP_OPT_VALUE(option)[i] - '0';

      /* don't allow delay to be less than COAP_RESOURCE_CHECK_TIME*/
      delay = d < COAP_RESOURCE_CHECK_TIME_SEC 
	? COAP_RESOURCE_CHECK_TIME_SEC
	: d;
      debug("set delay to %lu\n", delay);
      break;
    }
  }

  async = coap_register_async(ctx, peer, request, COAP_ASYNC_SEPARATE,
			      (void *)(COAP_TICKS_PER_SECOND * delay));
}
Example #9
0
void
t_error_response8(void) {
  const unsigned char code = COAP_RESPONSE_CODE(503);
  unsigned char 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;

  if (COAP_MAX_OPT < 2000) {
    CU_PASS("WARNING: bypassed due to COAP_MAX_OPT < 2000");
    return;
  }
  
  coap_pdu_clear(pdu, pdu->max_size);
  pdu->hdr->type = COAP_MESSAGE_CON;
  coap_add_token(pdu, 5, (unsigned char *)"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->length == sizeof(teststr));
  CU_ASSERT(response->hdr->version == 1);
  CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK);
  CU_ASSERT(response->hdr->token_length == 5);
  CU_ASSERT(response->hdr->code == code);
  
  CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
}
Example #10
0
static void
t_error_response7(void) {
  const unsigned char 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->hdr->type = COAP_MESSAGE_CON;
  coap_add_token(pdu, 5, (unsigned char *)"token");
  /* known option 11 */
  coap_add_option(pdu, 11, 4, (unsigned char *)"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->length == sizeof(teststr));
  CU_ASSERT(response->hdr->version == 1);
  CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK);
  CU_ASSERT(response->hdr->token_length == 5);
  CU_ASSERT(response->hdr->code == code);

  CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
  coap_delete_pdu(response);
}
Example #11
0
void 
hnd_get_query(coap_context_t  *ctx, struct coap_resource_t *resource, 
	      coap_address_t *peer, coap_pdu_t *request, str *token,
	      coap_pdu_t *response) {
  coap_opt_iterator_t opt_iter;
  coap_opt_filter_t f;
  coap_opt_t *q;
  size_t len, L;
  unsigned char buf[70];

  (void)ctx;
  (void)resource;
  (void)peer;
  (void)token;

  response->hdr->code = COAP_RESPONSE_CODE(205);

  coap_add_option(response, COAP_OPTION_CONTENT_TYPE,
	  coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf);

  coap_option_filter_clear(f);
  coap_option_setb(f, COAP_OPTION_URI_QUERY);
  
  coap_option_iterator_init(request, &opt_iter, f);
  
  len = 0;
  while ((len < sizeof(buf)) && (q = coap_option_next(&opt_iter))) {
    L = min(sizeof(buf) - len, 11);
    memcpy(buf + len, "Uri-Query: ", L);
    len += L;

    L = min(sizeof(buf) - len, COAP_OPT_LENGTH(q));
    memcpy(buf + len, COAP_OPT_VALUE(q), L);
    len += L;
    
    if (len < sizeof(buf))
      buf[len++] = '\n';
  }
  
  coap_add_data(response, len, buf);
}
Example #12
0
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);
}