Skip to content

Commit 9090db5

Browse files
committed
tests: posix: existence tests for standard POSIX includes
Add a trivial suite that simply ensures headers exist and that they supply standard symbols and constants. These tests are intended to be ordered exactly as the respective feature appears in the respective specification at https://pubs.opengroup.org/onlinepubs/9699919799 Over time, as POSIX support improves, we can enable additional checks. If `CONFIG_POSIX_API=n`, then we simply ensure that the header can be included, that constants and structures exist, including the existence of required fields in each structure. We check that a constant exist, by comparing its value against an arbitrary number. If the constant does not exist, it would of course be a compile error. ``` zassert_not_equal(-1, POLLIN); ``` We check that a structure contains required fields by comparing the field offset with an arbitrary number. If the field does not exist, of course, there would be a compile error. ``` zassert_not_equal(-1, offsetof(struct pollfd, fd)); ``` For non-scalar constants, we simply attempt to assign a value to the specific type: ``` struct in6_addr any6 = IN6ADDR_ANY_INIT; ``` If `CONFIG_POSIX_API=y`, then we additionally check that required functions are non-NULL (limited to what is currently supported in Zephyr). ``` zassert_not_null(pthread_create); ``` Note: functional verification tests should be done outside of this test suite. Signed-off-by: Chris Friedt <[email protected]>
1 parent 35a7b23 commit 9090db5

24 files changed

+1533
-0
lines changed

tests/posix/headers/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(posix_headers)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})

tests/posix/headers/prj.conf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
CONFIG_POSIX_API=y
2+
CONFIG_ZTEST=y
3+
CONFIG_ZTEST_NEW_API=y
4+
5+
# for POSIX_FS
6+
CONFIG_FILE_SYSTEM=y
7+
8+
# for select to work
9+
CONFIG_NET_TEST=y
10+
CONFIG_TEST_RANDOM_GENERATOR=y
11+
CONFIG_NET_SOCKETS=y
12+
CONFIG_NETWORKING=y
13+
14+
# for when CONFIG_POSIX_API is not selected
15+
CONFIG_PTHREAD_IPC=y
16+
CONFIG_POSIX_FS=y
17+
CONFIG_POSIX_CLOCK=y
18+
CONFIG_POSIX_MQUEUE=y
19+
CONFIG_EVENTFD=y
20+
CONFIG_GETOPT=y

tests/posix/headers/src/_common.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (c) 2022 Meta
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
9+
#include <limits.h>
10+
#include <stddef.h>
11+
#include <stdint.h>
12+
13+
#include <zephyr/ztest.h>

tests/posix/headers/src/_headers.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2022 Meta
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
9+
#include <getopt.h>
10+
11+
#ifdef CONFIG_POSIX_API
12+
13+
#include <arpa/inet.h>
14+
#include <dirent.h>
15+
#include <mqueue.h>
16+
#include <net/if.h>
17+
#include <netdb.h>
18+
#include <netinet/in.h>
19+
#include <netinet/tcp.h>
20+
#include <poll.h>
21+
#include <pthread.h>
22+
#include <sched.h>
23+
#include <semaphore.h>
24+
#include <signal.h>
25+
#include <sys/eventfd.h>
26+
#include <sys/ioctl.h>
27+
#include <sys/select.h>
28+
#include <sys/socket.h>
29+
#include <sys/time.h>
30+
#include <unistd.h>
31+
32+
#else
33+
34+
#include <zephyr/posix/arpa/inet.h>
35+
#include <zephyr/posix/dirent.h>
36+
#include <zephyr/posix/mqueue.h>
37+
#include <zephyr/posix/net/if.h>
38+
#include <zephyr/posix/netdb.h>
39+
#include <zephyr/posix/netinet/in.h>
40+
#include <zephyr/posix/netinet/tcp.h>
41+
#include <zephyr/posix/poll.h>
42+
#include <zephyr/posix/pthread.h>
43+
#include <zephyr/posix/sched.h>
44+
#include <zephyr/posix/semaphore.h>
45+
#include <zephyr/posix/signal.h>
46+
#include <zephyr/posix/sys/eventfd.h>
47+
#include <zephyr/posix/sys/ioctl.h>
48+
#include <zephyr/posix/sys/select.h>
49+
#include <zephyr/posix/sys/socket.h>
50+
#include <zephyr/posix/sys/time.h>
51+
#include <zephyr/posix/unistd.h>
52+
53+
#endif

