Secured BACnetAuthenticationFactorFormat decoder and added unit testing (#1127)

This commit is contained in:
Steve Karg
2025-11-06 14:08:38 -06:00
committed by GitHub
parent 35f3964b5a
commit 2acde643fd
6 changed files with 382 additions and 81 deletions
+1
View File
@@ -75,6 +75,7 @@ list(APPEND testdirs
bacnet/access_rule
bacnet/alarm_ack
bacnet/arf
bacnet/authentication_factor_format
bacnet/awf
bacnet/bacaddr
bacnet/bacapp
@@ -0,0 +1,68 @@
# 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
MAX_APDU=50
BACAPP_MINIMAL=1
)
include_directories(
${SRC_DIR}
${TST_DIR}/ztest/include
)
add_executable(${PROJECT_NAME}
# File(s) under test
${SRC_DIR}/bacnet/authentication_factor_format.c
# Support files and stubs (pathname alphabetical)
${SRC_DIR}/bacnet/bacaction.c
${SRC_DIR}/bacnet/bacaddr.c
${SRC_DIR}/bacnet/bacapp.c
${SRC_DIR}/bacnet/bacdcode.c
${SRC_DIR}/bacnet/bacdest.c
${SRC_DIR}/bacnet/bacint.c
${SRC_DIR}/bacnet/baclog.c
${SRC_DIR}/bacnet/bacreal.c
${SRC_DIR}/bacnet/bacstr.c
${SRC_DIR}/bacnet/bactext.c
${SRC_DIR}/bacnet/bacdevobjpropref.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/timer_value.c
${SRC_DIR}/bacnet/timestamp.c
${SRC_DIR}/bacnet/weeklyschedule.c
${SRC_DIR}/bacnet/bactimevalue.c
${SRC_DIR}/bacnet/dailyschedule.c
${SRC_DIR}/bacnet/calendar_entry.c
${SRC_DIR}/bacnet/special_event.c
${SRC_DIR}/bacnet/channel_value.c
# Test and test library files
./src/main.c
${ZTST_DIR}/ztest_mock.c
${ZTST_DIR}/ztest.c
)
@@ -0,0 +1,133 @@
/**
* @file
* @brief Unit test for BACnetAccessRule encode and decode API
* @author Steve Karg <skarg@users.sourceforge.net>
* @date September 2024
* @copyright SPDX-License-Identifier: MIT
*/
#include <zephyr/ztest.h>
#include <bacnet/authentication_factor_format.h>
#include <bacnet/bacdcode.h>
/**
* @addtogroup bacnet_tests
* @{
*/
/**
* @brief Test
*/
static void test_authentication_factor_format_context(
uint8_t tag, BACNET_AUTHENTICATION_FACTOR_FORMAT *data)
{
BACNET_AUTHENTICATION_FACTOR_FORMAT test_data = { 0 };
uint8_t apdu[MAX_APDU];
int apdu_len;
int test_len;
int null_len;
null_len =
bacapp_encode_context_authentication_factor_format(NULL, tag, data);
apdu_len =
bacapp_encode_context_authentication_factor_format(apdu, tag, data);
zassert_equal(null_len, apdu_len, NULL);
test_len = bacnet_authentication_factor_format_context_decode(
apdu, apdu_len, tag, &test_data);
zassert_equal(
test_len, apdu_len, "test_len=%d apdu_len=%d", test_len, apdu_len);
zassert_equal(data->format_type, test_data.format_type, "format_type");
if (data->format_type == AUTHENTICATION_FACTOR_CUSTOM) {
zassert_equal(
data->vendor_format, test_data.vendor_format, "vendor_format");
zassert_equal(data->vendor_id, test_data.vendor_id, "vendor_id");
}
/* test the deprecated function */
test_len = bacapp_decode_context_authentication_factor_format(
apdu, tag, &test_data);
zassert_equal(
test_len, apdu_len, "test_len=%d apdu_len=%d", test_len, apdu_len);
/* shrink the buffer, get some errors */
while (apdu_len) {
apdu_len--;
test_len = bacnet_authentication_factor_format_context_decode(
apdu, apdu_len, tag, &test_data);
zassert_true(
test_len < 0, "test_len=%d apdu_len=%d", test_len, apdu_len);
}
}
static void test_authentication_factor_format_positive(
BACNET_AUTHENTICATION_FACTOR_FORMAT *data)
{
BACNET_AUTHENTICATION_FACTOR_FORMAT test_data = { 0 };
uint8_t apdu[MAX_APDU];
int apdu_len;
int test_len;
int null_len;
null_len = bacapp_encode_authentication_factor_format(NULL, data);
apdu_len = bacapp_encode_authentication_factor_format(apdu, data);
zassert_equal(null_len, apdu_len, NULL);
test_len =
bacnet_authentication_factor_format_decode(apdu, apdu_len, &test_data);
zassert_equal(
test_len, apdu_len, "test_len=%d apdu_len=%d", test_len, apdu_len);
zassert_equal(data->format_type, test_data.format_type, "format_type");
if (data->format_type == AUTHENTICATION_FACTOR_CUSTOM) {
zassert_equal(
data->vendor_format, test_data.vendor_format, "vendor_format");
zassert_equal(data->vendor_id, test_data.vendor_id, "vendor_id");
}
/* test the deprecated function */
test_len = bacapp_decode_authentication_factor_format(apdu, &test_data);
zassert_equal(
test_len, apdu_len, "test_len=%d apdu_len=%d", test_len, apdu_len);
/* shrink the buffer, get some errors */
while (apdu_len) {
apdu_len--;
test_len = bacnet_authentication_factor_format_decode(
apdu, apdu_len, &test_data);
zassert_true(
test_len < 0, "test_len=%d apdu_len=%d", test_len, apdu_len);
}
}
/**
* @brief Test
*/
#if defined(CONFIG_ZTEST_NEW_API)
ZTEST(authentication_factor_format_tests, test_authentication_factor_format)
#else
static void test_authentication_factor_format(void)
#endif
{
BACNET_AUTHENTICATION_FACTOR_FORMAT data = { 0 };
data.format_type = AUTHENTICATION_FACTOR_CUSTOM;
data.vendor_id = 1;
data.vendor_format = 2;
test_authentication_factor_format_positive(&data);
test_authentication_factor_format_context(1, &data);
data.format_type = AUTHENTICATION_FACTOR_UNDEFINED;
data.vendor_id = 1;
data.vendor_format = 2;
test_authentication_factor_format_positive(&data);
test_authentication_factor_format_context(1, &data);
}
/**
* @}
*/
#if defined(CONFIG_ZTEST_NEW_API)
ZTEST_SUITE(authentication_factor_format_tests, NULL, NULL, NULL, NULL, NULL);
#else
void test_main(void)
{
ztest_test_suite(
authentication_factor_format_tests,
ztest_unit_test(test_authentication_factor_format));
ztest_run_test_suite(authentication_factor_format_tests);
}
#endif