コード例 #1
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);
}
コード例 #2
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);
}
コード例 #3
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);
}
コード例 #4
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);
}
コード例 #5
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);
}
コード例 #6
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);
}
コード例 #7
0
ファイル: test_pdu.c プロジェクト: prajoshpremdas/iotivity
void t_encode_pdu8(void)
{
    /* PDU with token and data */
    char teststr[] =
    { 0x42, 0x43, 0x12, 0x34, 0x00, 0x01, 0xff, 0x00 };
    int result;
    coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */

    pdu->hdr->type = COAP_MESSAGE_CON;
    pdu->hdr->code = COAP_RESPONSE_CODE(203);
    pdu->hdr->id = htons(0x1234);

    CU_ASSERT(pdu->length == 4);

    result = coap_add_token(pdu, 2, (unsigned char *) "\x00\x01");

    CU_ASSERT(result > 0);

    result = coap_add_data(pdu, 1, (unsigned char *) "\0");

    CU_ASSERT(result > 0);
    CU_ASSERT(pdu->length == 8);
    CU_ASSERT(pdu->data == (unsigned char *) pdu->hdr + 7);

    CU_ASSERT(pdu->length == sizeof(teststr));
    CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0);
}
コード例 #8
0
ファイル: test_pdu.c プロジェクト: prajoshpremdas/iotivity
void t_encode_pdu2(void)
{
    size_t old_max = pdu->max_size;
    int result;

    coap_pdu_clear(pdu, 7); /* set very small PDU size */

    pdu->hdr->type = COAP_MESSAGE_CON;
    pdu->hdr->code = COAP_REQUEST_GET;
    pdu->hdr->id = htons(0x1234);

    result = coap_add_token(pdu, 5, (unsigned char *) "token");

    CU_ASSERT(result == 0);

    coap_pdu_clear(pdu, old_max); /* restore PDU size */
}
コード例 #9
0
ファイル: pdu.c プロジェクト: thatking/liteos_3516c
coap_pdu_t *
coap_pdu_init(unsigned char type, unsigned char code, 
	      unsigned short id, size_t size) {
  coap_pdu_t *pdu;
#ifdef WITH_LWIP
    struct pbuf *p;
#endif

  assert(size <= COAP_MAX_PDU_SIZE);
  /* Size must be large enough to fit the header. */
  if (size < sizeof(coap_hdr_t) || size > COAP_MAX_PDU_SIZE)
    return NULL;

  /* size must be large enough for hdr */
#if defined(WITH_POSIX) || defined(WITH_CONTIKI)
  pdu = coap_malloc_type(COAP_PDU, sizeof(coap_pdu_t));
  if (!pdu) return NULL;
  pdu->hdr = coap_malloc_type(COAP_PDU_BUF, size);
  if (pdu->hdr == NULL) {
    coap_free_type(COAP_PDU, pdu);
    pdu = NULL;
  }
#endif /* WITH_POSIX or WITH_CONTIKI */

#ifdef WITH_LWIP
  pdu = (coap_pdu_t*)coap_malloc_type(COAP_PDU, sizeof(coap_pdu_t));

  if (!pdu) return NULL;
  
  p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM);
  
  if (p == NULL) {
    coap_free_type(COAP_PDU, pdu);
    pdu = NULL;
  }
  
