Skip to content

Commit afbec7d

Browse files
committed
feat(mdns): Add test template for mdns-sender
1 parent 0974c6b commit afbec7d

File tree

5 files changed

+73
-17
lines changed

5 files changed

+73
-17
lines changed

.github/workflows/mdns__host-tests.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ jobs:
9898
cmake -DUNIT_TESTS=test_receiver ..
9999
cmake --build .
100100
ctest --extra-verbose
101+
cd ..
102+
mkdir build3 && cd build3
103+
cmake -DUNIT_TESTS=test_sender ..
104+
cmake --build .
105+
ctest --extra-verbose
101106
102107
103108
fuzz_test:

components/mdns/tests/host_unit_test/unity/test_receiver/test_receiver.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#include "unity.h"
88
#include "create_test_packet.h"
99
#include "unity_main.h"
10+
#include "mock_mdns_pcb.h"
11+
#include "mock_mdns_send.h"
12+
#include "create_test_packet.h"
1013

1114
static void test_mdns_hostname_queries(void)
1215
{
@@ -58,6 +61,23 @@ static void test_mdns_with_answers(void)
5861

5962
}
6063

64+
static void mdns_priv_create_answer_from_parsed_packet_Callback(mdns_parsed_packet_t* parsed_packet, int cmock_num_calls)
65+
{
66+
printf("callback\n");
67+
}
68+
69+
void setup_cmock(void)
70+
{
71+
mdns_priv_probe_all_pcbs_CMockIgnore();
72+
mdns_priv_pcb_announce_CMockIgnore();
73+
mdns_priv_pcb_send_bye_service_CMockIgnore();
74+
mdns_priv_pcb_check_probing_services_CMockIgnore();
75+
mdns_priv_pcb_is_after_probing_IgnoreAndReturn(true);
76+
77+
_mdns_clear_tx_queue_head_CMockIgnore();
78+
_mdns_remove_scheduled_service_packets_CMockIgnore();
79+
mdns_priv_create_answer_from_parsed_packet_Stub(mdns_priv_create_answer_from_parsed_packet_Callback);
80+
}
6181

6282
void run_unity_tests(void)
6383
{
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Unlicense OR CC0-1.0
5+
*/
6+
7+
#include "unity.h"
8+
#include "unity_main.h"
9+
#include "mock_mdns_pcb.h"
10+
#include "mdns_send.h"
11+
12+
void setup_cmock(void)
13+
{
14+
mdns_priv_probe_all_pcbs_CMockIgnore();
15+
mdns_priv_pcb_announce_CMockIgnore();
16+
mdns_priv_pcb_send_bye_service_CMockIgnore();
17+
mdns_priv_pcb_check_probing_services_CMockIgnore();
18+
mdns_priv_pcb_is_after_probing_IgnoreAndReturn(true);
19+
}
20+
21+
22+
23+
// Example of a more complex test with answers and additional records
24+
static void test_dispatch_tx_packet(void)
25+
{
26+
mdns_tx_packet_t p = {};
27+
_mdns_dispatch_tx_packet(&p);
28+
}
29+
30+
31+
void run_unity_tests(void)
32+
{
33+
UNITY_BEGIN();
34+
35+
// Run hostname queries test
36+
RUN_TEST(test_dispatch_tx_packet);
37+
38+
39+
UNITY_END();
40+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Testing mDNS sender, need to mock pbb
2+
set(MOCK_FILES
3+
"mdns_pcb"
4+
)
5+
6+
list(APPEND SOURCES unity/${UNIT_TESTS}/test_sender.c)

components/mdns/tests/host_unit_test/unity/unity_main.c

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
#include "mdns_receive.h"
1313
#include "mdns_responder.h"
1414
#include "mdns_mem_caps.h"
15-
#include "mock_mdns_pcb.h"
16-
#include "mock_mdns_send.h"
17-
#include "create_test_packet.h"
1815

1916
void setUp(void)
2017
{
@@ -132,24 +129,12 @@ void test_mdns_receive_from_file(const char* filename)
132129

133130

134131
void run_unity_tests(void);
135-
136-
void mdns_priv_create_answer_from_parsed_packet_Callback(mdns_parsed_packet_t* parsed_packet, int cmock_num_calls)
137-
{
138-
printf("callback\n");
139-
}
132+
void setup_cmock(void);
140133

141134
int main(int argc, char **argv)
142135
{
143136
if (argc >= 2 && strcmp(argv[1], "--test") == 0) {
144-
mdns_priv_probe_all_pcbs_CMockIgnore();
145-
mdns_priv_pcb_announce_CMockIgnore();
146-
mdns_priv_pcb_send_bye_service_CMockIgnore();
147-
mdns_priv_pcb_check_probing_services_CMockIgnore();
148-
mdns_priv_pcb_is_after_probing_IgnoreAndReturn(true);
149-
150-
_mdns_clear_tx_queue_head_CMockIgnore();
151-
_mdns_remove_scheduled_service_packets_CMockIgnore();
152-
mdns_priv_create_answer_from_parsed_packet_Stub(mdns_priv_create_answer_from_parsed_packet_Callback);
137+
setup_cmock();
153138

154139
init_responder();
155140
run_unity_tests();

0 commit comments

Comments
 (0)