Skip to content

Commit 6db95af

Browse files
committed
fix(mdns): Add host unit tests
1 parent 0bd74cb commit 6db95af

File tree

8 files changed

+242
-73
lines changed

8 files changed

+242
-73
lines changed

components/mdns/mdns_send.c

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,6 @@ static mdns_tx_packet_t *s_tx_queue_head;
2525
#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
2626
#endif
2727

28-
29-
/**
30-
* @brief appends byte in a packet, incrementing the index
31-
*
32-
* @param packet MDNS packet
33-
* @param index offset in the packet
34-
* @param value the value to set
35-
*
36-
* @return length of added data: 0 on error or 1 on success
37-
*/
38-
static inline uint8_t _mdns_append_u8(uint8_t *packet, uint16_t *index, uint8_t value)
39-
{
40-
if (*index >= MDNS_MAX_PACKET_SIZE) {
41-
return 0;
42-
}
43-
packet[*index] = value;
44-
*index += 1;
45-
return 1;
46-
}
47-
48-
uint8_t mdns_utils_append_u16(uint8_t *packet, uint16_t *index, uint16_t value)
49-
{
50-
if ((*index + 1) >= MDNS_MAX_PACKET_SIZE) {
51-
return 0;
52-
}
53-
_mdns_append_u8(packet, index, (value >> 8) & 0xFF);
54-
_mdns_append_u8(packet, index, value & 0xFF);
55-
return 2;
56-
}
57-
5828
/**
5929
* @brief appends uint32_t in a packet, incrementing the index
6030
*

components/mdns/mdns_utils.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,13 @@ bool mdns_utils_ipv6_address_is_zero(esp_ip6_addr_t ip6)
255255
return true;
256256
}
257257
#endif /* CONFIG_LWIP_IPV6 */
258+
259+
uint8_t mdns_utils_append_u16(uint8_t *packet, uint16_t *index, uint16_t value)
260+
{
261+
if ((*index + 1) >= MDNS_MAX_PACKET_SIZE) {
262+
return 0;
263+
}
264+
_mdns_append_u8(packet, index, (value >> 8) & 0xFF);
265+
_mdns_append_u8(packet, index, value & 0xFF);
266+
return 2;
267+
}

components/mdns/private_include/mdns_querier.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ static inline void mdns_priv_query_update_result_ttl(mdns_result_t *r, uint32_t
101101
r->ttl = r->ttl < ttl ? r->ttl : ttl;
102102
}
103103

104+
/**
105+
* @brief Send a search query packet on the specified interface and protocol
106+
*/
107+
void _mdns_search_send_pcb(mdns_search_once_t *search, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol);
104108

105109
#ifdef __cplusplus
106110
}

