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 #include <zephyr/logging/log.h>
10 LOG_MODULE_REGISTER(net_test, CONFIG_NET_IPV6_LOG_LEVEL);
11 
12 #include <zephyr/types.h>
13 #include <stddef.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <zephyr/device.h>
17 #include <zephyr/init.h>
18 #include <zephyr/linker/sections.h>
19 #include <zephyr/random/random.h>
20 
21 #include <zephyr/ztest.h>
22 
23 #include <zephyr/net/net_core.h>
24 #include <zephyr/net/net_pkt.h>
25 #include <zephyr/net/net_ip.h>
26 #include <zephyr/net/dummy.h>
27 
28 #define NET_LOG_ENABLED 1
29 #include "net_private.h"
30 
31 #if defined(CONFIG_NET_IPV6_LOG_LEVEL) || defined(CONFIG_NET_IPV4_LOG_LEVEL)
32 #define DBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
33 #else
34 #define DBG(fmt, ...)
35 #endif
36 
37 extern struct net_if_addr *net_if_ipv6_get_ifaddr(struct net_if *iface,
38 						  const void *addr);
39 
40 static struct net_if *default_iface;
41 static struct net_if *second_iface;
42 
43 #define TEST_BYTE_1(value, expected)				 \
44 	do {							 \
45 		char out[3];					 \
46 		net_byte_to_hex(out, value, 'A', true);		 \
47 		zassert_false(strcmp(out, expected),		 \
48 			      "Test 0x%s failed.\n", expected);	 \
49 	} while (0)
50 
51 #define TEST_BYTE_2(value, expected)				 \
52 	do {							 \
53 		char out[3];					 \
54 		net_byte_to_hex(out, value, 'a', true);		 \
55 		zassert_false(strcmp(out, expected),		 \
56 			      "Test 0x%s failed.\n", expected);	 \
57 	} while (0)
58 
59 #define TEST_LL_6(a, b, c, d, e, f, expected)				\
60 	do {								\
61 		uint8_t ll[] = { a, b, c, d, e, f };			\
62 		zassert_false(strcmp(net_sprint_ll_addr(ll, sizeof(ll)),\
63 				     expected),				\
64 			      "Test %s failed.\n", expected);		\
65 	} while (0)
66 
67 #define TEST_LL_8(a, b, c, d, e, f, g, h, expected)			\
68 	do {								\
69 		uint8_t ll[] = { a, b, c, d, e, f, g, h };			\
70 		zassert_false(strcmp(net_sprint_ll_addr(ll, sizeof(ll)), \
71 				     expected),				\
72 			      "Test %s failed.\n", expected);		\
73 	} while (0)
74 
75 #define LL_ADDR_STR_SIZE sizeof("xx:xx:xx:xx:xx:xx")
76 
77 #define TEST_LL_6_TWO(a, b, c, d, e, f, expected)			\
78 	do {								\
79 		uint8_t ll1[] = { a, b, c, d, e, f };			\
80 		uint8_t ll2[] = { f, e, d, c, b, a };			\
81 		char out[2 * LL_ADDR_STR_SIZE + 1 + 1];	\
82 		snprintk(out, sizeof(out), "%s ",			\
83 			 net_sprint_ll_addr(ll1, sizeof(ll1)));		\
84 		snprintk(out + LL_ADDR_STR_SIZE,			\
85 			 sizeof(out) - LL_ADDR_STR_SIZE, "%s",		\
86 			 net_sprint_ll_addr(ll2, sizeof(ll2)));		\
87 		zassert_false(strcmp(out, expected),			\
88 			      "Test %s failed, got %s\n", expected,	\
89 			      out);					\
90 	} while (0)
91 
92 #define TEST_IPV6(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, expected)  \
93 	do {								     \
94 		struct net_in6_addr addr = { { { a, b, c, d, e, f, g, h,	     \
95 					       i, j, k, l, m, n, o, p } } }; \
96 		char *ptr = net_sprint_ipv6_addr(&addr);		     \
97 		zassert_false(strcmp(ptr, expected),			     \
98 			      "Test %s failed, got %s\n", expected,	     \
99 			      ptr);					     \
100 	} while (0)
101 
102 #define TEST_IPV4(a, b, c, d, expected)					\
103 	do {								\
104 		struct net_in_addr addr = { { { a, b, c, d } } };		\
105 		char *ptr = net_sprint_ipv4_addr(&addr);		\
106 		zassert_false(strcmp(ptr, expected),			\
107 			      "Test %s failed, got %s\n", expected,	\
108 			      ptr);					\
109 	} while (0)
110 
111 struct net_test_context {
112 	uint8_t mac_addr[6];
113 	struct net_linkaddr ll_addr;
114 };
115 
net_test_init(const struct device * dev)116 int net_test_init(const struct device *dev)
117 {
118 	struct net_test_context *net_test_context = dev->data;
119 
120 	net_test_context = net_test_context;
121 
122 	return 0;
123 }
124 
net_test_get_mac(const struct device * dev)125 static uint8_t *net_test_get_mac(const struct device *dev)
126 {
127 	struct net_test_context *context = dev->data;
128 
129 	if (context->mac_addr[2] == 0x00) {
130 		/* 00-00-5E-00-53-xx Documentation RFC 7042 */
131 		context->mac_addr[0] = 0x00;
132 		context->mac_addr[1] = 0x00;
133 		context->mac_addr[2] = 0x5E;
134 		context->mac_addr[3] = 0x00;
135 		context->mac_addr[4] = 0x53;
136 		context->mac_addr[5] = sys_rand8_get();
137 	}
138 
139 	return context->mac_addr;
140 }
141 
net_test_iface_init(struct net_if * iface)142 static void net_test_iface_init(struct net_if *iface)
143 {
144 	uint8_t *mac = net_test_get_mac(net_if_get_device(iface));
145 
146 	net_if_set_link_addr(iface, mac, 6, NET_LINK_ETHERNET);
147 }
148 
tester_send(const struct device * dev,struct net_pkt * pkt)149 static int tester_send(const struct device *dev, struct net_pkt *pkt)
150 {
151 	return 0;
152 }
153 
154 struct net_test_context net_test_context_data;
155 
156 static struct dummy_api net_test_if_api = {
157 	.iface_api.init = net_test_iface_init,
158 	.send = tester_send,
159 };
160 
161 #define _ETH_L2_LAYER DUMMY_L2
162 #define _ETH_L2_CTX_TYPE NET_L2_GET_CTX_TYPE(DUMMY_L2)
163 
164 NET_DEVICE_INIT_INSTANCE(net_addr_test1, "net_addr_test1", iface1,
165 			 net_test_init, NULL,
166 			 &net_test_context_data, NULL,
167 			 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
168 			 &net_test_if_api, _ETH_L2_LAYER, _ETH_L2_CTX_TYPE,
169 			 127);
170 
171 NET_DEVICE_INIT_INSTANCE(net_addr_test2, "net_addr_test2", iface2,
172 			 net_test_init, NULL,
173 			 &net_test_context_data, NULL,
174 			 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
175 			 &net_test_if_api, _ETH_L2_LAYER, _ETH_L2_CTX_TYPE,
176 			 127);
177 
ZTEST(ip_addr_fn,test_ip_addresses)178 ZTEST(ip_addr_fn, test_ip_addresses)
179 {
180 	TEST_BYTE_1(0xde, "DE");
181 	TEST_BYTE_1(0x09, "09");
182 	TEST_BYTE_2(0xa9, "a9");
183 	TEST_BYTE_2(0x80, "80");
184 
185 	TEST_LL_6(0x12, 0x9f, 0xe3, 0x01, 0x7f, 0x00, "12:9F:E3:01:7F:00");
186 	TEST_LL_8(0x12, 0x9f, 0xe3, 0x01, 0x7f, 0x00, 0xff, 0x0f, \
187 		  "12:9F:E3:01:7F:00:FF:0F");
188 
189 	TEST_LL_6_TWO(0x12, 0x9f, 0xe3, 0x01, 0x7f, 0x00,	\
190 		      "12:9F:E3:01:7F:00 00:7F:01:E3:9F:12");
191 
192 	TEST_IPV6(0x20, 1, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, \
193 		  "2001:db8::1");
194 
195 	TEST_IPV6(0x20, 0x01, 0x0d, 0xb8, 0x12, 0x34, 0x56, 0x78,	\
196 		  0x9a, 0xbc, 0xde, 0xf0, 0x01, 0x02, 0x03, 0x04,	\
197 		  "2001:db8:1234:5678:9abc:def0:102:304");
198 
199 	TEST_IPV6(0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	\
200 		  0x0c, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,	\
201 		  "fe80::cb8:0:0:2");
202 
203 	TEST_IPV6(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	\
204 		  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,	\
205 		  "::1");
206 
207 	TEST_IPV6(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	\
208 		  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	\
209 		  "::");
210 
211 	TEST_IPV4(192, 168, 0, 1, "192.168.0.1");
212 	TEST_IPV4(0, 0, 0, 0, "0.0.0.0");
213 	TEST_IPV4(127, 0, 0, 1, "127.0.0.1");
214 }
215 
216 #if defined(CONFIG_NET_IPV6_LOG_LEVEL) || defined(CONFIG_NET_IPV4_LOG_LEVEL)
addr_state_to_str(enum net_addr_state state)217 static const char *addr_state_to_str(enum net_addr_state state)
218 {
219 	switch (state) {
220 	case NET_ADDR_PREFERRED:
221 		return "preferred";
222 	case NET_ADDR_DEPRECATED:
223 		return "deprecated";
224 	case NET_ADDR_TENTATIVE:
225 		return "tentative";
226 	case NET_ADDR_ANY_STATE:
227 		return "invalid";
228 	default:
229 		break;
230 	}
231 
232 	return "unknown";
233 }
234 
get_addr_state(struct net_if * iface,const struct net_in6_addr * addr)235 static const char *get_addr_state(struct net_if *iface,
236 				  const struct net_in6_addr *addr)
237 {
238 	struct net_if_addr *ifaddr;
239 
240 	if (iface == NULL) {
241 		return "<iface not set>";
242 	}
243 
244 	ifaddr = net_if_ipv6_get_ifaddr(iface, addr);
245 	if (ifaddr) {
246 		return addr_state_to_str(ifaddr->addr_state);
247 	}
248 
249 	return "<addr not found>";
250 }
251 #endif
252 
ZTEST(ip_addr_fn,test_ipv6_addresses)253 ZTEST(ip_addr_fn, test_ipv6_addresses)
254 {
255 	struct net_in6_addr loopback = NET_IN6ADDR_LOOPBACK_INIT;
256 	struct net_in6_addr any = NET_IN6ADDR_ANY_INIT;
257 	struct net_in6_addr mcast = { { { 0xff, 0x84, 0, 0, 0, 0, 0, 0,
258 				      0, 0, 0, 0, 0, 0, 0, 0x2 } } };
259 	struct net_in6_addr addr6 = { { { 0xfe, 0x80, 0, 0, 0, 0, 0, 0,
260 				      0, 0, 0, 0, 0, 0, 0, 0x1 } } };
261 	struct net_in6_addr addr6_pref1 = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
262 					    0, 0, 0, 0, 0, 0, 0, 0x1 } } };
263 	struct net_in6_addr addr6_pref2 = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
264 					    0, 0, 0, 0, 0, 0, 0, 0x2 } } };
265 	struct net_in6_addr addr6_pref3 = { { { 0x20, 0x01, 0x0d, 0xb8, 0x64, 0, 0,
266 					    0, 0, 0, 0, 0, 0, 0, 0, 0x2 } } };
267 	struct net_in6_addr ula = { { { 0xfc, 0x00, 0xaa, 0xaa, 0, 0, 0, 0,
268 				    0, 0, 0, 0, 0xd1, 0xd2, 0xd3, 0xd4 } } };
269 	struct net_in6_addr ula2 = { { { 0xfc, 0x00, 0xaa, 0xaa, 0, 0, 0, 0,
270 				0, 0, 0, 0, 0xd1, 0xd2, 0xd3, 2 } } };
271 	struct net_in6_addr ula3 = { { { 0xfc, 0x00, 0xaa, 0xaa, 0, 0, 0, 0,
272 				0, 0, 0, 0, 0xd1, 0xd2, 0xf3, 3 } } };
273 	struct net_in6_addr ula4 = { { { 0xfc, 0x00, 0xaa, 0xaa, 0, 0, 0, 0,
274 				0, 0, 0, 0, 0xd1, 0xd2, 0xf3, 4 } } };
275 	struct net_in6_addr ula5 = { { { 0xfc, 0x00, 0xaa, 0xaa, 0, 0, 0, 0,
276 				0, 0, 0, 0, 0xd1, 0xd2, 0xd3, 0xd5 } } };
277 	struct net_in6_addr *tmp;
278 	const struct net_in6_addr *out;
279 	struct net_if_addr *ifaddr1, *ifaddr2, *ifaddr_ula, *ifaddr_ula3, *ifaddr_ula4;
280 	struct net_if_mcast_addr *ifmaddr1;
281 	struct net_if_ipv6_prefix *prefix;
282 	struct net_if *iface;
283 	int i;
284 
285 	/**TESTPOINT: Check if the IPv6 address is a loopback address*/
286 	zassert_true(net_ipv6_is_addr_loopback(&loopback),
287 		     "IPv6 loopback address check failed.");
288 
289 	/**TESTPOINT: Check if the IPv6 address is a multicast address*/
290 	zassert_true(net_ipv6_is_addr_mcast(&mcast),
291 		     "IPv6 multicast address check failed.");
292 
293 	ifaddr1 = net_if_ipv6_addr_add(default_iface,
294 				      &addr6,
295 				      NET_ADDR_MANUAL,
296 				      0);
297 	/**TESTPOINT: Check if IPv6 interface address is added*/
298 	zassert_not_null(ifaddr1, "IPv6 interface address add failed");
299 
300 	ifaddr2 = net_if_ipv6_addr_lookup(&addr6, NULL);
301 
302 	/**TESTPOINT: Check if addresses match*/
303 	zassert_equal_ptr(ifaddr1, ifaddr2,
304 			  "IPv6 interface address mismatch");
305 
306 	/**TESTPOINT: Check if the IPv6 address is a loopback address*/
307 	zassert_false(net_ipv6_is_my_addr(&loopback),
308 		      "My IPv6 loopback address check failed");
309 
310 	/**TESTPOINT: Check IPv6 address*/
311 	zassert_true(net_ipv6_is_my_addr(&addr6),
312 		     "My IPv6 address check failed");
313 
314 	/**TESTPOINTS: Check IPv6 prefix*/
315 	zassert_true(net_ipv6_is_prefix((uint8_t *)&addr6_pref1,
316 					(uint8_t *)&addr6_pref2, 64),
317 		     "Same IPv6 prefix test failed");
318 
319 	zassert_false(net_ipv6_is_prefix((uint8_t *)&addr6_pref1,
320 					 (uint8_t *)&addr6_pref3, 64),
321 		      "Different IPv6 prefix test failed");
322 
323 	zassert_false(net_ipv6_is_prefix((uint8_t *)&addr6_pref1,
324 					 (uint8_t *)&addr6_pref2, 128),
325 		      "Different full IPv6 prefix test failed");
326 
327 	zassert_false(net_ipv6_is_prefix((uint8_t *)&addr6_pref1,
328 					 (uint8_t *)&addr6_pref3, 255),
329 		      "Too long prefix test failed");
330 
331 	ifmaddr1 = net_if_ipv6_maddr_add(default_iface, &mcast);
332 
333 	/**TESTPOINTS: Check IPv6 addresses*/
334 	zassert_not_null(ifmaddr1, "IPv6 multicast address add failed");
335 
336 	ifmaddr1 = net_if_ipv6_maddr_add(default_iface, &addr6);
337 
338 	zassert_is_null(ifmaddr1,
339 			"IPv6 multicast address could be added failed");
340 
341 	zassert_false(memcmp(net_ipv6_unspecified_address(), &any, sizeof(any)),
342 		      "My IPv6 unspecified address check failed");
343 
344 	ifaddr2 = net_if_ipv6_addr_add(default_iface,
345 				       &addr6,
346 				       NET_ADDR_AUTOCONF,
347 				       0);
348 	zassert_not_null(ifaddr2, "IPv6 ll address autoconf add failed");
349 
350 	ifaddr2->addr_state = NET_ADDR_PREFERRED;
351 
352 	tmp = net_if_ipv6_get_ll(default_iface, NET_ADDR_PREFERRED);
353 	zassert_false(tmp && memcmp(tmp, &addr6.s6_addr,
354 				    sizeof(struct net_in6_addr)),
355 		      "IPv6 ll address fetch failed");
356 
357 	ifaddr2->addr_state = NET_ADDR_DEPRECATED;
358 
359 	tmp = net_if_ipv6_get_ll(default_iface, NET_ADDR_PREFERRED);
360 	zassert_false(tmp && !memcmp(tmp, &any, sizeof(struct net_in6_addr)),
361 		      "IPv6 preferred ll address fetch failed");
362 
363 	ifaddr1 = net_if_ipv6_addr_add(default_iface,
364 				       &addr6_pref2,
365 				       NET_ADDR_AUTOCONF,
366 				       0);
367 	zassert_not_null(ifaddr1, "IPv6 global address autoconf add failed");
368 
369 	ifaddr1->addr_state = NET_ADDR_PREFERRED;
370 
371 	/* Two tests for IPv6, first with interface given, then when
372 	 * iface is NULL
373 	 */
374 	for (i = 0, iface = default_iface; i < 2; i++, iface = NULL) {
375 		ifaddr2->addr_state = NET_ADDR_DEPRECATED;
376 
377 		out = net_if_ipv6_select_src_addr(iface, &addr6_pref1);
378 		zassert_not_null(out,
379 				 "IPv6 src addr selection failed, iface %p\n",
380 				 iface);
381 
382 		DBG("%d: Selected IPv6 address %s state %s, iface %p\n", __LINE__,
383 		    net_sprint_ipv6_addr(out),
384 		    get_addr_state(iface, out),
385 		    iface);
386 
387 		zassert_false(memcmp(out->s6_addr, &addr6_pref2.s6_addr,
388 				     sizeof(struct net_in6_addr)),
389 			      "IPv6 wrong src address selected, iface %p\n",
390 			      iface);
391 
392 		/* Now we should get :: address */
393 		out = net_if_ipv6_select_src_addr(iface, &addr6);
394 		zassert_not_null(out, "IPv6 src any addr selection failed, "
395 				 "iface %p\n", iface);
396 
397 		DBG("%d: Selected IPv6 address %s, iface %p\n", __LINE__,
398 		    net_sprint_ipv6_addr(out),
399 		    iface);
400 
401 		zassert_false(memcmp(out->s6_addr, &any.s6_addr,
402 				     sizeof(struct net_in6_addr)),
403 			      "IPv6 wrong src any address selected, iface %p\n",
404 			      iface);
405 
406 		ifaddr2->addr_state = NET_ADDR_PREFERRED;
407 
408 		/* Now we should get ll address */
409 		out = net_if_ipv6_select_src_addr(iface, &addr6);
410 		zassert_not_null(out,  "IPv6 src ll addr selection failed, "
411 				 "iface %p\n", iface);
412 
413 		DBG("%d: Selected IPv6 address %s state %s, iface %p\n", __LINE__,
414 		    net_sprint_ipv6_addr(out),
415 		    iface != NULL ? get_addr_state(iface, out) : "unknown",
416 		    iface);
417 
418 		zassert_false(memcmp(out->s6_addr, &addr6.s6_addr,
419 				     sizeof(struct net_in6_addr)),
420 			      "IPv6 wrong src ll address selected, iface %p\n",
421 			      iface);
422 	}
423 
424 	zassert_true(net_if_ipv6_addr_rm(default_iface, &addr6),
425 		     "IPv6 removing address failed\n");
426 	zassert_true(net_if_ipv6_addr_rm(default_iface, &addr6_pref2),
427 		     "IPv6 removing address failed\n");
428 
429 	/**TESTPOINTS: Check what IPv6 address is selected when some
430 	 * addresses are in preferred state and some in deprecated state.
431 	 */
432 	prefix = net_if_ipv6_prefix_add(default_iface, &ula, 96, 3600);
433 	zassert_not_null(prefix, "IPv6 ula prefix add failed");
434 
435 	prefix = net_if_ipv6_prefix_add(default_iface, &ula2, 64, 3600);
436 	zassert_not_null(prefix, "IPv6 ula prefix add failed");
437 
438 	ifaddr_ula = net_if_ipv6_addr_add(default_iface, &ula,
439 					  NET_ADDR_AUTOCONF, 0);
440 	zassert_not_null(ifaddr_ula, "IPv6 ula address add failed");
441 
442 	ifaddr_ula->addr_state = NET_ADDR_PREFERRED;
443 
444 	out = net_if_ipv6_select_src_addr(default_iface, &ula2);
445 	zassert_not_null(out, "IPv6 src ula addr selection failed, "
446 			 "iface %p\n", default_iface);
447 
448 	DBG("%d: Selected IPv6 address %s state %s, iface %p\n", __LINE__,
449 	    net_sprint_ipv6_addr(out), get_addr_state(default_iface, out),
450 	    iface);
451 
452 	zassert_false(memcmp(out->s6_addr, &ula.s6_addr, sizeof(struct net_in6_addr)),
453 		      "IPv6 wrong src ula address selected, iface %p\n", iface);
454 
455 	/* Allow selection of deprecated address if no other address
456 	 * is available.
457 	 */
458 	ifaddr_ula->addr_state = NET_ADDR_DEPRECATED;
459 
460 	out = net_if_ipv6_select_src_addr(default_iface, &ula3);
461 	zassert_not_null(out, "IPv6 src ula addr selection failed, "
462 			 "iface %p\n", default_iface);
463 
464 	/* Back to preferred state so that later checks work correctly */
465 	ifaddr_ula->addr_state = NET_ADDR_PREFERRED;
466 
467 	/* Then add another address with preferred state and check that we
468 	 * still do not select the deprecated address even if it is a better match.
469 	 */
470 	ifaddr_ula3 = net_if_ipv6_addr_add(default_iface, &ula3,
471 					   NET_ADDR_AUTOCONF, 0);
472 	zassert_not_null(ifaddr_ula3, "IPv6 ula address add failed");
473 
474 	ifaddr_ula3->addr_state = NET_ADDR_PREFERRED;
475 
476 	out = net_if_ipv6_select_src_addr(default_iface, &ula2);
477 	zassert_not_null(out, "IPv6 src ula addr selection failed, "
478 			 "iface %p\n", default_iface);
479 
480 	DBG("%d: Selected IPv6 address %s state %s, iface %p\n", __LINE__,
481 	    net_sprint_ipv6_addr(out), get_addr_state(default_iface, out),
482 	    iface);
483 
484 	zassert_false(memcmp(out->s6_addr, &ula3.s6_addr, sizeof(struct net_in6_addr)),
485 		      "IPv6 wrong src ula address selected, iface %p\n", iface);
486 
487 	/* Then change the address to deprecated state and check that we
488 	 * do select the deprecated address.
489 	 */
490 	ifaddr_ula3->addr_state = NET_ADDR_DEPRECATED;
491 
492 	out = net_if_ipv6_select_src_addr(default_iface, &ula2);
493 	zassert_not_null(out, "IPv6 src ula addr selection failed, "
494 			 "iface %p\n", default_iface);
495 
496 	DBG("%d: Selected IPv6 address %s state %s, iface %p\n", __LINE__,
497 	    net_sprint_ipv6_addr(out), get_addr_state(default_iface, out),
498 	    iface);
499 
500 	zassert_false(memcmp(out->s6_addr, &ula.s6_addr, sizeof(struct net_in6_addr)),
501 		      "IPv6 wrong src ula address selected, iface %p\n", iface);
502 
503 	/* Then have two deprecated addresses */
504 	ifaddr_ula->addr_state = NET_ADDR_DEPRECATED;
505 
506 	out = net_if_ipv6_select_src_addr(default_iface, &ula2);
507 	zassert_not_null(out, "IPv6 src ula addr selection failed, "
508 			 "iface %p\n", default_iface);
509 
510 	DBG("%d: Selected IPv6 address %s state %s, iface %p\n", __LINE__,
511 	    net_sprint_ipv6_addr(out), get_addr_state(default_iface, out),
512 	    iface);
513 
514 	zassert_false(memcmp(out->s6_addr, &ula3.s6_addr, sizeof(struct net_in6_addr)),
515 		      "IPv6 wrong src ula address selected, iface %p\n", iface);
516 
517 	ifaddr_ula4 = net_if_ipv6_addr_add(default_iface, &ula4,
518 					   NET_ADDR_AUTOCONF, 0);
519 	zassert_not_null(ifaddr_ula4, "IPv6 ula address add failed");
520 
521 	ifaddr_ula4->addr_state = NET_ADDR_DEPRECATED;
522 	ifaddr_ula3->addr_state = NET_ADDR_PREFERRED;
523 
524 	/* There is now one preferred and two deprecated addresses.
525 	 * The preferred address should be selected.
526 	 */
527 	out = net_if_ipv6_select_src_addr(default_iface, &ula5);
528 	zassert_not_null(out, "IPv6 src ula addr selection failed, "
529 			 "iface %p\n", default_iface);
530 
531 	DBG("%d: Selected IPv6 address %s state %s, iface %p\n", __LINE__,
532 	    net_sprint_ipv6_addr(out), get_addr_state(default_iface, out),
533 	    iface);
534 
535 	zassert_false(memcmp(out->s6_addr, &ula3.s6_addr, sizeof(struct net_in6_addr)),
536 		      "IPv6 wrong src ula address selected, iface %p\n", iface);
537 
538 	zassert_true(net_if_ipv6_addr_rm(default_iface, &ula),
539 		     "IPv6 removing address failed\n");
540 
541 	zassert_true(net_if_ipv6_addr_rm(default_iface, &ula3),
542 		     "IPv6 removing address failed\n");
543 
544 	zassert_true(net_if_ipv6_addr_rm(default_iface, &ula4),
545 		     "IPv6 removing address failed\n");
546 }
547 
ZTEST(ip_addr_fn,test_ipv4_ll_address_select_default_first)548 ZTEST(ip_addr_fn, test_ipv4_ll_address_select_default_first)
549 {
550 	struct net_if *iface;
551 	const struct net_in_addr *out;
552 	struct net_if_addr *ifaddr;
553 	struct net_in_addr lladdr4_1 = { { { 169, 254, 0, 1 } } };
554 	struct net_in_addr lladdr4_2 = { { { 169, 254, 0, 3 } } };
555 	struct net_in_addr netmask = { { { 255, 255, 0, 0 } } };
556 	struct net_in_addr dst4 = { { { 169, 254, 0, 2 } } };
557 
558 	ifaddr = net_if_ipv4_addr_add(default_iface, &lladdr4_1, NET_ADDR_MANUAL, 0);
559 	zassert_not_null(ifaddr, "IPv4 interface address add failed");
560 	zassert_true(net_ipv4_is_my_addr(&lladdr4_1),
561 		     "My IPv4 address check failed");
562 
563 	net_if_ipv4_set_netmask_by_addr(default_iface, &lladdr4_1, &netmask);
564 
565 	ifaddr = net_if_ipv4_addr_add(second_iface, &lladdr4_2, NET_ADDR_MANUAL, 0);
566 	zassert_not_null(ifaddr, "IPv4 interface address add failed");
567 	zassert_true(net_ipv4_is_my_addr(&lladdr4_2),
568 		     "My IPv4 address check failed");
569 
570 	net_if_ipv4_set_netmask_by_addr(second_iface, &lladdr4_2, &netmask);
571 
572 	/* In case two network interfaces have two equally good addresses
573 	 * (same net mask), default interface should be selected.
574 	 */
575 	out = net_if_ipv4_select_src_addr(NULL, &dst4);
576 	iface = net_if_ipv4_select_src_iface(&dst4);
577 	zassert_not_null(out, "IPv4 src addr selection failed, iface %p\n",
578 			 iface);
579 
580 	DBG("%d: Selected IPv4 address %s, iface %p\n", __LINE__,
581 	    net_sprint_ipv4_addr(out), iface);
582 
583 	zassert_equal_ptr(iface, default_iface, "Wrong iface selected");
584 	zassert_equal(out->s_addr, lladdr4_1.s_addr,
585 		      "IPv4 wrong src address selected, iface %p\n", iface);
586 }
587 
ZTEST(ip_addr_fn,test_ipv4_ll_address_select)588 ZTEST(ip_addr_fn, test_ipv4_ll_address_select)
589 {
590 	struct net_if *iface;
591 	const struct net_in_addr *out;
592 	struct net_if_addr *ifaddr;
593 	struct net_in_addr lladdr4_1 = { { { 169, 254, 250, 1 } } };
594 	struct net_in_addr lladdr4_2 = { { { 169, 254, 253, 1 } } };
595 	struct net_in_addr netmask_1 = { { { 255, 255, 255, 0 } } };
596 	struct net_in_addr netmask_2 = { { { 255, 255, 255, 252 } } };
597 	struct net_in_addr dst4_1 = { { { 169, 254, 250, 2 } } };
598 	struct net_in_addr dst4_2 = { { { 169, 254, 253, 2 } } };
599 
600 	ifaddr = net_if_ipv4_addr_add(default_iface, &lladdr4_1, NET_ADDR_MANUAL, 0);
601 	zassert_not_null(ifaddr, "IPv4 interface address add failed");
602 	zassert_true(net_ipv4_is_my_addr(&lladdr4_1),
603 		     "My IPv4 address check failed");
604 
605 	net_if_ipv4_set_netmask_by_addr(default_iface, &lladdr4_1, &netmask_1);
606 
607 	ifaddr = net_if_ipv4_addr_add(second_iface, &lladdr4_2, NET_ADDR_MANUAL, 0);
608 	zassert_not_null(ifaddr, "IPv4 interface address add failed");
609 	zassert_true(net_ipv4_is_my_addr(&lladdr4_2),
610 		     "My IPv4 address check failed");
611 
612 	net_if_ipv4_set_netmask_by_addr(second_iface, &lladdr4_2, &netmask_2);
613 
614 	out = net_if_ipv4_select_src_addr(NULL, &dst4_1);
615 	iface = net_if_ipv4_select_src_iface(&dst4_1);
616 	zassert_not_null(out, "IPv4 src addr selection failed, iface %p\n",
617 			 iface);
618 
619 	DBG("%d: Selected IPv4 address %s, iface %p\n", __LINE__,
620 	    net_sprint_ipv4_addr(out), iface);
621 
622 	zassert_equal(out->s_addr, lladdr4_1.s_addr,
623 		      "IPv4 wrong src address selected, iface %p\n", iface);
624 	zassert_equal_ptr(iface, default_iface, "Wrong iface selected");
625 
626 	out = net_if_ipv4_select_src_addr(NULL, &dst4_2);
627 	iface = net_if_ipv4_select_src_iface(&dst4_2);
628 	zassert_not_null(out, "IPv4 src addr selection failed, iface %p\n",
629 			 iface);
630 
631 	DBG("%d: Selected IPv4 address %s, iface %p\n", __LINE__,
632 	    net_sprint_ipv4_addr(out), iface);
633 
634 	zassert_equal(out->s_addr, lladdr4_2.s_addr,
635 		      "IPv4 wrong src address selected, iface %p\n", iface);
636 	zassert_equal_ptr(iface, second_iface, "Wrong iface selected");
637 }
638 
ZTEST(ip_addr_fn,test_ipv4_addresses)639 ZTEST(ip_addr_fn, test_ipv4_addresses)
640 {
641 	const struct net_in_addr *out;
642 	struct net_if_addr *ifaddr1;
643 	struct net_if_mcast_addr *ifmaddr1;
644 	struct net_in_addr addr4 = { { { 192, 168, 0, 1 } } };
645 	struct net_in_addr addr4b = { { { 192, 168, 1, 2 } } };
646 	struct net_in_addr addr4_not_found = { { { 10, 20, 30, 40 } } };
647 	struct net_in_addr lladdr4 = { { { 169, 254, 98, 203 } } };
648 	struct net_in_addr maddr4a = { { { 224, 0, 0, 1 } } };
649 	struct net_in_addr maddr4b = { { { 224, 0, 0, 2 } } };
650 	struct net_in_addr match_addr = { { { 192, 168, 0, 2 } } };
651 	struct net_in_addr fail_addr = { { { 10, 1, 0, 2 } } };
652 	struct net_in_addr netmask = { { { 255, 255, 255, 0 } } };
653 	struct net_in_addr netmask2 = { { { 255, 255, 0, 0 } } };
654 	struct net_in_addr gw = { { { 192, 168, 0, 42 } } };
655 	struct net_in_addr loopback4 = { { { 127, 0, 0, 1 } } };
656 	struct net_in_addr bcast_addr1 = { { { 255, 255, 255, 255 } } };
657 	struct net_in_addr bcast_addr2 = { { { 192, 168, 1, 255 } } };
658 	struct net_in_addr bcast_addr3 = { { { 192, 168, 255, 255 } } };
659 	struct net_in_addr bcast_addr4 = { { { 192, 0, 2, 255 } } };
660 	struct net_in_addr bcast_addr5 = { { { 192, 168, 0, 255 } } };
661 	struct net_if *iface, *iface1, *iface2;
662 	int i, ret;
663 
664 	ifaddr1 = net_if_ipv4_addr_add(default_iface,
665 				       &addr4,
666 				       NET_ADDR_MANUAL,
667 				       0);
668 	zassert_not_null(ifaddr1, "IPv4 interface address add failed");
669 
670 	zassert_true(net_ipv4_is_my_addr(&addr4),
671 		     "My IPv4 address check failed");
672 
673 	net_if_ipv4_set_netmask_by_addr(default_iface, &addr4, &netmask);
674 
675 	ifaddr1 = net_if_ipv4_addr_add(default_iface,
676 				       &lladdr4,
677 				       NET_ADDR_MANUAL,
678 				       0);
679 	zassert_not_null(ifaddr1, "IPv4 interface address add failed");
680 
681 	net_if_ipv4_set_netmask_by_addr(default_iface, &lladdr4, &netmask2);
682 
683 	zassert_true(net_ipv4_is_my_addr(&lladdr4),
684 		     "My IPv4 address check failed");
685 
686 	zassert_false(net_ipv4_is_my_addr(&loopback4),
687 		      "My IPv4 loopback address check failed");
688 
689 	/* Two tests for IPv4, first with interface given, then when
690 	 * iface is NULL
691 	 */
692 	for (i = 0, iface = default_iface; i < 2; i++, iface = NULL) {
693 		out = net_if_ipv4_select_src_addr(iface, &addr4);
694 		zassert_not_null(out,  "IPv4 src addr selection failed, "
695 				 "iface %p\n", iface);
696 
697 		DBG("%d: Selected IPv4 address %s, iface %p\n", __LINE__,
698 		       net_sprint_ipv4_addr(out), iface);
699 
700 		zassert_equal(out->s_addr, addr4.s_addr,
701 			      "IPv4 wrong src address selected, iface %p\n",
702 			      iface);
703 
704 		/* Now we should get ll address */
705 		out = net_if_ipv4_select_src_addr(iface, &lladdr4);
706 		zassert_not_null(out, "IPv4 src ll addr selection failed, "
707 				 "iface %p\n", iface);
708 
709 		DBG("%d: Selected IPv4 address %s, iface %p\n", __LINE__,
710 		       net_sprint_ipv4_addr(out), iface);
711 
712 		zassert_equal(out->s_addr, lladdr4.s_addr,
713 			      "IPv4 wrong src ll address selected, iface %p\n",
714 			      iface);
715 
716 		/* Now we should get 192.168.0.1 address */
717 		out = net_if_ipv4_select_src_addr(iface, &addr4b);
718 		zassert_not_null(out, "IPv4 src any addr selection failed, "
719 				 "iface %p\n", iface);
720 
721 		DBG("%d: Selected IPv4 address %s, iface %p\n", __LINE__,
722 		       net_sprint_ipv4_addr(out), iface);
723 
724 		zassert_equal(out->s_addr, addr4.s_addr,
725 			      "IPv4 wrong src address selected, iface %p\n",
726 			      iface);
727 
728 		/* Now we should get 192.168.0.1 address */
729 		out = net_if_ipv4_select_src_addr(iface, &addr4_not_found);
730 		zassert_not_null(out, "IPv4 src any addr selection failed, "
731 				 "iface %p\n", iface);
732 
733 		DBG("%d: Selected IPv4 address %s, iface %p\n", __LINE__,
734 		       net_sprint_ipv4_addr(out), iface);
735 
736 		zassert_equal(out->s_addr, addr4.s_addr,
737 			      "IPv4 wrong src address selected, iface %p\n",
738 			      iface);
739 	}
740 
741 	iface = default_iface;
742 
743 	net_if_ipv4_set_gw(iface, &gw);
744 
745 	zassert_false(net_ipv4_addr_mask_cmp(iface, &fail_addr),
746 		      "IPv4 wrong match failed");
747 
748 	zassert_true(net_ipv4_addr_mask_cmp(iface, &match_addr),
749 		     "IPv4 match failed");
750 
751 	zassert_true(net_ipv4_is_addr_mcast(&maddr4a),
752 		     "IPv4 multicast address");
753 
754 	zassert_true(net_ipv4_is_addr_mcast(&maddr4b),
755 		     "IPv4 multicast address");
756 
757 	zassert_false(net_ipv4_is_addr_mcast(&addr4), "IPv4 address");
758 
759 	zassert_false(net_ipv4_is_addr_mcast(&bcast_addr1), "IPv4 broadcast address");
760 
761 	ifmaddr1 = net_if_ipv4_maddr_add(default_iface, &maddr4a);
762 	zassert_not_null(ifmaddr1, "IPv4 multicast address add failed");
763 
764 	ifmaddr1 = net_if_ipv4_maddr_add(default_iface, &maddr4b);
765 	zassert_not_null(ifmaddr1, "IPv4 multicast address add failed");
766 
767 	iface = NULL;
768 
769 	iface1 = net_if_get_by_index(1);
770 	iface2 = net_if_get_by_index(2);
771 
772 	ifmaddr1 = net_if_ipv4_maddr_lookup(&maddr4a, &iface);
773 	zassert_not_null(ifmaddr1, "IPv4 multicast address lookup failed");
774 	zassert_equal(iface, iface1, "Interface not found");
775 
776 	ifmaddr1 = net_if_ipv4_maddr_lookup(&maddr4b, &iface);
777 	zassert_not_null(ifmaddr1, "IPv4 multicast address lookup failed");
778 	zassert_equal(iface, iface1, "Interface not found");
779 
780 	ifmaddr1 = net_if_ipv4_maddr_lookup(&maddr4a, &iface2);
781 	zassert_is_null(ifmaddr1, "IPv4 multicast address lookup succeed");
782 
783 	ret = net_if_ipv4_maddr_rm(iface2, &maddr4a);
784 	zassert_false(ret, "IPv4 rm succeed");
785 
786 	ret = net_if_ipv4_maddr_rm(iface1, &maddr4a);
787 	zassert_true(ret, "IPv4 rm failed");
788 
789 	ifmaddr1 = net_if_ipv4_maddr_lookup(&maddr4a, &iface1);
790 	zassert_is_null(ifmaddr1, "IPv4 multicast address lookup succeed");
791 
792 	ret = net_if_ipv4_maddr_rm(iface1, &maddr4b);
793 	zassert_true(ret, "IPv4 rm failed");
794 
795 	ifmaddr1 = net_if_ipv4_maddr_lookup(&maddr4b, &iface1);
796 	zassert_is_null(ifmaddr1, "IPv4 multicast address lookup succeed");
797 
798 	ret = net_ipv4_is_addr_bcast(iface, &bcast_addr1);
799 	zassert_true(ret, "IPv4 address 1 is not broadcast address");
800 
801 	ret = net_ipv4_is_addr_bcast(iface, &bcast_addr2);
802 	zassert_false(ret, "IPv4 address 2 is broadcast address");
803 
804 	ret = net_ipv4_is_addr_bcast(iface, &bcast_addr4);
805 	zassert_false(ret, "IPv4 address 4 is broadcast address");
806 
807 	ret = net_ipv4_is_addr_bcast(iface, &maddr4b);
808 	zassert_false(ret, "IPv4 address is broadcast address");
809 
810 	ret = net_ipv4_is_addr_bcast(iface, &bcast_addr5);
811 	zassert_true(ret, "IPv4 address 5 is not broadcast address");
812 
813 	ret = net_ipv4_is_addr_bcast(iface, &bcast_addr2);
814 	zassert_false(ret, "IPv4 address 2 is broadcast address");
815 
816 	net_if_ipv4_set_netmask_by_addr(iface, &addr4, &netmask2);
817 
818 	ret = net_ipv4_is_addr_bcast(iface, &bcast_addr3);
819 	zassert_true(ret, "IPv4 address 3 is not broadcast address");
820 }
821 
ZTEST(ip_addr_fn,test_ipv6_mesh_addresses)822 ZTEST(ip_addr_fn, test_ipv6_mesh_addresses)
823 {
824 	struct net_if_addr *ifaddr;
825 	const struct net_in6_addr *out;
826 	struct net_in6_addr lla = { { { 0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x54, 0xdb,
827 				    0x88, 0x1c, 0x38, 0x45, 0x57, 0xf4 } } };
828 	struct net_in6_addr ml_eid = { { { 0xfd, 0xe5, 0x8d, 0xba, 0x82, 0xe1, 0,
829 				       0x01, 0x40, 0x16, 0x99, 0x3c, 0x83, 0x99,
830 				       0x35, 0xab } } };
831 	struct net_in6_addr ll_mcast = { { { 0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0,
832 					 0, 0, 0, 0, 0x1 } } };
833 	struct net_in6_addr ml_mcast = { { { 0xff, 0x03, 0, 0, 0, 0, 0, 0, 0, 0, 0,
834 					 0, 0, 0, 0, 0x1 } } };
835 	struct net_if *iface = default_iface;
836 
837 	ifaddr = net_if_ipv6_addr_add(iface, &lla, NET_ADDR_AUTOCONF, 0);
838 	zassert_not_null(ifaddr, "IPv6 ll address autoconf add failed");
839 
840 	ifaddr = net_if_ipv6_addr_add(iface, &ml_eid, NET_ADDR_AUTOCONF, 0);
841 	zassert_not_null(ifaddr, "IPv6 ll address autoconf add failed");
842 
843 	ifaddr->is_mesh_local = true;
844 
845 	zassert_true(net_ipv6_is_addr_mcast_mesh(&ml_mcast),
846 		     "IPv6 multicast mesh address check failed");
847 
848 	out = net_if_ipv6_select_src_addr(iface, &ll_mcast);
849 	zassert_not_null(out, "IPv6 src addr selection failed\n");
850 
851 	DBG("IPv6: destination: %s - selected %s\n",
852 	    net_sprint_ipv6_addr(&ll_mcast), net_sprint_ipv6_addr(out));
853 
854 	zassert_false(memcmp(out->s6_addr, &lla.s6_addr,
855 			     sizeof(struct net_in6_addr)),
856 		      "IPv6 wrong src address selected\n");
857 
858 	out = net_if_ipv6_select_src_addr(iface, &ml_mcast);
859 	zassert_not_null(out, "IPv6 src addr selection failed\n");
860 
861 	DBG("IPv6: destination: %s - selected %s\n",
862 	    net_sprint_ipv6_addr(&ml_mcast), net_sprint_ipv6_addr(out));
863 
864 	zassert_false(memcmp(out->s6_addr, &ml_eid.s6_addr,
865 			     sizeof(struct net_in6_addr)),
866 		      "IPv6 wrong src address selected\n");
867 
868 	zassert_true(net_if_ipv6_addr_rm(iface, &lla),
869 		     "IPv6 removing address failed\n");
870 	zassert_true(net_if_ipv6_addr_rm(iface, &ml_eid),
871 		     "IPv6 removing address failed\n");
872 }
873 
ZTEST(ip_addr_fn,test_private_ipv6_addresses)874 ZTEST(ip_addr_fn, test_private_ipv6_addresses)
875 {
876 	bool ret;
877 	struct {
878 		struct net_in6_addr addr;
879 		bool is_private;
880 	} addrs[] = {
881 		{
882 			.addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
883 				      0, 0, 0, 0, 0, 0, 0x99, 0x1 } } },
884 			.is_private = true,
885 		},
886 		{
887 			.addr = { { { 0xfc, 0x01, 0, 0, 0, 0, 0, 0,
888 				      0, 0, 0, 0, 0, 0, 0, 1 } } },
889 			.is_private = true,
890 		},
891 		{
892 			.addr = { { { 0xfc, 0, 0, 0, 0, 0, 0, 0,
893 				      0, 0, 0, 0, 0, 0, 0, 2 } } },
894 			.is_private = true,
895 		},
896 		{
897 			.addr = { { { 0x20, 0x01, 0x1d, 0xb8, 0, 0, 0, 0,
898 				      0, 0, 0, 0, 0, 0, 0x99, 0x1 } } },
899 			.is_private = false,
900 		},
901 	};
902 
903 	for (int i = 0; i < ARRAY_SIZE(addrs); i++) {
904 		ret = net_ipv6_is_private_addr(&addrs[i].addr);
905 		zassert_equal(ret, addrs[i].is_private, "Address %s check failed",
906 			      net_sprint_ipv6_addr(&addrs[i].addr));
907 	}
908 
909 }
910 
ZTEST(ip_addr_fn,test_private_ipv4_addresses)911 ZTEST(ip_addr_fn, test_private_ipv4_addresses)
912 {
913 	bool ret;
914 	struct {
915 		struct net_in_addr addr;
916 		bool is_private;
917 	} addrs[] = {
918 		{
919 			.addr = { { { 192, 0, 2, 1 } } },
920 			.is_private = true,
921 		},
922 		{
923 			.addr = { { { 10, 1, 2, 1 } } },
924 			.is_private = true,
925 		},
926 		{
927 			.addr = { { { 100, 124, 2, 1 } } },
928 			.is_private = true,
929 		},
930 		{
931 			.addr = { { { 172, 24, 100, 12 } } },
932 			.is_private = true,
933 		},
934 		{
935 			.addr = { { { 172, 15, 254, 255 } } },
936 			.is_private = false,
937 		},
938 		{
939 			.addr = { { { 172, 16, 0, 0 } } },
940 			.is_private = true,
941 		},
942 		{
943 			.addr = { { { 192, 168, 10, 122 } } },
944 			.is_private = true,
945 		},
946 		{
947 			.addr = { { { 192, 51, 100, 255 } } },
948 			.is_private = true,
949 		},
950 		{
951 			.addr = { { { 203, 0, 113, 122 } } },
952 			.is_private = true,
953 		},
954 		{
955 			.addr = { { { 1, 2, 3, 4 } } },
956 			.is_private = false,
957 		},
958 		{
959 			.addr = { { { 192, 1, 32, 4 } } },
960 			.is_private = false,
961 		},
962 	};
963 
964 	for (int i = 0; i < ARRAY_SIZE(addrs); i++) {
965 		ret = net_ipv4_is_private_addr(&addrs[i].addr);
966 		zassert_equal(ret, addrs[i].is_private, "Address %s check failed",
967 			      net_sprint_ipv4_addr(&addrs[i].addr));
968 	}
969 
970 }
971 
clear_addr4(struct net_if * iface,struct net_if_addr * addr,void * user_data)972 void clear_addr4(struct net_if *iface, struct net_if_addr *addr, void *user_data)
973 {
974 	addr->is_used = false;
975 }
976 
test_before(void * f)977 static void test_before(void *f)
978 {
979 	ARG_UNUSED(f);
980 
981 	net_if_ipv4_addr_foreach(default_iface, clear_addr4, NULL);
982 	net_if_ipv4_addr_foreach(second_iface, clear_addr4, NULL);
983 }
984 
test_setup(void)985 void *test_setup(void)
986 {
987 	default_iface = net_if_get_by_index(1);
988 	second_iface = net_if_get_by_index(2);
989 
990 	return NULL;
991 }
992 
993 ZTEST_SUITE(ip_addr_fn, NULL, test_setup, test_before, NULL, NULL);
994