Update address in VMAC table for IPv6 (#429)

* Update address in VMAC table for IPv6

* fix compile warnings in IPv6 handler

* cleanup BIP6 PRINTF

* enable unit test for BBMD6 hander

* keylist should only return FALSE when growing fails

---------

Co-authored-by: Steve Karg <skarg@users.sourceforge.net>
This commit is contained in:
Steve Karg
2023-05-03 23:33:06 -05:00
committed by GitHub
parent ced9dff0f2
commit 65792ab408
6 changed files with 182 additions and 152 deletions
+1 -1
View File
@@ -81,8 +81,8 @@ list(APPEND testdirs
# bacnet/basic/*
list(APPEND testdirs
# basic/object/binding
bacnet/basic/binding/address
bacnet/basic/bbmd6
# basic/object
bacnet/basic/object/acc
bacnet/basic/object/access_credential
+48
View File
@@ -0,0 +1,48 @@
# 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-Z0-9_/-]*$"
"/src"
SRC_DIR
${CMAKE_CURRENT_SOURCE_DIR})
string(REGEX REPLACE
"/test/bacnet/[a-zA-Z0-9_/-]*$"
"/test"
TST_DIR
${CMAKE_CURRENT_SOURCE_DIR})
add_compile_definitions(
BIG_ENDIAN=0
)
include_directories(
${SRC_DIR}
)
add_executable(${PROJECT_NAME}
# File(s) under test
${SRC_DIR}/bacnet/basic/bbmd6/h_bbmd6.c
${SRC_DIR}/bacnet/basic/bbmd6/vmac.c
# Support files and stubs (pathname alphabetical)
${SRC_DIR}/bacnet/bacdcode.c
${SRC_DIR}/bacnet/bacint.c
${SRC_DIR}/bacnet/bacstr.c
${SRC_DIR}/bacnet/bacreal.c
${SRC_DIR}/bacnet/iam.c
${SRC_DIR}/bacnet/npdu.c
${SRC_DIR}/bacnet/basic/sys/bigend.c
${SRC_DIR}/bacnet/basic/sys/keylist.c
${SRC_DIR}/bacnet/basic/bbmd6/h_bbmd6.c
${SRC_DIR}/bacnet/basic/bbmd6/vmac.c
${SRC_DIR}/bacnet/datalink/bvlc6.c
# Test and test library files
./src/main.c
)
-52
View File
@@ -1,52 +0,0 @@
#Makefile to build test case
CC = gcc
SRC_DIR = ../../../../src
TEST_DIR = ../../..
INCLUDES = -I$(SRC_DIR) -I$(TEST_DIR)
DEFINES = -DBIG_ENDIAN=0 -DDEBUG_ENABLED=0
CFLAGS = -Wall $(INCLUDES) $(DEFINES) -g
SRCS = main.c \
$(SRC_DIR)/bacnet/bacdcode.c \
$(SRC_DIR)/bacnet/bacint.c \
$(SRC_DIR)/bacnet/bacstr.c \
$(SRC_DIR)/bacnet/bacreal.c \
$(SRC_DIR)/bacnet/iam.c \
$(SRC_DIR)/bacnet/npdu.c \
$(SRC_DIR)/bacnet/basic/sys/keylist.c \
$(SRC_DIR)/bacnet/basic/bbmd6/h_bbmd6.c \
$(SRC_DIR)/bacnet/basic/bbmd6/vmac.c \
$(SRC_DIR)/bacnet/datalink/bvlc6.c \
$(SRC_DIR)/bacnet/basic/sys/debug.c \
$(TEST_DIR)/ctest.c
TARGET_NAME = unittest
ifeq ($(OS),Windows_NT)
TARGET_EXT = .exe
else
TARGET_EXT =
endif
TARGET = $(TARGET_NAME)$(TARGET_EXT)
all: ${TARGET}
OBJS = ${SRCS:.c=.o}
${TARGET}: ${OBJS}
${CC} -o $@ ${OBJS}
.c.o:
${CC} -c ${CFLAGS} $*.c -o $@
depend:
rm -f .depend
${CC} -MM ${CFLAGS} *.c >> .depend
clean:
rm -rf ${TARGET} $(OBJS)
test: ${TARGET}
./${TARGET}
include: .depend
@@ -4,26 +4,7 @@
* @date April 2020
* @brief Test file for a basic BBMD for BVLC IPv6 handler
*
* @section LICENSE
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* SPDX-License-Identifier: MIT
*/
#include <stdio.h> /* for standard i/o, like printing */
#include <stdint.h> /* for standard integer types uint8_t etc. */
@@ -40,7 +21,6 @@
#include "bacnet/basic/object/device.h"
#include "bacnet/basic/bbmd6/h_bbmd6.h"
#include "bacnet/basic/bbmd6/vmac.h"
#include "ctest.h"
struct device_info_t {
uint32_t Device_ID;
@@ -51,6 +31,10 @@ struct device_info_t {
static struct device_info_t TD;
static struct device_info_t IUT;
#ifndef MAX_MPDU
#define MAX_MPDU 1497
#endif
/* for the reply sent from the handler */
static uint8_t Test_Sent_Message_Type;
static uint8_t Test_Sent_Message_Length;
@@ -171,7 +155,7 @@ static void test_cleanup(void)
/**
* @brief Test 15.2.1.1 Initiate Original-Broadcast-NPDU
*/
static void test_Initiate_Original_Broadcast_NPDU(Test *pTest)
static void test_Initiate_Original_Broadcast_NPDU(void)
{
uint8_t pdu[MAX_MPDU] = {0};
int npdu_len = 0;
@@ -194,22 +178,22 @@ static void test_Initiate_Original_Broadcast_NPDU(Test *pTest)
pdu_len = npdu_len + apdu_len;
bvlc6_send_pdu(&dest, &npdu_data, pdu, pdu_len);
/* DA=Link Local Multicast Address */
ct_test(pTest, !bvlc6_address_different(&TD.BIP6_Broadcast_Addr,
assert(!bvlc6_address_different(&TD.BIP6_Broadcast_Addr,
&Test_Sent_Message_Dest));
/* SA = IUT - done in port layer */
/* Original-Broadcast-NPDU */
ct_test(pTest, Test_Sent_Message_Type ==
assert(Test_Sent_Message_Type ==
BVLC6_ORIGINAL_BROADCAST_NPDU);
if (Test_Sent_Message_Type == BVLC6_ORIGINAL_BROADCAST_NPDU) {
function_len = bvlc6_decode_original_broadcast(
Test_Sent_Message_Buffer, Test_Sent_Message_Buffer_Length,
&test_vmac_src, test_pdu, sizeof(test_pdu), &test_pdu_len);
ct_test(pTest, function_len > 0);
assert(function_len > 0);
/* Source-Virtual-Address = IUT */
ct_test(pTest, test_vmac_src == IUT.Device_ID);
assert(test_vmac_src == IUT.Device_ID);
/* (any valid BACnet-Unconfirmed-Request-PDU,
with any valid broadcast network options */
ct_test(pTest, test_pdu_len == pdu_len);
assert(test_pdu_len == pdu_len);
}
test_cleanup();
}
@@ -217,12 +201,13 @@ static void test_Initiate_Original_Broadcast_NPDU(Test *pTest)
/**
* @brief Test 15.1.2 Execute Virtual-Address-Resolution
*/
static void test_Execute_Virtual_Address_Resolution(Test *pTest)
static void test_Execute_Virtual_Address_Resolution(void)
{
uint8_t mtu[MAX_MPDU] = {0};
uint16_t mtu_len = 0;
uint32_t test_vmac_src = 0;
uint32_t test_vmac_dst = 0;
uint32_t old_device_id = 0;
int result = 0;
int function_len = 0;
@@ -231,24 +216,59 @@ static void test_Execute_Virtual_Address_Resolution(Test *pTest)
&mtu[0], sizeof(mtu), TD.Device_ID);
result = bvlc6_bbmd_disabled_handler(&TD.BIP6_Addr, &TD.BACnet_Address,
&mtu[0], mtu_len);
ct_test(pTest, result == 0);
ct_test(pTest, bvlc6_get_function_code() ==
assert(result == 0);
assert(bvlc6_get_function_code() ==
BVLC6_VIRTUAL_ADDRESS_RESOLUTION);
ct_test(pTest, Test_Sent_Message_Type ==
assert(Test_Sent_Message_Type ==
BVLC6_VIRTUAL_ADDRESS_RESOLUTION_ACK);
ct_test(pTest, VMAC_Find_By_Key(TD.Device_ID) != NULL);
assert(VMAC_Find_By_Key(TD.Device_ID) != NULL);
if (Test_Sent_Message_Type == BVLC6_VIRTUAL_ADDRESS_RESOLUTION_ACK) {
function_len = bvlc6_decode_virtual_address_resolution_ack(
Test_Sent_Message_Buffer, Test_Sent_Message_Buffer_Length,
&test_vmac_src, &test_vmac_dst);
ct_test(pTest, function_len > 0);
ct_test(pTest, test_vmac_src == IUT.Device_ID);
ct_test(pTest, test_vmac_dst == TD.Device_ID);
assert(function_len > 0);
assert(test_vmac_src == IUT.Device_ID);
assert(test_vmac_dst == TD.Device_ID);
}
/* change Device ID */
old_device_id = TD.Device_ID;
TD.Device_ID += 42;
mtu_len = bvlc6_encode_virtual_address_resolution(
&mtu[0], sizeof(mtu), TD.Device_ID);
result = bvlc6_bbmd_disabled_handler(&TD.BIP6_Addr, &TD.BACnet_Address,
&mtu[0], mtu_len);
assert(result == 0);
assert(bvlc6_get_function_code() ==
BVLC6_VIRTUAL_ADDRESS_RESOLUTION);
assert(VMAC_Find_By_Key(TD.Device_ID) != NULL);
assert(VMAC_Find_By_Key(old_device_id) == NULL);
/* change IPv6 address */
mtu_len = bvlc6_encode_virtual_address_resolution(
&mtu[0], sizeof(mtu), TD.Device_ID);
bvlc6_address_set(&TD.BIP6_Addr,
0x2001, 0x0DBB, 0xAC10, 0xFE01, 0, 0, 42,
BIP6_MULTICAST_GROUP_ID);
result = bvlc6_bbmd_disabled_handler(&TD.BIP6_Addr, &TD.BACnet_Address,
&mtu[0], mtu_len);
assert(result == 0);
assert(bvlc6_get_function_code() ==
BVLC6_VIRTUAL_ADDRESS_RESOLUTION);
assert(VMAC_Find_By_Key(TD.Device_ID) != NULL);
/* repeat with same device ID and address */
/* change IPv6 address */
mtu_len = bvlc6_encode_virtual_address_resolution(
&mtu[0], sizeof(mtu), TD.Device_ID);
result = bvlc6_bbmd_disabled_handler(&TD.BIP6_Addr, &TD.BACnet_Address,
&mtu[0], mtu_len);
assert(result == 0);
assert(bvlc6_get_function_code() ==
BVLC6_VIRTUAL_ADDRESS_RESOLUTION);
assert(VMAC_Find_By_Key(TD.Device_ID) != NULL);
test_cleanup();
}
static void test_BBMD_Result(Test *pTest)
static void test_BBMD_Result(void)
{
int result = 0;
uint32_t vmac_src = 0x1234;
@@ -275,38 +295,19 @@ static void test_BBMD_Result(Test *pTest)
bvlc6_encode_result(&mtu[0], sizeof(mtu), vmac_src, result_code[i]);
result = bvlc6_bbmd_disabled_handler(&addr, &src, &mtu[0], mtu_len);
/* validate that the result is handled (0) */
ct_test(pTest, result == 0);
assert(result == 0);
test_result_code = bvlc6_get_last_result();
ct_test(pTest, test_result_code == result_code[i]);
assert(test_result_code == result_code[i]);
test_function_code = bvlc6_get_function_code();
ct_test(pTest, test_function_code == BVLC6_RESULT);
assert(test_function_code == BVLC6_RESULT);
}
}
static void test_BBMD6(Test *pTest)
{
bool rc;
/* individual tests */
rc = ct_addTestFunction(pTest, test_BBMD_Result);
assert(rc);
rc = ct_addTestFunction(pTest, test_Execute_Virtual_Address_Resolution);
assert(rc);
rc = ct_addTestFunction(pTest, test_Initiate_Original_Broadcast_NPDU);
assert(rc);
}
int main(void)
{
Test *pTest;
pTest = ct_create("BACnet Broadcast Management Device IP/v6", NULL);
test_BBMD6(pTest);
/* configure output */
ct_setStream(pTest, stdout);
ct_run(pTest);
(void)ct_report(pTest);
ct_destroy(pTest);
test_BBMD_Result();
test_Execute_Virtual_Address_Resolution();
test_Initiate_Original_Broadcast_NPDU();
return 0;
}