Debian Codegraph @

tests/irs/resconf_test.c

raw
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 <inttypes.h>
17 #include <sched.h> /* IWYU pragma: keep */
18 #include <setjmp.h>
19 #include <stdarg.h>
20 #include <stddef.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #define UNIT_TESTING
27 #include <cmocka.h>
28
29 #include <isc/mem.h>
30 #include <isc/util.h>
31
32 #include <irs/resconf.h>
33
34 static isc_mem_t *mctx = NULL;
35
36 static void
37 setup_test(void) {
38 isc_mem_create(&mctx);
39
40 /*
41 * the caller might run from another directory, but tests
42 * that access test data files must first chdir to the proper
43 * location.
44 */
45 assert_return_code(chdir(TESTS_DIR), 0);
46 }
47
48 static isc_result_t
49 check_number(unsigned int n, unsigned int expected) {
50 return (n == expected) ? ISC_R_SUCCESS : ISC_R_BADNUMBER;
51 }
52
53 static isc_result_t
54 check_attempts(irs_resconf_t *resconf) {
55 return check_number(irs_resconf_getattempts(resconf), 4);
56 }
57
58 static isc_result_t
59 check_timeout(irs_resconf_t *resconf) {
60 return check_number(irs_resconf_gettimeout(resconf), 1);
61 }
62
63 static isc_result_t
64 check_ndots(irs_resconf_t *resconf) {
65 return check_number(irs_resconf_getndots(resconf), 2);
66 }
67
68 static isc_result_t
69 check_options(irs_resconf_t *resconf) {
70 if (irs_resconf_getattempts(resconf) != 3) {
71 return ISC_R_BADNUMBER; /* default value only */
72 }
73
74 if (irs_resconf_getndots(resconf) != 2) {
75 return ISC_R_BADNUMBER;
76 }
77
78 if (irs_resconf_gettimeout(resconf) != 1) {
79 return ISC_R_BADNUMBER;
80 }
81
82 return ISC_R_SUCCESS;
83 }
84
85 /* test irs_resconf_load() */
86 static void
87 irs_resconf_load_test(void **state) {
88 isc_result_t result;
89 irs_resconf_t *resconf = NULL;
90 unsigned int i;
91 struct {
92 const char *file;
93 isc_result_t loadres;
94 isc_result_t (*check)(irs_resconf_t *resconf);
95 isc_result_t checkres;
96 } tests[] = {
97 { "testdata/domain.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
98 { "testdata/nameserver-v4.conf", ISC_R_SUCCESS, NULL,
99 ISC_R_SUCCESS },
100 { "testdata/nameserver-v6.conf", ISC_R_SUCCESS, NULL,
101 ISC_R_SUCCESS },
102 { "testdata/nameserver-v6-scoped.conf", ISC_R_SUCCESS, NULL,
103 ISC_R_SUCCESS },
104 { "testdata/options-attempts.conf", ISC_R_SUCCESS,
105 check_attempts, ISC_R_SUCCESS },
106 { "testdata/options-debug.conf", ISC_R_SUCCESS, NULL,
107 ISC_R_SUCCESS },
108 { "testdata/options-ndots.conf", ISC_R_SUCCESS, check_ndots,
109 ISC_R_SUCCESS },
110 { "testdata/options-timeout.conf", ISC_R_SUCCESS, check_timeout,
111 ISC_R_SUCCESS },
112 { "testdata/options-unknown.conf", ISC_R_SUCCESS, NULL,
113 ISC_R_SUCCESS },
114 { "testdata/options.conf", ISC_R_SUCCESS, check_options,
115 ISC_R_SUCCESS },
116 { "testdata/options-bad-ndots.conf", ISC_R_RANGE, NULL,
117 ISC_R_SUCCESS },
118 { "testdata/options-empty.conf", ISC_R_UNEXPECTEDEND, NULL,
119 ISC_R_SUCCESS },
120 { "testdata/port.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
121 { "testdata/resolv.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
122 { "testdata/search.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
123 { "testdata/sortlist-v4.conf", ISC_R_SUCCESS, NULL,
124 ISC_R_SUCCESS },
125 { "testdata/timeout.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
126 { "testdata/unknown.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS }
127 };
128
129 UNUSED(state);
130
131 setup_test();
132
133 for (i = 0; i < sizeof(tests) / sizeof(tests[1]); i++) {
134 result = irs_resconf_load(mctx, tests[i].file, &resconf);
135 if (result != tests[i].loadres) {
136 fail_msg("# unexpected result %s loading %s",
137 isc_result_totext(result), tests[i].file);
138 }
139
140 if (result == ISC_R_SUCCESS && resconf == NULL) {
141 fail_msg("# NULL on success loading %s", tests[i].file);
142 } else if (result != ISC_R_SUCCESS && resconf != NULL) {
143 fail_msg("# non-NULL on failure loading %s",
144 tests[i].file);
145 }
146
147 if (resconf != NULL && tests[i].check != NULL) {
148 result = (tests[i].check)(resconf);
149 if (result != tests[i].checkres) {
150 fail_msg("# unexpected result %s loading %s",
151 isc_result_totext(result),
152 tests[i].file);
153 }
154 }
155 if (resconf != NULL) {
156 irs_resconf_destroy(&resconf);
157 }
158 }
159
160 isc_mem_detach(&mctx);
161 }
162
163 int
164 main(void) {
165 const struct CMUnitTest tests[] = {
166 cmocka_unit_test(irs_resconf_load_test),
167 };
168
169 return cmocka_run_group_tests(tests, NULL, NULL);
170 }
171
172 #else /* HAVE_CMOCKA */
173
174 #include <stdio.h>
175
176 int
177 main(void) {
178 printf("1..0 # Skipped: cmocka not available\n");
179 return SKIPPED_TEST_EXIT_CODE;
180 }
181
182 #endif /* if HAVE_CMOCKA */