Debian Codegraph @

tests/dns/qpdb_test.c

raw
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 <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #define UNIT_TESTING
24 #include <cmocka.h>
25
26 #include <isc/util.h>
27
28 #include <dns/rbt.h>
29 #include <dns/rdatalist.h>
30 #include <dns/rdataset.h>
31 #include <dns/rdatastruct.h>
32 #define KEEP_BEFORE
33
34 /* Include the main file */
35
36 #pragma GCC diagnostic push
37 #pragma GCC diagnostic ignored "-Wshadow"
38 #undef CHECK
39 #include "qpcache.c"
40 #pragma GCC diagnostic pop
41
42 #undef CHECK
43 #include <tests/dns.h>
44
45 /* Set to true (or use -v option) for verbose output */
46 static bool verbose = false;
47
48 /*
49 * Add to a cache DB 'db' an rdataset of type 'rtype' at a name
50 * <idx>.example.com. The rdataset would contain one data, and rdata_len is
51 * its length. 'rtype' is supposed to be some private type whose data can be
52 * arbitrary (and it doesn't matter in this test).
53 */
54 static void
55 overmempurge_addrdataset(dns_db_t *db, isc_stdtime_t now, int idx,
56 dns_rdatatype_t rtype, size_t rdata_len,
57 bool longname) {
58 isc_result_t result;
59 dns_rdata_t rdata;
60 dns_dbnode_t *node = NULL;
61 dns_rdatalist_t rdatalist;
62 dns_rdataset_t rdataset;
63 dns_fixedname_t fname;
64 dns_name_t *name;
65 char namebuf[DNS_NAME_FORMATSIZE];
66 unsigned char rdatabuf[65535] = { 0 }; /* large enough for any valid
67 RDATA */
68
69 REQUIRE(rdata_len <= sizeof(rdatabuf));
70
71 if (longname) {
72 /*
73 * Build a longest possible name (in wire format) that would
74 * result in a new rbt node with the long name data.
75 */
76 snprintf(namebuf, sizeof(namebuf),
77 "%010d.%010dabcdef%010dabcdef%010dabcdef%010dabcde."
78 "%010dabcdef%010dabcdef%010dabcdef%010dabcde."
79 "%010dabcdef%010dabcdef%010dabcdef%010dabcde."
80 "%010dabcdef%010dabcdef%010dabcdef01.",
81 idx, idx, idx, idx, idx, idx, idx, idx, idx, idx, idx,
82 idx, idx, idx, idx, idx);
83 } else {
84 snprintf(namebuf, sizeof(namebuf), "%d.example.com.", idx);
85 }
86 dns_test_namefromstring(namebuf, &fname);
87 name = dns_fixedname_name(&fname);
88
89 result = dns_db_findnode(db, name, true, &node);
90 assert_int_equal(result, ISC_R_SUCCESS);
91 assert_non_null(node);
92
93 dns_rdata_init(&rdata);
94 rdata.length = rdata_len;
95 rdata.data = rdatabuf;
96 rdata.rdclass = dns_rdataclass_in;
97 rdata.type = rtype;
98
99 dns_rdatalist_init(&rdatalist);
100 rdatalist.rdclass = dns_rdataclass_in;
101 rdatalist.type = rtype;
102 rdatalist.ttl = 3600;
103 ISC_LIST_APPEND(rdatalist.rdata, &rdata, link);
104
105 dns_rdataset_init(&rdataset);
106 dns_rdatalist_tordataset(&rdatalist, &rdataset);
107
108 result = dns_db_addrdataset(db, node, NULL, now, &rdataset, 0, NULL);
109 assert_int_equal(result, ISC_R_SUCCESS);
110
111 dns_db_detachnode(db, &node);
112 }
113
114 ISC_LOOP_TEST_IMPL(overmempurge_bigrdata) {
115 size_t maxcache = 2097152U; /* 2MB - same as DNS_CACHE_MINSIZE */
116 size_t hiwater = maxcache - (maxcache >> 3); /* borrowed from cache.c */
117 size_t lowater = maxcache - (maxcache >> 2); /* ditto */
118 isc_result_t result;
119 dns_db_t *db = NULL;
120 isc_mem_t *mctx2 = NULL;
121 isc_stdtime_t now = isc_stdtime_now();
122 size_t i;
123
124 isc_mem_create(&mctx2);
125
126 result = dns_db_create(mctx2, CACHEDB_DEFAULT, dns_rootname,
127 dns_dbtype_cache, dns_rdataclass_in, 0, NULL,
128 &db);
129 assert_int_equal(result, ISC_R_SUCCESS);
130
131 isc_mem_setwater(mctx2, hiwater, lowater);
132
133 /*
134 * Add cache entries with minimum size of data until 'overmem'
135 * condition is triggered.
136 * This should eventually happen, but we also limit the number of
137 * iteration to avoid an infinite loop in case something gets wrong.
138 */
139 for (i = 0; !isc_mem_isovermem(mctx2) && i < (maxcache / 10); i++) {
140 overmempurge_addrdataset(db, now, i, 50053, 0, false);
141 }
142 assert_true(isc_mem_isovermem(mctx2));
143
144 /*
145 * Then try to add the same number of entries, each has very large data.
146 * 'overmem purge' should keep the total cache size from exceeding
147 * the 'hiwater' mark too much. So we should be able to assume the
148 * cache size doesn't reach the "max".
149 */
150 while (i-- > 0) {
151 overmempurge_addrdataset(db, now, i, 50054, 65535, false);
152 if (verbose) {
153 print_message("# inuse: %zd max: %zd\n",
154 isc_mem_inuse(mctx2), maxcache);
155 }
156 assert_true(isc_mem_inuse(mctx2) < maxcache);
157 }
158
159 dns_db_detach(&db);
160 isc_mem_destroy(&mctx2);
161 isc_loopmgr_shutdown(loopmgr);
162 }
163
164 ISC_LOOP_TEST_IMPL(overmempurge_longname) {
165 size_t maxcache = 2097152U; /* 2MB - same as DNS_CACHE_MINSIZE */
166 size_t hiwater = maxcache - (maxcache >> 3); /* borrowed from cache.c */
167 size_t lowater = maxcache - (maxcache >> 2); /* ditto */
168 isc_result_t result;
169 dns_db_t *db = NULL;
170 isc_mem_t *mctx2 = NULL;
171 isc_stdtime_t now = isc_stdtime_now();
172 size_t i;
173
174 isc_mem_create(&mctx2);
175
176 result = dns_db_create(mctx2, CACHEDB_DEFAULT, dns_rootname,
177 dns_dbtype_cache, dns_rdataclass_in, 0, NULL,
178 &db);
179 assert_int_equal(result, ISC_R_SUCCESS);
180
181 isc_mem_setwater(mctx2, hiwater, lowater);
182
183 /*
184 * Add cache entries with minimum size of data until 'overmem'
185 * condition is triggered.
186 * This should eventually happen, but we also limit the number of
187 * iteration to avoid an infinite loop in case something gets wrong.
188 */
189 for (i = 0; !isc_mem_isovermem(mctx2) && i < (maxcache / 10); i++) {
190 overmempurge_addrdataset(db, now, i, 50053, 0, false);
191 }
192 assert_true(isc_mem_isovermem(mctx2));
193
194 /*
195 * Then try to add the same number of entries, each has very long name.
196 * 'overmem purge' should keep the total cache size from not exceeding
197 * the 'hiwater' mark too much. So we should be able to assume the cache
198 * size doesn't reach the "max".
199 */
200 while (i-- > 0) {
201 overmempurge_addrdataset(db, now, i, 50054, 0, true);
202 if (verbose) {
203 print_message("# inuse: %zd max: %zd\n",
204 isc_mem_inuse(mctx2), maxcache);
205 }
206 assert_true(isc_mem_inuse(mctx2) < maxcache);
207 }
208
209 dns_db_detach(&db);
210 isc_mem_destroy(&mctx2);
211 isc_loopmgr_shutdown(loopmgr);
212 }
213
214 ISC_TEST_LIST_START
215 ISC_TEST_ENTRY_CUSTOM(overmempurge_bigrdata, setup_managers, teardown_managers)
216 ISC_TEST_ENTRY_CUSTOM(overmempurge_longname, setup_managers, teardown_managers)
217 ISC_TEST_LIST_END
218
219 ISC_TEST_MAIN