From d4d928a91518048a17fad6cc7ae7f599500b0171 Mon Sep 17 00:00:00 2001 From: Steve Karg Date: Wed, 15 Oct 2025 10:24:08 -0500 Subject: [PATCH] Added more unit testing for ReadProperty to increase code coverage. (#1118) --- src/bacnet/rp.c | 10 +--- test/bacnet/rp/src/main.c | 112 +++++++++++++++++++++++++++++++++++++- 2 files changed, 111 insertions(+), 11 deletions(-) diff --git a/src/bacnet/rp.c b/src/bacnet/rp.c index e5293a64..bcee486f 100644 --- a/src/bacnet/rp.c +++ b/src/bacnet/rp.c @@ -352,13 +352,7 @@ int rp_ack_encode_apdu_init( */ int rp_ack_encode_apdu_object_property_end(uint8_t *apdu) { - int apdu_len = 0; /* total length of the apdu, return value */ - - if (apdu) { - apdu_len = encode_closing_tag(&apdu[0], 3); - } - - return apdu_len; + return encode_closing_tag(apdu, 3); } /** @@ -415,7 +409,7 @@ int rp_ack_encode_apdu( if (apdu) { apdu += len; } - len = encode_closing_tag(apdu, 3); + len = rp_ack_encode_apdu_object_property_end(apdu); apdu_len += len; } diff --git a/test/bacnet/rp/src/main.c b/test/bacnet/rp/src/main.c index b659068e..2a410882 100644 --- a/test/bacnet/rp/src/main.c +++ b/test/bacnet/rp/src/main.c @@ -156,6 +156,50 @@ static void testReadPropertyAck(void) zassert_true( test_len < 0, "test_len=%d apdu_len=%d", test_len, apdu_len); } + /* optional parameters */ + rpdata.array_index = 0; + apdu_len = rp_ack_encode_apdu(&apdu[0], invoke_id, &rpdata); + zassert_not_equal(apdu_len, 0, NULL); + test_len = + rp_ack_decode_apdu(&apdu[0], apdu_len, &test_invoke_id, &test_data); + zassert_equal(apdu_len, test_len, NULL); + zassert_equal(test_data.array_index, rpdata.array_index, NULL); + /* alternate API */ + rpdata.array_index = BACNET_ARRAY_ALL; + null_len = read_property_ack_service_encode(&apdu[0], sizeof(apdu), NULL); + zassert_equal(null_len, 0, "null_len=%d", null_len); + null_len = read_property_ack_service_encode(NULL, sizeof(apdu), &rpdata); + apdu_len = + read_property_ack_service_encode(&apdu[0], sizeof(apdu), &rpdata); + zassert_equal( + apdu_len, null_len, "apdu_len=%d null_len=%d", apdu_len, null_len); + zassert_not_equal(apdu_len, 0, NULL); + test_len = rp_ack_decode_service_request(&apdu[0], apdu_len, &test_data); + zassert_equal( + test_len, apdu_len, "test_len=%d apdu_len=%d", test_len, apdu_len); + zassert_equal(test_data.object_type, rpdata.object_type, NULL); + zassert_equal(test_data.object_instance, rpdata.object_instance, NULL); + zassert_equal(test_data.object_property, rpdata.object_property, NULL); + zassert_equal(test_data.array_index, rpdata.array_index, NULL); + while (apdu_len) { + apdu_len--; + test_len = + read_property_ack_service_encode(&apdu[0], apdu_len, &rpdata); + zassert_equal( + test_len, 0, "test_len=%d apdu_len=%d", test_len, apdu_len); + } + /* optional parameters */ + rpdata.array_index = 0; + apdu_len = + read_property_ack_service_encode(&apdu[0], sizeof(apdu), &rpdata); + zassert_not_equal(apdu_len, 0, NULL); + test_len = rp_ack_decode_service_request(&apdu[0], apdu_len, &test_data); + zassert_equal( + test_len, apdu_len, "test_len=%d apdu_len=%d", test_len, apdu_len); + zassert_equal(test_data.object_type, rpdata.object_type, NULL); + zassert_equal(test_data.object_instance, rpdata.object_instance, NULL); + zassert_equal(test_data.object_property, rpdata.object_property, NULL); + zassert_equal(test_data.array_index, rpdata.array_index, NULL); } #if defined(CONFIG_ZTEST_NEW_API) @@ -168,8 +212,8 @@ static void testReadProperty(void) int apdu_len = 0, test_len = 0, null_len = 0; uint8_t invoke_id = 128; uint8_t test_invoke_id = 0; - BACNET_READ_PROPERTY_DATA rpdata; - BACNET_READ_PROPERTY_DATA test_data; + BACNET_READ_PROPERTY_DATA rpdata = { 0 }; + BACNET_READ_PROPERTY_DATA test_data = { 0 }; rpdata.object_type = OBJECT_DEVICE; rpdata.object_instance = 1; @@ -196,9 +240,70 @@ static void testReadProperty(void) zassert_true( test_len < 0, "test_len=%d apdu_len=%d", test_len, apdu_len); } + null_len = + read_property_request_service_encode(&apdu[0], sizeof(apdu), NULL); + zassert_equal(null_len, 0, "null_len=%d", null_len); + null_len = + read_property_request_service_encode(NULL, sizeof(apdu), &rpdata); + apdu_len = + read_property_request_service_encode(&apdu[0], sizeof(apdu), &rpdata); + zassert_equal( + apdu_len, null_len, "apdu_len=%d null_len=%d", apdu_len, null_len); + zassert_not_equal(apdu_len, 0, NULL); + test_len = rp_decode_service_request(&apdu[0], apdu_len, &test_data); + zassert_equal( + test_len, apdu_len, "test_len=%d apdu_len=%d", test_len, apdu_len); + zassert_equal(test_data.object_type, rpdata.object_type, NULL); + zassert_equal(test_data.object_instance, rpdata.object_instance, NULL); + zassert_equal(test_data.object_property, rpdata.object_property, NULL); + zassert_equal(test_data.array_index, rpdata.array_index, NULL); + null_len = + read_property_request_service_encode(&apdu[0], apdu_len - 1, &rpdata); + zassert_equal(null_len, 0, "null_len=%d apdu_len=%d", null_len, apdu_len); + while (apdu_len) { + apdu_len--; + test_len = rp_decode_service_request(&apdu[0], apdu_len, &test_data); + zassert_true( + test_len < 0, "test_len=%d apdu_len=%d", test_len, apdu_len); + } + /* ReadProperty options */ + rpdata.array_index = 0; + apdu_len = + read_property_request_service_encode(&apdu[0], sizeof(apdu), &rpdata); + zassert_not_equal(apdu_len, 0, NULL); + test_len = rp_decode_service_request(&apdu[0], apdu_len, &test_data); + zassert_equal( + test_len, apdu_len, "test_len=%d apdu_len=%d", test_len, apdu_len); + zassert_equal(test_data.object_type, rpdata.object_type, NULL); + zassert_equal(test_data.object_instance, rpdata.object_instance, NULL); + zassert_equal(test_data.object_property, rpdata.object_property, NULL); + zassert_equal(test_data.array_index, rpdata.array_index, NULL); + test_len = rp_decode_service_request(NULL, sizeof(apdu), &test_data); + zassert_true(test_len < 0, "test_len=%d", test_len); + test_len = rp_decode_service_request(apdu, sizeof(apdu), NULL); + zassert_true(test_len < 0, "test_len=%d", test_len); return; } + +#if defined(CONFIG_ZTEST_NEW_API) +ZTEST(rp_tests, testReadPropertyArray) +#else +static void testReadPropertyArray(void) +#endif +{ + bool status; + BACNET_READ_PROPERTY_DATA data = { 0 }; + + data.object_type = OBJECT_ANALOG_VALUE; + data.object_property = PROP_PRESENT_VALUE; + data.array_index = 0; + status = read_property_bacnet_array_valid(&data); + zassert_false(status, NULL); + data.array_index = BACNET_ARRAY_ALL; + status = read_property_bacnet_array_valid(&data); + zassert_true(status, NULL); +} /** * @} */ @@ -210,7 +315,8 @@ void test_main(void) { ztest_test_suite( rp_tests, ztest_unit_test(testReadProperty), - ztest_unit_test(testReadPropertyAck)); + ztest_unit_test(testReadPropertyAck), + ztest_unit_test(testReadPropertyArray)); ztest_run_test_suite(rp_tests); }