diff --git a/tests/cbor/Makefile b/tests/cbor/Makefile new file mode 100644 index 000000000..5e565c126 --- /dev/null +++ b/tests/cbor/Makefile @@ -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 diff --git a/tests/cbor/main.c b/tests/cbor/main.c new file mode 100644 index 000000000..14931b94f --- /dev/null +++ b/tests/cbor/main.c @@ -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 + * + * @} + */ + +#include +#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; +} diff --git a/tests/cbor/tests/01-run.py b/tests/cbor/tests/01-run.py new file mode 100755 index 000000000..bd97513d2 --- /dev/null +++ b/tests/cbor/tests/01-run.py @@ -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)) diff --git a/tests/unittests/tests-cbor/tests-cbor.c b/tests/unittests/tests-cbor/tests-cbor.c index 541568f88..b30d26920 100644 --- a/tests/unittests/tests-cbor/tests-cbor.c +++ b/tests/unittests/tests-cbor/tests-cbor.c @@ -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()); }