]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-errno-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "errno-util.h"
4 #include "stdio-util.h"
5 #include "string-util.h"
8 TEST(strerror_not_threadsafe
) {
9 /* Just check that strerror really is not thread-safe. */
10 log_info("strerror(%d) → %s", 200, strerror(200));
11 log_info("strerror(%d) → %s", 201, strerror(201));
12 log_info("strerror(%d) → %s", INT_MAX
, strerror(INT_MAX
));
14 log_info("strerror(%d), strerror(%d) → %p, %p", 200, 201, strerror(200), strerror(201));
16 /* This call is not allowed, because the first returned string becomes invalid when
17 * we call strerror the second time:
19 * log_info("strerror(%d), strerror(%d) → %s, %s", 200, 201, strerror(200), strerror(201));
24 /* Just check that STRERROR really is thread-safe. */
25 log_info("STRERROR(%d) → %s", 200, STRERROR(200));
26 log_info("STRERROR(%d) → %s", 201, STRERROR(201));
27 log_info("STRERROR(%d), STRERROR(%d) → %s, %s", 200, 201, STRERROR(200), STRERROR(201));
29 const char *a
= STRERROR(200), *b
= STRERROR(201);
30 assert_se(strstr(a
, "200"));
31 assert_se(strstr(b
, "201"));
33 /* Check with negative values */
34 assert_se(streq(a
, STRERROR(-200)));
35 assert_se(streq(b
, STRERROR(-201)));
37 const char *c
= STRERROR(INT_MAX
);
38 char buf
[DECIMAL_STR_MAX(int)];
39 xsprintf(buf
, "%d", INT_MAX
); /* INT_MAX is hexadecimal, use printf to convert to decimal */
40 log_info("STRERROR(%d) → %s", INT_MAX
, c
);
41 assert_se(strstr(c
, buf
));
44 TEST(STRERROR_OR_ELSE
) {
45 log_info("STRERROR_OR_ELSE(0, \"EOF\") → %s", STRERROR_OR_EOF(0));
46 log_info("STRERROR_OR_ELSE(EPERM, \"EOF\") → %s", STRERROR_OR_EOF(EPERM
));
47 log_info("STRERROR_OR_ELSE(-EPERM, \"EOF\") → %s", STRERROR_OR_EOF(-EPERM
));
56 assert_se(errno
== 12);
59 static void test_unprotect_errno_inner_function(void) {
65 TEST(UNPROTECT_ERRNO
) {
74 assert_se(errno
== 4711);
76 test_unprotect_errno_inner_function();
78 assert_se(errno
== 4711);
84 assert_se(RET_GATHER(x
, 5) == 0);
85 assert_se(RET_GATHER(x
, -5) == -5);
86 assert_se(RET_GATHER(x
, -1) == -5);
88 assert_se(RET_GATHER(x
, y
++) == -5);
92 TEST(ERRNO_IS_TRANSIENT
) {
93 assert_se( ERRNO_IS_NEG_TRANSIENT(-EINTR
));
94 assert_se(!ERRNO_IS_NEG_TRANSIENT(EINTR
));
95 assert_se( ERRNO_IS_TRANSIENT(-EINTR
));
96 assert_se( ERRNO_IS_TRANSIENT(EINTR
));
98 /* Test with type wider than int */
100 assert_se( ERRNO_IS_NEG_TRANSIENT(r
));
102 /* On 64-bit arches, now (int) r == EAGAIN */
103 r
= SSIZE_MAX
- EAGAIN
+ 1;
104 assert_se(!ERRNO_IS_NEG_TRANSIENT(r
));
106 assert_se(!ERRNO_IS_NEG_TRANSIENT(INT_MAX
));
107 assert_se(!ERRNO_IS_NEG_TRANSIENT(INT_MIN
));
108 assert_se(!ERRNO_IS_NEG_TRANSIENT(INTMAX_MAX
));
109 assert_se(!ERRNO_IS_NEG_TRANSIENT(INTMAX_MIN
));
112 DEFINE_TEST_MAIN(LOG_INFO
);