Debian Codegraph @

src/test/test-errno-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "errno-util.h"
4 #include "stdio-util.h"
5 #include "tests.h"
6
7 TEST(strerror_not_threadsafe) {
8 /* Just check that strerror really is not thread-safe. */
9 log_info("strerror(%d) → %s", 200, strerror(200)); /* NOLINT(bugprone-unsafe-functions) */
10 log_info("strerror(%d) → %s", 201, strerror(201)); /* NOLINT(bugprone-unsafe-functions) */
11 log_info("strerror(%d) → %s", INT_MAX, strerror(INT_MAX)); /* NOLINT(bugprone-unsafe-functions) */
12
13 /* NOLINTNEXTLINE(bugprone-unsafe-functions) */
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_NOT_NULL(strstr(a, "200"));
31 ASSERT_NOT_NULL(strstr(b, "201"));
32
33 /* Check with negative values */
34 ASSERT_STREQ(a, STRERROR(-200));
35 ASSERT_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_NOT_NULL(strstr(c, buf));
42 }
43
44 TEST(STRERROR_OR_EOF) {
45 log_info("STRERROR_OR_EOF(0, \"EOF\") → %s", STRERROR_OR_EOF(0));
46 log_info("STRERROR_OR_EOF(EPERM, \"EOF\") → %s", STRERROR_OR_EOF(EPERM));
47 log_info("STRERROR_OR_EOF(-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_EQ(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_EQ(errno, 4711);
75
76 test_unprotect_errno_inner_function();
77 ASSERT_EQ(errno, 4711);
78 }
79
80 TEST(RET_GATHER) {
81 int x = 0, y = 2;
82
83 ASSERT_EQ(RET_GATHER(x, 5), 0);
84 ASSERT_EQ(RET_GATHER(x, -5), -5);
85 ASSERT_EQ(RET_GATHER(x, -1), -5);
86
87 ASSERT_EQ(RET_GATHER(x, y++), -5);
88 ASSERT_EQ(y, 3);
89 }
90
91 TEST(ERRNO_IS_TRANSIENT) {
92 ASSERT_TRUE(ERRNO_IS_NEG_TRANSIENT(-EINTR));
93 ASSERT_FALSE(ERRNO_IS_NEG_TRANSIENT(EINTR));
94 ASSERT_TRUE(ERRNO_IS_TRANSIENT(-EINTR));
95 ASSERT_TRUE(ERRNO_IS_TRANSIENT(EINTR));
96
97 /* Test with type wider than int */
98 ssize_t r = -EAGAIN;
99 ASSERT_TRUE(ERRNO_IS_NEG_TRANSIENT(r));
100
101 /* On 64-bit arches, now (int) r == EAGAIN */
102 r = SSIZE_MAX - EAGAIN + 1;
103 ASSERT_FALSE(ERRNO_IS_NEG_TRANSIENT(r));
104
105 ASSERT_FALSE(ERRNO_IS_NEG_TRANSIENT(INT_MAX));
106 ASSERT_FALSE(ERRNO_IS_NEG_TRANSIENT(INT_MIN));
107 ASSERT_FALSE(ERRNO_IS_NEG_TRANSIENT(INTMAX_MAX));
108 ASSERT_FALSE(ERRNO_IS_NEG_TRANSIENT(INTMAX_MIN));
109 }
110
111 DEFINE_TEST_MAIN(LOG_INFO);