| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "errno-list.h" |
| 4 |
#include "tests.h" |
| 5 |
|
| 6 |
#include "errno-to-name.inc" |
| 7 |
|
| 8 |
TEST(errno_name_no_fallback) { |
| 9 |
ASSERT_NULL(errno_names[0]); |
| 10 |
ASSERT_NULL(errno_name_no_fallback(0)); |
| 11 |
|
| 12 |
for (size_t i = 0; i < ELEMENTSOF(errno_names); i++) |
| 13 |
if (errno_names[i]) { |
| 14 |
const char *mapped = errno_name_no_fallback(i); |
| 15 |
|
| 16 |
if (mapped) |
| 17 |
/* glibc might not know some errno names. |
| 18 |
* This is not an error. */ |
| 19 |
ASSERT_STREQ(mapped, errno_names[i]); |
| 20 |
|
| 21 |
assert_se(errno_from_name(errno_names[i]) == (int) i); |
| 22 |
} |
| 23 |
|
| 24 |
#ifdef ECANCELLED |
| 25 |
/* ECANCELLED is an alias of ECANCELED. */ |
| 26 |
ASSERT_STREQ(errno_name_no_fallback(ECANCELLED), "ECANCELED"); |
| 27 |
#endif |
| 28 |
ASSERT_STREQ(errno_name_no_fallback(ECANCELED), "ECANCELED"); |
| 29 |
|
| 30 |
#ifdef EREFUSED |
| 31 |
/* EREFUSED is an alias of ECONNREFUSED. */ |
| 32 |
ASSERT_STREQ(errno_name_no_fallback(EREFUSED), "ECONNREFUSED"); |
| 33 |
#endif |
| 34 |
ASSERT_STREQ(errno_name_no_fallback(ECONNREFUSED), "ECONNREFUSED"); |
| 35 |
} |
| 36 |
|
| 37 |
TEST(errno_name_full) { |
| 38 |
char buf[ERRNO_NAME_BUF_LEN]; |
| 39 |
|
| 40 |
ASSERT_STREQ(errno_name(0, buf), "0"); |
| 41 |
ASSERT_STREQ(errno_name(EPERM, buf), "EPERM"); |
| 42 |
ASSERT_STREQ(errno_name(ENOENT, buf), "ENOENT"); |
| 43 |
ASSERT_STREQ(errno_name(200, buf), "200"); |
| 44 |
ASSERT_STREQ(errno_name(-200, buf), "200"); |
| 45 |
} |
| 46 |
|
| 47 |
TEST(ERRNO_NAME_FULL) { |
| 48 |
ASSERT_STREQ(ERRNO_NAME(0), "0"); |
| 49 |
ASSERT_STREQ(ERRNO_NAME(EPERM), "EPERM"); |
| 50 |
ASSERT_STREQ(ERRNO_NAME(ENOENT), "ENOENT"); |
| 51 |
ASSERT_STREQ(ERRNO_NAME(200), "200"); |
| 52 |
ASSERT_STREQ(ERRNO_NAME(-200), "200"); |
| 53 |
|
| 54 |
int x = 0; |
| 55 |
ASSERT_STREQ(ERRNO_NAME(++x), "EPERM"); /* Confirm that eval happens just once. */ |
| 56 |
ASSERT_EQ(x, 1); |
| 57 |
} |
| 58 |
|
| 59 |
DEFINE_TEST_MAIN(LOG_INFO); |