1 /*
2  * Copyright (c) 2024, Friedt Professional Engineering Services, Inc
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdint.h>
8 
9 #include <zephyr/posix/arpa/inet.h>
10 #include <zephyr/posix/netinet/in.h>
11 #include <zephyr/sys/util.h>
12 #include <zephyr/ztest.h>
13 
ZTEST(net,test_inet_addr)14 ZTEST(net, test_inet_addr)
15 {
16 	in_addr_t ret;
17 	static const struct parm {
18 		const char *in;
19 		int out;
20 	} parms[] = {
21 	/* expect failure */
22 #ifndef CONFIG_ARCH_POSIX
23 		{NULL, (uint32_t)-1}, /* this value will segfault using the host libc */
24 #endif
25 		{".", (uint32_t)-1},
26 		{"..", (uint32_t)-1},
27 		{"...", (uint32_t)-1},
28 		{"-1.-2.-3.-4", (uint32_t)-1},
29 		{"256.65536.4294967296.18446744073709551616", (uint32_t)-1},
30 		{"a.b.c.d", (uint32_t)-1},
31 		{"0.0.0.1234", (uint32_t)-1},
32 		{"0.0.0.12a", (uint32_t)-1},
33 		{" 1.2.3.4", (uint32_t)-1},
34 
35 		/* expect success */
36 		{"0.0.0.0", htonl(0)},
37 		{"000.00.0.0", htonl(0)},
38 		{"127.0.0.1", htonl(0x7f000001)},
39 		{"1.2.3.4", htonl(0x01020304)},
40 		{"1.2.3.4    ", htonl(0x01020304)},
41 		{"0.0.0.123 a", htonl(0x0000007b)},
42 		{"255.255.255.255", htonl(0xffffffff)},
43 	};
44 
45 	ARRAY_FOR_EACH_PTR(parms, p) {
46 		ret = inet_addr(p->in);
47 		zexpect_equal(ret, p->out, "inet_addr(%s) failed. expect: %d actual: %d", p->in,
48 			      p->out, ret);
49 	}
50 }
51