components/mdns/private_include/mdns_send.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@
1212
extern "C" {
1313
#endif
1414

15-
/**
16-
* @brief Send a search query packet on the specified interface and protocol
17-
*/
18-
void _mdns_search_send_pcb(mdns_search_once_t *search, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol);
19-
2015
/**
2116
* @brief Free a transmit packet and its associated resources
2217
*/

components/mdns/private_include/mdns_utils.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,22 @@ void mdns_utils_free_address_list(mdns_ip_addr_t *address_list);
134134
* @return length of added data: 0 on error or 2 on success
135135
*/
136136
uint8_t mdns_utils_append_u16(uint8_t *packet, uint16_t *index, uint16_t value);
137+
138+
/**
139+
* @brief appends byte in a packet, incrementing the index
140+
*
141+
* @param packet MDNS packet
142+
* @param index offset in the packet
143+
* @param value the value to set
144+
*
145+
* @return length of added data: 0 on error or 1 on success
146+
*/
147+
static inline uint8_t _mdns_append_u8(uint8_t *packet, uint16_t *index, uint8_t value)
148+
{
149+
if (*index >= MDNS_MAX_PACKET_SIZE) {
150+
return 0;
151+
}
152+
packet[*index] = value;
153+
*index += 1;
154+
return 1;
155+
}

components/mdns/tests/host_unit_test/CMakeLists.txt

Lines changed: 117 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,92 @@ include_directories(
4444
set(UNITY_DIR "$ENV{IDF_PATH}/components/unity")
4545
include_directories(${UNITY_DIR}/unity/src)
4646

47+
# Add option to enable unit testing
48+
option(ENABLE_UNIT_TESTS "Enable Unity-based unit tests" OFF)
49+
50+
# Add preprocessor definition based on the option
51+
if(ENABLE_UNIT_TESTS)
52+
add_definitions(-DENABLE_UNIT_TESTS)
53+
endif()
54+
55+
# Add CMock options
56+
if(ENABLE_UNIT_TESTS)
57+
# CMock paths
58+
set(CMOCK_DIR "$ENV{IDF_PATH}/components/cmock")
59+
60+
# Use ruby command directly instead of looking for ruby.exe
61+
find_program(RUBY_EXECUTABLE ruby)
62+
if(NOT RUBY_EXECUTABLE)
63+
message(FATAL_ERROR "Ruby is required for CMock but was not found!")
64+
endif()
65+
66+
# Define list of files to mock
67+
set(MOCK_FILES
68+
"mdns_pcb"
69+
"mdns_send"
70+
# Add more files here as needed, without .h extension
71+
)
72+
73+
# Verify headers exist and create mock commands for each
74+
foreach(mock_file ${MOCK_FILES})
75+
set(header_path "${COMPONENT_DIR}/private_include/${mock_file}.h")
76+
if(NOT EXISTS ${header_path})
77+
message(FATAL_ERROR "Cannot find ${mock_file}.h at ${header_path}")
78+
endif()
79+
80+
list(APPEND MOCK_OUTPUTS
81+
${CMAKE_CURRENT_BINARY_DIR}/mocks/mock_${mock_file}.c
82+
${CMAKE_CURRENT_BINARY_DIR}/mocks/mock_${mock_file}.h
83+
)
84+
85+
add_custom_command(
86+
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/mocks/mock_${mock_file}.c
87+
${CMAKE_CURRENT_BINARY_DIR}/mocks/mock_${mock_file}.h
88+
COMMAND ${RUBY_EXECUTABLE}
89+
${CMOCK_DIR}/CMock/lib/cmock.rb
90+
-o${CMAKE_CURRENT_SOURCE_DIR}/cmock_config.yml
91+
${header_path}
92+
DEPENDS ${header_path}
93+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
94+
COMMENT "Generating mock for ${mock_file}.h"
95+
)
96+
endforeach()
97+
98+
# Include CMock headers
99+
include_directories(
100+
${CMOCK_DIR}/CMock/src
101+
${CMAKE_CURRENT_BINARY_DIR}/mocks
102+
${UNITY_DIR}/unity/src
103+
${UNITY_DIR}/include
104+
)
105+
106+
# Create directory for generated mocks
107+
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/mocks)
108+
109+
# Create CMock config file if it doesn't exist
110+
if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/cmock_config.yml)
111+
file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/cmock_config.yml
112+
"---
113+
:cmock:
114+
:mock_prefix: mock_
115+
:plugins:
116+
- :callback
117+
- :ignore
118+
- :expect_any_args
119+
- :array
120+
- :return_thru_ptr
121+
:strippables:
122+
- '(?:extern|static)\\s+'
123+
:treat_as:
124+
uint8_t: HEX8
125+
uint16_t: HEX16
126+
uint32_t: UINT32
127+
int8_t: INT8
128+
bool: UINT8"
129+
)
130+
endif()
131+
endif()
132+
47133
# Source files
48134
set(SOURCES
49135
${CMAKE_CURRENT_SOURCE_DIR}/main.c
@@ -59,9 +145,26 @@ set(SOURCES
59145
${CMAKE_CURRENT_SOURCE_DIR}/../../mdns_send.c
60146
${CMAKE_CURRENT_SOURCE_DIR}/../../mdns_pcb.c
61147
${CMAKE_CURRENT_SOURCE_DIR}/../../mdns_netif.c
62-
${UNITY_DIR}/unity/src/unity.c
63148
)
64149

150+
if(ENABLE_UNIT_TESTS)
151+
# Remove real implementations of mocked files
152+
foreach(mock_file ${MOCK_FILES})
153+
list(REMOVE_ITEM SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/../../${mock_file}.c)
154+
endforeach()
155+
156+
# Add test-related sources
157+
list(APPEND SOURCES
158+
${UNITY_DIR}/unity/src/unity.c
159+
${CMOCK_DIR}/CMock/src/cmock.c
160+
)
161+
162+
# Add all generated mock files
163+
foreach(mock_file ${MOCK_FILES})
164+
list(APPEND SOURCES ${CMAKE_CURRENT_BINARY_DIR}/mocks/mock_${mock_file}.c)
165+
endforeach()
166+
endif()
167+
65168
# Enable sanitizers
66169
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
67170
#set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
@@ -84,12 +187,11 @@ endif()
84187
# COMPILE_FLAGS "-Wall -Werror"
85188
#)
86189

87-
# Test command with ASAN options
88-
#enable_testing()
89-
#add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME})
90-
#set_tests_properties(${PROJECT_NAME} PROPERTIES
91-
# ENVIRONMENT "ASAN_OPTIONS=detect_leaks=1:verbosity=1:log_path=asan.log:print_stats=1:malloc_context_size=20"
92-
#)
190+
# Enable testing if unit tests are enabled
191+
if(ENABLE_UNIT_TESTS)
192+
enable_testing()
193+
add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME} --test)
194+
endif()
93195

94196
# Add libbsd dependency
95197
#find_package(PkgConfig REQUIRED)
@@ -102,3 +204,11 @@ endif()
102204

103205
# Add include directories if needed
104206
#target_include_directories(${PROJECT_NAME} PRIVATE ${LIBBSD_INCLUDE_DIRS})
207+
208+
# Add mock generation as dependency
209+
if(ENABLE_UNIT_TESTS)
210+
add_custom_target(generate_mocks
211+
DEPENDS ${MOCK_OUTPUTS}
212+
)
213+
add_dependencies(${PROJECT_NAME} generate_mocks)
214+
endif()

components/mdns/tests/host_unit_test/input/generate_cases.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,12 @@ def create_mdns_packet(queries, answers, additional, output_file='mdns_packet.bi
5454

5555
# Test case 1: Basic hostname queries
5656
queries = [
57+
('test4.local.', 1), # A record for main hostname
5758
('test.local.', 1), # A record for main hostname
58-
('test2.local.', 1), # A record for instance name
59-
('test3.local.', 1), # A record for delegate hostname
60-
('test4.local.', 28), # AAAA record for delegate hostname
6159
]
6260
answers = [
63-
('test.local.', 'A', '192.168.1.100', 120),
64-
('test2.local.', 'A', '192.168.1.101', 120),
65-
('test3.local.', 'A', '192.168.1.102', 120),
66-
('test4.local.', 'AAAA', 'fe80::1', 120),
6761
]
6862
additional = [
69-
('test.local.', 'TXT', 'board=esp32', 120),
7063
]
7164
create_mdns_packet(queries, answers, additional, 'test_hostname_queries.bin')
7265
print('Test case 1: Hostname queries')

0 commit comments

Comments
 (0)