Skip to content

Commit fa07b9f

Browse files
IVandeVeireMaureenHelm
authored andcommitted
test: net: igmp: Add IGMP query test
Add functionality to test the IGMP query handling. This test should prevent the IGMP logic from sending IGMPv3 messages to IGMPv2 queriers. The test does now also check backwards compatability when IGMPv3 queries are received but only IGMPv2 is enabled. Signed-off-by: Ibe Van de Veire <[email protected]>
1 parent 317de0c commit fa07b9f

File tree

1 file changed

+84
-2
lines changed

1 file changed

+84
-2
lines changed

tests/net/igmp/src/main.c

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ LOG_MODULE_REGISTER(net_test, CONFIG_NET_IPV4_LOG_LEVEL);
4545
#define DBG(fmt, ...)
4646
#endif
4747

48+
static const unsigned char igmp_v2_query[] = {
49+
/* IPv4 header */
50+
0x46, 0xc0, 0x00, 0x20, 0x1b, 0x58, 0x00, 0x00, 0x01, 0x02, 0x66, 0x79,
51+
0xc0, 0x00, 0x02, 0x45, 0xe0, 0x00, 0x00, 0x01, 0x94, 0x04, 0x00, 0x00,
52+
53+
/* IGMP header */
54+
0x11, 0xff, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00,
55+
};
56+
57+
static const unsigned char igmp_v3_query[] = {
58+
/* IPv4 header */
59+
0x46, 0xc0, 0x00, 0x24, 0xac, 0x72, 0x00, 0x00, 0x01, 0x02, 0xd5, 0x5a,
60+
0xc0, 0x00, 0x02, 0x45, 0xe0, 0x00, 0x00, 0x01, 0x94, 0x04, 0x00, 0x00,
61+
62+
/* IGMP header */
63+
0x11, 0x64, 0xec, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x02, 0x7d, 0x00, 0x00,
64+
};
65+
4866
static struct in_addr my_addr = { { { 192, 0, 2, 1 } } };
4967
static struct in_addr mcast_addr = { { { 224, 0, 2, 63 } } };
5068
static struct in_addr any_addr = INADDR_ANY_INIT;
@@ -56,6 +74,8 @@ static bool is_join_msg_ok;
5674
static bool is_leave_msg_ok;
5775
static bool is_query_received;
5876
static bool is_report_sent;
77+
static bool is_igmpv2_query_sent;
78+
static bool is_igmpv3_query_sent;
5979
static bool ignore_already;
6080
K_SEM_DEFINE(wait_data, 0, UINT_MAX);
6181

@@ -151,15 +171,16 @@ static int tester_send(const struct device *dev, struct net_pkt *pkt)
151171
k_sem_give(&wait_data);
152172
} else if (igmp_header->type == NET_IPV4_IGMP_REPORT_V2) {
153173
NET_DBG("Received v2 report....");
154-
zassert_false(IS_ENABLED(CONFIG_NET_IPV4_IGMPV3),
155-
"Wrong IGMP report received (IGMPv2)");
174+
zassert_true(!IS_ENABLED(CONFIG_NET_IPV4_IGMPV3) || is_igmpv2_query_sent,
175+
"Wrong IGMP report received (IGMPv2)");
156176
is_join_msg_ok = true;
157177
is_report_sent = true;
158178
k_sem_give(&wait_data);
159179
} else if (igmp_header->type == NET_IPV4_IGMP_REPORT_V3) {
160180
NET_DBG("Received v3 report....");
161181
zassert_true(IS_ENABLED(CONFIG_NET_IPV4_IGMPV3),
162182
"Wrong IGMP report received (IGMPv3)");
183+
zassert_false(is_igmpv2_query_sent, "IGMPv3 response to IGMPv2 request");
163184

164185
#if defined(CONFIG_NET_IPV4_IGMPV3)
165186
zassert_true(ntohs(igmp_header->groups_len) == 1,
@@ -285,6 +306,24 @@ static void igmp_teardown(void *dummy)
285306
net_if_ipv4_addr_rm(net_iface, &my_addr);
286307
}
287308

309+
static struct net_pkt *prepare_igmp_query(struct net_if *iface, bool is_igmpv3)
310+
{
311+
struct net_pkt *pkt;
312+
313+
const unsigned char *igmp_query = is_igmpv3 ? igmp_v3_query : igmp_v2_query;
314+
size_t igmp_query_size = is_igmpv3 ? sizeof(igmp_v3_query) : sizeof(igmp_v2_query);
315+
316+
pkt = net_pkt_alloc_with_buffer(iface, igmp_query_size, AF_INET, IPPROTO_IGMP, K_FOREVER);
317+
zassert_not_null(pkt, "Failed to allocate buffer");
318+
319+
zassert_ok(net_pkt_write(pkt, igmp_query, igmp_query_size));
320+
321+
net_pkt_set_overwrite(pkt, true);
322+
net_pkt_cursor_init(pkt);
323+
324+
return pkt;
325+
}
326+
288327
static void join_group(void)
289328
{
290329
int ret;
@@ -559,4 +598,47 @@ ZTEST_USER(net_igmp, test_socket_catch_join_with_index)
559598
socket_leave_group_with_index(&my_addr);
560599
}
561600

601+
static void igmp_send_query(bool is_imgpv3)
602+
{
603+
struct net_pkt *pkt;
604+
605+
is_report_sent = false;
606+
is_join_msg_ok = false;
607+
608+
is_igmpv2_query_sent = false;
609+
is_igmpv3_query_sent = false;
610+
611+
/* Joining group first to get reply on query*/
612+
join_group();
613+
614+
is_igmpv2_query_sent = !is_imgpv3;
615+
is_igmpv3_query_sent = is_imgpv3;
616+
617+
pkt = prepare_igmp_query(net_iface, is_imgpv3);
618+
zassert_not_null(pkt, "IGMPv2 query packet prep failed");
619+
620+
zassert_equal(net_ipv4_input(pkt, false), NET_OK, "Failed to send");
621+
622+
zassert_ok(k_sem_take(&wait_data, K_MSEC(WAIT_TIME)), "Timeout while waiting query event");
623+
624+
zassert_true(is_report_sent, "Did not catch query event");
625+
626+
zassert_true(is_join_msg_ok, "Join msg invalid");
627+
628+
is_igmpv2_query_sent = false;
629+
is_igmpv3_query_sent = false;
630+
631+
leave_group();
632+
}
633+
634+
ZTEST_USER(net_igmp, test_igmpv3_query)
635+
{
636+
igmp_send_query(true);
637+
}
638+
639+
ZTEST_USER(net_igmp, test_igmpv2_query)
640+
{
641+
igmp_send_query(false);
642+
}
643+
562644
ZTEST_SUITE(net_igmp, NULL, igmp_setup, NULL, NULL, igmp_teardown);

0 commit comments

Comments
 (0)