Esempio n. 1
0
static void test_map(void)
{
    {
        /* serialization */
        TEST_ASSERT(cbor_serialize_map(&stream, 2));
        TEST_ASSERT(cbor_serialize_int(&stream, 1));
        TEST_ASSERT(cbor_serialize_byte_string(&stream, "1"));
        TEST_ASSERT(cbor_serialize_int(&stream, 2));
        TEST_ASSERT(cbor_serialize_byte_string(&stream, "2"));
        unsigned char data[] = {0xa2,
                                0x01, 0x41, 0x31, /* kv-pair 1 */
                                0x02, 0x41, 0x32, /* kv-pair 2 */
                               };
        CBOR_CHECK_SERIALIZED(stream, data, sizeof(data));

        /* deserialization */
        size_t map_length;
        size_t offset = cbor_deserialize_map(&stream, 0, &map_length);
        TEST_ASSERT_EQUAL_INT(2, map_length);
        int key;
        char value[8];
        offset += cbor_deserialize_int(&stream, offset, &key);
        TEST_ASSERT_EQUAL_INT(1, key);
        offset += cbor_deserialize_byte_string(&stream, offset, value, sizeof(value));
        TEST_ASSERT_EQUAL_STRING("1", &(value[0]));
        offset += cbor_deserialize_int(&stream, offset, &key);
        TEST_ASSERT_EQUAL_INT(2, key);
        offset += cbor_deserialize_byte_string(&stream, offset, value, sizeof(value));
        TEST_ASSERT_EQUAL_STRING("2", &(value[0]));
        TEST_ASSERT_EQUAL_INT(sizeof(data), offset);
    }
}
Esempio n. 2
0
static void test_byte_string_no_copy(void)
{
    char buffer[128];

    {
        const char *input = "";
        unsigned char data[] = {0x40};
        unsigned char *out;
        size_t size;
        TEST_ASSERT(cbor_serialize_byte_string(&stream, input));
        CBOR_CHECK_SERIALIZED(stream, data, sizeof(data));
        TEST_ASSERT(cbor_deserialize_byte_string_no_copy(&stream, 0, &out, &size));
        memcpy(buffer, out, size);
        buffer[size] = '\0';
        CBOR_CHECK_DESERIALIZED(input, buffer, EQUAL_STRING);
    }

    cbor_clear(&stream);

    {
        const char *input = "a";
        unsigned char data[] = {0x41, 0x61};
        unsigned char *out;
        size_t size;
        TEST_ASSERT(cbor_serialize_byte_string(&stream, input));
        CBOR_CHECK_SERIALIZED(stream, data, sizeof(data));
        TEST_ASSERT(cbor_deserialize_byte_string_no_copy(&stream, 0, &out, &size));
        memcpy(buffer, out, size);
        buffer[size] = '\0';
        CBOR_CHECK_DESERIALIZED(input, buffer, EQUAL_STRING);
    }
}
Esempio n. 3
0
static void test_byte_string_invalid(void)
{
    {
        /* check writing to stream that is not large enough */
        cbor_stream_t stream;
        cbor_init(&stream, 0, 0);

        TEST_ASSERT_EQUAL_INT(0, cbor_serialize_byte_string(&stream, "foo"));

        cbor_destroy(&stream);
    }
}
Esempio n. 4
0
static void test_map_indefinite(void)
{
    {
        /* serialization */
        TEST_ASSERT(cbor_serialize_map_indefinite(&stream));
        TEST_ASSERT(cbor_serialize_int(&stream, 1));
        TEST_ASSERT(cbor_serialize_byte_string(&stream, "1"));
        TEST_ASSERT(cbor_serialize_int(&stream, 2));
        TEST_ASSERT(cbor_serialize_byte_string(&stream, "2"));
        TEST_ASSERT(cbor_write_break(&stream));
        unsigned char data[] = {0xbf,
                                0x01, 0x41, 0x31, /* kv-pair 1 */
                                0x02, 0x41, 0x32, /* kv-pair 2 */
                                0xff
                               };
        CBOR_CHECK_SERIALIZED(stream, data, sizeof(data));

        /* deserialization */
        size_t offset = cbor_deserialize_map_indefinite(&stream, 0);
        int count = 0;

        while (!cbor_at_break(&stream, offset)) {
            int key;
            char value[16];
            size_t read_bytes;
            offset += read_bytes = cbor_deserialize_int(&stream, offset, &key);
            TEST_ASSERT(read_bytes);
            offset += read_bytes = cbor_deserialize_byte_string(&stream, offset,
                                   value, sizeof(value));
            TEST_ASSERT(read_bytes);
            ++count;
        }

        TEST_ASSERT_EQUAL_INT(2, count);
        TEST_ASSERT(cbor_at_end(&stream, offset));
    }
}
Esempio n. 5
0
static void test_byte_string(void)
{
    char buffer[128];

    {
        const char *input = "";
        unsigned char data[] = {0x40};
        TEST_ASSERT(cbor_serialize_byte_string(&stream, input));
        CBOR_CHECK_SERIALIZED(stream, data, sizeof(data));
        TEST_ASSERT(cbor_deserialize_byte_string(&stream, 0, buffer, sizeof(buffer)));
        CBOR_CHECK_DESERIALIZED(input, buffer, EQUAL_STRING);
    }

    cbor_clear(&stream);

    {
        const char *input = "a";
        unsigned char data[] = {0x41, 0x61};
        TEST_ASSERT(cbor_serialize_byte_string(&stream, input));
        CBOR_CHECK_SERIALIZED(stream, data, sizeof(data));
        TEST_ASSERT(cbor_deserialize_byte_string(&stream, 0, buffer, sizeof(buffer)));
        CBOR_CHECK_DESERIALIZED(input, buffer, EQUAL_STRING);
    }
}
Esempio n. 6
0
/**
 * Manual test for testing the cbor_stream_decode function
 */
