2016-10-10 17:11:21 -07:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2016-10-10 17:11:21 -07:00
|
|
|
*
|
|
|
|
* 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
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2016-10-10 17:11:21 -07:00
|
|
|
*/
|
|
|
|
|
2018-10-24 09:10:24 -07:00
|
|
|
#if HAVE_CMOCKA
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <sched.h> /* IWYU pragma: keep */
|
|
|
|
#include <setjmp.h>
|
2018-10-24 09:10:24 -07:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
2016-10-10 17:11:21 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-10-24 09:10:24 -07:00
|
|
|
#define UNIT_TESTING
|
|
|
|
#include <cmocka.h>
|
|
|
|
|
2016-10-10 17:11:21 -07:00
|
|
|
#include <isc/buffer.h>
|
|
|
|
#include <isc/lex.h>
|
|
|
|
#include <isc/log.h>
|
|
|
|
#include <isc/mem.h>
|
2020-03-23 13:27:37 +11:00
|
|
|
#include <isc/print.h>
|
|
|
|
#include <isc/string.h>
|
2016-10-10 17:11:21 -07:00
|
|
|
#include <isc/types.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
|
|
|
#include <isccfg/cfg.h>
|
|
|
|
#include <isccfg/grammar.h>
|
|
|
|
#include <isccfg/namedconf.h>
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#define CHECK(r) \
|
|
|
|
do { \
|
|
|
|
result = (r); \
|
2016-10-10 17:11:21 -07:00
|
|
|
if (result != ISC_R_SUCCESS) \
|
2020-02-12 13:59:18 +01:00
|
|
|
goto cleanup; \
|
2016-10-10 17:11:21 -07:00
|
|
|
} while (0)
|
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mem_t *mctx = NULL;
|
|
|
|
isc_log_t *lctx = NULL;
|
2020-02-12 13:59:18 +01:00
|
|
|
static isc_logcategory_t categories[] = { { "", 0 },
|
|
|
|
{ "client", 0 },
|
|
|
|
{ "network", 0 },
|
|
|
|
{ "update", 0 },
|
|
|
|
{ "queries", 0 },
|
|
|
|
{ "unmatched", 0 },
|
|
|
|
{ "update-security", 0 },
|
|
|
|
{ "query-errors", 0 },
|
|
|
|
{ NULL, 0 } };
|
2016-10-10 17:11:21 -07:00
|
|
|
|
|
|
|
static void
|
2018-08-07 16:46:53 +02:00
|
|
|
cleanup(void) {
|
2018-10-24 09:10:24 -07:00
|
|
|
if (lctx != NULL) {
|
2020-03-23 13:27:37 +11:00
|
|
|
isc_log_setcontext(NULL);
|
2016-10-10 17:11:21 -07:00
|
|
|
isc_log_destroy(&lctx);
|
2018-10-24 09:10:24 -07:00
|
|
|
}
|
|
|
|
if (mctx != NULL) {
|
2016-10-10 17:11:21 -07:00
|
|
|
isc_mem_destroy(&mctx);
|
2018-10-24 09:10:24 -07:00
|
|
|
}
|
2016-10-10 17:11:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2018-08-07 16:46:53 +02:00
|
|
|
setup(void) {
|
2016-10-10 17:11:21 -07:00
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
isc_mem_debugging |= ISC_MEM_DEBUGRECORD;
|
2019-09-05 18:40:57 +02:00
|
|
|
isc_mem_create(&mctx);
|
2016-10-10 17:11:21 -07:00
|
|
|
|
|
|
|
isc_logdestination_t destination;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_logconfig_t *logconfig = NULL;
|
2016-10-10 17:11:21 -07:00
|
|
|
|
2020-03-18 14:17:55 +11:00
|
|
|
isc_log_create(mctx, &lctx, &logconfig);
|
2016-10-10 17:11:21 -07:00
|
|
|
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;
|
2020-03-18 14:17:55 +11:00
|
|
|
isc_log_createchannel(logconfig, "stderr", ISC_LOG_TOFILEDESC,
|
|
|
|
ISC_LOG_DYNAMIC, &destination, 0);
|
2016-10-10 17:11:21 -07:00
|
|
|
CHECK(isc_log_usechannel(logconfig, "stderr", NULL, NULL));
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
cleanup:
|
2016-10-10 17:11:21 -07:00
|
|
|
cleanup();
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2020-03-23 13:27:37 +11:00
|
|
|
static int
|
|
|
|
_setup(void **state) {
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
result = setup();
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_teardown(void **state) {
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
cleanup();
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* mimic calling nzf_append() */
|
|
|
|
static void
|
|
|
|
append(void *arg, const char *str, int len) {
|
|
|
|
char *buf = arg;
|
|
|
|
size_t l = strlen(buf);
|
|
|
|
snprintf(buf + l, 1024 - l, "%.*s", len, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
addzoneconf(void **state) {
|
|
|
|
isc_result_t result;
|
|
|
|
isc_buffer_t b;
|
|
|
|
cfg_parser_t *p = NULL;
|
|
|
|
const char *tests[] = {
|
|
|
|
"zone \"test4.baz\" { type master; file \"e.db\"; };",
|
|
|
|
"zone \"test/.baz\" { type master; file \"e.db\"; };",
|
|
|
|
"zone \"test\\\".baz\" { type master; file \"e.db\"; };",
|
|
|
|
"zone \"test\\.baz\" { type master; file \"e.db\"; };",
|
|
|
|
"zone \"test\\\\.baz\" { type master; file \"e.db\"; };",
|
|
|
|
"zone \"test\\032.baz\" { type master; file \"e.db\"; };",
|
|
|
|
"zone \"test\\010.baz\" { type master; file \"e.db\"; };"
|
|
|
|
};
|
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
/* Parse with default line numbering */
|
|
|
|
result = cfg_parser_create(mctx, lctx, &p);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
#define ARRAYSIZE(x) (sizeof(x) / sizeof(x[0]))
|
|
|
|
|
|
|
|
for (size_t i = 0; i < ARRAYSIZE(tests); i++) {
|
|
|
|
cfg_obj_t *conf = NULL;
|
|
|
|
const cfg_obj_t *obj = NULL, *zlist = NULL;
|
|
|
|
|
|
|
|
isc_buffer_constinit(&b, tests[i], strlen(tests[i]));
|
|
|
|
isc_buffer_add(&b, strlen(tests[i]));
|
|
|
|
|
|
|
|
result = cfg_parse_buffer(p, &b, "text1", 0,
|
|
|
|
&cfg_type_namedconf, 0, &conf);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mimic calling nzf_append() from bin/named/server.c
|
|
|
|
* and check that the output matches the input.
|
|
|
|
*/
|
|
|
|
result = cfg_map_get(conf, "zone", &zlist);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
obj = cfg_listelt_value(cfg_list_first(zlist));
|
|
|
|
assert_ptr_not_equal(obj, NULL);
|
|
|
|
|
|
|
|
strlcpy(buf, "zone ", sizeof(buf));
|
|
|
|
cfg_printx(obj, CFG_PRINTER_ONELINE, append, buf);
|
|
|
|
strlcat(buf, ";", sizeof(buf));
|
|
|
|
assert_string_equal(tests[i], buf);
|
|
|
|
|
|
|
|
cfg_obj_destroy(p, &conf);
|
|
|
|
cfg_parser_reset(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg_parser_destroy(&p);
|
|
|
|
}
|
|
|
|
|
2018-10-24 09:10:24 -07:00
|
|
|
/* test cfg_parse_buffer() */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
parse_buffer_test(void **state) {
|
|
|
|
isc_result_t result;
|
2016-10-10 17:11:21 -07:00
|
|
|
unsigned char text[] = "options\n{\nrecursion yes;\n};\n";
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_buffer_t buf1, buf2;
|
2016-10-10 17:11:21 -07:00
|
|
|
cfg_parser_t *p1 = NULL, *p2 = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
cfg_obj_t *c1 = NULL, *c2 = NULL;
|
2016-10-10 17:11:21 -07:00
|
|
|
|
2018-10-24 09:10:24 -07:00
|
|
|
UNUSED(state);
|
2016-10-10 17:11:21 -07:00
|
|
|
|
|
|
|
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);
|
2018-10-24 09:10:24 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2016-10-10 17:11:21 -07:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
result = cfg_parse_buffer(p1, &buf1, "text1", 0, &cfg_type_namedconf, 0,
|
|
|
|
&c1);
|
2018-10-24 09:10:24 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
assert_int_equal(p1->line, 5);
|
2016-10-10 17:11:21 -07:00
|
|
|
|
|
|
|
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);
|
2018-10-24 09:10:24 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2016-10-10 17:11:21 -07:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
result = cfg_parse_buffer(p2, &buf2, "text2", 100, &cfg_type_namedconf,
|
|
|
|
0, &c2);
|
2018-10-24 09:10:24 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
assert_int_equal(p2->line, 104);
|
2016-10-10 17:11:21 -07:00
|
|
|
|
|
|
|
cfg_obj_destroy(p1, &c1);
|
|
|
|
cfg_obj_destroy(p2, &c2);
|
|
|
|
|
|
|
|
cfg_parser_destroy(&p1);
|
|
|
|
cfg_parser_destroy(&p2);
|
|
|
|
}
|
|
|
|
|
2018-10-24 09:10:24 -07:00
|
|
|
/* test cfg_map_firstclause() */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
cfg_map_firstclause_test(void **state) {
|
|
|
|
const char *name = NULL;
|
|
|
|
const void *clauses = NULL;
|
2018-01-22 11:00:45 -08:00
|
|
|
unsigned int idx;
|
|
|
|
|
2018-10-24 09:10:24 -07:00
|
|
|
UNUSED(state);
|
2018-01-22 11:00:45 -08:00
|
|
|
|
|
|
|
name = cfg_map_firstclause(&cfg_type_zoneopts, &clauses, &idx);
|
2018-10-24 09:10:24 -07:00
|
|
|
assert_non_null(name);
|
|
|
|
assert_non_null(clauses);
|
|
|
|
assert_int_equal(idx, 0);
|
2018-01-22 11:00:45 -08:00
|
|
|
}
|
|
|
|
|
2018-10-24 09:10:24 -07:00
|
|
|
/* test cfg_map_nextclause() */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
cfg_map_nextclause_test(void **state) {
|
|
|
|
const char *name = NULL;
|
|
|
|
const void *clauses = NULL;
|
2018-01-22 11:00:45 -08:00
|
|
|
unsigned int idx;
|
|
|
|
|
2018-10-24 09:10:24 -07:00
|
|
|
UNUSED(state);
|
2018-01-22 11:00:45 -08:00
|
|
|
|
|
|
|
name = cfg_map_firstclause(&cfg_type_zoneopts, &clauses, &idx);
|
2018-10-24 09:10:24 -07:00
|
|
|
assert_non_null(name);
|
|
|
|
assert_non_null(clauses);
|
|
|
|
assert_int_equal(idx, ISC_R_SUCCESS);
|
2018-01-22 11:00:45 -08:00
|
|
|
|
|
|
|
do {
|
|
|
|
name = cfg_map_nextclause(&cfg_type_zoneopts, &clauses, &idx);
|
|
|
|
if (name != NULL) {
|
2018-10-24 09:10:24 -07:00
|
|
|
assert_non_null(clauses);
|
2018-01-22 11:00:45 -08:00
|
|
|
} else {
|
2018-10-24 09:10:24 -07:00
|
|
|
assert_null(clauses);
|
|
|
|
assert_int_equal(idx, 0);
|
2018-01-22 11:00:45 -08:00
|
|
|
}
|
|
|
|
} while (name != NULL);
|
|
|
|
}
|
|
|
|
|
2018-10-24 09:10:24 -07:00
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2018-10-24 09:10:24 -07:00
|
|
|
const struct CMUnitTest tests[] = {
|
2020-03-23 13:27:37 +11:00
|
|
|
cmocka_unit_test(addzoneconf),
|
2018-10-24 09:10:24 -07:00
|
|
|
cmocka_unit_test(parse_buffer_test),
|
|
|
|
cmocka_unit_test(cfg_map_firstclause_test),
|
|
|
|
cmocka_unit_test(cfg_map_nextclause_test),
|
|
|
|
};
|
|
|
|
|
2020-03-23 13:27:37 +11:00
|
|
|
return (cmocka_run_group_tests(tests, _setup, _teardown));
|
2018-10-24 09:10:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* HAVE_CMOCKA */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2018-10-24 09:10:24 -07:00
|
|
|
printf("1..0 # Skipped: cmocka not available\n");
|
|
|
|
return (0);
|
2016-10-10 17:11:21 -07:00
|
|
|
}
|
2018-10-24 09:10:24 -07:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if HAVE_CMOCKA */
|