| 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 <stdbool.h> |
| 19 |
#include <stddef.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/util.h> |
| 28 |
|
| 29 |
#include <dns/stats.h> |
| 30 |
|
| 31 |
#include <tests/dns.h> |
| 32 |
|
| 33 |
static void |
| 34 |
set_typestats(dns_stats_t *stats, dns_rdatatype_t type) { |
| 35 |
dns_rdatastatstype_t which; |
| 36 |
unsigned int attributes; |
| 37 |
|
| 38 |
attributes = 0; |
| 39 |
which = DNS_RDATASTATSTYPE_VALUE(type, attributes); |
| 40 |
dns_rdatasetstats_increment(stats, which); |
| 41 |
|
| 42 |
attributes = DNS_RDATASTATSTYPE_ATTR_NXRRSET; |
| 43 |
which = DNS_RDATASTATSTYPE_VALUE(type, attributes); |
| 44 |
dns_rdatasetstats_increment(stats, which); |
| 45 |
} |
| 46 |
|
| 47 |
static void |
| 48 |
set_nxdomainstats(dns_stats_t *stats) { |
| 49 |
dns_rdatastatstype_t which; |
| 50 |
unsigned int attributes; |
| 51 |
|
| 52 |
attributes = DNS_RDATASTATSTYPE_ATTR_NXDOMAIN; |
| 53 |
which = DNS_RDATASTATSTYPE_VALUE(0, attributes); |
| 54 |
dns_rdatasetstats_increment(stats, which); |
| 55 |
} |
| 56 |
|
| 57 |
static void |
| 58 |
mark_stale(dns_stats_t *stats, dns_rdatatype_t type, int from, int to) { |
| 59 |
dns_rdatastatstype_t which; |
| 60 |
unsigned int attributes; |
| 61 |
|
| 62 |
attributes = from; |
| 63 |
which = DNS_RDATASTATSTYPE_VALUE(type, attributes); |
| 64 |
dns_rdatasetstats_decrement(stats, which); |
| 65 |
|
| 66 |
attributes |= to; |
| 67 |
which = DNS_RDATASTATSTYPE_VALUE(type, attributes); |
| 68 |
dns_rdatasetstats_increment(stats, which); |
| 69 |
|
| 70 |
attributes = DNS_RDATASTATSTYPE_ATTR_NXRRSET | from; |
| 71 |
which = DNS_RDATASTATSTYPE_VALUE(type, attributes); |
| 72 |
dns_rdatasetstats_decrement(stats, which); |
| 73 |
|
| 74 |
attributes |= to; |
| 75 |
which = DNS_RDATASTATSTYPE_VALUE(type, attributes); |
| 76 |
dns_rdatasetstats_increment(stats, which); |
| 77 |
} |
| 78 |
|
| 79 |
static void |
| 80 |
mark_nxdomain_stale(dns_stats_t *stats, int from, int to) { |
| 81 |
dns_rdatastatstype_t which; |
| 82 |
unsigned int attributes; |
| 83 |
|
| 84 |
attributes = DNS_RDATASTATSTYPE_ATTR_NXDOMAIN | from; |
| 85 |
which = DNS_RDATASTATSTYPE_VALUE(0, attributes); |
| 86 |
dns_rdatasetstats_decrement(stats, which); |
| 87 |
|
| 88 |
attributes |= to; |
| 89 |
which = DNS_RDATASTATSTYPE_VALUE(0, attributes); |
| 90 |
dns_rdatasetstats_increment(stats, which); |
| 91 |
} |
| 92 |
|
| 93 |
#define ATTRIBUTE_SET(y) ((attributes & (y)) != 0) |
| 94 |
static void |
| 95 |
verify_active_counters(dns_rdatastatstype_t which, uint64_t value, void *arg) { |
| 96 |
unsigned int attributes; |
| 97 |
#if debug |
| 98 |
unsigned int type; |
| 99 |
#endif /* if debug */ |
| 100 |
|
| 101 |
UNUSED(which); |
| 102 |
UNUSED(arg); |
| 103 |
|
| 104 |
attributes = DNS_RDATASTATSTYPE_ATTR(which); |
| 105 |
#if debug |
| 106 |
type = DNS_RDATASTATSTYPE_BASE(which); |
| 107 |
|
| 108 |
fprintf(stderr, "%s%s%s%s%s/%u, %u\n", |
| 109 |
ATTRIBUTE_SET(DNS_RDATASTATSTYPE_ATTR_OTHERTYPE) ? "O" : " ", |
| 110 |
ATTRIBUTE_SET(DNS_RDATASTATSTYPE_ATTR_NXRRSET) ? "!" : " ", |
| 111 |
ATTRIBUTE_SET(DNS_RDATASTATSTYPE_ATTR_ANCIENT) ? "~" : " ", |
| 112 |
ATTRIBUTE_SET(DNS_RDATASTATSTYPE_ATTR_STALE) ? "#" : " ", |
| 113 |
ATTRIBUTE_SET(DNS_RDATASTATSTYPE_ATTR_NXDOMAIN) ? "X" : " ", |
| 114 |
type, (unsigned int)value); |
| 115 |
#endif /* if debug */ |
| 116 |
if ((attributes & DNS_RDATASTATSTYPE_ATTR_ANCIENT) == 0 && |
| 117 |
(attributes & DNS_RDATASTATSTYPE_ATTR_STALE) == 0) |
| 118 |
{ |
| 119 |
assert_int_equal(value, 1); |
| 120 |
} else { |
| 121 |
assert_int_equal(value, 0); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
static void |
| 126 |
verify_stale_counters(dns_rdatastatstype_t which, uint64_t value, void *arg) { |
| 127 |
unsigned int attributes; |
| 128 |
#if debug |
| 129 |
unsigned int type; |
| 130 |
#endif /* if debug */ |
| 131 |
|
| 132 |
UNUSED(which); |
| 133 |
UNUSED(arg); |
| 134 |
|
| 135 |
attributes = DNS_RDATASTATSTYPE_ATTR(which); |
| 136 |
#if debug |
| 137 |
type = DNS_RDATASTATSTYPE_BASE(which); |
| 138 |
|
| 139 |
fprintf(stderr, "%s%s%s%s%s/%u, %u\n", |
| 140 |
ATTRIBUTE_SET(DNS_RDATASTATSTYPE_ATTR_OTHERTYPE) ? "O" : " ", |
| 141 |
ATTRIBUTE_SET(DNS_RDATASTATSTYPE_ATTR_NXRRSET) ? "!" : " ", |
| 142 |
ATTRIBUTE_SET(DNS_RDATASTATSTYPE_ATTR_ANCIENT) ? "~" : " ", |
| 143 |
ATTRIBUTE_SET(DNS_RDATASTATSTYPE_ATTR_STALE) ? "#" : " ", |
| 144 |
ATTRIBUTE_SET(DNS_RDATASTATSTYPE_ATTR_NXDOMAIN) ? "X" : " ", |
| 145 |
type, (unsigned int)value); |
| 146 |
#endif /* if debug */ |
| 147 |
if ((attributes & DNS_RDATASTATSTYPE_ATTR_STALE) != 0) { |
| 148 |
assert_int_equal(value, 1); |
| 149 |
} else { |
| 150 |
assert_int_equal(value, 0); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
static void |
| 155 |
verify_ancient_counters(dns_rdatastatstype_t which, uint64_t value, void *arg) { |
| 156 |
unsigned int attributes; |
| 157 |
#if debug |
| 158 |
unsigned int type; |
| 159 |
#endif /* if debug */ |
| 160 |
|
| 161 |
UNUSED(which); |
| 162 |
UNUSED(arg); |
| 163 |
|
| 164 |
attributes = DNS_RDATASTATSTYPE_ATTR(which); |
| 165 |
#if debug |
| 166 |
type = DNS_RDATASTATSTYPE_BASE(which); |
| 167 |
|
| 168 |
fprintf(stderr, "%s%s%s%s%s/%u, %u\n", |
| 169 |
ATTRIBUTE_SET(DNS_RDATASTATSTYPE_ATTR_OTHERTYPE) ? "O" : " ", |
| 170 |
ATTRIBUTE_SET(DNS_RDATASTATSTYPE_ATTR_NXRRSET) ? "!" : " ", |
| 171 |
ATTRIBUTE_SET(DNS_RDATASTATSTYPE_ATTR_ANCIENT) ? "~" : " ", |
| 172 |
ATTRIBUTE_SET(DNS_RDATASTATSTYPE_ATTR_STALE) ? "#" : " ", |
| 173 |
ATTRIBUTE_SET(DNS_RDATASTATSTYPE_ATTR_NXDOMAIN) ? "X" : " ", |
| 174 |
type, (unsigned int)value); |
| 175 |
#endif /* if debug */ |
| 176 |
if ((attributes & DNS_RDATASTATSTYPE_ATTR_ANCIENT) != 0) { |
| 177 |
assert_int_equal(value, 1); |
| 178 |
} else { |
| 179 |
assert_int_equal(value, 0); |
| 180 |
} |
| 181 |
} |
| 182 |
/* |
| 183 |
* Individual unit tests |
| 184 |
*/ |
| 185 |
|
| 186 |
/* |
| 187 |
* Test that rdatasetstats counters are properly set when moving from |
| 188 |
* active -> stale -> ancient. |
| 189 |
*/ |
| 190 |
static void |
| 191 |
rdatasetstats(void **state ISC_ATTR_UNUSED, bool servestale) { |
| 192 |
unsigned int i; |
| 193 |
unsigned int from = 0; |
| 194 |
dns_stats_t *stats = NULL; |
| 195 |
|
| 196 |
dns_rdatasetstats_create(mctx, &stats); |
| 197 |
|
| 198 |
/* First 255 types. */ |
| 199 |
for (i = 1; i <= 255; i++) { |
| 200 |
set_typestats(stats, (dns_rdatatype_t)i); |
| 201 |
} |
| 202 |
/* Specials */ |
| 203 |
set_typestats(stats, (dns_rdatatype_t)1000); |
| 204 |
set_nxdomainstats(stats); |
| 205 |
|
| 206 |
/* Check that all active counters are set to appropriately. */ |
| 207 |
dns_rdatasetstats_dump(stats, verify_active_counters, NULL, 1); |
| 208 |
|
| 209 |
if (servestale) { |
| 210 |
/* Mark stale */ |
| 211 |
for (i = 1; i <= 255; i++) { |
| 212 |
mark_stale(stats, (dns_rdatatype_t)i, 0, |
| 213 |
DNS_RDATASTATSTYPE_ATTR_STALE); |
| 214 |
} |
| 215 |
mark_stale(stats, (dns_rdatatype_t)1000, 0, |
| 216 |
DNS_RDATASTATSTYPE_ATTR_STALE); |
| 217 |
mark_nxdomain_stale(stats, 0, DNS_RDATASTATSTYPE_ATTR_STALE); |
| 218 |
|
| 219 |
/* Check that all counters are set to appropriately. */ |
| 220 |
dns_rdatasetstats_dump(stats, verify_stale_counters, NULL, 1); |
| 221 |
|
| 222 |
/* Set correct staleness state */ |
| 223 |
from = DNS_RDATASTATSTYPE_ATTR_STALE; |
| 224 |
} |
| 225 |
|
| 226 |
/* Mark ancient */ |
| 227 |
for (i = 1; i <= 255; i++) { |
| 228 |
mark_stale(stats, (dns_rdatatype_t)i, from, |
| 229 |
DNS_RDATASTATSTYPE_ATTR_ANCIENT); |
| 230 |
} |
| 231 |
mark_stale(stats, (dns_rdatatype_t)1000, from, |
| 232 |
DNS_RDATASTATSTYPE_ATTR_ANCIENT); |
| 233 |
mark_nxdomain_stale(stats, from, DNS_RDATASTATSTYPE_ATTR_ANCIENT); |
| 234 |
|
| 235 |
/* |
| 236 |
* Check that all counters are set to appropriately. |
| 237 |
*/ |
| 238 |
dns_rdatasetstats_dump(stats, verify_ancient_counters, NULL, 1); |
| 239 |
|
| 240 |
dns_stats_detach(&stats); |
| 241 |
} |
| 242 |
|
| 243 |
/* |
| 244 |
* Test that rdatasetstats counters are properly set when moving from |
| 245 |
* active -> stale -> ancient. |
| 246 |
*/ |
| 247 |
ISC_RUN_TEST_IMPL(active_stale_ancient) { rdatasetstats(state, true); } |
| 248 |
|
| 249 |
/* |
| 250 |
* Test that rdatasetstats counters are properly set when moving from |
| 251 |
* active -> ancient. |
| 252 |
*/ |
| 253 |
ISC_RUN_TEST_IMPL(active_ancient) { rdatasetstats(state, false); } |
| 254 |
|
| 255 |
ISC_TEST_LIST_START |
| 256 |
ISC_TEST_ENTRY(active_stale_ancient) |
| 257 |
ISC_TEST_ENTRY(active_ancient) |
| 258 |
ISC_TEST_LIST_END |
| 259 |
|
| 260 |
ISC_TEST_MAIN |