From b30b4bcfd53fea62f3dd6b41411f5b9b62ec79e3 Mon Sep 17 00:00:00 2001 From: Christopher Friedt Date: Fri, 15 Mar 2024 03:59:45 -0400 Subject: [PATCH] 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 --- tests/posix/common/CMakeLists.txt | 2 + tests/posix/common/src/confstr.c | 67 +++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 tests/posix/common/src/confstr.c diff --git a/tests/posix/common/CMakeLists.txt b/tests/posix/common/CMakeLists.txt index 1a4aefa916..aa31ed4504 100644 --- a/tests/posix/common/CMakeLists.txt +++ b/tests/posix/common/CMakeLists.txt @@ -8,3 +8,5 @@ FILE(GLOB app_sources src/*.c) zephyr_include_directories(${ZEPHYR_BASE}/lib/posix) target_sources(app PRIVATE ${app_sources}) + +target_compile_options(app PRIVATE -U_POSIX_C_SOURCE -D_POSIX_C_SOURCE=200809L) diff --git a/tests/posix/common/src/confstr.c b/tests/posix/common/src/confstr.c new file mode 100644 index 0000000000..c1926faf60 --- /dev/null +++ b/tests/posix/common/src/confstr.c @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2024, Meta + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include +#include + +ZTEST(confstr, test_confstr) +{ + char buf[1]; + + /* degenerate cases */ + { + struct arg { + int name; + char *buf; + size_t len; + }; + + const struct arg arg1s[] = { + {-1, NULL, 0}, + {-1, NULL, sizeof(buf)}, + {-1, buf, 0}, + {-1, buf, sizeof(buf)}, + }; + + const struct arg arg2s[] = { + {_CS_PATH, NULL, 0}, + {_CS_PATH, buf, 0}, + }; + + const struct arg arg3s[] = { + {_CS_PATH, NULL, sizeof(buf)}, + }; + + ARRAY_FOR_EACH_PTR(arg1s, arg) { + errno = 0; + zassert_equal(0, confstr(arg->name, arg->buf, arg->len)); + zassert_equal(errno, EINVAL); + } + + ARRAY_FOR_EACH_PTR(arg2s, arg) { + errno = 0; + buf[0] = 0xff; + zassert_true(confstr(arg->name, arg->buf, arg->len) > 0); + zassert_equal(errno, 0); + zassert_equal((uint8_t)buf[0], 0xff); + } + + ARRAY_FOR_EACH_PTR(arg3s, arg) { + errno = 0; + zassert_true(confstr(arg->name, arg->buf, arg->len) > 0); + zassert_equal(errno, 0); + } + } + + buf[0] = 0xff; + zassert_true(confstr(_CS_PATH, buf, sizeof(buf) > 0)); + zassert_equal(buf[0], '\0'); +} + +ZTEST_SUITE(confstr, NULL, NULL, NULL, NULL, NULL);