]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-errno-util.c
Merge pull request #28862 from DaanDeMeyer/swap
[thirdparty/systemd.git] / src / test / test-errno-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "errno-util.h"
4 #include "stdio-util.h"
5 #include "string-util.h"
6 #include "tests.h"
7
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));
13
14 log_info("strerror(%d), strerror(%d) → %p, %p", 200, 201, strerror(200), strerror(201));
15
16 /* This call is not allowed, because the first returned string becomes invalid when
17 * we call strerror the second time:
18 *
19 * log_info("strerror(%d), strerror(%d) → %s, %s", 200, 201, strerror(200), strerror(201));
20 */
21 }
22
23 TEST(STRERROR) {
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));
28
29 const char *a = STRERROR(200), *b = STRERROR(201);
30 assert_se(strstr(a, "200"));
31 assert_se(strstr(b, "201"));
32
33 /* Check with negative values */
34 assert_se(streq(a, STRERROR(-200)));
35 assert_se(streq(b, STRERROR(-201)));
36
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));
42 }
43
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));
48 }
49
50 TEST(PROTECT_ERRNO) {
51 errno = 12;
52 {
53 PROTECT_ERRNO;
54 errno = 11;
55 }
56 assert_se(errno == 12);
57 }
58
59 static void test_unprotect_errno_inner_function(void) {
60 PROTECT_ERRNO;
61
62 errno = 2222;
63 }
64
65 TEST(UNPROTECT_ERRNO) {
66 errno = 4711;
67
68 PROTECT_ERRNO;
69
70 errno = 815;
71
72 UNPROTECT_ERRNO;
73
74 assert_se(errno == 4711);
75
76 test_unprotect_errno_inner_function();
77
78 assert_se(errno == 4711);
79 }
80
81 TEST(RET_GATHER) {
82 int x = 0, y = 2;
83
84 assert_se(RET_GATHER(x, 5) == 0);
85 assert_se(RET_GATHER(x, -5) == -5);
86 assert_se(RET_GATHER(x, -1) == -5);
87
88 assert_se(RET_GATHER(x, y++) == -5);
89 assert_se(y == 3);
90 }
91
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));
97
98 /* Test with type wider than int */
99 ssize_t r = -EAGAIN;
100 assert_se( ERRNO_IS_NEG_TRANSIENT(r));
101
102 /* On 64-bit arches, now (int) r == EAGAIN */
103 r = SSIZE_MAX - EAGAIN + 1;
104 assert_se(!ERRNO_IS_NEG_TRANSIENT(r));
105
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));
110 }
111
112 DEFINE_TEST_MAIN(LOG_INFO);