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