示例#1
0
static void
test_coap_parse_illegal_token_length(void)
{
    uint8_t pdu[] = { 0x59, 0x69, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', '1', '2', '3', '4' };
    struct sol_coap_packet *pkt;
    struct sol_buffer *buf;
    size_t offset;

    pkt = sol_coap_packet_new(NULL);
    ASSERT(pkt);

    ASSERT(!sol_coap_packet_get_payload(pkt, &buf, &offset));
    ASSERT(!sol_buffer_remove_data(buf, 0, offset));
    ASSERT(!sol_buffer_insert_bytes(buf, 0, pdu, sizeof(pdu)));

    ASSERT(coap_packet_parse(pkt));

    pdu[0] = 0x5f;
    ASSERT(!sol_buffer_remove_data(buf, 0, offset));
    ASSERT(!sol_buffer_insert_bytes(buf, 0, pdu, sizeof(pdu)));

    ASSERT(coap_packet_parse(pkt));

    sol_coap_packet_unref(pkt);
}
示例#2
0
static void
test_coap_parse_empty_pdu(void)
{
    uint8_t pdu[] = { 0x40, 0x01, 0, 0 };
    struct sol_coap_packet *pkt;
    struct sol_buffer *buf;
    size_t offset;
    uint8_t ver, type, code;
    uint16_t id;

    pkt = sol_coap_packet_new(NULL);
    ASSERT(pkt);

    ASSERT(!sol_coap_packet_get_payload(pkt, &buf, &offset));
    ASSERT(!sol_buffer_remove_data(buf, 0, offset));
    ASSERT(!sol_buffer_insert_bytes(buf, 0, pdu, sizeof(pdu)));

    ASSERT(!coap_packet_parse(pkt));

    sol_coap_header_get_version(pkt, &ver);
    sol_coap_header_get_type(pkt, &type);
    sol_coap_header_get_code(pkt, &code);
    sol_coap_header_get_id(pkt, &id);

    ASSERT_INT_EQ(ver, 1);
    ASSERT_INT_EQ(type, SOL_COAP_MESSAGE_TYPE_CON);
    ASSERT_INT_EQ(code, SOL_COAP_METHOD_GET);
    ASSERT_INT_EQ(id, 0);

    sol_coap_packet_unref(pkt);
}
示例#3
0
static void
test_coap_find_options(void)
{
    uint8_t pdu[] = { 0x55, 0xA5, 0x12, 0x34, 't', 'o', 'k', 'e', 'n',
                      0x00, 0xC1, 0x00, 0xff, 'p', 'a', 'y', 'l', 'o', 'a', 'd', 0x00 };
    struct sol_str_slice options[16];
    struct sol_coap_packet *pkt;
    struct sol_buffer *buf;
    int count = 16;
    size_t offset;

    pkt = sol_coap_packet_new(NULL);
    ASSERT(pkt);

    ASSERT(!sol_coap_packet_get_payload(pkt, &buf, &offset));
    ASSERT(!sol_buffer_remove_data(buf, 0, offset));
    ASSERT(!sol_buffer_insert_bytes(buf, 0, pdu, sizeof(pdu)));

    ASSERT(!coap_packet_parse(pkt));

    count = sol_coap_find_options(pkt, SOL_COAP_OPTION_CONTENT_FORMAT, options, count);
    ASSERT_INT_EQ(count, 1);
    ASSERT_INT_EQ(options[0].len, 1);
    ASSERT_INT_EQ((uint8_t)options[0].data[0], 0);

    count = sol_coap_find_options(pkt, SOL_COAP_OPTION_IF_MATCH, options, count);
    ASSERT_INT_EQ(count, 0);

    sol_coap_packet_unref(pkt);
}
示例#4
0
static void
test_coap_parse_simple_pdu(void)
{
    uint8_t pdu[] = { 0x55, 0xA5, 0x12, 0x34, 't', 'o', 'k', 'e',
                      'n',  0x00, 0xc1, 0x00, 0xff, 'p', 'a', 'y',
                      'l', 'o', 'a', 'd', 0x00 };
    struct sol_str_slice options[16];
    struct sol_coap_packet *pkt;
    struct sol_buffer *buf;
    uint8_t *token;
    int count = 16;
    size_t offset;
    uint8_t tkl, code, ver, type;
    uint16_t id;

    pkt = sol_coap_packet_new(NULL);
    ASSERT(pkt);

    ASSERT(!sol_coap_packet_get_payload(pkt, &buf, &offset));
    ASSERT(!sol_buffer_remove_data(buf, 0, offset));
    ASSERT(!sol_buffer_insert_bytes(buf, 0, pdu, sizeof(pdu)));

    ASSERT(!coap_packet_parse(pkt));

    sol_coap_header_get_version(pkt, &ver);
    sol_coap_header_get_type(pkt, &type);

    ASSERT_INT_EQ(ver, 1);
    ASSERT_INT_EQ(type, SOL_COAP_MESSAGE_TYPE_NON_CON);

    token = sol_coap_header_get_token(pkt, &tkl);
    ASSERT(token);
    ASSERT_INT_EQ(tkl, 5);
    ASSERT(!strcmp((char *)token, "token"));

    sol_coap_header_get_code(pkt, &code);
    sol_coap_header_get_id(pkt, &id);
    ASSERT_INT_EQ(code, SOL_COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED);
    ASSERT_INT_EQ(id, 0x1234);

    count = sol_coap_find_options(pkt, SOL_COAP_OPTION_CONTENT_FORMAT, options, count);
    ASSERT_INT_EQ(count, 1);
    ASSERT_INT_EQ(options[0].len, 1);
    ASSERT_INT_EQ((uint8_t)options[0].data[0], 0);

    /* Not existent. */
    count = sol_coap_find_options(pkt, SOL_COAP_OPTION_ETAG, options, count);
    ASSERT_INT_EQ(count, 0);

    ASSERT(!sol_coap_packet_get_payload(pkt, &buf, &offset));
    ASSERT_INT_EQ(offset + sizeof("payload"), buf->used);
    ASSERT(!strcmp((char *)buf->data + offset, "payload"));

    sol_coap_packet_unref(pkt);
}
示例#5
0
static void
test_coap_parse_options_that_exceed_pdu(void)
{
    uint8_t pdu[] = { 0x55, 0x73, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', 0x00, 0xc1, 0x00, 0xae, 0xf0, 0x03 };
    struct sol_coap_packet *pkt;
    struct sol_buffer *buf;
    size_t offset;

    pkt = sol_coap_packet_new(NULL);
    ASSERT(pkt);

    ASSERT(!sol_coap_packet_get_payload(pkt, &buf, &offset));
    ASSERT(!sol_buffer_remove_data(buf, 0, offset));
    ASSERT(!sol_buffer_insert_bytes(buf, 0, pdu, sizeof(pdu)));

    ASSERT(coap_packet_parse(pkt));
    sol_coap_packet_unref(pkt);
}
示例#6
0
static void
test_coap_parse_without_options_with_payload(void)
{
    uint8_t pdu[] = { 0x50, 0x73, 0x12, 0x34, 0xff, 'p', 'a', 'y', 'l', 'o', 'a', 'd' };
    struct sol_coap_packet *pkt;
    struct sol_buffer *buf;
    size_t offset;

    pkt = sol_coap_packet_new(NULL);
    ASSERT(pkt);

    ASSERT(!sol_coap_packet_get_payload(pkt, &buf, &offset));
    ASSERT(!sol_buffer_remove_data(buf, 0, offset));
    ASSERT(!sol_buffer_insert_bytes(buf, 0, pdu, sizeof(pdu)));

    ASSERT(!coap_packet_parse(pkt));

    sol_coap_packet_unref(pkt);
}
示例#7
0
static void
test_coap_payload_simple(void)
{
    uint8_t pdu[] = { 0x50, 0x73, 0x12, 0x34, 0xff, 'p', 'a', 'y', 'l', 'o', 'a', 'd', 0x00 };
    struct sol_coap_packet *pkt;
    struct sol_buffer *buf;
    size_t offset;

    pkt = sol_coap_packet_new(NULL);
    ASSERT(pkt);

    ASSERT(!sol_coap_packet_get_payload(pkt, &buf, &offset));
    ASSERT(!sol_buffer_remove_data(buf, 0, offset));
    ASSERT(!sol_buffer_insert_bytes(buf, 0, pdu, sizeof(pdu)));

    ASSERT_INT_EQ(buf->used - offset, sizeof("payload"));
    ASSERT(streq((char *)buf->data + offset, "payload"));

    ASSERT(!coap_packet_parse(pkt));

    sol_coap_packet_unref(pkt);
}
示例#8
0
        return 0;
    }

    if (num < 269) {
        *value = 13;
        r = sol_buffer_insert_char(buf, offset, num - 13);
        SOL_INT_CHECK(r, < 0, r);

        return 1;
    }

    *value = 14;

    v = sol_util_cpu_to_be16(num - 269);

    r = sol_buffer_insert_bytes(buf, offset, (uint8_t *)&v, sizeof(v));
    SOL_INT_CHECK(r, < 0, r);

    return 2;
}

int
coap_option_encode(struct option_context *context, uint16_t code,
    const void *value, uint16_t len)
{
    int delta, offset, r;
    uint8_t data;

    delta = code - context->delta;

    offset = 1;
示例#9
0
SOL_API int
sol_buffer_insert_slice(struct sol_buffer *buf, size_t pos, const struct sol_str_slice slice)
{
    return sol_buffer_insert_bytes(buf, pos, (uint8_t *)slice.data, slice.len);
}