1 /*	$NetBSD: rdatasetstats_test.c,v 1.1.1.3 2014/12/10 03:34:42 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2012  Internet Systems Consortium, Inc. ("ISC")
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /* Id */
20 
21 /*! \file */
22 
23 #include <config.h>
24 
25 #include <atf-c.h>
26 
27 #include <unistd.h>
28 
29 #include <dns/stats.h>
30 
31 #include "dnstest.h"
32 
33 /*
34  * Helper functions
35  */
36 static void
set_typestats(dns_stats_t * stats,dns_rdatatype_t type,isc_boolean_t stale)37 set_typestats(dns_stats_t *stats, dns_rdatatype_t type,
38 	      isc_boolean_t stale)
39 {
40 	dns_rdatastatstype_t which;
41 	unsigned int attributes;
42 
43 	attributes = 0;
44 	if (stale) attributes |= DNS_RDATASTATSTYPE_ATTR_STALE;
45 	which = DNS_RDATASTATSTYPE_VALUE(type, attributes);
46 	dns_rdatasetstats_increment(stats, which);
47 
48 	attributes = DNS_RDATASTATSTYPE_ATTR_NXRRSET;
49 	if (stale) attributes |= DNS_RDATASTATSTYPE_ATTR_STALE;
50 	which = DNS_RDATASTATSTYPE_VALUE(type, attributes);
51 	dns_rdatasetstats_increment(stats, which);
52 }
53 
54 static void
set_nxdomainstats(dns_stats_t * stats,isc_boolean_t stale)55 set_nxdomainstats(dns_stats_t *stats, isc_boolean_t stale) {
56 	dns_rdatastatstype_t which;
57 	unsigned int attributes;
58 
59 	attributes = DNS_RDATASTATSTYPE_ATTR_NXDOMAIN;
60 	if (stale) attributes |= DNS_RDATASTATSTYPE_ATTR_STALE;
61 	which = DNS_RDATASTATSTYPE_VALUE(0, attributes);
62 	dns_rdatasetstats_increment(stats, which);
63 }
64 
65 static void
checkit1(dns_rdatastatstype_t which,isc_uint64_t value,void * arg)66 checkit1(dns_rdatastatstype_t which, isc_uint64_t value, void *arg) {
67 	unsigned int attributes;
68 #if debug
69 	unsigned int type;
70 #endif
71 
72 	UNUSED(which);
73 	UNUSED(arg);
74 
75 	attributes = DNS_RDATASTATSTYPE_ATTR(which);
76 #if debug
77 	type = DNS_RDATASTATSTYPE_BASE(which);
78 
79 	fprintf(stderr, "%s%s%s%s/%u, %u\n",
80 		attributes & DNS_RDATASTATSTYPE_ATTR_OTHERTYPE ? "O" : " ",
81 		attributes & DNS_RDATASTATSTYPE_ATTR_NXRRSET ? "!" : " ",
82 		attributes & DNS_RDATASTATSTYPE_ATTR_STALE ? "#" : " ",
83 		attributes & DNS_RDATASTATSTYPE_ATTR_NXDOMAIN ? "X" : " ",
84 		type, (unsigned)value);
85 #endif
86 	if ((attributes & DNS_RDATASTATSTYPE_ATTR_STALE) == 0)
87 		ATF_REQUIRE_EQ(value, 1);
88 	else
89 		ATF_REQUIRE_EQ(value, 0);
90 }
91 
92 static void
checkit2(dns_rdatastatstype_t which,isc_uint64_t value,void * arg)93 checkit2(dns_rdatastatstype_t which, isc_uint64_t value, void *arg) {
94 	unsigned int attributes;
95 #if debug
96 	unsigned int type;
97 #endif
98 
99 	UNUSED(which);
100 	UNUSED(arg);
101 
102 	attributes = DNS_RDATASTATSTYPE_ATTR(which);
103 #if debug
104 	type = DNS_RDATASTATSTYPE_BASE(which);
105 
106 	fprintf(stderr, "%s%s%s%s/%u, %u\n",
107 		attributes & DNS_RDATASTATSTYPE_ATTR_OTHERTYPE ? "O" : " ",
108 		attributes & DNS_RDATASTATSTYPE_ATTR_NXRRSET ? "!" : " ",
109 		attributes & DNS_RDATASTATSTYPE_ATTR_STALE ? "#" : " ",
110 		attributes & DNS_RDATASTATSTYPE_ATTR_NXDOMAIN ? "X" : " ",
111 		type, (unsigned)value);
112 #endif
113 	if ((attributes & DNS_RDATASTATSTYPE_ATTR_STALE) == 0)
114 		ATF_REQUIRE_EQ(value, 0);
115 	else
116 		ATF_REQUIRE_EQ(value, 1);
117 }
118 /*
119  * Individual unit tests
120  */
121 
122 ATF_TC(rdatasetstats);
ATF_TC_HEAD(rdatasetstats,tc)123 ATF_TC_HEAD(rdatasetstats, tc) {
124 	atf_tc_set_md_var(tc, "descr", "test that rdatasetstats counters are properly set");
125 }
ATF_TC_BODY(rdatasetstats,tc)126 ATF_TC_BODY(rdatasetstats, tc) {
127 	unsigned int i;
128 	dns_stats_t *stats = NULL;
129 	isc_result_t result;
130 
131 	UNUSED(tc);
132 
133 	result = dns_test_begin(NULL, ISC_TRUE);
134 	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
135 
136 	result = dns_rdatasetstats_create(mctx, &stats);
137 	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
138 
139 	/* First 256 types. */
140 	for (i = 0; i <= 255; i++)
141 		set_typestats(stats, (dns_rdatatype_t)i, ISC_FALSE);
142 	/* Specials */
143 	set_typestats(stats, dns_rdatatype_dlv, ISC_FALSE);
144 	set_typestats(stats, (dns_rdatatype_t)1000, ISC_FALSE);
145 	set_nxdomainstats(stats, ISC_FALSE);
146 
147 	/*
148 	 * Check that all counters are set to appropriately.
149 	 */
150 	dns_rdatasetstats_dump(stats, checkit1, NULL, 1);
151 
152 	/* First 256 types. */
153 	for (i = 0; i <= 255; i++)
154 		set_typestats(stats, (dns_rdatatype_t)i, ISC_TRUE);
155 	/* Specials */
156 	set_typestats(stats, dns_rdatatype_dlv, ISC_TRUE);
157 	set_typestats(stats, (dns_rdatatype_t)1000, ISC_TRUE);
158 	set_nxdomainstats(stats, ISC_TRUE);
159 
160 	/*
161 	 * Check that all counters are set to appropriately.
162 	 */
163 	dns_rdatasetstats_dump(stats, checkit2, NULL, 1);
164 
165 	dns_stats_detach(&stats);
166 	dns_test_end();
167 }
168 
169 /*
170  * Main
171  */
ATF_TP_ADD_TCS(tp)172 ATF_TP_ADD_TCS(tp) {
173 	ATF_TP_ADD_TC(tp, rdatasetstats);
174 	return (atf_no_error());
175 }
176 
177