parser_test.c 4.48 KB
Newer Older
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 4 5 6
 *
 * 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 http://mozilla.org/MPL/2.0/.
7 8 9
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
10 11 12 13
 */

#include <config.h>

14 15 16 17 18
#if HAVE_CMOCKA

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
19 20 21 22 23 24

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

25 26 27
#define UNIT_TESTING
#include <cmocka.h>

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
#include <isc/buffer.h>
#include <isc/lex.h>
#include <isc/log.h>
#include <isc/mem.h>
#include <isc/types.h>
#include <isc/util.h>

#include <isccfg/cfg.h>
#include <isccfg/grammar.h>
#include <isccfg/namedconf.h>

#define CHECK(r) \
	do { \
		result = (r); \
		if (result != ISC_R_SUCCESS) \
			goto cleanup; \
	} while (0)

isc_mem_t *mctx = NULL;
isc_log_t *lctx = NULL;
static isc_logcategory_t categories[] = {
		{ "",                0 },
		{ "client",          0 },
		{ "network",         0 },
		{ "update",          0 },
		{ "queries",         0 },
		{ "unmatched",       0 },
		{ "update-security", 0 },
		{ "query-errors",    0 },
		{ NULL,              0 }
};

static void
cleanup() {
62
	if (lctx != NULL) {
63
		isc_log_destroy(&lctx);
64 65
	}
	if (mctx != NULL) {
66
		isc_mem_destroy(&mctx);
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
}

static isc_result_t
setup() {
	isc_result_t result;

	isc_mem_debugging |= ISC_MEM_DEBUGRECORD;
	CHECK(isc_mem_create(0, 0, &mctx));

	isc_logdestination_t destination;
	isc_logconfig_t *logconfig = NULL;

	CHECK(isc_log_create(mctx, &lctx, &logconfig));
	isc_log_registercategories(lctx, categories);
	isc_log_setcontext(lctx);

	destination.file.stream = stderr;
	destination.file.name = NULL;
	destination.file.versions = ISC_LOG_ROLLNEVER;
	destination.file.maximum_size = 0;
	CHECK(isc_log_createchannel(logconfig, "stderr",
				    ISC_LOG_TOFILEDESC,
				    ISC_LOG_DYNAMIC,
				    &destination, 0));
	CHECK(isc_log_usechannel(logconfig, "stderr", NULL, NULL));

	return (ISC_R_SUCCESS);

  cleanup:
	cleanup();
	return (result);
}

101 102 103
/* test cfg_parse_buffer() */
static void
parse_buffer_test(void **state) {
104 105 106 107 108 109
	isc_result_t result;
	unsigned char text[] = "options\n{\nrecursion yes;\n};\n";
	isc_buffer_t buf1, buf2;
	cfg_parser_t *p1 = NULL, *p2 = NULL;
	cfg_obj_t *c1 = NULL, *c2 = NULL;

110
	UNUSED(state);
111 112 113 114 115 116 117 118

	setup();

	isc_buffer_init(&buf1, &text[0], sizeof(text) - 1);
	isc_buffer_add(&buf1, sizeof(text) - 1);

	/* Parse with default line numbering */
	result = cfg_parser_create(mctx, lctx, &p1);
119
	assert_int_equal(result, ISC_R_SUCCESS);
120

121 122
	result = cfg_parse_buffer(p1, &buf1, "text1", 0,
				  &cfg_type_namedconf, 0, &c1);
123 124
	assert_int_equal(result, ISC_R_SUCCESS);
	assert_int_equal(p1->line, 5);
125 126 127 128 129 130

	isc_buffer_init(&buf2, &text[0], sizeof(text) - 1);
	isc_buffer_add(&buf2, sizeof(text) - 1);

	/* Parse with changed line number */
	result = cfg_parser_create(mctx, lctx, &p2);
131
	assert_int_equal(result, ISC_R_SUCCESS);
132

133 134
	result = cfg_parse_buffer(p2, &buf2, "text2", 100,
				  &cfg_type_namedconf, 0, &c2);
135 136
	assert_int_equal(result, ISC_R_SUCCESS);
	assert_int_equal(p2->line, 104);
137 138 139 140 141 142 143 144 145 146

	cfg_obj_destroy(p1, &c1);
	cfg_obj_destroy(p2, &c2);

	cfg_parser_destroy(&p1);
	cfg_parser_destroy(&p2);

	cleanup();
}

147 148 149
/* test cfg_map_firstclause() */
static void
cfg_map_firstclause_test(void **state) {
150 151 152 153
	const char *name = NULL;
	const void *clauses = NULL;
	unsigned int idx;

154
	UNUSED(state);
155 156

	name = cfg_map_firstclause(&cfg_type_zoneopts, &clauses, &idx);
157 158 159
	assert_non_null(name);
	assert_non_null(clauses);
	assert_int_equal(idx, 0);
160 161
}

162 163 164
/* test cfg_map_nextclause() */
static void
cfg_map_nextclause_test(void **state) {
165 166 167 168
	const char *name = NULL;
	const void *clauses = NULL;
	unsigned int idx;

169
	UNUSED(state);
170 171

	name = cfg_map_firstclause(&cfg_type_zoneopts, &clauses, &idx);
172 173 174
	assert_non_null(name);
	assert_non_null(clauses);
	assert_int_equal(idx, ISC_R_SUCCESS);
175 176 177 178

	do {
		name = cfg_map_nextclause(&cfg_type_zoneopts, &clauses, &idx);
		if (name != NULL) {
179
			assert_non_null(clauses);
180
		} else {
181 182
			assert_null(clauses);
			assert_int_equal(idx, 0);
183 184 185 186
		}
	} while (name != NULL);
}

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
int
main(void) {
	const struct CMUnitTest tests[] = {
		cmocka_unit_test(parse_buffer_test),
		cmocka_unit_test(cfg_map_firstclause_test),
		cmocka_unit_test(cfg_map_nextclause_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 (0);
206
}
207 208

#endif