sys: cbor: add test app for cbor_stream_decode

This commit is contained in:
Pieter Willemsen 2017-06-27 15:06:46 +02:00
parent 29e8bd1351
commit 6706e763d4
4 changed files with 161 additions and 65 deletions

26
tests/cbor/Makefile Normal file
View File

@ -0,0 +1,26 @@
APPLICATION = cbor
include ../Makefile.tests_common
BOARD_BLACKLIST := arduino-duemilanove arduino-mega2560 arduino-uno
BOARD_BLACKLIST += chronos
BOARD_BLACKLIST += mips-malta
BOARD_BLACKLIST += msb-430 msb-430h
BOARD_BLACKLIST += nucleo32-f031
BOARD_BLACKLIST += pic32-clicker pic32-wifire
BOARD_BLACKLIST += qemu-i386
BOARD_BLACKLIST += telosb
BOARD_BLACKLIST += waspmote-pro
BOARD_BLACKLIST += wsn430-v1_3b wsn430-v1_4
BOARD_BLACKLIST += z1
USEMODULE += cbor
USEMODULE += cbor_ctime
USEMODULE += cbor_float
USEMODULE += cbor_semantic_tagging
include $(RIOTBASE)/Makefile.include
test:
# `testrunner` calls `make term` recursively, results in duplicated `TERMFLAGS`.
# So clears `TERMFLAGS` before run.
TERMFLAGS= tests/01-run.py

81
tests/cbor/main.c Normal file
View File

@ -0,0 +1,81 @@
/*
* Copyright (C) 2017 OTA keys S.A.
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup tests
* @{
*
* @file
* @brief cbor stream decode test application
*
* @author Pieter Willemsen <pieter.willemsen@altran.com>
*
* @}
*/
#include <stdio.h>
#include "cbor.h"
static unsigned char stream_data[1024];
static cbor_stream_t stream = {stream_data, sizeof(stream_data), 0};
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);
cbor_serialize_float_half(&stream, 1.1f);
cbor_serialize_float(&stream, 1.5f);
cbor_serialize_double(&stream, 2.0);
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);
time_t rawtime;
time(&rawtime);
struct tm *timeinfo = localtime(&rawtime);
cbor_serialize_date_time(&stream, timeinfo);
cbor_serialize_date_time_epoch(&stream, rawtime);
/* decoder should skip the tag and print 'unsupported' here */
cbor_write_tag(&stream, 2);
cbor_serialize_byte_string(&stream, "1");
cbor_stream_decode(&stream);
}
int main(void)
{
test_stream_decode();
return 0;
}

54
tests/cbor/tests/01-run.py Executable file
View File

@ -0,0 +1,54 @@
#!/usr/bin/env python3
# Copyright (C) 2017 OTA keys S.A.
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.
import os
import sys
sys.path.append(os.path.join(os.environ['RIOTBASE'], 'dist/tools/testrunner'))
import testrunner
ACCEPTED_ERROR = 20
def testfunc(child):
child.expect_exact('Data:')
child.expect_exact('(uint64_t, 1)')
child.expect_exact('(uint64_t, 2)')
child.expect_exact('(uint64_t, 3)')
child.expect_exact('(int64_t, -5)')
child.expect_exact('(bool, 1)')
child.expect_exact('(float, 1.099609)')
child.expect_exact('(float, 1.500000)')
child.expect_exact('(double, 2.000000)')
child.expect_exact('(byte string, "abc")')
child.expect_exact('(unicode string, "def")')
child.expect_exact('(array, length: 2)')
child.expect_exact(' (uint64_t, 0)')
child.expect_exact(' (uint64_t, 1)')
child.expect_exact('(array, length: [indefinite])')
child.expect_exact(' (uint64_t, 10)')
child.expect_exact(' (uint64_t, 11)')
child.expect_exact('(map, length: 2)')
child.expect_exact(' (uint64_t, 1)')
child.expect_exact(' (byte string, "1")')
child.expect_exact(' (uint64_t, 2)')
child.expect_exact(' (byte string, "2")')
child.expect_exact('(map, length: [indefinite])')
child.expect_exact(' (uint64_t, 10)')
child.expect_exact(' (byte string, "10")')
child.expect_exact(' (uint64_t, 11)')
child.expect_exact(' (byte string, "11")')
child.expect(r'\(tag: 0, date/time string: "[\w :]+"\)')
child.expect(r'\(tag: 1, date/time epoch: \d+\)')
child.expect_exact('(unsupported, 0xC2')
child.expect_exact(')')
child.expect_exact('(byte string, "1")')
print("All tests successful")
if __name__ == "__main__":
sys.exit(testrunner.run(testfunc, echo=False))

View File

@ -748,67 +748,6 @@ static void test_double_invalid(void)
}
#endif /* MODULE_CBOR_FLOAT */
#ifdef MODULE_CBOR_PRINT
/**
* 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);
#ifdef MODULE_CBOR_FLOAT
cbor_serialize_float_half(&stream, 1.1f);
cbor_serialize_float(&stream, 1.5f);
cbor_serialize_double(&stream, 2.0);
#endif /* MODULE_CBOR_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);
#ifdef MODULE_CBOR_SEMANTIC_TAGGING
#ifdef MODULE_CBOR_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 /* MODULE_CBOR_CTIME */
/* decoder should skip the tag and print 'unsupported' here */
cbor_write_tag(&stream, 2);
cbor_serialize_byte_string(&stream, "1");
#endif /* MODULE_CBOR_SEMANTIC_TAGGING */
//cbor_stream_decode(&stream);
}
#endif /* MODULE_CBOR_PRINT */
/**
* See examples from CBOR RFC (cf. Appendix A. Examples)
*/
@ -858,9 +797,5 @@ TestRef tests_cbor_all(void)
void tests_cbor(void)
{
#ifdef MODULE_CBOR_PRINT
test_stream_decode();
#endif /* MODULE_CBOR_PRINT */
TESTS_RUN(tests_cbor_all());
}