Skip to content

Commit a04f4bc

Browse files
committed
tests: posix: common: add a test for confstr()
Add a test for confstr(), which is required by the POSIX_SINGLE_PROCESS Option Group, as per IEEE 1003.1-2017. Signed-off-by: Christopher Friedt <[email protected]>
1 parent 9a7563b commit a04f4bc

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

tests/posix/common/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ FILE(GLOB app_sources src/*.c)
88
zephyr_include_directories(${ZEPHYR_BASE}/lib/posix)
99

1010
target_sources(app PRIVATE ${app_sources})
11+
12+
target_compile_options(app PRIVATE -U_POSIX_C_SOURCE -D_POSIX_C_SOURCE=200809L)

tests/posix/common/src/confstr.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) 2024, Meta
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <errno.h>
8+
#include <unistd.h>
9+
10+
#include <zephyr/ztest.h>
11+
#include <zephyr/sys/util.h>
12+
13+
ZTEST(confstr, test_confstr)
14+
{
15+
char buf[1];
16+
17+
/* degenerate cases */
18+
{
19+
struct arg {
20+
int name;
21+
char *buf;
22+
size_t len;
23+
};
24+
25+
const struct arg arg1s[] = {
26+
{-1, NULL, 0},
27+
{-1, NULL, sizeof(buf)},
28+
{-1, buf, 0},
29+
{-1, buf, sizeof(buf)},
30+
};
31+
32+
const struct arg arg2s[] = {
33+
{_CS_PATH, NULL, 0},
34+
{_CS_PATH, buf, 0},
35+
};
36+
37+
const struct arg arg3s[] = {
38+
{_CS_PATH, NULL, sizeof(buf)},
39+
};
40+
41+
ARRAY_FOR_EACH_PTR(arg1s, arg) {
42+
errno = 0;
43+
zassert_equal(0, confstr(arg->name, arg->buf, arg->len));
44+
zassert_equal(errno, EINVAL);
45+
}
46+
47+
ARRAY_FOR_EACH_PTR(arg2s, arg) {
48+
errno = 0;
49+
buf[0] = 0xff;
50+
zassert_true(confstr(arg->name, arg->buf, arg->len) > 0);
51+
zassert_equal(errno, 0);
52+
zassert_equal((uint8_t)buf[0], 0xff);
53+
}
54+
55+
ARRAY_FOR_EACH_PTR(arg3s, arg) {
56+
errno = 0;
57+
zassert_true(confstr(arg->name, arg->buf, arg->len) > 0);
58+
zassert_equal(errno, 0);
59+
}
60+
}
61+
62+
buf[0] = 0xff;
63+
zassert_true(confstr(_CS_PATH, buf, sizeof(buf) > 0));
64+
zassert_equal(buf[0], '\0');
65+
}
66+
67+
ZTEST_SUITE(confstr, NULL, NULL, NULL, NULL, NULL);

0 commit comments

Comments
 (0)