f641aacddb
* 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>
143 lines
4.3 KiB
C
143 lines
4.3 KiB
C
/*
|
|
* Copyright (c) 2020 Legrand North America, LLC.
|
|
*
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
/* @file
|
|
* @brief test BACnet integer encode/decode APIs
|
|
*/
|
|
|
|
#include <zephyr/ztest.h>
|
|
#include <bacnet/basic/object/device.h>
|
|
#include <bacnet/bactext.h>
|
|
|
|
/**
|
|
* @addtogroup bacnet_tests
|
|
* @{
|
|
*/
|
|
|
|
/**
|
|
* @brief Test ReadProperty API
|
|
*/
|
|
static void test_Device_ReadProperty(void)
|
|
{
|
|
uint8_t apdu[MAX_APDU] = { 0 };
|
|
int len = 0;
|
|
int test_len = 0;
|
|
BACNET_READ_PROPERTY_DATA rpdata;
|
|
/* for decode value data */
|
|
BACNET_APPLICATION_DATA_VALUE value;
|
|
const int *pRequired = NULL;
|
|
const int *pOptional = NULL;
|
|
const int *pProprietary = NULL;
|
|
unsigned count = 0;
|
|
|
|
Device_Init(NULL);
|
|
count = Device_Count();
|
|
zassert_true(count > 0, NULL);
|
|
rpdata.application_data = &apdu[0];
|
|
rpdata.application_data_len = sizeof(apdu);
|
|
rpdata.object_type = OBJECT_DEVICE;
|
|
rpdata.object_instance = Device_Index_To_Instance(0);;
|
|
Device_Property_Lists(&pRequired, &pOptional, &pProprietary);
|
|
while ((*pRequired) != -1) {
|
|
rpdata.object_property = *pRequired;
|
|
rpdata.array_index = BACNET_ARRAY_ALL;
|
|
len = Device_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));
|
|
}
|
|
if (rpdata.object_property == PROP_PRIORITY_ARRAY) {
|
|
/* FIXME: known fail to decode */
|
|
len = test_len;
|
|
}
|
|
zassert_true(test_len >= 0, 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 = Device_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++;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Test basic API
|
|
*/
|
|
#if defined(CONFIG_ZTEST_NEW_API)
|
|
ZTEST(device_tests, testDevice)
|
|
#else
|
|
static void testDevice(void)
|
|
#endif
|
|
{
|
|
bool status = false;
|
|
const char *name = "Patricia";
|
|
|
|
status = Device_Set_Object_Instance_Number(0);
|
|
zassert_equal(Device_Object_Instance_Number(), 0, NULL);
|
|
zassert_true(status, NULL);
|
|
status = Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE);
|
|
zassert_equal(Device_Object_Instance_Number(), BACNET_MAX_INSTANCE, NULL);
|
|
zassert_true(status, NULL);
|
|
status = Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE / 2);
|
|
zassert_equal(
|
|
Device_Object_Instance_Number(), (BACNET_MAX_INSTANCE / 2), NULL);
|
|
zassert_true(status, NULL);
|
|
status = Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE + 1);
|
|
zassert_not_equal(
|
|
Device_Object_Instance_Number(), (BACNET_MAX_INSTANCE + 1), NULL);
|
|
zassert_false(status, NULL);
|
|
|
|
Device_Set_System_Status(STATUS_NON_OPERATIONAL, true);
|
|
zassert_equal(Device_System_Status(), STATUS_NON_OPERATIONAL, NULL);
|
|
|
|
zassert_equal(Device_Vendor_Identifier(), BACNET_VENDOR_ID, NULL);
|
|
|
|
Device_Set_Model_Name(name, strlen(name));
|
|
zassert_equal(strcmp(Device_Model_Name(), name), 0, NULL);
|
|
|
|
return;
|
|
}
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
|
|
#if defined(CONFIG_ZTEST_NEW_API)
|
|
ZTEST_SUITE(device_tests, NULL, NULL, NULL, NULL, NULL);
|
|
#else
|
|
void test_main(void)
|
|
{
|
|
ztest_test_suite(device_tests,
|
|
ztest_unit_test(testDevice),
|
|
ztest_unit_test(test_Device_ReadProperty)
|
|
);
|
|
|
|
ztest_run_test_suite(device_tests);
|
|
}
|
|
#endif
|