| 1 |
/* |
| 2 |
* Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MPL-2.0 |
| 5 |
* |
| 6 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
* |
| 10 |
* See the COPYRIGHT file distributed with this work for additional |
| 11 |
* information regarding copyright ownership. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <errno.h> |
| 15 |
#include <inttypes.h> |
| 16 |
#include <sched.h> /* IWYU pragma: keep */ |
| 17 |
#include <setjmp.h> |
| 18 |
#include <stdarg.h> |
| 19 |
#include <stddef.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#define UNIT_TESTING |
| 24 |
#include <cmocka.h> |
| 25 |
|
| 26 |
#include <isc/errno.h> |
| 27 |
#include <isc/result.h> |
| 28 |
#include <isc/util.h> |
| 29 |
|
| 30 |
#include <tests/isc.h> |
| 31 |
|
| 32 |
typedef struct { |
| 33 |
int err; |
| 34 |
isc_result_t result; |
| 35 |
} testpair_t; |
| 36 |
|
| 37 |
testpair_t testpair[] = { { EPERM, ISC_R_NOPERM }, |
| 38 |
{ ENOENT, ISC_R_FILENOTFOUND }, |
| 39 |
{ EIO, ISC_R_IOERROR }, |
| 40 |
{ EBADF, ISC_R_INVALIDFILE }, |
| 41 |
{ ENOMEM, ISC_R_NOMEMORY }, |
| 42 |
{ EACCES, ISC_R_NOPERM }, |
| 43 |
{ EEXIST, ISC_R_FILEEXISTS }, |
| 44 |
{ ENOTDIR, ISC_R_INVALIDFILE }, |
| 45 |
{ EINVAL, ISC_R_INVALIDFILE }, |
| 46 |
{ ENFILE, ISC_R_TOOMANYOPENFILES }, |
| 47 |
{ EMFILE, ISC_R_TOOMANYOPENFILES }, |
| 48 |
{ EPIPE, ISC_R_CONNECTIONRESET }, |
| 49 |
{ ENAMETOOLONG, ISC_R_INVALIDFILE }, |
| 50 |
{ ELOOP, ISC_R_INVALIDFILE }, |
| 51 |
#ifdef EOVERFLOW |
| 52 |
{ EOVERFLOW, ISC_R_RANGE }, |
| 53 |
#endif /* ifdef EOVERFLOW */ |
| 54 |
#ifdef EAFNOSUPPORT |
| 55 |
{ EAFNOSUPPORT, ISC_R_FAMILYNOSUPPORT }, |
| 56 |
#endif /* ifdef EAFNOSUPPORT */ |
| 57 |
#ifdef EADDRINUSE |
| 58 |
{ EADDRINUSE, ISC_R_ADDRINUSE }, |
| 59 |
#endif /* ifdef EADDRINUSE */ |
| 60 |
{ EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL }, |
| 61 |
#ifdef ENETDOWN |
| 62 |
{ ENETDOWN, ISC_R_NETDOWN }, |
| 63 |
#endif /* ifdef ENETDOWN */ |
| 64 |
#ifdef ENETUNREACH |
| 65 |
{ ENETUNREACH, ISC_R_NETUNREACH }, |
| 66 |
#endif /* ifdef ENETUNREACH */ |
| 67 |
#ifdef ECONNABORTED |
| 68 |
{ ECONNABORTED, ISC_R_CONNECTIONRESET }, |
| 69 |
#endif /* ifdef ECONNABORTED */ |
| 70 |
#ifdef ECONNRESET |
| 71 |
{ ECONNRESET, ISC_R_CONNECTIONRESET }, |
| 72 |
#endif /* ifdef ECONNRESET */ |
| 73 |
#ifdef ENOBUFS |
| 74 |
{ ENOBUFS, ISC_R_NORESOURCES }, |
| 75 |
#endif /* ifdef ENOBUFS */ |
| 76 |
#ifdef ENOTCONN |
| 77 |
{ ENOTCONN, ISC_R_NOTCONNECTED }, |
| 78 |
#endif /* ifdef ENOTCONN */ |
| 79 |
#ifdef ETIMEDOUT |
| 80 |
{ ETIMEDOUT, ISC_R_TIMEDOUT }, |
| 81 |
#endif /* ifdef ETIMEDOUT */ |
| 82 |
{ ECONNREFUSED, ISC_R_CONNREFUSED }, |
| 83 |
#ifdef EHOSTDOWN |
| 84 |
{ EHOSTDOWN, ISC_R_HOSTDOWN }, |
| 85 |
#endif /* ifdef EHOSTDOWN */ |
| 86 |
#ifdef EHOSTUNREACH |
| 87 |
{ EHOSTUNREACH, ISC_R_HOSTUNREACH }, |
| 88 |
#endif /* ifdef EHOSTUNREACH */ |
| 89 |
{ 0, ISC_R_UNEXPECTED } }; |
| 90 |
|
| 91 |
/* convert errno to ISC result */ |
| 92 |
ISC_RUN_TEST_IMPL(isc_errno_toresult) { |
| 93 |
isc_result_t result, expect; |
| 94 |
size_t i; |
| 95 |
|
| 96 |
for (i = 0; i < sizeof(testpair) / sizeof(testpair[0]); i++) { |
| 97 |
result = isc_errno_toresult(testpair[i].err); |
| 98 |
expect = testpair[i].result; |
| 99 |
assert_int_equal(result, expect); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
ISC_TEST_LIST_START |
| 104 |
|
| 105 |
ISC_TEST_ENTRY(isc_errno_toresult) |
| 106 |
|
| 107 |
ISC_TEST_LIST_END |
| 108 |
|
| 109 |
ISC_TEST_MAIN |