From 309964e929792a5c6af6db7bc1fb934b2d257087 Mon Sep 17 00:00:00 2001 From: Steve Karg Date: Thu, 30 May 2024 09:02:49 -0500 Subject: [PATCH] Added existing BBMD unit test by converting to cmake (#657) --- test/CMakeLists.txt | 19 ++++-- test/bacnet/basic/bbmd/CMakeLists.txt | 47 +++++++++++++ test/bacnet/basic/bbmd/Makefile | 50 -------------- test/bacnet/basic/bbmd/{ => src}/main.c | 89 +++++++------------------ 4 files changed, 84 insertions(+), 121 deletions(-) create mode 100644 test/bacnet/basic/bbmd/CMakeLists.txt delete mode 100644 test/bacnet/basic/bbmd/Makefile rename test/bacnet/basic/bbmd/{ => src}/main.c (71%) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f90e7166..73f11638 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -14,16 +14,20 @@ endif() if (CMAKE_C_COMPILER_ID MATCHES "Clang" OR CMAKE_C_COMPILER_ID MATCHES "GNU") add_definitions(-fprofile-arcs -ftest-coverage) add_compile_options(-g -O0 -W -fprofile-arcs -ftest-coverage) + # enable all relevant warnings that find bugs add_compile_options(-Wall -Wextra -pedantic) - add_compile_options(-Wfloat-equal -Wconversion -Wparentheses) - add_compile_options(-Wunused-value -Wreturn-type -Wswitch-default) - add_compile_options(-Wuninitialized -Winit-self) + add_compile_options(-Wfloat-equal -Wconversion) + add_compile_options(-Wredundant-decls -Wswitch-default) + # don't warn about conversion, sign, compares, long long and attributes + # since they are common in embedded add_compile_options(-Wno-sign-conversion -Wno-conversion) - add_compile_options(-Wno-sign-compare -Wno-long-long) - add_compile_options(-Wno-implicit-fallthrough -Wno-attributes) + add_compile_options(-Wno-sign-compare -Wno-long-long -Wno-attributes) + # don't warn about implicit fallthrough since it's common in network protocols + add_compile_options(-Wno-implicit-fallthrough) + # FIXME: ignore some warnings that occur in the unit tests add_compile_options(-Wno-missing-braces) - # ignore some warnings that occur during unit testing - add_compile_options(-Wno-unused-variable -Wno-unused-function) + add_compile_options(-Wno-unused-variable) + add_compile_options(-Wno-unused-function) add_compile_options(-Wno-unused-parameter) add_link_options(-fprofile-arcs -ftest-coverage) endif() @@ -101,6 +105,7 @@ list(APPEND testdirs # bacnet/basic/* list(APPEND testdirs bacnet/basic/binding/address + bacnet/basic/bbmd bacnet/basic/bbmd6 # basic/object bacnet/basic/object/acc diff --git a/test/bacnet/basic/bbmd/CMakeLists.txt b/test/bacnet/basic/bbmd/CMakeLists.txt new file mode 100644 index 00000000..269f6c7e --- /dev/null +++ b/test/bacnet/basic/bbmd/CMakeLists.txt @@ -0,0 +1,47 @@ +# 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 + "/test/bacnet/[a-zA-Z0-9_/-]*$" + "/src" + SRC_DIR + ${CMAKE_CURRENT_SOURCE_DIR}) +string(REGEX REPLACE + "/test/bacnet/[a-zA-Z0-9_/-]*$" + "/test" + TST_DIR + ${CMAKE_CURRENT_SOURCE_DIR}) + +add_compile_definitions( + BIG_ENDIAN=0 + ) + +include_directories( + ${SRC_DIR} + ) + +add_executable(${PROJECT_NAME} + # File(s) under test + ${SRC_DIR}/bacnet/basic/bbmd/h_bbmd.c + # Support files and stubs (pathname alphabetical) + ${SRC_DIR}/bacnet/bacdcode.c + ${SRC_DIR}/bacnet/bacint.c + ${SRC_DIR}/bacnet/bacstr.c + ${SRC_DIR}/bacnet/bacreal.c + ${SRC_DIR}/bacnet/hostnport.c + ${SRC_DIR}/bacnet/iam.c + ${SRC_DIR}/bacnet/npdu.c + ${SRC_DIR}/bacnet/basic/sys/bigend.c + ${SRC_DIR}/bacnet/basic/sys/debug.c + ${SRC_DIR}/bacnet/basic/sys/keylist.c + ${SRC_DIR}/bacnet/datalink/bvlc.c + # Test and test library files + ./src/main.c + ) diff --git a/test/bacnet/basic/bbmd/Makefile b/test/bacnet/basic/bbmd/Makefile deleted file mode 100644 index dea1623f..00000000 --- a/test/bacnet/basic/bbmd/Makefile +++ /dev/null @@ -1,50 +0,0 @@ -#Makefile to build test case -CC = gcc -SRC_DIR = ../../../../src -TEST_DIR = ../../.. -INCLUDES = -I$(SRC_DIR) -I$(TEST_DIR) -DEFINES = -DBIG_ENDIAN=0 -DDEBUG_ENABLED=0 - -CFLAGS = -Wall $(INCLUDES) $(DEFINES) -g - -SRCS = main.c \ - $(SRC_DIR)/bacnet/basic/bbmd/h_bbmd.c \ - $(SRC_DIR)/bacnet/bacdcode.c \ - $(SRC_DIR)/bacnet/bacint.c \ - $(SRC_DIR)/bacnet/bacstr.c \ - $(SRC_DIR)/bacnet/bacreal.c \ - $(SRC_DIR)/bacnet/iam.c \ - $(SRC_DIR)/bacnet/npdu.c \ - $(SRC_DIR)/bacnet/datalink/bvlc.c \ - $(SRC_DIR)/bacnet/basic/sys/debug.c \ - $(TEST_DIR)/ctest.c - -TARGET_NAME = unittest -ifeq ($(OS),Windows_NT) -TARGET_EXT = .exe -else -TARGET_EXT = -endif -TARGET = $(TARGET_NAME)$(TARGET_EXT) - -all: ${TARGET} - -OBJS = ${SRCS:.c=.o} - -${TARGET}: ${OBJS} - ${CC} -o $@ ${OBJS} - -.c.o: - ${CC} -c ${CFLAGS} $*.c -o $@ - -depend: - rm -f .depend - ${CC} -MM ${CFLAGS} *.c >> .depend - -clean: - rm -rf ${TARGET} $(OBJS) - -test: ${TARGET} - ./${TARGET} - -include: .depend diff --git a/test/bacnet/basic/bbmd/main.c b/test/bacnet/basic/bbmd/src/main.c similarity index 71% rename from test/bacnet/basic/bbmd/main.c rename to test/bacnet/basic/bbmd/src/main.c index 5f4a1c95..772839e0 100644 --- a/test/bacnet/basic/bbmd/main.c +++ b/test/bacnet/basic/bbmd/src/main.c @@ -1,29 +1,9 @@ /** * @file - * @author Steve Karg + * @author Steve Karg * @date April 2020 * @brief Test file for a basic BBMD for BVLC IPv4 handler - * - * @section LICENSE - * - * 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. + * @copyright SPDX-License-Identifier: MIT */ #include /* for standard i/o, like printing */ #include /* for standard integer types uint8_t etc. */ @@ -31,6 +11,7 @@ #include /* for memcpy */ #include #include +#include "bacnet/bacdef.h" #include "bacnet/bacdcode.h" #include "bacnet/iam.h" #include "bacnet/npdu.h" @@ -39,7 +20,6 @@ #include "bacnet/basic/sys/debug.h" #include "bacnet/basic/object/device.h" #include "bacnet/basic/bbmd/h_bbmd.h" -#include "ctest.h" struct device_info_t { uint32_t Device_ID; @@ -53,7 +33,7 @@ static struct device_info_t IUT; /* for the reply sent from the handler */ static uint8_t Test_Sent_Message_Type; static uint8_t Test_Sent_Message_Length; -static uint8_t Test_Sent_Message_Buffer[MAX_MPDU]; +static uint8_t Test_Sent_Message_Buffer[MAX_APDU]; static uint16_t Test_Sent_Message_Buffer_Length; static BACNET_IP_ADDRESS Test_Sent_Message_Dest; @@ -156,15 +136,15 @@ static void test_cleanup(void) /** * @brief Test 15.2.1.1 Initiate Original-Broadcast-NPDU */ -static void test_Initiate_Original_Broadcast_NPDU(Test *pTest) +static void test_Initiate_Original_Broadcast_NPDU(void) { - uint8_t pdu[MAX_MPDU] = { 0 }; + uint8_t pdu[MAX_APDU] = { 0 }; int npdu_len = 0; int apdu_len = 0; int pdu_len = 0; BACNET_ADDRESS dest = { 0 }; BACNET_NPDU_DATA npdu_data = { 0 }; - uint8_t test_pdu[MAX_MPDU] = { 0 }; + uint8_t test_pdu[MAX_APDU] = { 0 }; uint16_t test_pdu_len = 0; int function_len = 0; @@ -179,30 +159,28 @@ static void test_Initiate_Original_Broadcast_NPDU(Test *pTest) pdu_len = npdu_len + apdu_len; bvlc_send_pdu(&dest, &npdu_data, pdu, pdu_len); /* DA=Link Local Multicast Address */ - ct_test( - pTest, - !bvlc_address_different( - &TD.BIP_Broadcast_Addr, &Test_Sent_Message_Dest)); + assert(!bvlc_address_different( + &TD.BIP_Broadcast_Addr, &Test_Sent_Message_Dest)); /* SA = IUT - done in port layer */ /* Original-Broadcast-NPDU */ - ct_test(pTest, Test_Sent_Message_Type == BVLC_ORIGINAL_BROADCAST_NPDU); + assert(Test_Sent_Message_Type == BVLC_ORIGINAL_BROADCAST_NPDU); if (Test_Sent_Message_Type == BVLC_ORIGINAL_BROADCAST_NPDU) { function_len = bvlc_decode_original_broadcast( Test_Sent_Message_Buffer, Test_Sent_Message_Buffer_Length, test_pdu, sizeof(test_pdu), &test_pdu_len); printf( - "len=%u pdu[%u] test_pdu[%u]\n", (unsigned)function_len, + "len=%u pdu[%u] test_pdu[%u] %s\n", (unsigned)function_len, (unsigned)Test_Sent_Message_Buffer_Length, - (unsigned)sizeof(test_pdu)); - ct_test(pTest, function_len > 0); + (unsigned)sizeof(test_pdu), (function_len > 0) ? "PASS" : "FAIL"); + assert(function_len > 0); /* (any valid BACnet-Unconfirmed-Request-PDU, with any valid broadcast network options */ - ct_test(pTest, test_pdu_len == pdu_len); + assert(test_pdu_len == pdu_len); } test_cleanup(); } -static void test_BBMD_Result(Test *pTest) +static void test_BBMD_Result(void) { int result = 0; uint16_t result_code[] = { @@ -220,7 +198,7 @@ static void test_BBMD_Result(Test *pTest) BACNET_IP_ADDRESS addr; BACNET_ADDRESS src; unsigned int i = 0; - uint8_t mtu[MAX_MPDU] = { 0 }; + uint8_t mtu[MAX_APDU] = { 0 }; uint16_t mtu_len = 0; bvlc_address_port_from_ascii(&addr, "192.168.0.1", "0xBAC0"); @@ -228,43 +206,26 @@ static void test_BBMD_Result(Test *pTest) mtu_len = bvlc_encode_result(&mtu[0], sizeof(mtu), result_code[i]); result = bvlc_bbmd_disabled_handler(&addr, &src, &mtu[0], mtu_len); /* validate that the result is handled (0) */ - ct_test(pTest, result == 0); + assert(result == 0); test_result_code = bvlc_get_last_result(); - ct_test(pTest, test_result_code == result_code[i]); + assert(test_result_code == result_code[i]); test_function_code = bvlc_get_function_code(); - ct_test(pTest, test_function_code == BVLC_RESULT); + assert(test_function_code == BVLC_RESULT); result = bvlc_bbmd_enabled_handler(&addr, &src, &mtu[0], mtu_len); /* validate that the result is handled (0) */ - ct_test(pTest, result == 0); + assert(result == 0); test_result_code = bvlc_get_last_result(); - ct_test(pTest, test_result_code == result_code[i]); + assert(test_result_code == result_code[i]); test_function_code = bvlc_get_function_code(); - ct_test(pTest, test_function_code == BVLC_RESULT); + assert(test_function_code == BVLC_RESULT); } } -static void test_BBMD_Handler(Test *pTest) -{ - bool rc; - - /* individual tests */ - rc = ct_addTestFunction(pTest, test_BBMD_Result); - assert(rc); - rc = ct_addTestFunction(pTest, test_Initiate_Original_Broadcast_NPDU); - assert(rc); -} - int main(void) { - Test *pTest; - - pTest = ct_create("BACnet Broadcast Management Device IP/v4", NULL); - test_BBMD_Handler(pTest); - /* configure output */ - ct_setStream(pTest, stdout); - ct_run(pTest); - (void)ct_report(pTest); - ct_destroy(pTest); + /* individual tests */ + test_BBMD_Result(); + test_Initiate_Original_Broadcast_NPDU(); return 0; }