Beispiel #1
0
static void test_array_invalid(void)
{
    TEST_ASSERT_EQUAL_INT(0, cbor_serialize_array(&empty_stream, 1));

    size_t array_length;
    TEST_ASSERT_EQUAL_INT(0, cbor_deserialize_array(&invalid_stream, 0, &array_length));
}
Beispiel #2
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);
    }
}
Beispiel #3
0
int SenMLPack::toCbor(char *dest, int length, SenMLStreamMethod format)
{
    StreamContext renderTo;                                              //set up the global record that configures the rendering. This saves us some bytes on the stack and in code by not having to pass along the values as function arguments.
    _streamCtx = &renderTo;
    this->setupStreamCtx(dest, length, format);
    int res = cbor_serialize_array(this->getArrayLength());
    res += this->contentToCbor();
    return res;
}
Beispiel #4
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);
}
Beispiel #5
0
size_t cbor_serialize(const cbor_item_t *item, unsigned char *buffer, size_t buffer_size)
{
	switch (cbor_typeof(item)) {
	case CBOR_TYPE_UINT:
		return cbor_serialize_uint(item, buffer, buffer_size);
	case CBOR_TYPE_NEGINT:
		return cbor_serialize_negint(item, buffer, buffer_size);
	case CBOR_TYPE_BYTESTRING:
		return cbor_serialize_bytestring(item, buffer, buffer_size);
	case CBOR_TYPE_STRING:
		return cbor_serialize_string(item, buffer, buffer_size);
	case CBOR_TYPE_ARRAY:
		return cbor_serialize_array(item, buffer, buffer_size);
	case CBOR_TYPE_MAP:
		return cbor_serialize_map(item, buffer, buffer_size);
	case CBOR_TYPE_TAG:
		return cbor_serialize_tag(item, buffer, buffer_size);
	case CBOR_TYPE_FLOAT_CTRL:
		return cbor_serialize_float_ctrl(item, buffer, buffer_size);
	default:
		return 0;
	}
}