1 /* $NetBSD: errno_test.c,v 1.8 2022/09/23 12:15:34 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 #if HAVE_CMOCKA
17
18 #include <errno.h>
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 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 static void
isc_errno_toresult_test(void ** state)93 isc_errno_toresult_test(void **state) {
94 isc_result_t result, expect;
95 size_t i;
96
97 UNUSED(state);
98
99 for (i = 0; i < sizeof(testpair) / sizeof(testpair[0]); i++) {
100 result = isc_errno_toresult(testpair[i].err);
101 expect = testpair[i].result;
102 assert_int_equal(result, expect);
103 }
104 }
105
106 int
main(void)107 main(void) {
108 const struct CMUnitTest tests[] = {
109 cmocka_unit_test(isc_errno_toresult_test),
110 };
111
112 return (cmocka_run_group_tests(tests, NULL, NULL));
113 }
114
115 #else /* HAVE_CMOCKA */
116
117 #include <stdio.h>
118
119 int
main(void)120 main(void) {
121 printf("1..0 # Skipped: cmocka not available\n");
122 return (SKIPPED_TEST_EXIT_CODE);
123 }
124
125 #endif /* if HAVE_CMOCKA */
126