1*c0b5d9fbSchristos /*	$NetBSD: resconf_test.c,v 1.8 2022/09/23 12:15:32 christos Exp $	*/
2e2b1b9c0Schristos 
3e2b1b9c0Schristos /*
4e2b1b9c0Schristos  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5e2b1b9c0Schristos  *
6*c0b5d9fbSchristos  * SPDX-License-Identifier: MPL-2.0
7*c0b5d9fbSchristos  *
8e2b1b9c0Schristos  * This Source Code Form is subject to the terms of the Mozilla Public
9e2b1b9c0Schristos  * License, v. 2.0. If a copy of the MPL was not distributed with this
1073584a28Schristos  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11e2b1b9c0Schristos  *
12e2b1b9c0Schristos  * See the COPYRIGHT file distributed with this work for additional
13e2b1b9c0Schristos  * information regarding copyright ownership.
14e2b1b9c0Schristos  */
15e2b1b9c0Schristos 
16f2e20987Schristos #if HAVE_CMOCKA
17f2e20987Schristos 
189742fdb4Schristos #include <sched.h> /* IWYU pragma: keep */
199742fdb4Schristos #include <setjmp.h>
20f2e20987Schristos #include <stdarg.h>
21f2e20987Schristos #include <stddef.h>
22e2b1b9c0Schristos #include <stdio.h>
23e2b1b9c0Schristos #include <stdlib.h>
24e2b1b9c0Schristos #include <string.h>
25e2b1b9c0Schristos #include <unistd.h>
26e2b1b9c0Schristos 
27f2e20987Schristos #define UNIT_TESTING
28f2e20987Schristos #include <cmocka.h>
29f2e20987Schristos 
30e2b1b9c0Schristos #include <isc/mem.h>
31e2b1b9c0Schristos #include <isc/util.h>
32e2b1b9c0Schristos 
33e2b1b9c0Schristos #include <irs/resconf.h>
349742fdb4Schristos #include <irs/types.h>
35e2b1b9c0Schristos 
36e2b1b9c0Schristos static isc_mem_t *mctx = NULL;
37e2b1b9c0Schristos 
38e2b1b9c0Schristos static void
setup_test()39e2b1b9c0Schristos setup_test() {
409742fdb4Schristos 	isc_mem_create(&mctx);
41e2b1b9c0Schristos 
42e2b1b9c0Schristos 	/*
43f2e20987Schristos 	 * the caller might run from another directory, but tests
44e2b1b9c0Schristos 	 * that access test data files must first chdir to the proper
45e2b1b9c0Schristos 	 * location.
46e2b1b9c0Schristos 	 */
47f2e20987Schristos 	assert_return_code(chdir(TESTS), 0);
48e2b1b9c0Schristos }
49e2b1b9c0Schristos 
50f2e20987Schristos /* test irs_resconf_load() */
51f2e20987Schristos static void
irs_resconf_load_test(void ** state)52f2e20987Schristos irs_resconf_load_test(void **state) {
53e2b1b9c0Schristos 	isc_result_t result;
54e2b1b9c0Schristos 	irs_resconf_t *resconf = NULL;
55e2b1b9c0Schristos 	unsigned int i;
56e2b1b9c0Schristos 	struct {
57e2b1b9c0Schristos 		const char *file;
58e2b1b9c0Schristos 		isc_result_t loadres;
59e2b1b9c0Schristos 		isc_result_t (*check)(irs_resconf_t *resconf);
60e2b1b9c0Schristos 		isc_result_t checkres;
61e2b1b9c0Schristos 	} tests[] = {
629742fdb4Schristos 		{ "testdata/domain.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
639742fdb4Schristos 		{ "testdata/nameserver-v4.conf", ISC_R_SUCCESS, NULL,
649742fdb4Schristos 		  ISC_R_SUCCESS },
659742fdb4Schristos 		{ "testdata/nameserver-v6.conf", ISC_R_SUCCESS, NULL,
669742fdb4Schristos 		  ISC_R_SUCCESS },
679742fdb4Schristos 		{ "testdata/nameserver-v6-scoped.conf", ISC_R_SUCCESS, NULL,
689742fdb4Schristos 		  ISC_R_SUCCESS },
699742fdb4Schristos 		{ "testdata/options-debug.conf", ISC_R_SUCCESS, NULL,
709742fdb4Schristos 		  ISC_R_SUCCESS },
719742fdb4Schristos 		{ "testdata/options-ndots.conf", ISC_R_SUCCESS, NULL,
729742fdb4Schristos 		  ISC_R_SUCCESS },
739742fdb4Schristos 		{ "testdata/options-timeout.conf", ISC_R_SUCCESS, NULL,
749742fdb4Schristos 		  ISC_R_SUCCESS },
759742fdb4Schristos 		{ "testdata/options-unknown.conf", ISC_R_SUCCESS, NULL,
769742fdb4Schristos 		  ISC_R_SUCCESS },
779742fdb4Schristos 		{ "testdata/options.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
789742fdb4Schristos 		{ "testdata/options-bad-ndots.conf", ISC_R_RANGE, NULL,
799742fdb4Schristos 		  ISC_R_SUCCESS },
809742fdb4Schristos 		{ "testdata/options-empty.conf", ISC_R_UNEXPECTEDEND, NULL,
819742fdb4Schristos 		  ISC_R_SUCCESS },
829742fdb4Schristos 		{ "testdata/port.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
839742fdb4Schristos 		{ "testdata/resolv.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
849742fdb4Schristos 		{ "testdata/search.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
859742fdb4Schristos 		{ "testdata/sortlist-v4.conf", ISC_R_SUCCESS, NULL,
869742fdb4Schristos 		  ISC_R_SUCCESS },
879742fdb4Schristos 		{ "testdata/timeout.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
889742fdb4Schristos 		{ "testdata/unknown.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS }
89e2b1b9c0Schristos 	};
90e2b1b9c0Schristos 
91f2e20987Schristos 	UNUSED(state);
92e2b1b9c0Schristos 
93e2b1b9c0Schristos 	setup_test();
94e2b1b9c0Schristos 
95e2b1b9c0Schristos 	for (i = 0; i < sizeof(tests) / sizeof(tests[1]); i++) {
96e2b1b9c0Schristos 		result = irs_resconf_load(mctx, tests[i].file, &resconf);
97f2e20987Schristos 		if (result != tests[i].loadres) {
98f2e20987Schristos 			fail_msg("# unexpected result %s loading %s",
99f2e20987Schristos 				 isc_result_totext(result), tests[i].file);
100f2e20987Schristos 		}
101f2e20987Schristos 
102f2e20987Schristos 		if (result == ISC_R_SUCCESS && resconf == NULL) {
1039742fdb4Schristos 			fail_msg("# NULL on success loading %s", tests[i].file);
104f2e20987Schristos 		} else if (result != ISC_R_SUCCESS && resconf != NULL) {
105f2e20987Schristos 			fail_msg("# non-NULL on failure loading %s",
106e2b1b9c0Schristos 				 tests[i].file);
107e2b1b9c0Schristos 		}
108f2e20987Schristos 
109f2e20987Schristos 		if (resconf != NULL && tests[i].check != NULL) {
110f2e20987Schristos 			result = (tests[i].check)(resconf);
111f2e20987Schristos 			if (result != tests[i].checkres) {
112f2e20987Schristos 				fail_msg("# unexpected result %s loading %s",
113f2e20987Schristos 					 isc_result_totext(result),
114f2e20987Schristos 					 tests[i].file);
115f2e20987Schristos 			}
116f2e20987Schristos 		}
117f2e20987Schristos 		if (resconf != NULL) {
118e2b1b9c0Schristos 			irs_resconf_destroy(&resconf);
119e2b1b9c0Schristos 		}
120f2e20987Schristos 	}
121e2b1b9c0Schristos 
122e2b1b9c0Schristos 	isc_mem_detach(&mctx);
123e2b1b9c0Schristos }
124e2b1b9c0Schristos 
125f2e20987Schristos int
main(void)126f2e20987Schristos main(void) {
127f2e20987Schristos 	const struct CMUnitTest tests[] = {
128f2e20987Schristos 		cmocka_unit_test(irs_resconf_load_test),
129f2e20987Schristos 	};
130f2e20987Schristos 
131f2e20987Schristos 	return (cmocka_run_group_tests(tests, NULL, NULL));
132e2b1b9c0Schristos }
133f2e20987Schristos 
134f2e20987Schristos #else /* HAVE_CMOCKA */
135f2e20987Schristos 
136f2e20987Schristos #include <stdio.h>
137f2e20987Schristos 
138f2e20987Schristos int
main(void)139f2e20987Schristos main(void) {
140f2e20987Schristos 	printf("1..0 # Skipped: cmocka not available\n");
14176b1fd8fSchristos 	return (SKIPPED_TEST_EXIT_CODE);
142f2e20987Schristos }
143f2e20987Schristos 
1449742fdb4Schristos #endif /* if HAVE_CMOCKA */
145