1 /* main.c - Application main entry point */
2
3 /*
4 * Copyright (c) 2016 Intel Corporation
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #define NET_LOG_LEVEL CONFIG_NET_UDP_LOG_LEVEL
10
11 #include <zephyr/logging/log.h>
12 LOG_MODULE_REGISTER(net_test, NET_LOG_LEVEL);
13
14 #include <zephyr/kernel.h>
15 #include <zephyr/linker/sections.h>
16
17 #include <zephyr/types.h>
18 #include <stddef.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <errno.h>
22 #include <zephyr/device.h>
23 #include <zephyr/init.h>
24 #include <zephyr/sys/printk.h>
25 #include <zephyr/net_buf.h>
26 #include <zephyr/net/net_core.h>
27 #include <zephyr/net/net_pkt.h>
28 #include <zephyr/net/net_ip.h>
29 #include <zephyr/net/ethernet.h>
30 #include <zephyr/net/dummy.h>
31 #include <zephyr/net/udp.h>
32 #include <zephyr/random/random.h>
33
34 #include "ipv4.h"
35 #include "ipv6.h"
36
37 #include <zephyr/ztest.h>
38
39 #if NET_LOG_LEVEL >= LOG_LEVEL_DBG
40 #define DBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
41 #else
42 #define DBG(fmt, ...)
43 #endif
44
45 #include "udp_internal.h"
46
47 #if NET_LOG_LEVEL >= LOG_LEVEL_DBG
48 #define NET_LOG_ENABLED 1
49 #endif
50 #include "net_private.h"
51 #include "ipv4.h"
52
53 static bool test_failed;
54 static bool fail = true;
55 static struct k_sem recv_lock;
56 static char payload[] = { 'f', 'o', 'o', 'b', 'a', 'r', '\0' };
57
58 struct net_udp_context {
59 uint8_t mac_addr[sizeof(struct net_eth_addr)];
60 struct net_linkaddr ll_addr;
61 };
62
net_udp_dev_init(const struct device * dev)63 int net_udp_dev_init(const struct device *dev)
64 {
65 struct net_udp_context *net_udp_context = dev->data;
66
67 net_udp_context = net_udp_context;
68
69 return 0;
70 }
71
net_udp_get_mac(const struct device * dev)72 static uint8_t *net_udp_get_mac(const struct device *dev)
73 {
74 struct net_udp_context *context = dev->data;
75
76 if (context->mac_addr[2] == 0x00) {
77 /* 00-00-5E-00-53-xx Documentation RFC 7042 */
78 context->mac_addr[0] = 0x00;
79 context->mac_addr[1] = 0x00;
80 context->mac_addr[2] = 0x5E;
81 context->mac_addr[3] = 0x00;
82 context->mac_addr[4] = 0x53;
83 context->mac_addr[5] = sys_rand8_get();
84 }
85
86 return context->mac_addr;
87 }
88
net_udp_iface_init(struct net_if * iface)89 static void net_udp_iface_init(struct net_if *iface)
90 {
91 uint8_t *mac = net_udp_get_mac(net_if_get_device(iface));
92
93 net_if_set_link_addr(iface, mac, 6, NET_LINK_ETHERNET);
94 }
95
96 static int send_status = -EINVAL;
97
tester_send(const struct device * dev,struct net_pkt * pkt)98 static int tester_send(const struct device *dev, struct net_pkt *pkt)
99 {
100 if (!pkt->frags) {
101 DBG("No data to send!\n");
102 return -ENODATA;
103 }
104
105 DBG("Data was sent successfully\n");
106
107 send_status = 0;
108
109 return 0;
110 }
111
112 struct net_udp_context net_udp_context_data;
113
114 static struct dummy_api net_udp_if_api = {
115 .iface_api.init = net_udp_iface_init,
116 .send = tester_send,
117 };
118
119 #define _ETH_L2_LAYER DUMMY_L2
120 #define _ETH_L2_CTX_TYPE NET_L2_GET_CTX_TYPE(DUMMY_L2)
121
122 NET_DEVICE_INIT(net_udp_test, "net_udp_test",
123 net_udp_dev_init, NULL,
124 &net_udp_context_data, NULL,
125 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
126 &net_udp_if_api, _ETH_L2_LAYER, _ETH_L2_CTX_TYPE, 127);
127
128 struct ud {
129 const struct sockaddr *remote_addr;
130 const struct sockaddr *local_addr;
131 uint16_t remote_port;
132 uint16_t local_port;
133 char *test;
134 void *handle;
135 };
136
137 static struct ud *returned_ud;
138
test_ok(struct net_conn * conn,struct net_pkt * pkt,union net_ip_header * ip_hdr,union net_proto_header * proto_hdr,void * user_data)139 static enum net_verdict test_ok(struct net_conn *conn,
140 struct net_pkt *pkt,
141 union net_ip_header *ip_hdr,
142 union net_proto_header *proto_hdr,
143 void *user_data)
144 {
145 struct ud *ud = (struct ud *)user_data;
146
147 k_sem_give(&recv_lock);
148
149 if (!ud) {
150 fail = true;
151
152 DBG("Test %s failed.", ud->test);
153
154 return NET_DROP;
155 }
156
157 fail = false;
158
159 returned_ud = user_data;
160
161 net_pkt_unref(pkt);
162
163 return NET_OK;
164 }
165
test_fail(struct net_conn * conn,struct net_pkt * pkt,union net_ip_header * ip_hdr,union net_proto_header * proto_hdr,void * user_data)166 static enum net_verdict test_fail(struct net_conn *conn,
167 struct net_pkt *pkt,
168 union net_ip_header *ip_hdr,
169 union net_proto_header *proto_hdr,
170 void *user_data)
171 {
172 /* This function should never be called as there should not
173 * be a matching UDP connection.
174 */
175
176 fail = true;
177 return NET_DROP;
178 }
179
180 uint8_t ipv6_hop_by_hop_ext_hdr[] = {
181 /* Next header UDP */
182 0x11,
183 /* Length (multiple of 8 octets) */
184 0x0C,
185 /* Experimental extension */
186 0x3e,
187 /* Length in bytes */
188 0x20,
189 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
190 0x49, 0x4A, 0x4B, 0x4C, 0x4E, 0x4F, 0x50, 0x51,
191 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
192 0x5A, 0x5B, 0x5C, 0x5D, 0x5F, 0x60, 0x61, 0x62,
193 /* Another experimental extension */
194 0x3e,
195 /* Length in bytes */
196 0x20,
197 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A,
198 0x6B, 0x6C, 0x6D, 0x6F, 0x70, 0x71, 0x72, 0x73,
199 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B,
200 0x7C, 0x7D, 0x7E, 0x21, 0x22, 0x23, 0x24, 0x25,
201 /* Another experimental extension */
202 0x3e,
203 /* Length in bytes */
204 0x20,
205 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D,
206 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
207 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D,
208 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
209 };
210
211 #define TIMEOUT K_MSEC(200)
212
send_ipv6_udp_msg(struct net_if * iface,struct in6_addr * src,struct in6_addr * dst,uint16_t src_port,uint16_t dst_port,struct ud * ud,bool expect_failure)213 static bool send_ipv6_udp_msg(struct net_if *iface,
214 struct in6_addr *src,
215 struct in6_addr *dst,
216 uint16_t src_port,
217 uint16_t dst_port,
218 struct ud *ud,
219 bool expect_failure)
220 {
221 struct net_pkt *pkt;
222 int ret;
223
224 pkt = net_pkt_alloc_with_buffer(iface, 0, AF_INET6,
225 IPPROTO_UDP, K_SECONDS(1));
226 zassert_not_null(pkt, "Out of mem");
227
228 if (net_ipv6_create(pkt, src, dst) ||
229 net_udp_create(pkt, htons(src_port), htons(dst_port))) {
230 printk("Cannot create IPv6 UDP pkt %p", pkt);
231 zassert_true(0, "exiting");
232 }
233
234 net_pkt_cursor_init(pkt);
235 net_ipv6_finalize(pkt, IPPROTO_UDP);
236
237 ret = net_recv_data(iface, pkt);
238 if (ret < 0) {
239 printk("Cannot recv pkt %p, ret %d\n", pkt, ret);
240 zassert_true(0, "exiting");
241 }
242
243 if (k_sem_take(&recv_lock, TIMEOUT)) {
244
245 /**TESTPOINT: Check for failure*/
246 zassert_true(expect_failure, "Timeout, packet not received");
247 return true;
248 }
249
250 /* Check that the returned user data is the same as what was given
251 * as a parameter.
252 */
253 if (ud != returned_ud && !expect_failure) {
254 printk("IPv6 wrong user data %p returned, expected %p\n",
255 returned_ud, ud);
256 zassert_true(0, "exiting");
257 }
258
259 return !fail;
260 }
261
send_ipv6_udp_long_msg(struct net_if * iface,struct in6_addr * src,struct in6_addr * dst,uint16_t src_port,uint16_t dst_port,struct ud * ud,bool expect_failure)262 static bool send_ipv6_udp_long_msg(struct net_if *iface,
263 struct in6_addr *src,
264 struct in6_addr *dst,
265 uint16_t src_port,
266 uint16_t dst_port,
267 struct ud *ud,
268 bool expect_failure)
269 {
270 struct net_pkt *pkt;
271 int ret;
272
273 pkt = net_pkt_alloc_with_buffer(iface,
274 sizeof(ipv6_hop_by_hop_ext_hdr) +
275 sizeof(payload), AF_INET6,
276 IPPROTO_UDP, K_SECONDS(1));
277 zassert_not_null(pkt, "Out of mem");
278
279 if (net_ipv6_create(pkt, src, dst)) {
280 printk("Cannot create IPv6 pkt %p", pkt);
281 zassert_true(0, "exiting");
282 }
283
284 if (net_pkt_write(pkt, (uint8_t *)ipv6_hop_by_hop_ext_hdr,
285 sizeof(ipv6_hop_by_hop_ext_hdr))) {
286 printk("Cannot write IPv6 ext header pkt %p", pkt);
287 zassert_true(0, "exiting");
288 }
289
290 net_pkt_set_ipv6_ext_len(pkt, sizeof(ipv6_hop_by_hop_ext_hdr));
291 net_pkt_set_ipv6_next_hdr(pkt, NET_IPV6_NEXTHDR_HBHO);
292
293 if (net_udp_create(pkt, htons(src_port), htons(dst_port))) {
294 printk("Cannot create IPv6 pkt %p", pkt);
295 zassert_true(0, "exiting");
296 }
297
298 if (net_pkt_write(pkt, (uint8_t *)payload, sizeof(payload))) {
299 printk("Cannot write IPv6 ext header pkt %p", pkt);
300 zassert_true(0, "exiting");
301 }
302
303 net_pkt_cursor_init(pkt);
304 net_ipv6_finalize(pkt, IPPROTO_UDP);
305
306 ret = net_recv_data(iface, pkt);
307 if (ret < 0) {
308 printk("Cannot recv pkt %p, ret %d\n", pkt, ret);
309 zassert_true(0, "exiting");
310 }
311
312 if (k_sem_take(&recv_lock, TIMEOUT)) {
313 /**TESTPOINT: Check for failure*/
314 zassert_true(expect_failure, "Timeout, packet not received");
315 return true;
316 }
317
318 /* Check that the returned user data is the same as what was given
319 * as a parameter.
320 */
321 if (ud != returned_ud && !expect_failure) {
322 printk("IPv6 wrong user data %p returned, expected %p\n",
323 returned_ud, ud);
324 zassert_true(0, "exiting");
325 }
326
327 return !fail;
328 }
329
send_ipv4_udp_msg(struct net_if * iface,struct in_addr * src,struct in_addr * dst,uint16_t src_port,uint16_t dst_port,struct ud * ud,bool expect_failure)330 static bool send_ipv4_udp_msg(struct net_if *iface,
331 struct in_addr *src,
332 struct in_addr *dst,
333 uint16_t src_port,
334 uint16_t dst_port,
335 struct ud *ud,
336 bool expect_failure)
337 {
338 struct net_pkt *pkt;
339 int ret;
340
341 pkt = net_pkt_alloc_with_buffer(iface, 0, AF_INET,
342 IPPROTO_UDP, K_SECONDS(1));
343 zassert_not_null(pkt, "Out of mem");
344
345 if (net_ipv4_create(pkt, src, dst) ||
346 net_udp_create(pkt, htons(src_port), htons(dst_port))) {
347 printk("Cannot create IPv4 UDP pkt %p", pkt);
348 zassert_true(0, "exiting");
349 }
350
351 net_pkt_cursor_init(pkt);
352 net_ipv4_finalize(pkt, IPPROTO_UDP);
353
354 ret = net_recv_data(iface, pkt);
355 if (ret < 0) {
356 printk("Cannot recv pkt %p, ret %d\n", pkt, ret);
357 zassert_true(0, "exiting");
358 }
359
360 if (k_sem_take(&recv_lock, TIMEOUT)) {
361
362 /**TESTPOINT: Check for failure*/
363 zassert_true(expect_failure, "Timeout, packet not received");
364 return true;
365 }
366
367 /* Check that the returned user data is the same as what was given
368 * as a parameter.
369 */
370 if (ud != returned_ud && !expect_failure) {
371 printk("IPv4 wrong user data %p returned, expected %p\n",
372 returned_ud, ud);
373 zassert_true(0, "exiting");
374 }
375
376 return !fail;
377 }
378
set_port(sa_family_t family,struct sockaddr * raddr,struct sockaddr * laddr,uint16_t rport,uint16_t lport)379 static void set_port(sa_family_t family, struct sockaddr *raddr,
380 struct sockaddr *laddr, uint16_t rport,
381 uint16_t lport)
382 {
383 if (family == AF_INET6) {
384 if (raddr) {
385 ((struct sockaddr_in6 *)raddr)->
386 sin6_port = htons(rport);
387 }
388 if (laddr) {
389 ((struct sockaddr_in6 *)laddr)->
390 sin6_port = htons(lport);
391 }
392 } else if (family == AF_INET) {
393 if (raddr) {
394 ((struct sockaddr_in *)raddr)->
395 sin_port = htons(rport);
396 }
397 if (laddr) {
398 ((struct sockaddr_in *)laddr)->
399 sin_port = htons(lport);
400 }
401 }
402 }
403
ZTEST(udp_fn_tests,test_udp)404 ZTEST(udp_fn_tests, test_udp)
405 {
406 if (IS_ENABLED(CONFIG_NET_TC_THREAD_COOPERATIVE)) {
407 k_thread_priority_set(k_current_get(),
408 K_PRIO_COOP(CONFIG_NUM_COOP_PRIORITIES - 1));
409 } else {
410 k_thread_priority_set(k_current_get(), K_PRIO_PREEMPT(9));
411 }
412
413 test_failed = false;
414
415 struct net_conn_handle *handlers[CONFIG_NET_MAX_CONN];
416 struct net_if *iface;
417 struct net_if_addr *ifaddr;
418 struct ud *ud;
419 int ret, i = 0;
420 bool st;
421
422 struct sockaddr_in6 any_addr6;
423 const struct in6_addr in6addr_anyaddr = IN6ADDR_ANY_INIT;
424
425 struct sockaddr_in6 my_addr6;
426 struct in6_addr in6addr_my = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
427 0, 0, 0, 0, 0, 0, 0, 0x1 } } };
428
429 struct sockaddr_in6 peer_addr6;
430 struct in6_addr in6addr_peer = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
431 0, 0, 0, 0x4e, 0x11, 0, 0, 0x2 } } };
432
433 struct sockaddr_in any_addr4;
434 const struct in_addr in4addr_any = { { { 0 } } };
435
436 struct sockaddr_in my_addr4;
437 struct in_addr in4addr_my = { { { 192, 0, 2, 1 } } };
438
439 struct sockaddr_in peer_addr4;
440 struct in_addr in4addr_peer = { { { 192, 0, 2, 9 } } };
441
442 iface = net_if_get_first_by_type(&NET_L2_GET_NAME(DUMMY));
443
444 net_ipaddr_copy(&any_addr6.sin6_addr, &in6addr_anyaddr);
445 any_addr6.sin6_family = AF_INET6;
446
447 net_ipaddr_copy(&my_addr6.sin6_addr, &in6addr_my);
448 my_addr6.sin6_family = AF_INET6;
449
450 net_ipaddr_copy(&peer_addr6.sin6_addr, &in6addr_peer);
451 peer_addr6.sin6_family = AF_INET6;
452
453 net_ipaddr_copy(&any_addr4.sin_addr, &in4addr_any);
454 any_addr4.sin_family = AF_INET;
455
456 net_ipaddr_copy(&my_addr4.sin_addr, &in4addr_my);
457 my_addr4.sin_family = AF_INET;
458
459 net_ipaddr_copy(&peer_addr4.sin_addr, &in4addr_peer);
460 peer_addr4.sin_family = AF_INET;
461
462 k_sem_init(&recv_lock, 0, UINT_MAX);
463
464 ifaddr = net_if_ipv6_addr_add(iface, &in6addr_my, NET_ADDR_MANUAL, 0);
465 if (!ifaddr) {
466 printk("Cannot add %s to interface %p\n",
467 net_sprint_ipv6_addr(&in6addr_my), iface);
468 zassert_true(0, "exiting");
469 }
470
471 ifaddr = net_if_ipv4_addr_add(iface, &in4addr_my, NET_ADDR_MANUAL, 0);
472 if (!ifaddr) {
473 printk("Cannot add %s to interface %p\n",
474 net_sprint_ipv4_addr(&in4addr_my), iface);
475 zassert_true(0, "exiting");
476 }
477
478 #define REGISTER(family, raddr, laddr, rport, lport) \
479 ({ \
480 static struct ud user_data; \
481 \
482 user_data.remote_addr = (struct sockaddr *)raddr; \
483 user_data.local_addr = (struct sockaddr *)laddr; \
484 user_data.remote_port = rport; \
485 user_data.local_port = lport; \
486 user_data.test = "DST="#raddr"-SRC="#laddr"-RP="#rport \
487 "-LP="#lport; \
488 \
489 set_port(family, (struct sockaddr *)raddr, \
490 (struct sockaddr *)laddr, rport, lport); \
491 \
492 ret = net_udp_register(family, \
493 (struct sockaddr *)raddr, \
494 (struct sockaddr *)laddr, \
495 rport, lport, \
496 NULL, test_ok, &user_data, \
497 &handlers[i]); \
498 if (ret) { \
499 printk("UDP register %s failed (%d)\n", \
500 user_data.test, ret); \
501 zassert_true(0, "exiting"); \
502 } \
503 user_data.handle = handlers[i++]; \
504 &user_data; \
505 })
506
507 #define REGISTER_FAIL(raddr, laddr, rport, lport) \
508 ret = net_udp_register(AF_INET, \
509 (struct sockaddr *)raddr, \
510 (struct sockaddr *)laddr, \
511 rport, lport, \
512 NULL, test_fail, INT_TO_POINTER(0), \
513 NULL); \
514 if (!ret) { \
515 printk("UDP register invalid match %s failed\n", \
516 "DST="#raddr"-SRC="#laddr"-RP="#rport"-LP="#lport); \
517 zassert_true(0, "exiting"); \
518 }
519
520 #define UNREGISTER(ud) \
521 ret = net_udp_unregister(ud->handle); \
522 if (ret) { \
523 printk("UDP unregister %p failed (%d)\n", ud->handle, \
524 ret); \
525 zassert_true(0, "exiting"); \
526 }
527
528 #define TEST_IPV6_OK(ud, raddr, laddr, rport, lport) \
529 st = send_ipv6_udp_msg(iface, raddr, laddr, rport, lport, ud, \
530 false); \
531 if (!st) { \
532 printk("%d: UDP test \"%s\" fail\n", __LINE__, \
533 ud->test); \
534 zassert_true(0, "exiting"); \
535 }
536
537 #define TEST_IPV6_LONG_OK(ud, raddr, laddr, rport, lport) \
538 st = send_ipv6_udp_long_msg(iface, raddr, laddr, rport, lport, ud, \
539 false); \
540 if (!st) { \
541 printk("%d: UDP long test \"%s\" fail\n", __LINE__, \
542 ud->test); \
543 zassert_true(0, "exiting"); \
544 }
545
546 #define TEST_IPV4_OK(ud, raddr, laddr, rport, lport) \
547 st = send_ipv4_udp_msg(iface, raddr, laddr, rport, lport, ud, \
548 false); \
549 if (!st) { \
550 printk("%d: UDP test \"%s\" fail\n", __LINE__, \
551 ud->test); \
552 zassert_true(0, "exiting"); \
553 }
554
555 #define TEST_IPV6_FAIL(ud, raddr, laddr, rport, lport) \
556 st = send_ipv6_udp_msg(iface, raddr, laddr, rport, lport, ud, \
557 true); \
558 if (!st) { \
559 printk("%d: UDP neg test \"%s\" fail\n", __LINE__, \
560 ud->test); \
561 zassert_true(0, "exiting"); \
562 }
563
564 #define TEST_IPV4_FAIL(ud, raddr, laddr, rport, lport) \
565 st = send_ipv4_udp_msg(iface, raddr, laddr, rport, lport, ud, \
566 true); \
567 if (!st) { \
568 printk("%d: UDP neg test \"%s\" fail\n", __LINE__, \
569 ud->test); \
570 zassert_true(0, "exiting"); \
571 }
572
573 ud = REGISTER(AF_INET6, &any_addr6, &any_addr6, 1234, 4242);
574 TEST_IPV6_OK(ud, &in6addr_peer, &in6addr_my, 1234, 4242);
575 TEST_IPV6_OK(ud, &in6addr_peer, &in6addr_my, 1234, 4242);
576 TEST_IPV6_LONG_OK(ud, &in6addr_peer, &in6addr_my, 1234, 4242);
577 TEST_IPV6_LONG_OK(ud, &in6addr_peer, &in6addr_my, 1234, 4242);
578 TEST_IPV6_FAIL(ud, &in6addr_peer, &in6addr_my, 1234, 61400);
579 TEST_IPV6_FAIL(ud, &in6addr_peer, &in6addr_my, 1234, 61400);
580 UNREGISTER(ud);
581
582 ud = REGISTER(AF_INET, &any_addr4, &any_addr4, 1234, 4242);
583 TEST_IPV4_OK(ud, &in4addr_peer, &in4addr_my, 1234, 4242);
584 TEST_IPV4_OK(ud, &in4addr_peer, &in4addr_my, 1234, 4242);
585 TEST_IPV4_FAIL(ud, &in4addr_peer, &in4addr_my, 1234, 4325);
586 TEST_IPV4_FAIL(ud, &in4addr_peer, &in4addr_my, 1234, 4325);
587 UNREGISTER(ud);
588
589 ud = REGISTER(AF_INET6, &any_addr6, NULL, 1234, 4242);
590 TEST_IPV6_OK(ud, &in6addr_peer, &in6addr_my, 1234, 4242);
591 TEST_IPV6_OK(ud, &in6addr_peer, &in6addr_my, 1234, 4242);
592 TEST_IPV6_FAIL(ud, &in6addr_peer, &in6addr_my, 1234, 61400);
593 TEST_IPV6_FAIL(ud, &in6addr_peer, &in6addr_my, 1234, 61400);
594 UNREGISTER(ud);
595
596 ud = REGISTER(AF_INET6, NULL, &any_addr6, 1234, 4242);
597 TEST_IPV6_OK(ud, &in6addr_peer, &in6addr_my, 1234, 4242);
598 TEST_IPV6_OK(ud, &in6addr_peer, &in6addr_my, 1234, 4242);
599 TEST_IPV6_LONG_OK(ud, &in6addr_peer, &in6addr_my, 1234, 4242);
600 TEST_IPV6_LONG_OK(ud, &in6addr_peer, &in6addr_my, 1234, 4242);
601 TEST_IPV6_FAIL(ud, &in6addr_peer, &in6addr_my, 1234, 61400);
602 TEST_IPV6_FAIL(ud, &in6addr_peer, &in6addr_my, 1234, 61400);
603 UNREGISTER(ud);
604
605 ud = REGISTER(AF_INET6, &peer_addr6, &my_addr6, 1234, 4242);
606 TEST_IPV6_OK(ud, &in6addr_peer, &in6addr_my, 1234, 4242);
607 TEST_IPV6_FAIL(ud, &in6addr_peer, &in6addr_my, 1234, 4243);
608
609 ud = REGISTER(AF_INET, &peer_addr4, &my_addr4, 1234, 4242);
610 TEST_IPV4_OK(ud, &in4addr_peer, &in4addr_my, 1234, 4242);
611 TEST_IPV4_FAIL(ud, &in4addr_peer, &in4addr_my, 1234, 4243);
612
613 ud = REGISTER(AF_UNSPEC, NULL, NULL, 1234, 42423);
614 TEST_IPV4_OK(ud, &in4addr_peer, &in4addr_my, 1234, 42423);
615 TEST_IPV6_OK(ud, &in6addr_peer, &in6addr_my, 1234, 42423);
616
617 ud = REGISTER(AF_UNSPEC, NULL, NULL, 1234, 0);
618 TEST_IPV4_OK(ud, &in4addr_peer, &in4addr_my, 1234, 42422);
619 TEST_IPV6_OK(ud, &in6addr_peer, &in6addr_my, 1234, 42422);
620 TEST_IPV4_OK(ud, &in4addr_peer, &in4addr_my, 1234, 42422);
621 TEST_IPV6_OK(ud, &in6addr_peer, &in6addr_my, 1234, 42422);
622
623 TEST_IPV4_FAIL(ud, &in4addr_peer, &in4addr_my, 12345, 42421);
624 TEST_IPV6_FAIL(ud, &in6addr_peer, &in6addr_my, 12345, 42421);
625
626 ud = REGISTER(AF_UNSPEC, NULL, NULL, 0, 0);
627 TEST_IPV4_OK(ud, &in4addr_peer, &in4addr_my, 12345, 42421);
628 TEST_IPV6_OK(ud, &in6addr_peer, &in6addr_my, 12345, 42421);
629 TEST_IPV6_LONG_OK(ud, &in6addr_peer, &in6addr_my, 12345, 42421);
630
631 /* Remote addr same as local addr, these two will never match */
632 REGISTER(AF_INET6, &my_addr6, NULL, 1234, 4242);
633 REGISTER(AF_INET, &my_addr4, NULL, 1234, 4242);
634
635 /* IPv4 remote addr and IPv6 remote addr, impossible combination */
636 REGISTER_FAIL(&my_addr4, &my_addr6, 1234, 4242);
637
638 /**TESTPOINT: Check if tests passed*/
639 zassert_false(fail, "Tests failed");
640
641 i--;
642 while (i) {
643 ret = net_udp_unregister(handlers[i]);
644 if (ret < 0 && ret != -ENOENT) {
645 printk("Cannot unregister udp %d\n", i);
646 zassert_true(0, "exiting");
647 }
648
649 i--;
650 }
651
652 zassert_true((net_udp_unregister(NULL) < 0), "Unregister udp failed");
653 zassert_false(test_failed, "udp tests failed");
654 }
655
656 ZTEST_SUITE(udp_fn_tests, NULL, NULL, NULL, NULL, NULL);
657