#endif

  if (pdu) {
#ifdef WITH_LWIP
    pdu->pbuf = p;
#endif
    coap_pdu_clear(pdu, size);
    pdu->hdr->id = id;
    pdu->hdr->type = type;
    pdu->hdr->code = code;
  }
  
  return pdu;
}
コード例 #10
0
ファイル: test_pdu.c プロジェクト: prajoshpremdas/iotivity
void t_encode_pdu4(void)
{
    /* PDU with options */
    char teststr[] =
    { 0x60, 0x99, 0x12, 0x34, 0x3d, 0x05, 0x66, 0x61, 0x6e, 0x63, 0x79, 0x70, 0x72, 0x6f, 0x78,
            0x79, 0x2e, 0x63, 0x6f, 0x61, 0x70, 0x2e, 0x6d, 0x65, 0x84, 0x70, 0x61, 0x74, 0x68,
            0x00, 0xe8, 0x1e, 0x28, 0x66, 0x61, 0x6e, 0x63, 0x79, 0x6f, 0x70, 0x74 };
    int result;

    coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */

    pdu->hdr->type = COAP_MESSAGE_ACK;
    pdu->hdr->code = 0x99;
    pdu->hdr->id = htons(0x1234);

    CU_ASSERT(pdu->length == 4);

    result = coap_add_option(pdu, COAP_OPTION_URI_HOST, 18, (unsigned char *) "fancyproxy.coap.me");

    CU_ASSERT(result == 20);
    CU_ASSERT(pdu->max_delta == 3);
    CU_ASSERT(pdu->length == 24);
    CU_ASSERT_PTR_NULL(pdu->data);

    result = coap_add_option(pdu, COAP_OPTION_URI_PATH, 4, (unsigned char *) "path");

    CU_ASSERT(result == 5);
    CU_ASSERT(pdu->max_delta == 11);
    CU_ASSERT(pdu->length == 29);
    CU_ASSERT_PTR_NULL(pdu->data);

    result = coap_add_option(pdu, COAP_OPTION_URI_PATH, 0, NULL);

    CU_ASSERT(result == 1);
    CU_ASSERT(pdu->max_delta == 11);
    CU_ASSERT(pdu->length == 30);
    CU_ASSERT_PTR_NULL(pdu->data);

    result = coap_add_option(pdu, 8000, 8, (unsigned char *) "fancyopt");

    CU_ASSERT(result == 11);
    CU_ASSERT(pdu->max_delta == 8000);
    CU_ASSERT(pdu->length == 41);
    CU_ASSERT_PTR_NULL(pdu->data);

    CU_ASSERT(pdu->length == sizeof(teststr));
    CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0);
}
コード例 #11
0
ファイル: pdu.c プロジェクト: hisilicon/IoTGateway
coap_pdu_t *
coap_pdu_init(unsigned char type, unsigned char code,
              unsigned short id, size_t size)
{
    coap_pdu_t *pdu;
#ifdef WITH_LWIP
    struct pbuf *p;
#endif

    assert(size <= COAP_MAX_PDU_SIZE);
    /* Size must be large enough to fit the header. */
    if (size < sizeof(coap_hdr_t) || size > COAP_MAX_PDU_SIZE)
        return NULL;

    /* size must be large enough for hdr */
#ifdef WITH_POSIX
    pdu = coap_malloc(sizeof(coap_pdu_t) + size);
#endif
#ifdef WITH_CONTIKI
    pdu = (coap_pdu_t *)memb_alloc(&pdu_storage);
#endif
#ifdef WITH_LWIP
    p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM);
    if (p != NULL)
    {
        u8_t header_error = pbuf_header(p, sizeof(coap_pdu_t));
        /* we could catch that case and allocate larger memory in advance, but then
         * again, we'd run into greater trouble with incoming packages anyway */
        LWIP_ASSERT("CoAP PDU header does not fit in transport header", header_error == 0);
        pdu = p->payload;
    }
    else
    {
        pdu = NULL;
    }