tests/posix/headers/src/_main.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* Copyright (c) 2022 Meta
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "_common.h"
8+
#include "_headers.h"
9+
10+
ZTEST_SUITE(posix_headers, NULL, NULL, NULL, NULL, NULL);

tests/posix/headers/src/arpa_inet_h.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2022 Meta
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "_common.h"
8+
#include "_headers.h"
9+
10+
/**
11+
* @brief existence test for `<arpa/inet.h>`
12+
*
13+
* @see <a href="https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/arpa_inet.h.html">arpa/inet.h</a>
14+
*/
15+
ZTEST(posix_headers, test_arpa_inet_h)
16+
{
17+
zassert_not_equal(-1, htonl(0));
18+
zassert_not_equal(-1, htons(0));
19+
zassert_not_equal(-1, ntohl(0));
20+
zassert_not_equal(-1, ntohs(0));
21+
22+
if (IS_ENABLED(CONFIG_POSIX_API)) {
23+
/* zassert_not_null(inet_addr); */ /* not implemented */
24+
/* zassert_not_null(inet_ntoa); */ /* not implemented */
25+
zassert_not_null(inet_ntop);
26+
zassert_not_null(inet_pton);
27+
}
28+
}

tests/posix/headers/src/dirent_h.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2022 Meta
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "_common.h"
8+
#include "_headers.h"
9+
10+
/**
11+
* @brief existence test for `<dirent.h>`
12+
*
13+
* @see <a href="https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/dirent.h.html">dirent.h</a>
14+
*/
15+
ZTEST(posix_headers, test_dirent_h)
16+
{
17+
zassert_not_equal((DIR *)-1, (DIR *)NULL);
18+
19+
zassert_not_equal(-1, offsetof(struct dirent, d_ino));
20+
zassert_not_equal(-1, offsetof(struct dirent, d_name));
21+
22+
if (IS_ENABLED(CONFIG_POSIX_API)) {
23+
/* zassert_not_null(alphasort); */ /* not implemented */
24+
zassert_not_null(closedir);
25+
/* zassert_not_null(dirfd); */ /* not implemented */
26+
/* zassert_not_null(fdopendir); */ /* not implemented */
27+
zassert_not_null(opendir);
28+
zassert_not_null(readdir);
29+
/* zassert_not_null(readdir_r); */ /* not implemented */
30+
/* zassert_not_null(rewinddir); */ /* not implemented */
31+
/* zassert_not_null(scandir); */ /* not implemented */
32+
/* zassert_not_null(seekdir); */ /* not implemented */
33+
/* zassert_not_null(telldir); */ /* not implemented */
34+
}
35+
}

tests/posix/headers/src/mqueue_h.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2022 Meta
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "_common.h"
8+
#include "_headers.h"
9+
10+
/**
11+
* @brief existence test for `<mqueue.h>`
12+
*
13+
* @see <a href="https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/mqueue.h.html">mqueue.h</a>
14+
*/
15+
ZTEST(posix_headers, test_mqueue_h)
16+
{
17+
zassert_not_equal((mqd_t)-1, (mqd_t)NULL);
18+
19+
zassert_not_equal(-1, offsetof(struct mq_attr, mq_flags));
20+
zassert_not_equal(-1, offsetof(struct mq_attr, mq_maxmsg));
21+
zassert_not_equal(-1, offsetof(struct mq_attr, mq_msgsize));
22+
zassert_not_equal(-1, offsetof(struct mq_attr, mq_curmsgs));
23+
24+
if (IS_ENABLED(CONFIG_POSIX_API)) {
25+
zassert_not_null(mq_close);
26+
zassert_not_null(mq_getattr);
27+
/* zassert_not_null(mq_notify); */ /* not implemented */
28+
zassert_not_null(mq_open);
29+
zassert_not_null(mq_receive);
30+
zassert_not_null(mq_send);
31+
zassert_not_null(mq_setattr);
32+
zassert_not_null(mq_timedreceive);
33+
zassert_not_null(mq_timedsend);
34+
zassert_not_null(mq_unlink);
35+
}
36+
}

