linux: cache netmask for accurate subnet prefix calculation to fix implementation which always returned 0 (#1155)
Co-authored-by: Syed Amer Gilani <syed.gilani@trilux.com>
This commit is contained in:
@@ -231,6 +231,7 @@ elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
|
||||
|
||||
list(APPEND testdirs
|
||||
ports/linux/bsc_event
|
||||
ports/linux/bip_subnet
|
||||
)
|
||||
|
||||
elseif(WIN32)
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
# 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)
|
||||
|
||||
find_package(Threads)
|
||||
|
||||
set(CMAKE_C_FLAGS -pthread)
|
||||
|
||||
string(REGEX REPLACE
|
||||
"/test/ports/[a-zA-Z_/-]*$"
|
||||
"/src"
|
||||
SRC_DIR
|
||||
${CMAKE_CURRENT_SOURCE_DIR})
|
||||
string(REGEX REPLACE
|
||||
"/test/ports/[a-zA-Z_/-]*$"
|
||||
"/ports"
|
||||
PORTS_DIR
|
||||
${CMAKE_CURRENT_SOURCE_DIR})
|
||||
string(REGEX REPLACE
|
||||
"/test/ports/[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
|
||||
)
|
||||
|
||||
include_directories(
|
||||
${SRC_DIR}
|
||||
${TST_DIR}/ztest/include
|
||||
)
|
||||
|
||||
add_executable(${PROJECT_NAME}
|
||||
${PORTS_DIR}/linux/bip-init.c
|
||||
./src/bvlc_stubs.c
|
||||
./src/main.c
|
||||
${ZTST_DIR}/ztest_mock.c
|
||||
${ZTST_DIR}/ztest.c
|
||||
)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} Threads::Threads)
|
||||
@@ -0,0 +1,48 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
#include "bacnet/basic/bbmd/h_bbmd.h"
|
||||
|
||||
int bvlc_handler(
|
||||
BACNET_IP_ADDRESS *addr,
|
||||
BACNET_ADDRESS *src,
|
||||
uint8_t *npdu,
|
||||
uint16_t npdu_len)
|
||||
{
|
||||
(void)addr;
|
||||
(void)src;
|
||||
(void)npdu;
|
||||
(void)npdu_len;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int bvlc_broadcast_handler(
|
||||
BACNET_IP_ADDRESS *addr,
|
||||
BACNET_ADDRESS *src,
|
||||
uint8_t *npdu,
|
||||
uint16_t npdu_len)
|
||||
{
|
||||
(void)addr;
|
||||
(void)src;
|
||||
(void)npdu;
|
||||
(void)npdu_len;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int bvlc_send_pdu(
|
||||
const BACNET_ADDRESS *dest,
|
||||
const BACNET_NPDU_DATA *npdu_data,
|
||||
const uint8_t *pdu,
|
||||
unsigned pdu_len)
|
||||
{
|
||||
(void)dest;
|
||||
(void)npdu_data;
|
||||
(void)pdu;
|
||||
(void)pdu_len;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void bvlc_init(void)
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Tests for BACnet/IP subnet prefix caching on Linux
|
||||
* @copyright SPDX-License-Identifier: MIT
|
||||
*/
|
||||
#include <zephyr/ztest.h>
|
||||
#include <bacnet/datalink/bip.h>
|
||||
|
||||
static void test_prefix_defaults_to_zero(void)
|
||||
{
|
||||
bip_cleanup();
|
||||
zassert_equal(
|
||||
bip_get_subnet_prefix(), 0, "Prefix should be zero by default");
|
||||
}
|
||||
|
||||
static void test_prefix_roundtrip(void)
|
||||
{
|
||||
bip_cleanup();
|
||||
zassert_true(bip_set_subnet_prefix(24), NULL);
|
||||
zassert_equal(bip_get_subnet_prefix(), 24, NULL);
|
||||
|
||||
zassert_true(bip_set_subnet_prefix(16), NULL);
|
||||
zassert_equal(bip_get_subnet_prefix(), 16, NULL);
|
||||
}
|
||||
|
||||
static void test_prefix_invalid_values(void)
|
||||
{
|
||||
bip_cleanup();
|
||||
zassert_false(bip_set_subnet_prefix(0), NULL);
|
||||
zassert_false(bip_set_subnet_prefix(33), NULL);
|
||||
zassert_equal(bip_get_subnet_prefix(), 0, NULL);
|
||||
}
|
||||
|
||||
static void test_prefix_all_bits_set(void)
|
||||
{
|
||||
bip_cleanup();
|
||||
zassert_true(bip_set_subnet_prefix(32), NULL);
|
||||
zassert_equal(bip_get_subnet_prefix(), 32, NULL);
|
||||
}
|
||||
|
||||
void test_main(void)
|
||||
{
|
||||
ztest_test_suite(
|
||||
bip_subnet_test, ztest_unit_test(test_prefix_defaults_to_zero),
|
||||
ztest_unit_test(test_prefix_roundtrip),
|
||||
ztest_unit_test(test_prefix_invalid_values),
|
||||
ztest_unit_test(test_prefix_all_bits_set));
|
||||
ztest_run_test_suite(bip_subnet_test);
|
||||
}
|
||||
Reference in New Issue
Block a user