#endif
    if (pdu)
    {
        coap_pdu_clear(pdu, size);
        pdu->hdr->id = id;
        pdu->hdr->type = type;
        pdu->hdr->code = code;
#ifdef WITH_LWIP
        pdu->pbuf = p;
#endif
    }
    return pdu;
}
コード例 #12
0
ファイル: test_pdu.c プロジェクト: prajoshpremdas/iotivity
void t_encode_pdu11(void)
{
    /* data too long for PDU */
    size_t old_max = pdu->max_size;
    int result;

    coap_pdu_clear(pdu, 8); /* clear PDU, with small maximum */

    CU_ASSERT(pdu->data == NULL);
    result = coap_add_data(pdu, 10, (unsigned char *) "0123456789");

    CU_ASSERT(result == 0);
    CU_ASSERT(pdu->data == NULL);

    pdu->max_size = old_max;
}
コード例 #13
0
ファイル: test_pdu.c プロジェクト: prajoshpremdas/iotivity
void t_encode_pdu9(void)
{
    /* PDU with options and data */
    char teststr[] =
    { 0x60, 0x44, 0x12, 0x34, 0x48, 's', 'o', 'm', 'e', 'e', 't', 'a', 'g', 0x10, 0xdd, 0x11, 0x04,
            's', 'o', 'm', 'e', 'r', 'a', 't', 'h', 'e', 'r', 'l', 'o', 'n', 'g', 'u', 'r', 'i',
            0xff, 'd', 'a', 't', 'a' };
    int result;

    coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */

    pdu->hdr->type = COAP_MESSAGE_ACK;
    pdu->hdr->code = COAP_RESPONSE_CODE(204);
    pdu->hdr->id = htons(0x1234);

    CU_ASSERT(pdu->length == 4);

    result = coap_add_option(pdu, COAP_OPTION_ETAG, 8, (unsigned char *) "someetag");

    CU_ASSERT(result == 9);
    CU_ASSERT(pdu->max_delta == 4);
    CU_ASSERT(pdu->length == 13);
    CU_ASSERT_PTR_NULL(pdu->data);

    result = coap_add_option(pdu, COAP_OPTION_IF_NONE_MATCH, 0, NULL);

    CU_ASSERT(result == 1);
    CU_ASSERT(pdu->max_delta == 5);
    CU_ASSERT(pdu->length == 14);
    CU_ASSERT_PTR_NULL(pdu->data);

    result = coap_add_option(pdu, COAP_OPTION_PROXY_URI, 17, (unsigned char *) "someratherlonguri");

    CU_ASSERT(result == 20);
    CU_ASSERT(pdu->max_delta == 35);
    CU_ASSERT(pdu->length == 34);
    CU_ASSERT_PTR_NULL(pdu->data);

    result = coap_add_data(pdu, 4, (unsigned char *) "data");

    CU_ASSERT(result > 0);
    CU_ASSERT(pdu->length == 39);
    CU_ASSERT(pdu->data == (unsigned char *) pdu->hdr + 35);

    CU_ASSERT(pdu->length == sizeof(teststr));
    CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0);
}
コード例 #14
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);
}
コード例 #15
0
ファイル: test_pdu.c プロジェクト: prajoshpremdas/iotivity
void t_encode_pdu1(void)
{
    char teststr[] =
    { 0x45, 0x01, 0x12, 0x34, 't', 'o', 'k', 'e', 'n' };
    int result;

    coap_pdu_clear(pdu, pdu->max_size);
    pdu->hdr->type = COAP_MESSAGE_CON;
    pdu->hdr->code = COAP_REQUEST_GET;
    pdu->hdr->id = htons(0x1234);

    result = coap_add_token(pdu, 5, (unsigned char *) "token");

    CU_ASSERT(result == 1);
    CU_ASSERT(pdu->length = sizeof(teststr));
    CU_ASSERT_PTR_NULL(pdu->data);
    CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0);
}
コード例 #16
0
ファイル: test_pdu.c プロジェクト: prajoshpremdas/iotivity
void t_encode_pdu6(void)
{
    /* PDU with data */
    char teststr[] =
    { 0x50, 0x02, 0x12, 0x34, 0xff, '1', '2', '3', '4', '5', '6', '7', '8' };
    coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */

    pdu->hdr->type = COAP_MESSAGE_NON;
    pdu->hdr->code = COAP_REQUEST_POST;
    pdu->hdr->id = htons(0x1234);

    CU_ASSERT(pdu->length == 4);
    CU_ASSERT_PTR_NULL(pdu->data);

    coap_add_data(pdu, 8, (unsigned char *) "12345678");

    CU_ASSERT(pdu->length == sizeof(teststr));
    CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0);
}
コード例 #17
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);
}
コード例 #18
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);
}
コード例 #19
0
ファイル: test_pdu.c プロジェクト: prajoshpremdas/iotivity
void t_encode_pdu7(void)
{
    /* PDU with empty data */
    char teststr[] =
    { 0x40, 0x43, 0x12, 0x34 };
    int result;
    coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */

    pdu->hdr->type = COAP_MESSAGE_CON;
    pdu->hdr->code = COAP_RESPONSE_CODE(203);
    pdu->hdr->id = htons(0x1234);

    CU_ASSERT(pdu->length == 4);

    result = coap_add_data(pdu, 0, NULL);

    CU_ASSERT(result > 0);
    CU_ASSERT(pdu->length == 4);
    CU_ASSERT_PTR_NULL(pdu->data);

    CU_ASSERT(pdu->length == sizeof(teststr));
    CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0);
}
コード例 #20
0
ファイル: test_pdu.c プロジェクト: prajoshpremdas/iotivity
void t_encode_pdu5(void)
{
    /* PDU with token and options */
    char teststr[] =
    { 0x68, 0x84, 0x12, 0x34, '1', '2', '3', '4', '5', '6', '7', '8', 0x18, 0x41, 0x42, 0x43, 0x44,
            0x45, 0x46, 0x47, 0x48, 0xd1, 0x03, 0x12 };
    int result;

    coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */

    pdu->hdr->type = COAP_MESSAGE_ACK;
    pdu->hdr->code = COAP_RESPONSE_CODE(404);
    pdu->hdr->id = htons(0x1234);

    CU_ASSERT(pdu->length == 4);

    result = coap_add_token(pdu, 8, (unsigned char *) "12345678");

    CU_ASSERT(pdu->length == 12);

    result = coap_add_option(pdu, COAP_OPTION_IF_MATCH, 8, (unsigned char *) "ABCDEFGH");

    CU_ASSERT(result == 9);
    CU_ASSERT(pdu->max_delta == 1);
    CU_ASSERT(pdu->length == 21);
    CU_ASSERT_PTR_NULL(pdu->data);

    result = coap_add_option(pdu, COAP_OPTION_ACCEPT, 1, (unsigned char *) "\x12");

    CU_ASSERT(result == 3);
    CU_ASSERT(pdu->max_delta == 17);
    CU_ASSERT(pdu->length == 24);
    CU_ASSERT_PTR_NULL(pdu->data);

    CU_ASSERT(pdu->length == sizeof(teststr));
    CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0);
}
コード例 #21
0
ファイル: test_pdu.c プロジェクト: prajoshpremdas/iotivity
void t_encode_pdu10(void)
{
    /* PDU with token, options and data */
    char teststr[] =
    { 0x62, 0x44, 0x12, 0x34, 0x00, 0x00, 0x8d, 0xf2, 'c', 'o', 'a', 'p', ':', '/', '/', 'e', 'x',
            'a', 'm', 'p', 'l', 'e', '.', 'c', 'o', 'm', '/', '1', '2', '3', '4', '5', '/', '%',
            '3', 'F', 'x', 'y', 'z', '/', '3', '0', '4', '8', '2', '3', '4', '2', '3', '4', '/',
            '2', '3', '4', '0', '2', '3', '4', '8', '2', '3', '4', '/', '2', '3', '9', '0', '8',
            '4', '2', '3', '4', '-', '2', '3', '/', '%', 'A', 'B', '%', '3', '0', '%', 'a', 'f',
            '/', '+', '1', '2', '3', '/', 'h', 'f', 'k', 's', 'd', 'h', '/', '2', '3', '4', '8',
            '0', '-', '2', '3', '4', '-', '9', '8', '2', '3', '5', '/', '1', '2', '0', '4', '/',
            '2', '4', '3', '5', '4', '6', '3', '4', '5', '3', '4', '5', '2', '4', '3', '/', '0',
            '1', '9', '8', 's', 'd', 'n', '3', '-', 'a', '-', '3', '/', '/', '/', 'a', 'f', 'f',
            '0', '9', '3', '4', '/', '9', '7', 'u', '2', '1', '4', '1', '/', '0', '0', '0', '2',
            '/', '3', '9', '3', '2', '4', '2', '3', '5', '3', '2', '/', '5', '6', '2', '3', '4',
            '0', '2', '3', '/', '-', '-', '-', '-', '/', '=', '1', '2', '3', '4', '=', '/', '0',
            '9', '8', '1', '4', '1', '-', '9', '5', '6', '4', '6', '4', '3', '/', '2', '1', '9',
            '7', '0', '-', '-', '-', '-', '-', '/', '8', '2', '3', '6', '4', '9', '2', '3', '4',
            '7', '2', 'w', 'e', 'r', 'e', 'r', 'e', 'w', 'r', '0', '-', '9', '2', '1', '-', '3',
            '9', '1', '2', '3', '-', '3', '4', '/', 0x0d, 0x01, '/', '/', '4', '9', '2', '4', '0',
            '3', '-', '-', '0', '9', '8', '/', 0xc1, '*', 0xff, 'd', 'a', 't', 'a' };
    int result;

    coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */

    pdu->hdr->type = COAP_MESSAGE_ACK;
    pdu->hdr->code = COAP_RESPONSE_CODE(204);
    pdu->hdr->id = htons(0x1234);

    CU_ASSERT(pdu->length == 4);

    result = coap_add_token(pdu, 2, (unsigned char *) "\0\0");

    CU_ASSERT(result > 0);
    result =
            coap_add_option(pdu, COAP_OPTION_LOCATION_PATH, 255,
                    (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/");

    CU_ASSERT(result == 257);
    CU_ASSERT(pdu->max_delta == 8);
    CU_ASSERT(pdu->length == 263);
    CU_ASSERT_PTR_NULL(pdu->data);

    result = coap_add_option(pdu, COAP_OPTION_LOCATION_PATH, 14,
            (unsigned char *) "//492403--098/");

    CU_ASSERT(result == 16);
    CU_ASSERT(pdu->max_delta == 8);
    CU_ASSERT(pdu->length == 279);
    CU_ASSERT_PTR_NULL(pdu->data);

    result = coap_add_option(pdu, COAP_OPTION_LOCATION_QUERY, 1, (unsigned char *) "*");

    CU_ASSERT(result == 2);
    CU_ASSERT(pdu->max_delta == 20);
    CU_ASSERT(pdu->length == 281);
    CU_ASSERT_PTR_NULL(pdu->data);

    result = coap_add_data(pdu, 4, (unsigned char *) "data");

    CU_ASSERT(result > 0);
    CU_ASSERT(pdu->length == 286);
    CU_ASSERT(pdu->data == (unsigned char *) pdu->hdr + 282);

    CU_ASSERT(pdu->length == sizeof(teststr));
    CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0);
}
コード例 #22
0
ファイル: pdu.c プロジェクト: Dosercyan/iotivity
coap_pdu_t *
coap_pdu_init(unsigned char type, unsigned char code, unsigned short id,
              size_t size, coap_transport_type transport)
{
    coap_pdu_t *pdu;
#ifdef WITH_LWIP
    struct pbuf *p;
#endif

    unsigned int length = 0;
    switch(transport)
    {
        case coap_udp:
            length = sizeof(pdu->hdr->coap_hdr_udp_t);
            break;
#ifdef WITH_TCP
        case coap_tcp:
            length = COAP_TCP_HEADER_NO_FIELD;
            break;
        case coap_tcp_8bit:
            length = COAP_TCP_HEADER_8_BIT;
            break;
        case coap_tcp_16bit:
            length = COAP_TCP_HEADER_16_BIT;
            break;
        case coap_tcp_32bit:
            length = COAP_TCP_HEADER_32_BIT;
            break;
#endif
        default:
            debug("it has wrong type\n");
    }

#ifndef WITH_TCP
    assert(size <= COAP_MAX_PDU_SIZE);
    /* Size must be large enough to fit the header. */
    if (size < length || size > COAP_MAX_PDU_SIZE)
        return NULL;
#endif

    /* size must be large enough for hdr */
#if defined(WITH_POSIX) || defined(WITH_ARDUINO)
    pdu = (coap_pdu_t *) coap_malloc(sizeof(coap_pdu_t) + size);
#endif
#ifdef WITH_CONTIKI
    pdu = (coap_pdu_t *)memb_alloc(&pdu_storage);
#endif
#ifdef WITH_LWIP
    p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM);
    if (p != NULL)
    {
        u8_t header_error = pbuf_header(p, sizeof(coap_pdu_t));
        /* we could catch that case and allocate larger memory in advance, but then
         * again, we'd run into greater trouble with incoming packages anyway */
        LWIP_ASSERT("CoAP PDU header does not fit in transport header", header_error == 0);
        pdu = p->payload;
    }
    else
    {
        pdu = NULL;
    }
#endif
    if (pdu)
    {
        coap_pdu_clear(pdu, size, transport, length);

        switch(transport)
        {
            case coap_udp:
                pdu->hdr->coap_hdr_udp_t.id = id;
                pdu->hdr->coap_hdr_udp_t.type = type;
                pdu->hdr->coap_hdr_udp_t.code = code;
                break;
#ifdef WITH_TCP
            case coap_tcp:
                pdu->hdr->coap_hdr_tcp_t.header_data[0] = 0;
                pdu->hdr->coap_hdr_tcp_t.header_data[1] = code;
                break;
            case coap_tcp_8bit:
                pdu->hdr->coap_hdr_tcp_8bit_t.header_data[0] = COAP_TCP_LENGTH_FIELD_NUM_8_BIT << 4;
                pdu->hdr->coap_hdr_tcp_8bit_t.header_data[2] = code;
                break;
            case coap_tcp_16bit:
                pdu->hdr->coap_hdr_tcp_16bit_t.header_data[0] = COAP_TCP_LENGTH_FIELD_NUM_16_BIT << 4;
                pdu->hdr->coap_hdr_tcp_16bit_t.header_data[3] = code;
                break;
            case coap_tcp_32bit:
                pdu->hdr->coap_hdr_tcp_32bit_t.header_data[0] = COAP_TCP_LENGTH_FIELD_NUM_32_BIT << 4;
                pdu->hdr->coap_hdr_tcp_32bit_t.header_data[5] = code;
                break;
#endif
            default:
                debug("it has wrong type\n");
        }

#ifdef WITH_LWIP
        pdu->pbuf = p;
#endif
    }
    return pdu;
}