1 /*	$NetBSD: resconf_test.c,v 1.8 2022/09/23 12:15:32 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 <sched.h> /* IWYU pragma: keep */
19 #include <setjmp.h>
20 #include <stdarg.h>
21 #include <stddef.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 
27 #define UNIT_TESTING
28 #include <cmocka.h>
29 
30 #include <isc/mem.h>
31 #include <isc/util.h>
32 
33 #include <irs/resconf.h>
34 #include <irs/types.h>
35 
36 static isc_mem_t *mctx = NULL;
37 
38 static void
setup_test()39 setup_test() {
40 	isc_mem_create(&mctx);
41 
42 	/*
43 	 * the caller might run from another directory, but tests
44 	 * that access test data files must first chdir to the proper
45 	 * location.
46 	 */
47 	assert_return_code(chdir(TESTS), 0);
48 }
49 
50 /* test irs_resconf_load() */
51 static void
irs_resconf_load_test(void ** state)52 irs_resconf_load_test(void **state) {
53 	isc_result_t result;
54 	irs_resconf_t *resconf = NULL;
55 	unsigned int i;
56 	struct {
57 		const char *file;
58 		isc_result_t loadres;
59 		isc_result_t (*check)(irs_resconf_t *resconf);
60 		isc_result_t checkres;
61 	} tests[] = {
62 		{ "testdata/domain.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
63 		{ "testdata/nameserver-v4.conf", ISC_R_SUCCESS, NULL,
64 		  ISC_R_SUCCESS },
65 		{ "testdata/nameserver-v6.conf", ISC_R_SUCCESS, NULL,
66 		  ISC_R_SUCCESS },
67 		{ "testdata/nameserver-v6-scoped.conf", ISC_R_SUCCESS, NULL,
68 		  ISC_R_SUCCESS },
69 		{ "testdata/options-debug.conf", ISC_R_SUCCESS, NULL,
70 		  ISC_R_SUCCESS },
71 		{ "testdata/options-ndots.conf", ISC_R_SUCCESS, NULL,
72 		  ISC_R_SUCCESS },
73 		{ "testdata/options-timeout.conf", ISC_R_SUCCESS, NULL,
74 		  ISC_R_SUCCESS },
75 		{ "testdata/options-unknown.conf", ISC_R_SUCCESS, NULL,
76 		  ISC_R_SUCCESS },
77 		{ "testdata/options.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
78 		{ "testdata/options-bad-ndots.conf", ISC_R_RANGE, NULL,
79 		  ISC_R_SUCCESS },
80 		{ "testdata/options-empty.conf", ISC_R_UNEXPECTEDEND, NULL,
81 		  ISC_R_SUCCESS },
82 		{ "testdata/port.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
83 		{ "testdata/resolv.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
84 		{ "testdata/search.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
85 		{ "testdata/sortlist-v4.conf", ISC_R_SUCCESS, NULL,
86 		  ISC_R_SUCCESS },
87 		{ "testdata/timeout.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
88 		{ "testdata/unknown.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS }
89 	};
90 
91 	UNUSED(state);
92 
93 	setup_test();
94 
95 	for (i = 0; i < sizeof(tests) / sizeof(tests[1]); i++) {
96 		result = irs_resconf_load(mctx, tests[i].file, &resconf);
97 		if (result != tests[i].loadres) {
98 			fail_msg("# unexpected result %s loading %s",
99 				 isc_result_totext(result), tests[i].file);
100 		}
101 
102 		if (result == ISC_R_SUCCESS && resconf == NULL) {
103 			fail_msg("# NULL on success loading %s", tests[i].file);
104 		} else if (result != ISC_R_SUCCESS && resconf != NULL) {
105 			fail_msg("# non-NULL on failure loading %s",
106 				 tests[i].file);
107 		}
108 
109 		if (resconf != NULL && tests[i].check != NULL) {
110 			result = (tests[i].check)(resconf);
111 			if (result != tests[i].checkres) {
112 				fail_msg("# unexpected result %s loading %s",
113 					 isc_result_totext(result),
114 					 tests[i].file);
115 			}
116 		}
117 		if (resconf != NULL) {
118 			irs_resconf_destroy(&resconf);
119 		}
120 	}
121 
122 	isc_mem_detach(&mctx);
123 }
124 
125 int
main(void)126 main(void) {
127 	const struct CMUnitTest tests[] = {
128 		cmocka_unit_test(irs_resconf_load_test),
129 	};
130 
131 	return (cmocka_run_group_tests(tests, NULL, NULL));
132 }
133 
134 #else /* HAVE_CMOCKA */
135 
136 #include <stdio.h>
137 
138 int
main(void)139 main(void) {
140 	printf("1..0 # Skipped: cmocka not available\n");
141 	return (SKIPPED_TEST_EXIT_CODE);
142 }
143 
144 #endif /* if HAVE_CMOCKA */
145