Skip to content

Commit 9dd7e32

Browse files
committed
tests: posix: add test for the xsi_single_process option group
Add tests for the XSI_SINGLE_PROCESS Option Group. Signed-off-by: Chris Friedt <[email protected]>
1 parent c823280 commit 9dd7e32

File tree

7 files changed

+206
-0
lines changed

7 files changed

+206
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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(xsi_single_process)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
9+
target_sources(app PRIVATE ${app_sources})
10+
11+
target_compile_options(app PRIVATE -U_POSIX_C_SOURCE -D_POSIX_C_SOURCE=200809L)
12+
target_compile_options(app PRIVATE -U_XOPEN_SOURCE -D_XOPEN_SOURCE=700)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CONFIG_ZTEST=y
2+
3+
CONFIG_POSIX_API=y
4+
CONFIG_XSI_SINGLE_PROCESS=y
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2025 Tenstorrent AI ULC
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include <unistd.h>
7+
8+
#include <zephyr/ztest.h>
9+
10+
ZTEST(xsi_single_process, test_gethostid)
11+
{
12+
long id = gethostid();
13+
14+
if (id == -ENOSYS) {
15+
printk("CONFIG_HWINFO not implemented for %s\n", CONFIG_BOARD);
16+
ztest_test_skip();
17+
}
18+
19+
uint32_t id32 = gethostid();
20+
21+
zassert_equal((uint32_t)id, id32, "gethostid() returned inconsistent values",
22+
(uint32_t)id, id32);
23+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2018 Intel Corporation
3+
* Copyright (c) 2023, Meta
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
#include <sys/time.h>
8+
#include <time.h>
9+
#include <unistd.h>
10+
11+
#include <zephyr/ztest.h>
12+
13+
static inline int64_t ts_to_ns(const struct timespec *ts)
14+
{
15+
return ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec;
16+
}
17+
18+
static inline void tv_to_ts(const struct timeval *tv, struct timespec *ts)
19+
{
20+
ts->tv_sec = tv->tv_sec;
21+
ts->tv_nsec = tv->tv_usec * NSEC_PER_USEC;
22+
}
23+
24+
#define _tp_op(_a, _b, _op) (ts_to_ns(_a) _op ts_to_ns(_b))
25+
26+
#define _decl_op(_type, _name, _op) \
27+
static inline _type _name(const struct timespec *_a, const struct timespec *_b) \
28+
{ \
29+
return _tp_op(_a, _b, _op); \
30+
}
31+
32+
_decl_op(bool, tp_ge, >=); /* a >= b */
33+
34+
ZTEST(xsi_single_process, test_gettimeofday)
35+
{
36+
struct timeval tv;
37+
struct timespec ts;
38+
struct timespec rts;
39+
40+
if (false) {
41+
/* undefined behaviour */
42+
errno = 0;
43+
zassert_equal(gettimeofday(NULL, NULL), -1);
44+
zassert_equal(errno, EINVAL);
45+
}
46+
47+
/* Validate gettimeofday API */
48+
zassert_ok(gettimeofday(&tv, NULL));
49+
zassert_ok(clock_gettime(CLOCK_REALTIME, &rts));
50+
51+
/* TESTPOINT: Check if time obtained from
52+
* gettimeofday is same or more than obtained
53+
* from clock_gettime
54+
*/
55+
tv_to_ts(&tv, &ts);
56+
zassert_true(tp_ge(&rts, &ts));
57+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
* Copyright (c) 2025 Tenstorrent AI ULC
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/ztest.h>
8+
9+
ZTEST_SUITE(xsi_single_process, NULL, NULL, NULL, NULL, NULL);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2025 Tenstorrent AI ULC
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include <errno.h>
7+
#include <stdlib.h>
8+
9+
#include <zephyr/ztest.h>
10+
11+
ZTEST(xsi_single_process, test_putenv)
12+
{
13+
char buf[64];
14+
15+
{
16+
/* degenerate cases */
17+
static const char *cases[] = {
18+
NULL,
19+
"",
20+
"=",
21+
"abc",
22+
"42",
23+
"=abc",
24+
/*
25+
* Note:
26+
* There are many poorly-formatted environment variable names and values that are
27+
* invalid (from the perspective of a POSIX shell), but still accepted by setenv()
28+
* and subsequently putenv().
29+
*
30+
* See also tests/posix/single_process/src/env.c
31+
* See also lib/posix/shell/env.c:101
32+
*/
33+
};
34+
35+
ARRAY_FOR_EACH(cases, i) {
36+
char *s;
37+
38+
if (cases[i] == NULL) {
39+
s = NULL;
40+
} else {
41+
strncpy(buf, cases[i], sizeof(buf));
42+
buf[sizeof(buf) - 1] = '\0';
43+
s = buf;
44+
}
45+
46+
errno = 0;
47+
zexpect_equal(-1, putenv(s), "putenv(%s) unexpectedly succeeded", s);
48+
zexpect_not_equal(0, errno, "putenv(%s) did not set errno", s);
49+
}
50+
}
51+
52+
{
53+
static const char *cases[] = {
54+
"FOO=bar",
55+
};
56+
57+
ARRAY_FOR_EACH(cases, i) {
58+
strncpy(buf, cases[i], sizeof(buf));
59+
buf[sizeof(buf) - 1] = '\0';
60+
61+
zexpect_ok(putenv(buf), "putenv(%s) failed: %d", buf, errno);
62+
}
63+
}
64+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
common:
2+
filter: not CONFIG_NATIVE_LIBC
3+
tags:
4+
- posix
5+
- xsi
6+
- single_process
7+
# 1 tier0 platform per supported architecture
8+
platform_key:
9+
- arch
10+
- simulation
11+
integration_platforms:
12+
- qemu_cortex_m0
13+
tests:
14+
portability.xsi.single_process:
15+
extra_configs:
16+
- CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE=256
17+
portability.xsi.single_process.armclang_std_libc:
18+
toolchain_allow: armclang
19+
extra_configs:
20+
- CONFIG_ARMCLANG_STD_LIBC=y
21+
portability.xsi.single_process.arcmwdtlib:
22+
toolchain_allow: arcmwdt
23+
extra_configs:
24+
- CONFIG_ARCMWDT_LIBC=y
25+
portability.xsi.single_process.minimal:
26+
extra_configs:
27+
- CONFIG_MINIMAL_LIBC=y
28+
- CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE=256
29+
portability.xsi.single_process.newlib:
30+
filter: TOOLCHAIN_HAS_NEWLIB == 1
31+
extra_configs:
32+
- CONFIG_NEWLIB_LIBC=y
33+
portability.xsi.single_process.picolibc:
34+
tags: picolibc
35+
filter: CONFIG_PICOLIBC_SUPPORTED
36+
extra_configs:
37+
- CONFIG_PICOLIBC=y

0 commit comments

Comments
 (0)