| 1 |
/* |
| 2 |
* Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MPL-2.0 |
| 5 |
* |
| 6 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
* |
| 10 |
* See the COPYRIGHT file distributed with this work for additional |
| 11 |
* information regarding copyright ownership. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <inttypes.h> |
| 15 |
#include <sched.h> /* IWYU pragma: keep */ |
| 16 |
#include <setjmp.h> |
| 17 |
#include <stdarg.h> |
| 18 |
#include <stddef.h> |
| 19 |
#include <stdio.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <string.h> |
| 22 |
#include <unistd.h> |
| 23 |
|
| 24 |
#define UNIT_TESTING |
| 25 |
#include <cmocka.h> |
| 26 |
|
| 27 |
#include <isc/buffer.h> |
| 28 |
#include <isc/lex.h> |
| 29 |
#include <isc/log.h> |
| 30 |
#include <isc/mem.h> |
| 31 |
#include <isc/string.h> |
| 32 |
#include <isc/types.h> |
| 33 |
#include <isc/util.h> |
| 34 |
|
| 35 |
#include <isccfg/cfg.h> |
| 36 |
#include <isccfg/grammar.h> |
| 37 |
#include <isccfg/namedconf.h> |
| 38 |
|
| 39 |
#include <tests/isc.h> |
| 40 |
|
| 41 |
static isc_logcategory_t categories[] = { { "", 0 }, |
| 42 |
{ "client", 0 }, |
| 43 |
{ "network", 0 }, |
| 44 |
{ "update", 0 }, |
| 45 |
{ "queries", 0 }, |
| 46 |
{ "unmatched", 0 }, |
| 47 |
{ "update-security", 0 }, |
| 48 |
{ "query-errors", 0 }, |
| 49 |
{ NULL, 0 } }; |
| 50 |
|
| 51 |
ISC_SETUP_TEST_IMPL(group) { |
| 52 |
isc_result_t result; |
| 53 |
isc_logdestination_t destination; |
| 54 |
isc_logconfig_t *logconfig = NULL; |
| 55 |
|
| 56 |
isc_log_create(mctx, &lctx, &logconfig); |
| 57 |
isc_log_registercategories(lctx, categories); |
| 58 |
isc_log_setcontext(lctx); |
| 59 |
|
| 60 |
destination.file.stream = stderr; |
| 61 |
destination.file.name = NULL; |
| 62 |
destination.file.versions = ISC_LOG_ROLLNEVER; |
| 63 |
destination.file.maximum_size = 0; |
| 64 |
isc_log_createchannel(logconfig, "stderr", ISC_LOG_TOFILEDESC, |
| 65 |
ISC_LOG_DYNAMIC, &destination, 0); |
| 66 |
result = isc_log_usechannel(logconfig, "stderr", NULL, NULL); |
| 67 |
|
| 68 |
if (result != ISC_R_SUCCESS) { |
| 69 |
return -1; |
| 70 |
} |
| 71 |
|
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
ISC_TEARDOWN_TEST_IMPL(group) { |
| 76 |
if (lctx == NULL) { |
| 77 |
return -1; |
| 78 |
} |
| 79 |
|
| 80 |
isc_log_setcontext(NULL); |
| 81 |
isc_log_destroy(&lctx); |
| 82 |
|
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
/* mimic calling nzf_append() */ |
| 87 |
static void |
| 88 |
append(void *arg, const char *str, int len) { |
| 89 |
char *buf = arg; |
| 90 |
size_t l = strlen(buf); |
| 91 |
snprintf(buf + l, 1024 - l, "%.*s", len, str); |
| 92 |
} |
| 93 |
|
| 94 |
ISC_RUN_TEST_IMPL(addzoneconf) { |
| 95 |
isc_result_t result; |
| 96 |
isc_buffer_t b; |
| 97 |
cfg_parser_t *p = NULL; |
| 98 |
const char *tests[] = { |
| 99 |
"zone \"test4.baz\" { type primary; file \"e.db\"; };", |
| 100 |
"zone \"test/.baz\" { type primary; file \"e.db\"; };", |
| 101 |
"zone \"test\\\".baz\" { type primary; file \"e.db\"; };", |
| 102 |
"zone \"test\\.baz\" { type primary; file \"e.db\"; };", |
| 103 |
"zone \"test\\\\.baz\" { type primary; file \"e.db\"; };", |
| 104 |
"zone \"test\\032.baz\" { type primary; file \"e.db\"; };", |
| 105 |
"zone \"test\\010.baz\" { type primary; file \"e.db\"; };" |
| 106 |
}; |
| 107 |
char buf[1024]; |
| 108 |
|
| 109 |
/* Parse with default line numbering */ |
| 110 |
result = cfg_parser_create(mctx, lctx, &p); |
| 111 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 112 |
|
| 113 |
for (size_t i = 0; i < ARRAY_SIZE(tests); i++) { |
| 114 |
cfg_obj_t *conf = NULL; |
| 115 |
const cfg_obj_t *obj = NULL, *zlist = NULL; |
| 116 |
|
| 117 |
isc_buffer_constinit(&b, tests[i], strlen(tests[i])); |
| 118 |
isc_buffer_add(&b, strlen(tests[i])); |
| 119 |
|
| 120 |
result = cfg_parse_buffer(p, &b, "text1", 0, |
| 121 |
&cfg_type_namedconf, 0, &conf); |
| 122 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 123 |
|
| 124 |
/* |
| 125 |
* Mimic calling nzf_append() from bin/named/server.c |
| 126 |
* and check that the output matches the input. |
| 127 |
*/ |
| 128 |
result = cfg_map_get(conf, "zone", &zlist); |
| 129 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 130 |
|
| 131 |
obj = cfg_listelt_value(cfg_list_first(zlist)); |
| 132 |
assert_ptr_not_equal(obj, NULL); |
| 133 |
|
| 134 |
strlcpy(buf, "zone ", sizeof(buf)); |
| 135 |
cfg_printx(obj, CFG_PRINTER_ONELINE, append, buf); |
| 136 |
strlcat(buf, ";", sizeof(buf)); |
| 137 |
assert_string_equal(tests[i], buf); |
| 138 |
|
| 139 |
cfg_obj_destroy(p, &conf); |
| 140 |
cfg_parser_reset(p); |
| 141 |
} |
| 142 |
|
| 143 |
cfg_parser_destroy(&p); |
| 144 |
} |
| 145 |
|
| 146 |
/* test cfg_parse_buffer() */ |
| 147 |
ISC_RUN_TEST_IMPL(parse_buffer) { |
| 148 |
isc_result_t result; |
| 149 |
unsigned char text[] = "options\n{\nrecursion yes;\n};\n"; |
| 150 |
isc_buffer_t buf1, buf2; |
| 151 |
cfg_parser_t *p1 = NULL, *p2 = NULL; |
| 152 |
cfg_obj_t *c1 = NULL, *c2 = NULL; |
| 153 |
|
| 154 |
isc_buffer_init(&buf1, &text[0], sizeof(text) - 1); |
| 155 |
isc_buffer_add(&buf1, sizeof(text) - 1); |
| 156 |
|
| 157 |
/* Parse with default line numbering */ |
| 158 |
result = cfg_parser_create(mctx, lctx, &p1); |
| 159 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 160 |
|
| 161 |
result = cfg_parse_buffer(p1, &buf1, "text1", 0, &cfg_type_namedconf, 0, |
| 162 |
&c1); |
| 163 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 164 |
assert_int_equal(p1->line, 5); |
| 165 |
|
| 166 |
isc_buffer_init(&buf2, &text[0], sizeof(text) - 1); |
| 167 |
isc_buffer_add(&buf2, sizeof(text) - 1); |
| 168 |
|
| 169 |
/* Parse with changed line number */ |
| 170 |
result = cfg_parser_create(mctx, lctx, &p2); |
| 171 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 172 |
|
| 173 |
result = cfg_parse_buffer(p2, &buf2, "text2", 100, &cfg_type_namedconf, |
| 174 |
0, &c2); |
| 175 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 176 |
assert_int_equal(p2->line, 104); |
| 177 |
|
| 178 |
cfg_obj_destroy(p1, &c1); |
| 179 |
cfg_obj_destroy(p2, &c2); |
| 180 |
|
| 181 |
cfg_parser_destroy(&p1); |
| 182 |
cfg_parser_destroy(&p2); |
| 183 |
} |
| 184 |
|
| 185 |
/* test cfg_map_firstclause() */ |
| 186 |
ISC_RUN_TEST_IMPL(cfg_map_firstclause) { |
| 187 |
const char *name = NULL; |
| 188 |
const void *clauses = NULL; |
| 189 |
unsigned int idx; |
| 190 |
|
| 191 |
name = cfg_map_firstclause(&cfg_type_zoneopts, &clauses, &idx); |
| 192 |
assert_non_null(name); |
| 193 |
assert_non_null(clauses); |
| 194 |
assert_int_equal(idx, 0); |
| 195 |
} |
| 196 |
|
| 197 |
/* test cfg_map_nextclause() */ |
| 198 |
ISC_RUN_TEST_IMPL(cfg_map_nextclause) { |
| 199 |
const char *name = NULL; |
| 200 |
const void *clauses = NULL; |
| 201 |
unsigned int idx; |
| 202 |
|
| 203 |
name = cfg_map_firstclause(&cfg_type_zoneopts, &clauses, &idx); |
| 204 |
assert_non_null(name); |
| 205 |
assert_non_null(clauses); |
| 206 |
assert_int_equal(idx, ISC_R_SUCCESS); |
| 207 |
|
| 208 |
do { |
| 209 |
name = cfg_map_nextclause(&cfg_type_zoneopts, &clauses, &idx); |
| 210 |
if (name != NULL) { |
| 211 |
assert_non_null(clauses); |
| 212 |
} else { |
| 213 |
assert_null(clauses); |
| 214 |
assert_int_equal(idx, 0); |
| 215 |
} |
| 216 |
} while (name != NULL); |
| 217 |
} |
| 218 |
|
| 219 |
ISC_TEST_LIST_START |
| 220 |
|
| 221 |
ISC_TEST_ENTRY(addzoneconf) |
| 222 |
ISC_TEST_ENTRY(parse_buffer) |
| 223 |
ISC_TEST_ENTRY(cfg_map_firstclause) |
| 224 |
ISC_TEST_ENTRY(cfg_map_nextclause) |
| 225 |
|
| 226 |
ISC_TEST_LIST_END |
| 227 |
|
| 228 |
ISC_TEST_MAIN_CUSTOM(setup_test_group, teardown_test_group) |