summaryrefslogtreecommitdiffstats
path: root/tests/irs/resconf_test.c
blob: 367898f8c2a164795e9410c01ec755d1ea964eb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * SPDX-License-Identifier: MPL-2.0
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
 */

#if HAVE_CMOCKA

#include <inttypes.h>
#include <sched.h> /* IWYU pragma: keep */
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define UNIT_TESTING
#include <cmocka.h>

#include <isc/mem.h>
#include <isc/util.h>

#include <irs/resconf.h>

static isc_mem_t *mctx = NULL;

static void
setup_test(void) {
	isc_mem_create(&mctx);

	/*
	 * the caller might run from another directory, but tests
	 * that access test data files must first chdir to the proper
	 * location.
	 */
	assert_return_code(chdir(TESTS_DIR), 0);
}

static isc_result_t
check_number(unsigned int n, unsigned int expected) {
	return ((n == expected) ? ISC_R_SUCCESS : ISC_R_BADNUMBER);
}

static isc_result_t
check_attempts(irs_resconf_t *resconf) {
	return (check_number(irs_resconf_getattempts(resconf), 4));
}

static isc_result_t
check_timeout(irs_resconf_t *resconf) {
	return (check_number(irs_resconf_gettimeout(resconf), 1));
}

static isc_result_t
check_ndots(irs_resconf_t *resconf) {
	return (check_number(irs_resconf_getndots(resconf), 2));
}

static isc_result_t
check_options(irs_resconf_t *resconf) {
	if (irs_resconf_getattempts(resconf) != 3) {
		return ISC_R_BADNUMBER; /* default value only */
	}

	if (irs_resconf_getndots(resconf) != 2) {
		return ISC_R_BADNUMBER;
	}

	if (irs_resconf_gettimeout(resconf) != 1) {
		return ISC_R_BADNUMBER;
	}

	return (ISC_R_SUCCESS);
}

/* test irs_resconf_load() */
static void
irs_resconf_load_test(void **state) {
	isc_result_t result;
	irs_resconf_t *resconf = NULL;
	unsigned int i;
	struct {
		const char *file;
		isc_result_t loadres;
		isc_result_t (*check)(irs_resconf_t *resconf);
		isc_result_t checkres;
	} tests[] = {
		{ "testdata/domain.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
		{ "testdata/nameserver-v4.conf", ISC_R_SUCCESS, NULL,
		  ISC_R_SUCCESS },
		{ "testdata/nameserver-v6.conf", ISC_R_SUCCESS, NULL,
		  ISC_R_SUCCESS },
		{ "testdata/nameserver-v6-scoped.conf", ISC_R_SUCCESS, NULL,
		  ISC_R_SUCCESS },
		{ "testdata/options-attempts.conf", ISC_R_SUCCESS,
		  check_attempts, ISC_R_SUCCESS },
		{ "testdata/options-debug.conf", ISC_R_SUCCESS, NULL,
		  ISC_R_SUCCESS },
		{ "testdata/options-ndots.conf", ISC_R_SUCCESS, check_ndots,
		  ISC_R_SUCCESS },
		{ "testdata/options-timeout.conf", ISC_R_SUCCESS, check_timeout,
		  ISC_R_SUCCESS },
		{ "testdata/options-unknown.conf", ISC_R_SUCCESS, NULL,
		  ISC_R_SUCCESS },
		{ "testdata/options.conf", ISC_R_SUCCESS, check_options,
		  ISC_R_SUCCESS },
		{ "testdata/options-bad-ndots.conf", ISC_R_RANGE, NULL,
		  ISC_R_SUCCESS },
		{ "testdata/options-empty.conf", ISC_R_UNEXPECTEDEND, NULL,
		  ISC_R_SUCCESS },
		{ "testdata/port.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
		{ "testdata/resolv.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
		{ "testdata/search.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
		{ "testdata/sortlist-v4.conf", ISC_R_SUCCESS, NULL,
		  ISC_R_SUCCESS },
		{ "testdata/timeout.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
		{ "testdata/unknown.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS }
	};

	UNUSED(state);

	setup_test();

	for (i = 0; i < sizeof(tests) / sizeof(tests[1]); i++) {
		result = irs_resconf_load(mctx, tests[i].file, &resconf);
		if (result != tests[i].loadres) {
			fail_msg("# unexpected result %s loading %s",
				 isc_result_totext(result), tests[i].file);
		}

		if (result == ISC_R_SUCCESS && resconf == NULL) {
			fail_msg("# NULL on success loading %s", tests[i].file);
		} else if (result != ISC_R_SUCCESS && resconf != NULL) {
			fail_msg("# non-NULL on failure loading %s",
				 tests[i].file);
		}

		if (resconf != NULL && tests[i].check != NULL) {
			result = (tests[i].check)(resconf);
			if (result != tests[i].checkres) {
				fail_msg("# unexpected result %s loading %s",
					 isc_result_totext(result),
					 tests[i].file);
			}
		}
		if (resconf != NULL) {
			irs_resconf_destroy(&resconf);
		}
	}

	isc_mem_detach(&mctx);
}

int
main(void) {
	const struct CMUnitTest tests[] = {
		cmocka_unit_test(irs_resconf_load_test),
	};

	return (cmocka_run_group_tests(tests, NULL, NULL));
}

#else /* HAVE_CMOCKA */

#include <stdio.h>

int
main(void) {
	printf("1..0 # Skipped: cmocka not available\n");
	return (SKIPPED_TEST_EXIT_CODE);
}

#endif /* if HAVE_CMOCKA */