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