Feature/zephyr ztest (#118)
* Leverage (older) embedded unit tests into external unit tests build upon copy of Zephyr's ztest library and CMake. * Expand top-level CMake build to run external unit tests. * Expand Zephyr module extension to run external unit tests via west or sanitycheck. Co-authored-by: Gregory Shue <gregory.shue@legrand.us>
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
|
||||
|
||||
get_filename_component(basename ${CMAKE_CURRENT_SOURCE_DIR} NAME)
|
||||
project(test_${basename}
|
||||
VERSION 1.0.0
|
||||
LANGUAGES C)
|
||||
|
||||
|
||||
string(REGEX REPLACE "/bacnet/[a-zA-Z_/-]*$" "" TST_DIR ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
set(ZTST_DIR "${TST_DIR}/ztest/src")
|
||||
string(REGEX REPLACE "/test$" "/src" SRC_DIR ${TST_DIR})
|
||||
|
||||
add_compile_definitions(
|
||||
BIG_ENDIAN=0
|
||||
CONFIG_ZTEST=1
|
||||
)
|
||||
|
||||
include_directories(
|
||||
${SRC_DIR}
|
||||
${TST_DIR}/ztest/include
|
||||
)
|
||||
|
||||
add_executable(${PROJECT_NAME}
|
||||
# File(s) under test
|
||||
${SRC_DIR}/bacnet/npdu.c
|
||||
# Support files and stubs (pathname alphabetical)
|
||||
${SRC_DIR}/bacnet/bacaddr.c
|
||||
${SRC_DIR}/bacnet/bacdcode.c
|
||||
${SRC_DIR}/bacnet/bacint.c
|
||||
${SRC_DIR}/bacnet/bacreal.c
|
||||
${SRC_DIR}/bacnet/bacstr.c
|
||||
${SRC_DIR}/bacnet/basic/service/h_apdu.c
|
||||
${SRC_DIR}/bacnet/basic/sys/bigend.c
|
||||
${SRC_DIR}/bacnet/basic/tsm/tsm.c
|
||||
${SRC_DIR}/bacnet/dcc.c
|
||||
./stubs.c
|
||||
# Test and test library files
|
||||
./src/main.c
|
||||
${ZTST_DIR}/ztest_mock.c
|
||||
${ZTST_DIR}/ztest.c
|
||||
)
|
||||
@@ -0,0 +1,153 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Legrand North America, LLC.
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
/* @file
|
||||
* @brief test BACnet integer encode/decode APIs
|
||||
*/
|
||||
|
||||
#include <ztest.h>
|
||||
#include <bacnet/npdu.h>
|
||||
|
||||
/**
|
||||
* @addtogroup bacnet_tests
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Test
|
||||
*/
|
||||
static void testNPDU2(void)
|
||||
{
|
||||
uint8_t pdu[480] = { 0 };
|
||||
BACNET_ADDRESS dest = { 0 };
|
||||
BACNET_ADDRESS src = { 0 };
|
||||
BACNET_ADDRESS npdu_dest = { 0 };
|
||||
BACNET_ADDRESS npdu_src = { 0 };
|
||||
int len = 0;
|
||||
bool data_expecting_reply = true;
|
||||
BACNET_MESSAGE_PRIORITY priority = MESSAGE_PRIORITY_NORMAL;
|
||||
BACNET_NPDU_DATA npdu_data = { 0 };
|
||||
int i = 0; /* counter */
|
||||
int npdu_len = 0;
|
||||
bool network_layer_message = false; /* false if APDU */
|
||||
BACNET_NETWORK_MESSAGE_TYPE network_message_type = 0; /* optional */
|
||||
uint16_t vendor_id = 0; /* optional, if net message type is > 0x80 */
|
||||
|
||||
dest.mac_len = 6;
|
||||
for (i = 0; i < dest.mac_len; i++) {
|
||||
dest.mac[i] = i;
|
||||
}
|
||||
/* DNET,DLEN,DADR */
|
||||
dest.net = 1;
|
||||
dest.len = 6;
|
||||
for (i = 0; i < dest.len; i++) {
|
||||
dest.adr[i] = i * 10;
|
||||
}
|
||||
src.mac_len = 1;
|
||||
for (i = 0; i < src.mac_len; i++) {
|
||||
src.mac[i] = 0x80;
|
||||
}
|
||||
/* SNET,SLEN,SADR */
|
||||
src.net = 2;
|
||||
src.len = 1;
|
||||
for (i = 0; i < src.len; i++) {
|
||||
src.adr[i] = 0x40;
|
||||
}
|
||||
npdu_encode_npdu_data(&npdu_data, true, priority);
|
||||
len = npdu_encode_pdu(&pdu[0], &dest, &src, &npdu_data);
|
||||
zassert_not_equal(len, 0, NULL);
|
||||
/* can we get the info back? */
|
||||
npdu_len = npdu_decode(&pdu[0], &npdu_dest, &npdu_src, &npdu_data);
|
||||
zassert_not_equal(npdu_len, 0, NULL);
|
||||
zassert_equal(npdu_data.data_expecting_reply, data_expecting_reply, NULL);
|
||||
zassert_equal(npdu_data.network_layer_message, network_layer_message, NULL);
|
||||
if (npdu_data.network_layer_message) {
|
||||
zassert_equal(npdu_data.network_message_type, network_message_type, NULL);
|
||||
}
|
||||
zassert_equal(npdu_data.vendor_id, vendor_id, NULL);
|
||||
zassert_equal(npdu_data.priority, priority, NULL);
|
||||
/* DNET,DLEN,DADR */
|
||||
zassert_equal(npdu_dest.net, dest.net, NULL);
|
||||
zassert_equal(npdu_dest.len, dest.len, NULL);
|
||||
for (i = 0; i < dest.len; i++) {
|
||||
zassert_equal(npdu_dest.adr[i], dest.adr[i], NULL);
|
||||
}
|
||||
/* SNET,SLEN,SADR */
|
||||
zassert_equal(npdu_src.net, src.net, NULL);
|
||||
zassert_equal(npdu_src.len, src.len, NULL);
|
||||
for (i = 0; i < src.len; i++) {
|
||||
zassert_equal(npdu_src.adr[i], src.adr[i], NULL);
|
||||
}
|
||||
}
|
||||
|
||||
static void testNPDU1(void)
|
||||
{
|
||||
uint8_t pdu[480] = { 0 };
|
||||
BACNET_ADDRESS dest = { 0 };
|
||||
BACNET_ADDRESS src = { 0 };
|
||||
BACNET_ADDRESS npdu_dest = { 0 };
|
||||
BACNET_ADDRESS npdu_src = { 0 };
|
||||
int len = 0;
|
||||
bool data_expecting_reply = false;
|
||||
BACNET_MESSAGE_PRIORITY priority = MESSAGE_PRIORITY_NORMAL;
|
||||
BACNET_NPDU_DATA npdu_data = { 0 };
|
||||
int i = 0; /* counter */
|
||||
int npdu_len = 0;
|
||||
bool network_layer_message = false; /* false if APDU */
|
||||
BACNET_NETWORK_MESSAGE_TYPE network_message_type = 0; /* optional */
|
||||
uint16_t vendor_id = 0; /* optional, if net message type is > 0x80 */
|
||||
|
||||
/* mac_len = 0 if global address */
|
||||
dest.mac_len = 0;
|
||||
for (i = 0; i < MAX_MAC_LEN; i++) {
|
||||
dest.mac[i] = 0;
|
||||
}
|
||||
/* DNET,DLEN,DADR */
|
||||
dest.net = 0;
|
||||
dest.len = 0;
|
||||
for (i = 0; i < MAX_MAC_LEN; i++) {
|
||||
dest.adr[i] = 0;
|
||||
}
|
||||
src.mac_len = 0;
|
||||
for (i = 0; i < MAX_MAC_LEN; i++) {
|
||||
src.mac[i] = 0;
|
||||
}
|
||||
/* SNET,SLEN,SADR */
|
||||
src.net = 0;
|
||||
src.len = 0;
|
||||
for (i = 0; i < MAX_MAC_LEN; i++) {
|
||||
src.adr[i] = 0;
|
||||
}
|
||||
npdu_encode_npdu_data(&npdu_data, false, priority);
|
||||
len = npdu_encode_pdu(&pdu[0], &dest, &src, &npdu_data);
|
||||
zassert_not_equal(len, 0, NULL);
|
||||
/* can we get the info back? */
|
||||
npdu_len = npdu_decode(&pdu[0], &npdu_dest, &npdu_src, &npdu_data);
|
||||
zassert_not_equal(npdu_len, 0, NULL);
|
||||
zassert_equal(npdu_data.data_expecting_reply, data_expecting_reply, NULL);
|
||||
zassert_equal(npdu_data.network_layer_message, network_layer_message, NULL);
|
||||
if (npdu_data.network_layer_message) {
|
||||
zassert_equal(npdu_data.network_message_type, network_message_type, NULL);
|
||||
}
|
||||
zassert_equal(npdu_data.vendor_id, vendor_id, NULL);
|
||||
zassert_equal(npdu_data.priority, priority, NULL);
|
||||
zassert_equal(npdu_dest.mac_len, src.mac_len, NULL);
|
||||
zassert_equal(npdu_src.mac_len, dest.mac_len, NULL);
|
||||
}
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
void test_main(void)
|
||||
{
|
||||
ztest_test_suite(npdu_tests,
|
||||
ztest_unit_test(testNPDU1),
|
||||
ztest_unit_test(testNPDU2)
|
||||
);
|
||||
|
||||
ztest_run_test_suite(npdu_tests);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/**************************************************************************
|
||||
*
|
||||
* Copyright (C) 2006 Steve Karg <skarg@users.sourceforge.net>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*********************************************************************/
|
||||
|
||||
/* Binary Input Objects customize for your use */
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include "bacnet/bacdef.h"
|
||||
#include "bacnet/datalink/bip.h"
|
||||
|
||||
|
||||
int bip_send_pdu(
|
||||
BACNET_ADDRESS * dest,
|
||||
BACNET_NPDU_DATA * npdu_data,
|
||||
uint8_t * pdu,
|
||||
unsigned pdu_len)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user