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