| 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/types.h> |
| 32 |
#include <isc/util.h> |
| 33 |
|
| 34 |
#include <isccfg/cfg.h> |
| 35 |
#include <isccfg/grammar.h> |
| 36 |
#include <isccfg/namedconf.h> |
| 37 |
|
| 38 |
#include <tests/isc.h> |
| 39 |
|
| 40 |
static isc_logcategory_t categories[] = { { "", 0 }, |
| 41 |
{ "client", 0 }, |
| 42 |
{ "network", 0 }, |
| 43 |
{ "update", 0 }, |
| 44 |
{ "queries", 0 }, |
| 45 |
{ "unmatched", 0 }, |
| 46 |
{ "update-security", 0 }, |
| 47 |
{ "query-errors", 0 }, |
| 48 |
{ NULL, 0 } }; |
| 49 |
|
| 50 |
ISC_SETUP_TEST_IMPL(group) { |
| 51 |
isc_result_t result; |
| 52 |
isc_logdestination_t destination; |
| 53 |
isc_logconfig_t *logconfig = NULL; |
| 54 |
|
| 55 |
isc_log_create(mctx, &lctx, &logconfig); |
| 56 |
isc_log_registercategories(lctx, categories); |
| 57 |
isc_log_setcontext(lctx); |
| 58 |
|
| 59 |
destination.file.stream = stderr; |
| 60 |
destination.file.name = NULL; |
| 61 |
destination.file.versions = ISC_LOG_ROLLNEVER; |
| 62 |
destination.file.maximum_size = 0; |
| 63 |
isc_log_createchannel(logconfig, "stderr", ISC_LOG_TOFILEDESC, |
| 64 |
ISC_LOG_DYNAMIC, &destination, 0); |
| 65 |
result = isc_log_usechannel(logconfig, "stderr", NULL, NULL); |
| 66 |
|
| 67 |
if (result != ISC_R_SUCCESS) { |
| 68 |
return -1; |
| 69 |
} |
| 70 |
|
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
ISC_TEARDOWN_TEST_IMPL(group) { |
| 75 |
if (lctx == NULL) { |
| 76 |
return -1; |
| 77 |
} |
| 78 |
|
| 79 |
isc_log_setcontext(NULL); |
| 80 |
isc_log_destroy(&lctx); |
| 81 |
|
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
struct duration_conf { |
| 86 |
const char *string; |
| 87 |
uint32_t time; |
| 88 |
const char *out; |
| 89 |
}; |
| 90 |
typedef struct duration_conf duration_conf_t; |
| 91 |
|
| 92 |
static void |
| 93 |
output(void *closure, const char *text, int textlen) { |
| 94 |
int r; |
| 95 |
|
| 96 |
REQUIRE(textlen >= 0 && textlen < CFG_DURATION_MAXLEN); |
| 97 |
|
| 98 |
r = snprintf(closure, CFG_DURATION_MAXLEN, "%s", text); |
| 99 |
INSIST(r == textlen); |
| 100 |
} |
| 101 |
|
| 102 |
/* test cfg_obj_asduration() and cfg_print_duration() */ |
| 103 |
ISC_RUN_TEST_IMPL(duration) { |
| 104 |
isc_result_t result; |
| 105 |
/* |
| 106 |
* When 'out' is NULL, the printed result is expected to be the same as |
| 107 |
* the input 'string', compared by ignoring the case of the characters. |
| 108 |
*/ |
| 109 |
duration_conf_t durations[] = { |
| 110 |
{ .string = "unlimited", .time = 0 }, |
| 111 |
{ .string = "PT0S", .time = 0 }, |
| 112 |
{ .string = "PT42S", .time = 42 }, |
| 113 |
{ .string = "PT10m", .time = 600 }, |
| 114 |
{ .string = "PT10m4S", .time = 604 }, |
| 115 |
{ .string = "PT3600S", .time = 3600 }, |
| 116 |
{ .string = "pT2H", .time = 7200 }, |
| 117 |
{ .string = "Pt2H3S", .time = 7203 }, |
| 118 |
{ .string = "PT2h1m3s", .time = 7263 }, |
| 119 |
{ .string = "p7d", .time = 604800 }, |
| 120 |
{ .string = "P7DT2h", .time = 612000 }, |
| 121 |
{ .string = "P2W", .time = 1209600 }, |
| 122 |
{ .string = "P3M", .time = 8035200 }, |
| 123 |
{ .string = "P3MT10M", .time = 8035800 }, |
| 124 |
{ .string = "p5y", .time = 157680000 }, |
| 125 |
{ .string = "P5YT2H", .time = 157687200 }, |
| 126 |
{ .string = "P1Y1M1DT1H1M1S", .time = 34304461 }, |
| 127 |
{ .string = "P99Y399M999DT3999H9999M2911754S", |
| 128 |
.time = UINT32_MAX - 1 }, |
| 129 |
{ .string = "P99Y399M999DT3999H9999M2911755S", |
| 130 |
.time = UINT32_MAX }, |
| 131 |
{ .string = "P4294967295Y4294967295M4294967295D" |
| 132 |
"T4294967295H4294967295M4294967295S", |
| 133 |
.time = UINT32_MAX }, |
| 134 |
{ .string = "PT4294967294S", .time = UINT32_MAX - 1 }, |
| 135 |
{ .string = "PT4294967295S", .time = UINT32_MAX }, |
| 136 |
{ .string = "0", .time = 0 }, |
| 137 |
{ .string = "30", .time = 30 }, |
| 138 |
{ .string = "42s", .time = 42, .out = "42" }, |
| 139 |
{ .string = "10m", .time = 600, .out = "600" }, |
| 140 |
{ .string = "2H", .time = 7200, .out = "7200" }, |
| 141 |
{ .string = "7d", .time = 604800, .out = "604800" }, |
| 142 |
{ .string = "2w", .time = 1209600, .out = "1209600" }, |
| 143 |
{ 0 }, /* Indicates that the remaining durations are invalid. */ |
| 144 |
{ .string = "PT4Y" }, |
| 145 |
{ .string = "P-4Y2M" }, |
| 146 |
{ .string = "P5H1M30S" }, |
| 147 |
{ .string = "P7Y4W" }, |
| 148 |
{ .string = "X7Y4M" }, |
| 149 |
{ .string = "T7H4M" }, |
| 150 |
{ .string = "1Y6M" }, |
| 151 |
{ .string = "PT4294967296S" }, |
| 152 |
{ .string = "PT99999999999S" }, |
| 153 |
{ .string = "P99999999999Y99999999999M99999999999D" |
| 154 |
"T99999999999H99999999999M99999999999S" }, |
| 155 |
}; |
| 156 |
isc_buffer_t buf1; |
| 157 |
cfg_parser_t *p1 = NULL; |
| 158 |
cfg_obj_t *c1 = NULL; |
| 159 |
bool must_fail = false; |
| 160 |
|
| 161 |
for (size_t i = 0; i < ARRAY_SIZE(durations); i++) { |
| 162 |
const cfg_listelt_t *element; |
| 163 |
const cfg_obj_t *kasps = NULL; |
| 164 |
const char cfg_tpl[] = |
| 165 |
"dnssec-policy \"dp\"\n" |
| 166 |
"{\nkeys {csk lifetime %s algorithm rsasha256;};\n};\n"; |
| 167 |
char conf[sizeof(cfg_tpl) + CFG_DURATION_MAXLEN] = { 0 }; |
| 168 |
char out[CFG_DURATION_MAXLEN] = { 0 }; |
| 169 |
cfg_printer_t pctx = { .f = output, .closure = out }; |
| 170 |
|
| 171 |
if (durations[i].string == NULL) { |
| 172 |
must_fail = true; |
| 173 |
continue; |
| 174 |
} |
| 175 |
|
| 176 |
snprintf(&conf[0], sizeof(conf), cfg_tpl, durations[i].string); |
| 177 |
|
| 178 |
isc_buffer_init(&buf1, conf, strlen(conf) - 1); |
| 179 |
isc_buffer_add(&buf1, strlen(conf) - 1); |
| 180 |
|
| 181 |
/* Parse with default line numbering */ |
| 182 |
result = cfg_parser_create(mctx, lctx, &p1); |
| 183 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 184 |
|
| 185 |
result = cfg_parse_buffer(p1, &buf1, "text1", 0, |
| 186 |
&cfg_type_namedconf, 0, &c1); |
| 187 |
if (must_fail) { |
| 188 |
assert_int_equal(result, DNS_R_BADTTL); |
| 189 |
cfg_parser_destroy(&p1); |
| 190 |
continue; |
| 191 |
} |
| 192 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 193 |
|
| 194 |
(void)cfg_map_get(c1, "dnssec-policy", &kasps); |
| 195 |
assert_non_null(kasps); |
| 196 |
for (element = cfg_list_first(kasps); element != NULL; |
| 197 |
element = cfg_list_next(element)) |
| 198 |
{ |
| 199 |
const cfg_listelt_t *key_element; |
| 200 |
const cfg_obj_t *lifetime = NULL; |
| 201 |
const cfg_obj_t *keys = NULL; |
| 202 |
const cfg_obj_t *key = NULL; |
| 203 |
const cfg_obj_t *kopts = NULL; |
| 204 |
cfg_obj_t *kconf = cfg_listelt_value(element); |
| 205 |
int cmp; |
| 206 |
|
| 207 |
assert_non_null(kconf); |
| 208 |
|
| 209 |
kopts = cfg_tuple_get(kconf, "options"); |
| 210 |
result = cfg_map_get(kopts, "keys", &keys); |
| 211 |
|
| 212 |
key_element = cfg_list_first(keys); |
| 213 |
assert_non_null(key_element); |
| 214 |
|
| 215 |
key = cfg_listelt_value(key_element); |
| 216 |
assert_non_null(key); |
| 217 |
|
| 218 |
lifetime = cfg_tuple_get(key, "lifetime"); |
| 219 |
assert_non_null(lifetime); |
| 220 |
|
| 221 |
assert_int_equal(durations[i].time, |
| 222 |
cfg_obj_asduration(lifetime)); |
| 223 |
|
| 224 |
cfg_print_duration_or_unlimited(&pctx, lifetime); |
| 225 |
cmp = strncasecmp(durations[i].out != NULL |
| 226 |
? durations[i].out |
| 227 |
: durations[i].string, |
| 228 |
out, strlen(durations[i].string)); |
| 229 |
assert_int_equal(cmp, 0); |
| 230 |
} |
| 231 |
|
| 232 |
cfg_obj_destroy(p1, &c1); |
| 233 |
cfg_parser_destroy(&p1); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
ISC_TEST_LIST_START |
| 238 |
|
| 239 |
ISC_TEST_ENTRY(duration) |
| 240 |
|
| 241 |
ISC_TEST_LIST_END |
| 242 |
|
| 243 |
ISC_TEST_MAIN_CUSTOM(setup_test_group, teardown_test_group) |