From d66f35c31ad0222afa16e4edea7e3c04a2bfd01c Mon Sep 17 00:00:00 2001 From: Aydogan Ersoz Date: Sun, 8 Mar 2020 19:11:23 +0100 Subject: [PATCH] add QCBOR package --- pkg/qcbor/Makefile | 9 +++ pkg/qcbor/Makefile.dep | 1 + pkg/qcbor/Makefile.include | 1 + pkg/qcbor/Makefile.qcbor | 3 + pkg/qcbor/doc.txt | 16 ++++ tests/pkg_qcbor/Makefile | 7 ++ tests/pkg_qcbor/Makefile.ci | 3 + tests/pkg_qcbor/main.c | 135 ++++++++++++++++++++++++++++++++ tests/pkg_qcbor/tests/01-run.py | 18 +++++ 9 files changed, 193 insertions(+) create mode 100644 pkg/qcbor/Makefile create mode 100644 pkg/qcbor/Makefile.dep create mode 100644 pkg/qcbor/Makefile.include create mode 100644 pkg/qcbor/Makefile.qcbor create mode 100644 pkg/qcbor/doc.txt create mode 100644 tests/pkg_qcbor/Makefile create mode 100644 tests/pkg_qcbor/Makefile.ci create mode 100644 tests/pkg_qcbor/main.c create mode 100755 tests/pkg_qcbor/tests/01-run.py diff --git a/pkg/qcbor/Makefile b/pkg/qcbor/Makefile new file mode 100644 index 0000000000..7059b66539 --- /dev/null +++ b/pkg/qcbor/Makefile @@ -0,0 +1,9 @@ +PKG_NAME = qcbor +PKG_URL = https://github.com/laurencelundblade/QCBOR +PKG_VERSION = 7e8effa94174c33e4acaf737adf6aef16c092c49 # Commit on Mar 9, 2020 +PKG_LICENSE = BSD-3-Clause + +include $(RIOTBASE)/pkg/pkg.mk + +all: + "$(MAKE)" -C $(PKG_BUILDDIR)/src -f $(RIOTPKG)/qcbor/Makefile.$(PKG_NAME) diff --git a/pkg/qcbor/Makefile.dep b/pkg/qcbor/Makefile.dep new file mode 100644 index 0000000000..bdbbad5895 --- /dev/null +++ b/pkg/qcbor/Makefile.dep @@ -0,0 +1 @@ +FEATURES_REQUIRED += arch_32bit diff --git a/pkg/qcbor/Makefile.include b/pkg/qcbor/Makefile.include new file mode 100644 index 0000000000..b5f3f397ea --- /dev/null +++ b/pkg/qcbor/Makefile.include @@ -0,0 +1 @@ +INCLUDES += -I$(PKGDIRBASE)/qcbor/inc diff --git a/pkg/qcbor/Makefile.qcbor b/pkg/qcbor/Makefile.qcbor new file mode 100644 index 0000000000..4086ce7225 --- /dev/null +++ b/pkg/qcbor/Makefile.qcbor @@ -0,0 +1,3 @@ +MODULE = qcbor + +include $(RIOTBASE)/Makefile.base diff --git a/pkg/qcbor/doc.txt b/pkg/qcbor/doc.txt new file mode 100644 index 0000000000..9f192a0a41 --- /dev/null +++ b/pkg/qcbor/doc.txt @@ -0,0 +1,16 @@ +/** + * @defgroup pkg_qcbor QCBOR for RIOT + * @ingroup pkg + * @brief Small footprint CBOR encoder/decoder + * @see https://github.com/laurencelundblade/QCBOR + * + * Usage + * ----- + * + * It can be added as a package in the Makefile of the application: + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ {.mk} + * USEPKG += qcbor + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + */ \ No newline at end of file diff --git a/tests/pkg_qcbor/Makefile b/tests/pkg_qcbor/Makefile new file mode 100644 index 0000000000..bc8b9a28d8 --- /dev/null +++ b/tests/pkg_qcbor/Makefile @@ -0,0 +1,7 @@ +include ../Makefile.tests_common + +USEPKG += qcbor +# Used for verification +USEMODULE += embunit + +include $(RIOTBASE)/Makefile.include diff --git a/tests/pkg_qcbor/Makefile.ci b/tests/pkg_qcbor/Makefile.ci new file mode 100644 index 0000000000..518b330a9e --- /dev/null +++ b/tests/pkg_qcbor/Makefile.ci @@ -0,0 +1,3 @@ +BOARD_INSUFFICIENT_MEMORY := \ + stm32f030f4-demo \ + # diff --git a/tests/pkg_qcbor/main.c b/tests/pkg_qcbor/main.c new file mode 100644 index 0000000000..9925d2bbb3 --- /dev/null +++ b/tests/pkg_qcbor/main.c @@ -0,0 +1,135 @@ +/* + * Copyright (C) 2018 Aydogan Ersoz + * + * 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 QCBOR library test + * + * @author Aydogan Ersoz + * + * @} + */ + +#include "qcbor.h" +#include +#include "embUnit.h" + +/* + [ + null, + 123456, + -123456, + { + -1: h'31323334353600', + 0: false, + -999: "hello world" + }, + true + ] +*/ +static const uint8_t expected[] = { + 0x85, 0xF6, 0x1A, 0x00, 0x01, 0xE2, 0x40, 0x3A, 0x00, 0x01, 0xE2, 0x3F, + 0xA3, 0x20, 0x47, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x00, 0x00, 0xF4, + 0x39, 0x03, 0xE6, 0x6B, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, + 0x72, 0x6C, 0x64, 0xF5}; +static uint8_t buf[sizeof(expected)]; + +static void test_qcbor_encode(void) +{ + QCBOREncodeContext encode_context; + UsefulBufC encoded_cbor; + QCBORError error; + uint8_t bytes[] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x00}; + + QCBOREncode_Init(&encode_context, UsefulBuf_FROM_BYTE_ARRAY(buf)); + + QCBOREncode_OpenArray(&encode_context); + QCBOREncode_AddNULL(&encode_context); + QCBOREncode_AddUInt64(&encode_context, 123456); + QCBOREncode_AddInt64(&encode_context, -123456); + QCBOREncode_OpenMap(&encode_context); + QCBOREncode_AddBytesToMapN(&encode_context, + -1, + ((UsefulBufC){bytes, sizeof(bytes)})); + QCBOREncode_AddBoolToMapN(&encode_context, 0, false); + QCBOREncode_AddTextToMapN(&encode_context, + -999, + UsefulBuf_FROM_SZ_LITERAL("hello world")); + QCBOREncode_CloseMap(&encode_context); + QCBOREncode_AddBool(&encode_context, true); + QCBOREncode_CloseArray(&encode_context); + error = QCBOREncode_Finish(&encode_context, &encoded_cbor); + TEST_ASSERT_EQUAL_INT(QCBOR_SUCCESS, error); + + TEST_ASSERT_EQUAL_INT(0, memcmp(encoded_cbor.ptr, + expected, sizeof(expected))); +} + +static void test_qcbor_decode(void) +{ + QCBORDecodeContext decode_context; + QCBORItem cbor_item; + QCBORError error; + + QCBORDecode_Init(&decode_context, + UsefulBuf_FROM_BYTE_ARRAY_LITERAL(expected), + QCBOR_DECODE_MODE_NORMAL); + error = QCBORDecode_GetNext(&decode_context, &cbor_item); + TEST_ASSERT_EQUAL_INT(QCBOR_SUCCESS, error); + TEST_ASSERT_EQUAL_INT(QCBOR_TYPE_ARRAY, cbor_item.uDataType); + error = QCBORDecode_GetNext(&decode_context, &cbor_item); + TEST_ASSERT_EQUAL_INT(QCBOR_SUCCESS, error); + TEST_ASSERT_EQUAL_INT(QCBOR_TYPE_NULL, cbor_item.uDataType); + error = QCBORDecode_GetNext(&decode_context, &cbor_item); + TEST_ASSERT_EQUAL_INT(QCBOR_SUCCESS, error); + TEST_ASSERT_EQUAL_INT(123456, cbor_item.val.uint64); + error = QCBORDecode_GetNext(&decode_context, &cbor_item); + TEST_ASSERT_EQUAL_INT(QCBOR_SUCCESS, error); + TEST_ASSERT_EQUAL_INT(-123456, cbor_item.val.int64); + error = QCBORDecode_GetNext(&decode_context, &cbor_item); + TEST_ASSERT_EQUAL_INT(QCBOR_SUCCESS, error); + TEST_ASSERT_EQUAL_INT(QCBOR_TYPE_MAP, cbor_item.uDataType); + error = QCBORDecode_GetNext(&decode_context, &cbor_item); + TEST_ASSERT_EQUAL_INT(QCBOR_SUCCESS, error); + TEST_ASSERT_EQUAL_INT(QCBOR_TYPE_BYTE_STRING, cbor_item.uDataType); + TEST_ASSERT_EQUAL_INT(-1, cbor_item.label.int64); + error = QCBORDecode_GetNext(&decode_context, &cbor_item); + TEST_ASSERT_EQUAL_INT(QCBOR_SUCCESS, error); + TEST_ASSERT_EQUAL_INT(QCBOR_TYPE_FALSE, cbor_item.uDataType); + TEST_ASSERT_EQUAL_INT(0, cbor_item.label.uint64); + error = QCBORDecode_GetNext(&decode_context, &cbor_item); + TEST_ASSERT_EQUAL_INT(QCBOR_SUCCESS, error); + TEST_ASSERT_EQUAL_INT(QCBOR_TYPE_TEXT_STRING, cbor_item.uDataType); + TEST_ASSERT_EQUAL_INT(-999, cbor_item.label.int64); + error = QCBORDecode_GetNext(&decode_context, &cbor_item); + TEST_ASSERT_EQUAL_INT(QCBOR_SUCCESS, error); + TEST_ASSERT_EQUAL_INT(QCBOR_TYPE_TRUE, cbor_item.uDataType); + error = QCBORDecode_Finish(&decode_context); + TEST_ASSERT_EQUAL_INT(QCBOR_SUCCESS, error); +} + +Test *tests_qcbor(void) +{ + EMB_UNIT_TESTFIXTURES(fixtures){ + new_TestFixture(test_qcbor_encode), + new_TestFixture(test_qcbor_decode), + }; + EMB_UNIT_TESTCALLER(qcbor_tests, NULL, NULL, fixtures); + return (Test *)&qcbor_tests; +} + +int main(void) +{ + TESTS_START(); + TESTS_RUN(tests_qcbor()); + TESTS_END(); + return 0; +} diff --git a/tests/pkg_qcbor/tests/01-run.py b/tests/pkg_qcbor/tests/01-run.py new file mode 100755 index 0000000000..5fc8788f24 --- /dev/null +++ b/tests/pkg_qcbor/tests/01-run.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2017 Freie Universität Berlin +# +# 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 sys +from testrunner import run + + +def testfunc(child): + child.expect(r'OK \(\d+ tests\)') + + +if __name__ == "__main__": + sys.exit(run(testfunc))