diff --git a/src/bacnet/access_rule.c b/src/bacnet/access_rule.c index 1b0fc757..2a4010d7 100644 --- a/src/bacnet/access_rule.c +++ b/src/bacnet/access_rule.c @@ -197,37 +197,62 @@ int bacnet_access_rule_decode( /** * @brief Decode the BACnetAccessRule * @param apdu Pointer to the buffer for decoding. - * @param rule Pointer to the data to be stored + * @param data Pointer to the data to be stored * @return number of bytes decoded * @deprecated Use bacapp_access_rule_decode() instead */ -int bacapp_decode_access_rule(const uint8_t *apdu, BACNET_ACCESS_RULE *rule) +int bacapp_decode_access_rule(const uint8_t *apdu, BACNET_ACCESS_RULE *data) { - return bacnet_access_rule_decode(apdu, MAX_APDU, rule); + return bacnet_access_rule_decode(apdu, MAX_APDU, data); } -int bacapp_decode_context_access_rule( - const uint8_t *apdu, uint8_t tag_number, BACNET_ACCESS_RULE *rule) +/** + * @brief Decode the BACnetAccessRule as Context Tagged + * @param apdu Pointer to the buffer for decoding. + * @param apdu_size The size of the buffer for decoding. + * @param tag_number Tag number + * @param data Pointer to the data to be stored + * @return number of bytes decoded or BACNET_STATUS_ERROR on error + */ +int bacnet_access_rule_context_decode( + const uint8_t *apdu, + size_t apdu_size, + uint8_t tag_number, + BACNET_ACCESS_RULE *data) { int len = 0; - int section_length; + int apdu_len = 0; - if (decode_is_opening_tag_number(&apdu[len], tag_number)) { - len++; - section_length = bacapp_decode_access_rule(&apdu[len], rule); - - if (section_length == -1) { - len = -1; - } else { - len += section_length; - if (decode_is_closing_tag_number(&apdu[len], tag_number)) { - len++; - } else { - len = -1; - } - } - } else { - len = -1; + if (!bacnet_is_opening_tag_number( + &apdu[apdu_len], apdu_size - apdu_len, tag_number, &len)) { + return BACNET_STATUS_ERROR; } - return len; + apdu_len += len; + len = + bacnet_access_rule_decode(&apdu[apdu_len], apdu_size - apdu_len, data); + if (len <= 0) { + return BACNET_STATUS_ERROR; + } + apdu_len += len; + if (!bacnet_is_closing_tag_number( + &apdu[apdu_len], apdu_size - apdu_len, tag_number, &len)) { + return BACNET_STATUS_ERROR; + } + apdu_len += len; + + return apdu_len; +} + +/** + * @brief Decode the BACnetAccessRule as Context Tagged + * @param apdu Pointer to the buffer for decoding. + * @param tag_number Tag number + * @param data Pointer to the data to be stored + * @return number of bytes decoded or BACNET_STATUS_ERROR on error + * @deprecated Use bacnet_access_rule_context_decode() instead + */ +int bacapp_decode_context_access_rule( + const uint8_t *apdu, uint8_t tag_number, BACNET_ACCESS_RULE *data) +{ + return bacnet_access_rule_context_decode(apdu, MAX_APDU, tag_number, data); } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 00a411eb..bc51b4d1 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -70,6 +70,7 @@ list(APPEND testdirs list(APPEND testdirs bacnet/abort + bacnet/access_rule bacnet/alarm_ack bacnet/arf bacnet/awf diff --git a/test/bacnet/access_rule/CMakeLists.txt b/test/bacnet/access_rule/CMakeLists.txt new file mode 100644 index 00000000..550f185c --- /dev/null +++ b/test/bacnet/access_rule/CMakeLists.txt @@ -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 + BACAPP_DEVICE_OBJECT_PROPERTY_REFERENCE=1 + BACAPP_DEVICE_OBJECT_REFERENCE=1 + BACAPP_OBJECT_PROPERTY_REFERENCE=1 +) + +include_directories( + ${SRC_DIR} + ${TST_DIR}/ztest/include + ) + +add_executable(${PROJECT_NAME} + # File(s) under test + ${SRC_DIR}/bacnet/access_rule.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/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/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 + # Test and test library files + ./src/main.c + ${ZTST_DIR}/ztest_mock.c + ${ZTST_DIR}/ztest.c + ) diff --git a/test/bacnet/access_rule/src/main.c b/test/bacnet/access_rule/src/main.c new file mode 100644 index 00000000..cb72c585 --- /dev/null +++ b/test/bacnet/access_rule/src/main.c @@ -0,0 +1,153 @@ +/** + * @file + * @brief Unit test for BACnetAccessRule encode and decode API + * @author Steve Karg + * @date September 2024 + * @copyright SPDX-License-Identifier: MIT + */ +#include +#include +#include +#include + +/** + * @addtogroup bacnet_tests + * @{ + */ + +/** + * @brief Test + */ +static void test_access_rule_positive(BACNET_ACCESS_RULE *data) +{ + BACNET_ACCESS_RULE test_data = { 0 }; + uint8_t apdu[MAX_APDU]; + uint8_t tag_number = 1; + int len; + int test_len; + int null_len; + bool status; + + null_len = bacapp_encode_access_rule(NULL, data); + len = bacapp_encode_access_rule(apdu, data); + zassert_equal(null_len, len, NULL); + test_len = bacnet_access_rule_decode(apdu, len, &test_data); + zassert_equal(test_len, len, NULL); + zassert_equal( + data->time_range_specifier, test_data.time_range_specifier, NULL); + zassert_equal(data->location_specifier, test_data.location_specifier, NULL); + zassert_equal(data->enable, test_data.enable, NULL); + if (data->time_range_specifier == TIME_RANGE_SPECIFIER_SPECIFIED) { + status = bacnet_device_object_property_reference_same( + &data->time_range, &test_data.time_range); + zassert_true(status, NULL); + } + if (data->location_specifier == LOCATION_SPECIFIER_SPECIFIED) { + status = bacnet_device_object_reference_same( + &data->location, &test_data.location); + zassert_true(status, NULL); + } + while (--len) { + test_len = bacnet_access_rule_decode(apdu, len, &test_data); + zassert_true(test_len <= 0, NULL); + } + + /* context tagged */ + null_len = bacapp_encode_context_access_rule(NULL, tag_number, data); + len = bacapp_encode_context_access_rule(apdu, tag_number, data); + zassert_equal(null_len, len, NULL); + test_len = + bacnet_access_rule_context_decode(apdu, len, tag_number, &test_data); + zassert_equal(test_len, len, NULL); + zassert_equal( + data->time_range_specifier, test_data.time_range_specifier, NULL); + zassert_equal(data->location_specifier, test_data.location_specifier, NULL); + zassert_equal(data->enable, test_data.enable, NULL); + if (data->time_range_specifier == TIME_RANGE_SPECIFIER_SPECIFIED) { + status = bacnet_device_object_property_reference_same( + &data->time_range, &test_data.time_range); + zassert_true(status, NULL); + } + if (data->location_specifier == LOCATION_SPECIFIER_SPECIFIED) { + status = bacnet_device_object_reference_same( + &data->location, &test_data.location); + zassert_true(status, NULL); + } + while (--len) { + test_len = bacnet_access_rule_context_decode( + apdu, len, tag_number, &test_data); + zassert_true(test_len <= 0, NULL); + } +} + +/** + * @brief Test + */ +#if defined(CONFIG_ZTEST_NEW_API) +ZTEST(access_rule_tests, test_access_rule) +#else +static void test_access_rule(void) +#endif +{ + BACNET_ACCESS_RULE data = { 0 }; + + data.enable = true; + data.time_range_specifier = TIME_RANGE_SPECIFIER_ALWAYS; + data.location_specifier = LOCATION_SPECIFIER_ALL; + test_access_rule_positive(&data); + + data.enable = false; + data.time_range_specifier = TIME_RANGE_SPECIFIER_ALWAYS; + data.location_specifier = LOCATION_SPECIFIER_ALL; + test_access_rule_positive(&data); + + data.enable = true; + data.time_range_specifier = TIME_RANGE_SPECIFIER_SPECIFIED; + data.time_range.arrayIndex = 1; + data.time_range.objectIdentifier.type = OBJECT_ANALOG_INPUT; + data.time_range.objectIdentifier.instance = 1; + data.time_range.propertyIdentifier = PROP_PRESENT_VALUE; + data.time_range.deviceIdentifier.type = OBJECT_DEVICE; + data.time_range.deviceIdentifier.instance = 1; + data.location_specifier = LOCATION_SPECIFIER_ALL; + test_access_rule_positive(&data); + + data.enable = true; + data.time_range_specifier = TIME_RANGE_SPECIFIER_ALWAYS; + data.location_specifier = LOCATION_SPECIFIER_SPECIFIED; + data.location.objectIdentifier.type = OBJECT_ANALOG_INPUT; + data.location.objectIdentifier.instance = 1; + data.location.deviceIdentifier.type = OBJECT_DEVICE; + data.location.deviceIdentifier.instance = 1; + test_access_rule_positive(&data); + + data.enable = true; + data.time_range_specifier = TIME_RANGE_SPECIFIER_SPECIFIED; + data.time_range.arrayIndex = 1; + data.time_range.objectIdentifier.type = OBJECT_ANALOG_INPUT; + data.time_range.objectIdentifier.instance = 1; + data.time_range.propertyIdentifier = PROP_PRESENT_VALUE; + data.time_range.deviceIdentifier.type = OBJECT_DEVICE; + data.time_range.deviceIdentifier.instance = 1; + data.location_specifier = LOCATION_SPECIFIER_SPECIFIED; + data.location.objectIdentifier.type = OBJECT_ANALOG_INPUT; + data.location.objectIdentifier.instance = 1; + data.location.deviceIdentifier.type = OBJECT_DEVICE; + data.location.deviceIdentifier.instance = 1; + test_access_rule_positive(&data); +} + +/** + * @} + */ + +#if defined(CONFIG_ZTEST_NEW_API) +ZTEST_SUITE(access_rule_tests, NULL, NULL, NULL, NULL, NULL); +#else +void test_main(void) +{ + ztest_test_suite(access_rule_tests, ztest_unit_test(test_access_rule)); + + ztest_run_test_suite(access_rule_tests); +} +#endif