1 /*
2  * Copyright (c) 2022 Meta
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "_common.h"
8 
9 #ifdef CONFIG_POSIX_API
10 #include <sys/socket.h>
11 #else
12 #include <zephyr/posix/sys/socket.h>
13 #endif
14 
15 /**
16  * @brief existence test for `<sys/socket.h>`
17  *
18  * @see <a href="https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_socket.h.html">sys/socket.h</a>
19  */
ZTEST(posix_headers,test_sys_socket_h)20 ZTEST(posix_headers, test_sys_socket_h)
21 {
22 	struct cmsghdr cmsg = {0};
23 	struct msghdr mhdr = {0};
24 
25 	zassert_true(sizeof(socklen_t) >= sizeof(uint32_t));
26 	zassert_true((sa_family_t)-1 >= 0);
27 
28 	zassert_not_equal(-1, offsetof(struct sockaddr, sa_family));
29 	/*
30 	 * FIXME: in zephyr, we define struct sockaddr in <zephyr/net/net_ip.h>
31 	 * The sa_data field is defined (incorrectly) as data.
32 	 * Fixing that is a (possibly breaking) tree-wide change.
33 	 */
34 	/* zassert_not_equal(-1, offsetof(struct sockaddr, sa_data)); */ /* not implemented */
35 
36 	zassert_not_equal(-1, offsetof(struct sockaddr_storage, ss_family));
37 	zassert_equal(offsetof(struct sockaddr, sa_family),
38 		      offsetof(struct sockaddr_storage, ss_family));
39 
40 	zassert_not_equal(-1, offsetof(struct msghdr, msg_name));
41 	zassert_not_equal(-1, offsetof(struct msghdr, msg_namelen));
42 	zassert_not_equal(-1, offsetof(struct msghdr, msg_iov));
43 	zassert_not_equal(-1, offsetof(struct msghdr, msg_iovlen));
44 	zassert_not_equal(-1, offsetof(struct msghdr, msg_control));
45 	zassert_not_equal(-1, offsetof(struct msghdr, msg_controllen));
46 	zassert_not_equal(-1, offsetof(struct msghdr, msg_flags));
47 
48 	zassert_not_equal(-1, offsetof(struct cmsghdr, cmsg_len));
49 	zassert_not_equal(-1, offsetof(struct cmsghdr, cmsg_level));
50 	zassert_not_equal(-1, offsetof(struct cmsghdr, cmsg_type));
51 
52 	ARG_UNUSED(CMSG_DATA(&cmsg));
53 	__unused struct cmsghdr *next = CMSG_NXTHDR(&mhdr, &cmsg);
54 	__unused struct cmsghdr *first = CMSG_FIRSTHDR(&mhdr);
55 
56 	zassert_not_equal(-1, offsetof(struct linger, l_onoff));
57 	zassert_not_equal(-1, offsetof(struct linger, l_linger));
58 
59 	zassert_not_equal(-1, SOCK_DGRAM);
60 	zassert_not_equal(-1, SOCK_RAW);
61 	/* zassert_not_equal(-1, SOCK_SEQPACKET); */ /* not implemented */
62 	zassert_not_equal(-1, SOCK_STREAM);
63 
64 	zassert_not_equal(-1, SO_ACCEPTCONN);
65 	zassert_not_equal(-1, SO_BROADCAST);
66 	zassert_not_equal(-1, SO_DEBUG);
67 	zassert_not_equal(-1, SO_DONTROUTE);
68 	zassert_not_equal(-1, SO_ERROR);
69 	zassert_not_equal(-1, SO_KEEPALIVE);
70 	zassert_not_equal(-1, SO_LINGER);
71 	zassert_not_equal(-1, SO_OOBINLINE);
72 	zassert_not_equal(-1, SO_RCVBUF);
73 	zassert_not_equal(-1, SO_RCVLOWAT);
74 	zassert_not_equal(-1, SO_RCVTIMEO);
75 	zassert_not_equal(-1, SO_REUSEADDR);
76 	zassert_not_equal(-1, SO_SNDBUF);
77 	zassert_not_equal(-1, SO_SNDLOWAT);
78 	zassert_not_equal(-1, SO_SNDTIMEO);
79 	zassert_not_equal(-1, SO_TYPE);
80 
81 	zassert_not_equal(-1, SOMAXCONN);
82 
83 	/* zassert_not_equal(-1, MSG_CTRUNC); */ /* not implemented */
84 	/* zassert_not_equal(-1, MSG_DONTROUTE); */ /* not implemented */
85 	/* zassert_not_equal(-1, MSG_EOR); */ /* not implemented */
86 	/* zassert_not_equal(-1, MSG_OOB); */ /* not implemented */
87 	/* zassert_not_equal(-1, MSG_NOSIGNAL); */ /* not implemented */
88 	zassert_not_equal(-1, MSG_PEEK);
89 	zassert_not_equal(-1, MSG_TRUNC);
90 	zassert_not_equal(-1, MSG_WAITALL);
91 
92 	zassert_not_equal(-1, AF_INET);
93 	zassert_not_equal(-1, AF_INET6);
94 	zassert_not_equal(-1, AF_UNIX);
95 	zassert_not_equal(-1, AF_UNSPEC);
96 
97 	zassert_not_equal(-1, SHUT_RD);
98 	zassert_not_equal(-1, SHUT_RDWR);
99 	zassert_not_equal(-1, SHUT_WR);
100 
101 	if (IS_ENABLED(CONFIG_POSIX_NETWORKING)) {
102 		zassert_not_null(accept);
103 		zassert_not_null(bind);
104 		zassert_not_null(connect);
105 		zassert_not_null(getpeername);
106 		zassert_not_null(getsockname);
107 		zassert_not_null(listen);
108 		zassert_not_null(recv);
109 		zassert_not_null(recvfrom);
110 		/* zassert_not_null(recvmsg); */ /* not implemented */
111 		zassert_not_null(send);
112 		zassert_not_null(sendmsg);
113 		zassert_not_null(sendto);
114 		zassert_not_null(setsockopt);
115 		zassert_not_null(shutdown);
116 		zassert_not_null(sockatmark);
117 		zassert_not_null(socket);
118 		zassert_not_null(socketpair);
119 	}
120 }
121