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