void test_stream_decode(void)
{
    cbor_clear(&stream);

    cbor_serialize_int(&stream, 1);
    cbor_serialize_uint64_t(&stream, 2llu);
    cbor_serialize_int64_t(&stream, 3);
    cbor_serialize_int64_t(&stream, -5);
    cbor_serialize_bool(&stream, true);
#ifndef CBOR_NO_FLOAT
    cbor_serialize_float_half(&stream, 1.1f);
    cbor_serialize_float(&stream, 1.5f);
    cbor_serialize_double(&stream, 2.0);
#endif /* CBOR_NO_FLOAT */
    cbor_serialize_byte_string(&stream, "abc");
    cbor_serialize_unicode_string(&stream, "def");

    cbor_serialize_array(&stream, 2);
    cbor_serialize_int(&stream, 0);
    cbor_serialize_int(&stream, 1);

    cbor_serialize_array_indefinite(&stream);
    cbor_serialize_int(&stream, 10);
    cbor_serialize_int(&stream, 11);
    cbor_write_break(&stream);

    cbor_serialize_map(&stream, 2);
    cbor_serialize_int(&stream, 1);
    cbor_serialize_byte_string(&stream, "1");
    cbor_serialize_int(&stream, 2);
    cbor_serialize_byte_string(&stream, "2");

    cbor_serialize_map_indefinite(&stream);
    cbor_serialize_int(&stream, 10);
    cbor_serialize_byte_string(&stream, "10");
    cbor_serialize_int(&stream, 11);
    cbor_serialize_byte_string(&stream, "11");
    cbor_write_break(&stream);

#ifndef CBOR_NO_SEMANTIC_TAGGING
#ifndef CBOR_NO_CTIME
    time_t rawtime;
    time(&rawtime);
    struct tm *timeinfo = localtime(&rawtime);
    cbor_serialize_date_time(&stream, timeinfo);
    cbor_serialize_date_time_epoch(&stream, rawtime);
#endif /* CBOR_NO_CTIME */

    /* decoder should skip the tag and print 'unsupported' here */
    cbor_write_tag(&stream, 2);
    cbor_serialize_byte_string(&stream, "1");
#endif /* CBOR_NO_SEMANTIC_TAGGING */

    cbor_stream_decode(&stream);
}
Esempio n. 7
0
static void test_array(void)
{
    /* uniform types */
    {
        /* serialization */
        TEST_ASSERT(cbor_serialize_array(&stream, 2));
        TEST_ASSERT(cbor_serialize_int(&stream, 1));
        TEST_ASSERT(cbor_serialize_int(&stream, 2));
        unsigned char data[] = {0x82, 0x01, 0x02};
        CBOR_CHECK_SERIALIZED(stream, data, sizeof(data));

        /* deserialization */
        size_t array_length;
        size_t offset = cbor_deserialize_array(&stream, 0, &array_length);
        TEST_ASSERT_EQUAL_INT(2, array_length);
        int i;
        offset += cbor_deserialize_int(&stream, offset, &i);
        TEST_ASSERT_EQUAL_INT(1, i);
        offset += cbor_deserialize_int(&stream, offset, &i);
        TEST_ASSERT_EQUAL_INT(2, i);
        TEST_ASSERT_EQUAL_INT(sizeof(data), offset);
    }

    cbor_clear(&stream);

    /* mixed types */
    {
        TEST_ASSERT(cbor_serialize_array(&stream, 2));
        TEST_ASSERT(cbor_serialize_int(&stream, 1));
        TEST_ASSERT(cbor_serialize_byte_string(&stream, "a"));
        unsigned char data[] = {0x82, 0x01, 0x41, 0x61};
        CBOR_CHECK_SERIALIZED(stream, data, sizeof(data));

        /* deserialization */
        size_t array_length;
        size_t offset = cbor_deserialize_array(&stream, 0, &array_length);
        TEST_ASSERT(offset);
        TEST_ASSERT_EQUAL_INT(2, array_length);
        int i;
        offset += cbor_deserialize_int(&stream, offset, &i);
        TEST_ASSERT_EQUAL_INT(1, i);
        char buffer[1024];
        offset += cbor_deserialize_byte_string(&stream, offset, buffer, sizeof(buffer));
        TEST_ASSERT_EQUAL_STRING("a", &(buffer[0]));
        TEST_ASSERT_EQUAL_INT(sizeof(data), offset);
    }
}
Esempio n. 8
0
static void test_semantic_tagging(void)
{
    char buffer[128];

    const char *input = "1";
    /* CBOR: byte string of length 1 marked with a tag to indicate it is a positive bignum */
    /* byte 1: (major type 6, additional information */
    /* byte 2: (major type 2, additional 1 for the length) */
    /* byte 3: bytes representing the bignum */
    unsigned char data[] = {0xc2, 0x41, 0x31};
    TEST_ASSERT(cbor_write_tag(&stream, 2)); /* write byte 1 */
    TEST_ASSERT(cbor_serialize_byte_string(&stream, input)); /* write byte 2 and 3 */
    CBOR_CHECK_SERIALIZED(stream, data, sizeof(data));
    TEST_ASSERT(cbor_at_tag(&stream, 0));
    TEST_ASSERT(cbor_deserialize_byte_string(&stream, 1, buffer, sizeof(buffer)));
    CBOR_CHECK_DESERIALIZED(input, buffer, EQUAL_STRING);
}