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:
Steve Karg
2023-09-08 11:39:27 -05:00
committed by GitHub
parent bc8c261153
commit f641aacddb
67 changed files with 6103 additions and 3145 deletions
@@ -0,0 +1,61 @@
# 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/basic/object/iv.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/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/wp.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
)
+96
View File
@@ -0,0 +1,96 @@
/**
* @file
* @brief Unit test for object
* @author Steve Karg <skarg@users.sourceforge.net>
* @date July 2023
*
* SPDX-License-Identifier: MIT
*/
#include <zephyr/ztest.h>
#include <bacnet/basic/object/iv.h>
#include <bacnet/bactext.h>
/**
* @addtogroup bacnet_tests
* @{
*/
/**
* @brief Test
*/
static void testInteger_Value(void)
{
uint8_t apdu[MAX_APDU] = { 0 };
int len = 0, test_len = 0;
BACNET_READ_PROPERTY_DATA rpdata = { 0 };
BACNET_APPLICATION_DATA_VALUE value = {0};
const int *pRequired = NULL;
const int *pOptional = NULL;
const int *pProprietary = NULL;
unsigned count = 0;
bool status = false;
Integer_Value_Init();
count = Integer_Value_Count();
zassert_true(count > 0, NULL);
rpdata.application_data = &apdu[0];
rpdata.application_data_len = sizeof(apdu);
rpdata.object_type = OBJECT_INTEGER_VALUE;
rpdata.object_instance = Integer_Value_Index_To_Instance(0);
rpdata.array_index = BACNET_ARRAY_ALL;
status = Integer_Value_Valid_Instance(rpdata.object_instance);
zassert_true(status, NULL);
Integer_Value_Property_Lists(&pRequired, &pOptional, &pProprietary);
while ((*pRequired) >= 0) {
rpdata.object_property = *pRequired;
len = Integer_Value_Read_Property(&rpdata);
zassert_true(len >= 0, NULL);
if (len >= 0) {
test_len = bacapp_decode_known_property(rpdata.application_data,
len, &value, rpdata.object_type, rpdata.object_property);
if (len != test_len) {
printf("property '%s': failed to decode!\n",
bactext_property_name(rpdata.object_property));
}
if (rpdata.object_property == PROP_PRIORITY_ARRAY) {
/* FIXME: known fail to decode */
len = test_len;
}
zassert_equal(len, test_len, NULL);
} else {
printf("property '%s': failed to read!\n",
bactext_property_name(rpdata.object_property));
}
pRequired++;
}
while ((*pOptional) != -1) {
rpdata.object_property = *pOptional;
rpdata.array_index = BACNET_ARRAY_ALL;
len = Integer_Value_Read_Property(&rpdata);
zassert_not_equal(len, BACNET_STATUS_ERROR, NULL);
if (len > 0) {
test_len = bacapp_decode_application_data(rpdata.application_data,
(uint8_t)rpdata.application_data_len, &value);
if (len != test_len) {
printf("property '%s': failed to decode!\n",
bactext_property_name(rpdata.object_property));
}
zassert_true(test_len >= 0, NULL);
} else {
printf("property '%s': failed to read!\n",
bactext_property_name(rpdata.object_property));
}
pOptional++;
}
}
/**
* @}
*/
void test_main(void)
{
ztest_test_suite(piv_tests, ztest_unit_test(testInteger_Value));
ztest_run_test_suite(piv_tests);
}