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 #if HAVE_CMOCKA
15 
16 #include <errno.h>
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 typedef struct {
31 	int err;
32 	isc_result_t result;
33 } testpair_t;
34 
35 testpair_t testpair[] = { { EPERM, ISC_R_NOPERM },
36 			  { ENOENT, ISC_R_FILENOTFOUND },
37 			  { EIO, ISC_R_IOERROR },
38 			  { EBADF, ISC_R_INVALIDFILE },
39 			  { ENOMEM, ISC_R_NOMEMORY },
40 			  { EACCES, ISC_R_NOPERM },
41 			  { EEXIST, ISC_R_FILEEXISTS },
42 			  { ENOTDIR, ISC_R_INVALIDFILE },
43 			  { EINVAL, ISC_R_INVALIDFILE },
44 			  { ENFILE, ISC_R_TOOMANYOPENFILES },
45 			  { EMFILE, ISC_R_TOOMANYOPENFILES },
46 			  { EPIPE, ISC_R_CONNECTIONRESET },
47 			  { ENAMETOOLONG, ISC_R_INVALIDFILE },
48 			  { ELOOP, ISC_R_INVALIDFILE },
49 #ifdef EOVERFLOW
50 			  { EOVERFLOW, ISC_R_RANGE },
51 #endif /* ifdef EOVERFLOW */
52 #ifdef EAFNOSUPPORT
53 			  { EAFNOSUPPORT, ISC_R_FAMILYNOSUPPORT },
54 #endif /* ifdef EAFNOSUPPORT */
55 #ifdef EADDRINUSE
56 			  { EADDRINUSE, ISC_R_ADDRINUSE },
57 #endif /* ifdef EADDRINUSE */
58 			  { EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL },
59 #ifdef ENETDOWN
60 			  { ENETDOWN, ISC_R_NETDOWN },
61 #endif /* ifdef ENETDOWN */
62 #ifdef ENETUNREACH
63 			  { ENETUNREACH, ISC_R_NETUNREACH },
64 #endif /* ifdef ENETUNREACH */
65 #ifdef ECONNABORTED
66 			  { ECONNABORTED, ISC_R_CONNECTIONRESET },
67 #endif /* ifdef ECONNABORTED */
68 #ifdef ECONNRESET
69 			  { ECONNRESET, ISC_R_CONNECTIONRESET },
70 #endif /* ifdef ECONNRESET */
71 #ifdef ENOBUFS
72 			  { ENOBUFS, ISC_R_NORESOURCES },
73 #endif /* ifdef ENOBUFS */
74 #ifdef ENOTCONN
75 			  { ENOTCONN, ISC_R_NOTCONNECTED },
76 #endif /* ifdef ENOTCONN */
77 #ifdef ETIMEDOUT
78 			  { ETIMEDOUT, ISC_R_TIMEDOUT },
79 #endif /* ifdef ETIMEDOUT */
80 			  { ECONNREFUSED, ISC_R_CONNREFUSED },
81 #ifdef EHOSTDOWN
82 			  { EHOSTDOWN, ISC_R_HOSTDOWN },
83 #endif /* ifdef EHOSTDOWN */
84 #ifdef EHOSTUNREACH
85 			  { EHOSTUNREACH, ISC_R_HOSTUNREACH },
86 #endif /* ifdef EHOSTUNREACH */
87 			  { 0, ISC_R_UNEXPECTED } };
88 
89 /* convert errno to ISC result */
90 static void
isc_errno_toresult_test(void ** state)91 isc_errno_toresult_test(void **state) {
92 	isc_result_t result, expect;
93 	size_t i;
94 
95 	UNUSED(state);
96 
97 	for (i = 0; i < sizeof(testpair) / sizeof(testpair[0]); i++) {
98 		result = isc_errno_toresult(testpair[i].err);
99 		expect = testpair[i].result;
100 		assert_int_equal(result, expect);
101 	}
102 }
103 
104 int
main(void)105 main(void) {
106 	const struct CMUnitTest tests[] = {
107 		cmocka_unit_test(isc_errno_toresult_test),
108 	};
109 
110 	return (cmocka_run_group_tests(tests, NULL, NULL));
111 }
112 
113 #else /* HAVE_CMOCKA */
114 
115 #include <stdio.h>
116 
117 int
main(void)118 main(void) {
119 	printf("1..0 # Skipped: cmocka not available\n");
120 	return (SKIPPED_TEST_EXIT_CODE);
121 }
122 
123 #endif /* if HAVE_CMOCKA */
124