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