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