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
52 static bool test_failed;
53 static struct k_sem recv_lock;
54
55 struct net_udp_context {
56 uint8_t mac_addr[sizeof(struct net_eth_addr)];
57 struct net_linkaddr ll_addr;
58 };
59
net_udp_dev_init(const struct device * dev)60 int net_udp_dev_init(const struct device *dev)
61 {
62 struct net_udp_context *net_udp_context = dev->data;
63
64 net_udp_context = net_udp_context;
65
66 return 0;
67 }
68
net_udp_get_mac(const struct device * dev)69 static uint8_t *net_udp_get_mac(const struct device *dev)
70 {
71 struct net_udp_context *context = dev->data;
72
73 if (context->mac_addr[2] == 0x00) {
74 /* 00-00-5E-00-53-xx Documentation RFC 7042 */
75 context->mac_addr[0] = 0x00;
76 context->mac_addr[1] = 0x00;
77 context->mac_addr[2] = 0x5E;
78 context->mac_addr[3] = 0x00;
79 context->mac_addr[4] = 0x53;
80 context->mac_addr[5] = sys_rand8_get();
81 }
82
83 return context->mac_addr;
84 }
85
net_udp_iface_init(struct net_if * iface)86 static void net_udp_iface_init(struct net_if *iface)
87 {
88 uint8_t *mac = net_udp_get_mac(net_if_get_device(iface));
89
90 net_if_set_link_addr(iface, mac, 6, NET_LINK_ETHERNET);
91 }
92
93 static int send_status = -EINVAL;
94
tester_send(const struct device * dev,struct net_pkt * pkt)95 static int tester_send(const struct device *dev, struct net_pkt *pkt)
96 {
97 if (!pkt->frags) {
98 DBG("No data to send!\n");
99 return -ENODATA;
100 }
101
102 DBG("Data was sent successfully\n");
103
104 send_status = 0;
105
106 return 0;
107 }
108
109 struct net_udp_context net_udp_context_data;
110
111 static struct dummy_api net_udp_if_api = {
112 .iface_api.init = net_udp_iface_init,
113 .send = tester_send,
114 };
115
116 #define _ETH_L2_LAYER DUMMY_L2
117 #define _ETH_L2_CTX_TYPE NET_L2_GET_CTX_TYPE(DUMMY_L2)
118
119 NET_DEVICE_INIT(net_udp_test, "net_udp_test",
120 net_udp_dev_init, NULL,
121 &net_udp_context_data, NULL,
122 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
123 &net_udp_if_api, _ETH_L2_LAYER, _ETH_L2_CTX_TYPE, 127);
124
test_setup(void)125 static void *test_setup(void)
126 {
127 struct net_if *iface;
128 struct net_if_addr *ifaddr;
129
130 struct net_sockaddr_in6 any_addr6;
131 const struct net_in6_addr in6addr_anyaddr = NET_IN6ADDR_ANY_INIT;
132
133 struct net_sockaddr_in6 my_addr6;
134 struct net_in6_addr in6addr_my = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
135 0, 0, 0, 0, 0, 0, 0, 0x1 } } };
136
137 struct net_sockaddr_in6 peer_addr6;
138 struct net_in6_addr in6addr_peer = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
139 0, 0, 0, 0x4e, 0x11, 0, 0, 0x2 } } };
140
141 struct net_sockaddr_in any_addr4;
142 const struct net_in_addr in4addr_any = { { { 0 } } };
143
144 struct net_sockaddr_in my_addr4;
145 struct net_in_addr in4addr_my = { { { 192, 0, 2, 1 } } };
146
147 struct net_sockaddr_in peer_addr4;
148 struct net_in_addr in4addr_peer = { { { 192, 0, 2, 9 } } };
149
150 iface = net_if_get_first_by_type(&NET_L2_GET_NAME(DUMMY));
151 test_failed = false;
152
153 net_ipaddr_copy(&any_addr6.sin6_addr, &in6addr_anyaddr);
154 any_addr6.sin6_family = NET_AF_INET6;
155
156 net_ipaddr_copy(&my_addr6.sin6_addr, &in6addr_my);
157 my_addr6.sin6_family = NET_AF_INET6;
158
159 net_ipaddr_copy(&peer_addr6.sin6_addr, &in6addr_peer);
160 peer_addr6.sin6_family = NET_AF_INET6;
161
162 net_ipaddr_copy(&any_addr4.sin_addr, &in4addr_any);
163 any_addr4.sin_family = NET_AF_INET;
164
165 net_ipaddr_copy(&my_addr4.sin_addr, &in4addr_my);
166 my_addr4.sin_family = NET_AF_INET;
167
168 net_ipaddr_copy(&peer_addr4.sin_addr, &in4addr_peer);
169 peer_addr4.sin_family = NET_AF_INET;
170
171 k_sem_init(&recv_lock, 0, UINT_MAX);
172
173 ifaddr = net_if_ipv6_addr_add(iface, &in6addr_my, NET_ADDR_MANUAL, 0);
174 if (!ifaddr) {
175 printk("Cannot add %s to interface %p\n",
176 net_sprint_ipv6_addr(&in6addr_my), iface);
177 zassert_true(0, "exiting");
178 }
179
180 ifaddr = net_if_ipv4_addr_add(iface, &in4addr_my, NET_ADDR_MANUAL, 0);
181 if (!ifaddr) {
182 printk("Cannot add %s to interface %p\n",
183 net_sprint_ipv4_addr(&in4addr_my), iface);
184 zassert_true(0, "exiting");
185 }
186
187 return NULL;
188 }
189
ZTEST(net_shell_test_suite,test_net_shell)190 ZTEST(net_shell_test_suite, test_net_shell)
191 {
192 int ret;
193
194 /* Test that command exists */
195 ret = shell_execute_cmd(NULL, "net iface");
196 zassert_equal(ret, 0, "");
197
198 /* There is no foobar command */
199 ret = shell_execute_cmd(NULL, "net foobar");
200 zassert_equal(ret, 1, "");
201 }
202
203 ZTEST_SUITE(net_shell_test_suite, NULL, test_setup, NULL, NULL, NULL);
204