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
/**
 * 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. 3
0
static void test_map_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_map(&stream, 1));

        cbor_destroy(&stream);
    }
    {
        /* check reading from stream that contains other type of data */
        unsigned char data[] = {0x40}; /* empty string encoded in CBOR */
        cbor_stream_t stream = {data, 1, 1};

        size_t map_length;
        TEST_ASSERT_EQUAL_INT(0, cbor_deserialize_map(&stream, 0, &map_length));
    }
}
Esempio n. 4
0
int SenMLPack::contentToCbor()
{
    int length = cbor_serialize_map(this->getFieldLength());

    int res = this->fieldsToCbor();
    SenMLBase *next = this->_start;
    if(next && next->isPack() == false){                        //we can only inline the first record. If the first item is a Pack (child device), then don't inline it.
        res += next->fieldsToCbor();
        next = next->getNext();
    }

    while(next){
        if(next->isPack() == false)
            res += next->contentToCbor();
        else
            res += ((SenMLPack*)next)->contentToCbor();
        next = next->getNext();
    }
    return res;
}
Esempio n. 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;
	}
}
Esempio n. 6
0
int SenMLRecord::contentToCbor()
{
    int res = cbor_serialize_map(this->getFieldLength());
    res += this->fieldsToCbor();
    return res;
}