Bugfix/deprecate decode tag number and value (#481)
* added or updated secure the BACnet primitive value decoders - the core codecs - named bacnet_x_decode(), bacnet_x_application_decode() and bacnet_x_context_decode where x is one of the 13 BACnet primitive value names. The updated API includes an APDU size to prevent over-reading of an APDU buffer while decoding. Improved or added unit test code coverage for the BACnet primitive value decoders. * marked the insecure decoding API as 'deprecated' which is defined in src/bacnet/basic/sys/platform.h and can be disabled during a build. * added secure decoders for BACnetTimeValue, BACnetHostNPort, BACnetTimeStamp, BACnetAddress, and Weekly_Schedule and improved unit test code coverage. * improved test code coverage for BACnet objects and properties. * secured AtomicReadFile and AtomicWriteFile service decoders and improved unit test code coverage. * secured BACnet Error service decoder and improved unit test code coverage. --------- Co-authored-by: Steve Karg <skarg@users.sourceforge.net>
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
# 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-Z_/-]*$"
|
||||
"/src"
|
||||
SRC_DIR
|
||||
${CMAKE_CURRENT_SOURCE_DIR})
|
||||
string(REGEX REPLACE
|
||||
"/test/bacnet/[a-zA-Z_/-]*$"
|
||||
"/test"
|
||||
TST_DIR
|
||||
${CMAKE_CURRENT_SOURCE_DIR})
|
||||
set(ZTST_DIR "${TST_DIR}/ztest/src")
|
||||
|
||||
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/hostnport.c
|
||||
# Support files and stubs (pathname alphabetical)
|
||||
${SRC_DIR}/bacnet/bacaddr.c
|
||||
${SRC_DIR}/bacnet/bacapp.c
|
||||
${SRC_DIR}/bacnet/bacdcode.c
|
||||
${SRC_DIR}/bacnet/bacdest.c
|
||||
${SRC_DIR}/bacnet/bacdevobjpropref.c
|
||||
${SRC_DIR}/bacnet/bacerror.c
|
||||
${SRC_DIR}/bacnet/bacint.c
|
||||
${SRC_DIR}/bacnet/bacreal.c
|
||||
${SRC_DIR}/bacnet/bacstr.c
|
||||
${SRC_DIR}/bacnet/bactext.c
|
||||
${SRC_DIR}/bacnet/basic/sys/bigend.c
|
||||
${SRC_DIR}/bacnet/datetime.c
|
||||
${SRC_DIR}/bacnet/basic/sys/days.c
|
||||
${SRC_DIR}/bacnet/indtext.c
|
||||
${SRC_DIR}/bacnet/hostnport.c
|
||||
${SRC_DIR}/bacnet/lighting.c
|
||||
${SRC_DIR}/bacnet/timestamp.c
|
||||
${SRC_DIR}/bacnet/memcopy.c
|
||||
${SRC_DIR}/bacnet/weeklyschedule.c
|
||||
${SRC_DIR}/bacnet/bactimevalue.c
|
||||
${SRC_DIR}/bacnet/dailyschedule.c
|
||||
# Test and test library files
|
||||
./src/main.c
|
||||
${ZTST_DIR}/ztest_mock.c
|
||||
${ZTST_DIR}/ztest.c
|
||||
)
|
||||
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Unit test for BACnetHostNPort encode and decode API
|
||||
* @author Steve Karg <skarg@users.sourceforge.net>
|
||||
* @date August 2023
|
||||
* @section LICENSE
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
#include <zephyr/ztest.h>
|
||||
#include <bacnet/bacdcode.h>
|
||||
#include <bacnet/bacdest.h>
|
||||
#include <bacnet/hostnport.h>
|
||||
|
||||
static void test_HostNPortCodec(BACNET_HOST_N_PORT *data)
|
||||
{
|
||||
uint8_t apdu[MAX_APDU] = { 0 };
|
||||
BACNET_HOST_N_PORT test_data = { 0 };
|
||||
BACNET_ERROR_CODE error_code = ERROR_CODE_SUCCESS;
|
||||
int len = 0, apdu_len = 0, null_len = 0, test_len = 0;
|
||||
uint8_t tag_number = 0;
|
||||
bool status = false;
|
||||
|
||||
null_len = host_n_port_encode(NULL, data);
|
||||
apdu_len = host_n_port_encode(apdu, data);
|
||||
zassert_equal(apdu_len, null_len, NULL);
|
||||
zassert_true(apdu_len != BACNET_STATUS_ERROR, NULL);
|
||||
null_len = host_n_port_decode(apdu, apdu_len, NULL, NULL);
|
||||
test_len = host_n_port_decode(apdu, apdu_len, &error_code, &test_data);
|
||||
zassert_equal(test_len, null_len, NULL);
|
||||
zassert_equal(
|
||||
apdu_len, test_len, "apdu_len=%d test_len=%d", apdu_len, test_len);
|
||||
while (test_len) {
|
||||
test_len--;
|
||||
len = host_n_port_decode(apdu, test_len, NULL, NULL);
|
||||
zassert_true(len < 0, "len=%d test_len=%d", len, test_len);
|
||||
}
|
||||
|
||||
null_len = host_n_port_context_encode(NULL, tag_number, data);
|
||||
apdu_len = host_n_port_context_encode(apdu, tag_number, data);
|
||||
zassert_equal(apdu_len, null_len, NULL);
|
||||
zassert_true(apdu_len != BACNET_STATUS_ERROR, NULL);
|
||||
|
||||
status = bacnet_is_opening_tag_number(apdu, apdu_len, tag_number, &len);
|
||||
zassert_true(status, NULL);
|
||||
zassert_true(len > 0, "len=%d", len);
|
||||
|
||||
null_len = host_n_port_decode(&apdu[len], apdu_len-len, NULL, NULL);
|
||||
test_len = host_n_port_decode(&apdu[len], apdu_len-len, &error_code, &test_data);
|
||||
zassert_equal(test_len, null_len, NULL);
|
||||
zassert_true(test_len > 0, "test_len=%d", len);
|
||||
len += test_len;
|
||||
|
||||
status = bacnet_is_closing_tag_number(&apdu[len], apdu_len-len, tag_number, &len);
|
||||
zassert_true(status, NULL);
|
||||
zassert_true(len > 0, "len=%d", len);
|
||||
|
||||
status = host_n_port_copy(&test_data, data);
|
||||
zassert_true(status, NULL);
|
||||
status = host_n_port_same(&test_data, data);
|
||||
zassert_true(status, NULL);
|
||||
}
|
||||
|
||||
#if defined(CONFIG_ZTEST_NEW_API)
|
||||
ZTEST(create_object_tests, test_HostNPort)
|
||||
#else
|
||||
static void test_HostNPort(void)
|
||||
#endif
|
||||
{
|
||||
BACNET_HOST_N_PORT data = { 0 };
|
||||
int len = 0, apdu_len = 0, null_len = 0, test_len = 0;
|
||||
|
||||
/* none */
|
||||
test_HostNPortCodec(&data);
|
||||
/* IP Address */
|
||||
octetstring_init_ascii_hex(&data.host.ip_address, "c0a80101");
|
||||
data.host_ip_address = true;
|
||||
data.host_name = false;
|
||||
data.port = 0xBAC0;
|
||||
test_HostNPortCodec(&data);
|
||||
/* Host Name */
|
||||
characterstring_init_ansi(&data.host.name, "bacnet.org");
|
||||
data.host_ip_address = false;
|
||||
data.host_name = true;
|
||||
data.port = 0xBAC0;
|
||||
test_HostNPortCodec(&data);
|
||||
}
|
||||
|
||||
#if defined(CONFIG_ZTEST_NEW_API)
|
||||
ZTEST_SUITE(host_n_port_tests, NULL, NULL, NULL, NULL, NULL);
|
||||
#else
|
||||
void test_main(void)
|
||||
{
|
||||
ztest_test_suite(host_n_port_tests, ztest_unit_test(test_HostNPort));
|
||||
|
||||
ztest_run_test_suite(host_n_port_tests);
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user