tests/posix/headers/src/net_if_h.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2022 Meta
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "_common.h"
8+
#include "_headers.h"
9+
10+
/**
11+
* @brief existence test for `<net/if.h>`
12+
*
13+
* @see <a href="https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/net_if.h.html">net/if.h</a>
14+
*/
15+
ZTEST(posix_headers, test_net_if_h)
16+
{
17+
/* zassert_not_equal(-1, offsetof(struct if_nameindex, if_index)); */ /* not implemented */
18+
/* zassert_not_equal(-1, offsetof(struct if_nameindex, if_name)); */ /* not implemented */
19+
20+
/* zassert_not_equal(-1, IF_NAMESIZE); */ /* not implemented */
21+
22+
if (IS_ENABLED(CONFIG_POSIX_API)) {
23+
/* zassert_not_null(if_freenameindex); */ /* not implemented */
24+
/* zassert_not_null(if_indextoname); */ /* not implemented */
25+
/* zassert_not_null(if_nameindex); */ /* not implemented */
26+
/* zassert_not_null(if_nametoindex); */ /* not implemented */
27+
}
28+
}

tests/posix/headers/src/netdb_h.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright (c) 2022 Meta
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "_common.h"
8+
#include "_headers.h"
9+
10+
/**
11+
* @brief existence test for `<netdb.h>`
12+
*
13+
* @see <a href="https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/netdb.h.html>netdb.h</a>
14+
*/
15+
ZTEST(posix_headers, test_netdb_h)
16+
{
17+
/* zassert_not_equal(-1, offsetof(struct hostent, h_name)); */ /* not implemented */
18+
/* zassert_not_equal(-1, offsetof(struct hostent, h_aliases)); */ /* not implemented */
19+
/* zassert_not_equal(-1, offsetof(struct hostent, h_addrtype)); */ /* not implemented */
20+
/* zassert_not_equal(-1, offsetof(struct hostent, h_length)); */ /* not implemented */
21+
/* zassert_not_equal(-1, offsetof(struct hostent, h_addr_list)); */ /* not implemented */
22+
23+
/* zassert_not_equal(-1, offsetof(struct netent, n_name)); */ /* not implemented */
24+
/* zassert_not_equal(-1, offsetof(struct netent, n_aliases)); */ /* not implemented */
25+
/* zassert_not_equal(-1, offsetof(struct netent, n_addrtype)); */ /* not implemented */
26+
/* zassert_not_equal(-1, offsetof(struct netent, n_net)); */ /* not implemented */
27+
28+
/* zassert_not_equal(-1, offsetof(struct protoent, p_name)); */ /* not implemented */
29+
/* zassert_not_equal(-1, offsetof(struct protoent, p_aliases)); */ /* not implemented */
30+
/* zassert_not_equal(-1, offsetof(struct protoent, p_proto)); */ /* not implemented */
31+
32+
/* zassert_not_equal(-1, offsetof(struct servent, s_name)); */ /* not implemented */
33+
/* zassert_not_equal(-1, offsetof(struct servent, s_aliases)); */ /* not implemented */
34+
/* zassert_not_equal(-1, offsetof(struct servent, s_port)); */ /* not implemented */
35+
/* zassert_not_equal(-1, offsetof(struct servent, s_proto)); */ /* not implemented */
36+
37+
/* zassert_equal(IPPORT_RESERVED, UINT16_MAX); */ /* not implemented */
38+
39+
zassert_not_equal(-1, offsetof(struct addrinfo, ai_flags));
40+
zassert_not_equal(-1, offsetof(struct addrinfo, ai_family));
41+
zassert_not_equal(-1, offsetof(struct addrinfo, ai_socktype));
42+
zassert_not_equal(-1, offsetof(struct addrinfo, ai_protocol));
43+
zassert_not_equal(-1, offsetof(struct addrinfo, ai_addrlen));
44+
zassert_not_equal(-1, offsetof(struct addrinfo, ai_addr));
45+
zassert_not_equal(-1, offsetof(struct addrinfo, ai_canonname));
46+
zassert_not_equal(-1, offsetof(struct addrinfo, ai_next));
47+
48+
zassert_not_equal(-1, AI_PASSIVE);
49+
zassert_not_equal(-1, AI_CANONNAME);
50+
zassert_not_equal(-1, AI_NUMERICHOST);
51+
zassert_not_equal(-1, AI_NUMERICSERV);
52+
zassert_not_equal(-1, AI_V4MAPPED);
53+
zassert_not_equal(-1, AI_ALL);
54+
zassert_not_equal(-1, AI_ADDRCONFIG);
55+
56+
zassert_not_equal(-1, NI_NOFQDN);
57+
zassert_not_equal(-1, NI_NUMERICHOST);
58+
zassert_not_equal(-1, NI_NAMEREQD);
59+
zassert_not_equal(-1, NI_NUMERICSERV);
60+
/* zassert_not_equal(-1, NI_NUMERICSCOPE); */ /* not implemented */
61+
zassert_not_equal(-1, NI_DGRAM);
62+
63+
zassert_not_equal(-1, EAI_AGAIN);
64+
zassert_equal(-1, EAI_BADFLAGS);
65+
zassert_not_equal(-1, EAI_FAIL);
66+
zassert_not_equal(-1, EAI_FAMILY);
67+
zassert_not_equal(-1, EAI_MEMORY);
68+
zassert_not_equal(-1, EAI_NONAME);
69+
zassert_not_equal(-1, EAI_SERVICE);
70+
zassert_not_equal(-1, EAI_SOCKTYPE);
71+
zassert_not_equal(-1, EAI_SYSTEM);
72+
zassert_not_equal(-1, EAI_OVERFLOW);
73+
74+
if (IS_ENABLED(CONFIG_POSIX_API)) {
75+
/* zassert_not_null(endhostent); */ /* not implemented */
76+
/* zassert_not_null(endnetent); */ /* not implemented */
77+
/* zassert_not_null(endprotoent); */ /* not implemented */
78+
/* zassert_not_null(endservent); */ /* not implemented */
79+
zassert_not_null(freeaddrinfo);
80+
zassert_not_null(gai_strerror);
81+
zassert_not_null(getaddrinfo);
82+
/* zassert_not_null(gethostent); */ /* not implemented */
83+
zassert_not_null(getnameinfo);
84+
/* zassert_not_null(getnetbyaddr); */ /* not implemented */
85+
/* zassert_not_null(getnetbyname); */ /* not implemented */
86+
/* zassert_not_null(getnetent); */ /* not implemented */
87+
/* zassert_not_null(getprotobyname); */ /* not implemented */
88+
/* zassert_not_null(getprotobynumber); */ /* not implemented */
89+
/* zassert_not_null(getprotoent); */ /* not implemented */
90+
/* zassert_not_null(getservbyname); */ /* not implemented */
91+
/* zassert_not_null(getservbyport); */ /* not implemented */
92+
/* zassert_not_null(getservent); */ /* not implemented */
93+
/* zassert_not_null(sethostent); */ /* not implemented */
94+
/* zassert_not_null(setnetent); */ /* not implemented */
95+
/* zassert_not_null(setprotoent); */ /* not implemented */
96+
/* zassert_not_null(setservent); */ /* not implemented */
97+
}
98+
}

0 commit comments

Comments
 (0)