]>
Commit | Line | Data |
---|---|---|
1c2d079d | 1 | #ifndef BOOST_TEST_DYN_LINK |
c8cb955a | 2 | #define BOOST_TEST_DYN_LINK |
1c2d079d FM |
3 | #endif |
4 | ||
c8cb955a PL |
5 | #define BOOST_TEST_NO_MAIN |
6 | #include <boost/test/unit_test.hpp> | |
7 | ||
8 | #include "negcache.hh" | |
9 | #include "dnsrecords.hh" | |
10 | #include "utility.hh" | |
11 | ||
42dcf516 OM |
12 | static recordsAndSignatures genRecsAndSigs(const DNSName& name, const uint16_t qtype, const string& content, bool sigs) |
13 | { | |
c8cb955a PL |
14 | recordsAndSignatures ret; |
15 | ||
16 | DNSRecord rec; | |
17 | rec.d_name = name; | |
18 | rec.d_type = qtype; | |
19 | rec.d_ttl = 600; | |
20 | rec.d_place = DNSResourceRecord::AUTHORITY; | |
d525b58b | 21 | rec.setContent(DNSRecordContent::make(qtype, QClass::IN, content)); |
c8cb955a PL |
22 | |
23 | ret.records.push_back(rec); | |
24 | ||
25 | if (sigs) { | |
26 | rec.d_type = QType::RRSIG; | |
d06dcda4 | 27 | rec.setContent(std::make_shared<RRSIGRecordContent>(QType(qtype).toString() + " 5 3 600 2037010100000000 2037010100000000 24567 dummy data")); |
c8cb955a PL |
28 | ret.signatures.push_back(rec); |
29 | } | |
30 | ||
31 | return ret; | |
32 | } | |
33 | ||
42dcf516 OM |
34 | static NegCache::NegCacheEntry genNegCacheEntry(const DNSName& name, const DNSName& auth, const struct timeval& now, const uint16_t qtype = 0) |
35 | { | |
c8cb955a PL |
36 | NegCache::NegCacheEntry ret; |
37 | ||
38 | ret.d_name = name; | |
39 | ret.d_qtype = QType(qtype); | |
40 | ret.d_auth = auth; | |
41 | ret.d_ttd = now.tv_sec + 600; | |
4bab9d74 | 42 | ret.d_orig_ttl = 600; |
c8cb955a PL |
43 | ret.authoritySOA = genRecsAndSigs(auth, QType::SOA, "ns1 hostmaster 1 2 3 4 5", true); |
44 | ret.DNSSECRecords = genRecsAndSigs(auth, QType::NSEC, "deadbeef", true); | |
45 | ||
46 | return ret; | |
47 | } | |
48 | ||
49 | BOOST_AUTO_TEST_SUITE(negcache_cc) | |
50 | ||
42dcf516 OM |
51 | BOOST_AUTO_TEST_CASE(test_get_entry) |
52 | { | |
c8cb955a PL |
53 | /* Add a full name negative entry to the cache and attempt to get an entry for |
54 | * the A record. Should yield the full name does not exist entry | |
55 | */ | |
56 | DNSName qname("www2.powerdns.com"); | |
57 | DNSName auth("powerdns.com"); | |
58 | ||
59 | struct timeval now; | |
60 | Utility::gettimeofday(&now, 0); | |
61 | ||
62 | NegCache cache; | |
63 | cache.add(genNegCacheEntry(qname, auth, now)); | |
64 | ||
690b86b7 | 65 | BOOST_CHECK_EQUAL(cache.size(), 1U); |
c8cb955a | 66 | |
33433a8a RG |
67 | NegCache::NegCacheEntry ne; |
68 | bool ret = cache.get(qname, QType(1), now, ne); | |
c8cb955a PL |
69 | |
70 | BOOST_CHECK(ret); | |
33433a8a | 71 | BOOST_CHECK_EQUAL(ne.d_name, qname); |
d5fcd583 | 72 | BOOST_CHECK_EQUAL(ne.d_qtype.toString(), QType(0).toString()); |
33433a8a | 73 | BOOST_CHECK_EQUAL(ne.d_auth, auth); |
c8cb955a PL |
74 | } |
75 | ||
82b0db0d O |
76 | BOOST_AUTO_TEST_CASE(test_get_entry2038) |
77 | { | |
78 | /* Add a full name negative entry to the cache and attempt to get an entry for | |
79 | * the A record. Should yield the full name does not exist entry | |
80 | */ | |
81 | DNSName qname("www2.powerdns.com"); | |
82 | DNSName auth("powerdns.com"); | |
83 | ||
b7a41068 | 84 | timeval now{INT_MAX - 300, 0}; |
82b0db0d O |
85 | |
86 | NegCache cache; | |
87 | cache.add(genNegCacheEntry(qname, auth, now)); | |
88 | ||
89 | BOOST_CHECK_EQUAL(cache.size(), 1U); | |
90 | ||
91 | NegCache::NegCacheEntry ne; | |
aac7307d | 92 | bool ret = cache.get(qname, QType(QType::A), now, ne); |
82b0db0d O |
93 | |
94 | BOOST_CHECK(ret); | |
95 | BOOST_CHECK_EQUAL(ne.d_name, qname); | |
d5fcd583 | 96 | BOOST_CHECK_EQUAL(ne.d_qtype.toString(), QType(0).toString()); |
82b0db0d O |
97 | BOOST_CHECK_EQUAL(ne.d_auth, auth); |
98 | } | |
99 | ||
42dcf516 OM |
100 | BOOST_AUTO_TEST_CASE(test_get_entry_exact_type) |
101 | { | |
b0c164a2 RG |
102 | /* Add a full name negative entry to the cache and attempt to get an entry for |
103 | * the A record, asking only for an exact match. | |
104 | */ | |
105 | DNSName qname("www2.powerdns.com"); | |
106 | DNSName auth("powerdns.com"); | |
107 | ||
108 | struct timeval now; | |
109 | Utility::gettimeofday(&now, 0); | |
110 | ||
111 | NegCache cache; | |
112 | cache.add(genNegCacheEntry(qname, auth, now)); | |
113 | ||
690b86b7 | 114 | BOOST_CHECK_EQUAL(cache.size(), 1U); |
b0c164a2 | 115 | |
33433a8a RG |
116 | NegCache::NegCacheEntry ne; |
117 | bool ret = cache.get(qname, QType(1), now, ne, true); | |
b0c164a2 RG |
118 | |
119 | BOOST_CHECK_EQUAL(ret, false); | |
120 | } | |
121 | ||
42dcf516 OM |
122 | BOOST_AUTO_TEST_CASE(test_get_NODATA_entry) |
123 | { | |
c8cb955a PL |
124 | DNSName qname("www2.powerdns.com"); |
125 | DNSName auth("powerdns.com"); | |
126 | ||
127 | struct timeval now; | |
128 | Utility::gettimeofday(&now, 0); | |
129 | ||
130 | NegCache cache; | |
131 | cache.add(genNegCacheEntry(qname, auth, now, 1)); | |
132 | ||
690b86b7 | 133 | BOOST_CHECK_EQUAL(cache.size(), 1U); |
c8cb955a | 134 | |
33433a8a RG |
135 | NegCache::NegCacheEntry ne; |
136 | bool ret = cache.get(qname, QType(1), now, ne); | |
c8cb955a PL |
137 | |
138 | BOOST_CHECK(ret); | |
33433a8a | 139 | BOOST_CHECK_EQUAL(ne.d_name, qname); |
d5fcd583 | 140 | BOOST_CHECK_EQUAL(ne.d_qtype.toString(), QType(1).toString()); |
33433a8a | 141 | BOOST_CHECK_EQUAL(ne.d_auth, auth); |
c8cb955a | 142 | |
33433a8a RG |
143 | NegCache::NegCacheEntry ne2; |
144 | ret = cache.get(qname, QType(16), now, ne2); | |
c8cb955a PL |
145 | BOOST_CHECK_EQUAL(ret, false); |
146 | } | |
147 | ||
42dcf516 OM |
148 | BOOST_AUTO_TEST_CASE(test_getRootNXTrust_entry) |
149 | { | |
c8cb955a PL |
150 | DNSName qname("com"); |
151 | DNSName auth("."); | |
152 | ||
153 | struct timeval now; | |
154 | Utility::gettimeofday(&now, 0); | |
155 | ||
156 | NegCache cache; | |
157 | cache.add(genNegCacheEntry(qname, auth, now)); | |
158 | ||
690b86b7 | 159 | BOOST_CHECK_EQUAL(cache.size(), 1U); |
c8cb955a | 160 | |
33433a8a | 161 | NegCache::NegCacheEntry ne; |
4bab9d74 | 162 | bool ret = cache.getRootNXTrust(qname, now, ne, false, false); |
c8cb955a PL |
163 | |
164 | BOOST_CHECK(ret); | |
33433a8a | 165 | BOOST_CHECK_EQUAL(ne.d_name, qname); |
d5fcd583 | 166 | BOOST_CHECK_EQUAL(ne.d_qtype.toString(), QType(0).toString()); |
33433a8a | 167 | BOOST_CHECK_EQUAL(ne.d_auth, auth); |
c8cb955a PL |
168 | } |
169 | ||
42dcf516 OM |
170 | BOOST_AUTO_TEST_CASE(test_add_and_get_expired_entry) |
171 | { | |
c8cb955a PL |
172 | DNSName qname("www2.powerdns.com"); |
173 | DNSName auth("powerdns.com"); | |
174 | ||
175 | struct timeval now; | |
176 | Utility::gettimeofday(&now, 0); | |
177 | now.tv_sec -= 1000; | |
178 | ||
179 | NegCache cache; | |
180 | cache.add(genNegCacheEntry(qname, auth, now)); | |
181 | ||
690b86b7 | 182 | BOOST_CHECK_EQUAL(cache.size(), 1U); |
c8cb955a | 183 | |
33433a8a | 184 | NegCache::NegCacheEntry ne; |
c8cb955a PL |
185 | |
186 | now.tv_sec += 1000; | |
33433a8a | 187 | bool ret = cache.get(qname, QType(1), now, ne); |
c8cb955a PL |
188 | |
189 | BOOST_CHECK_EQUAL(ret, false); | |
c8cb955a PL |
190 | } |
191 | ||
42dcf516 OM |
192 | BOOST_AUTO_TEST_CASE(test_getRootNXTrust_expired_entry) |
193 | { | |
0f84cbf7 PL |
194 | DNSName qname("com"); |
195 | DNSName auth("."); | |
196 | ||
197 | struct timeval now; | |
198 | Utility::gettimeofday(&now, 0); | |
199 | now.tv_sec -= 1000; | |
200 | ||
201 | NegCache cache; | |
202 | cache.add(genNegCacheEntry(qname, auth, now)); | |
203 | ||
690b86b7 | 204 | BOOST_CHECK_EQUAL(cache.size(), 1U); |
0f84cbf7 | 205 | |
33433a8a | 206 | NegCache::NegCacheEntry ne; |
0f84cbf7 PL |
207 | |
208 | now.tv_sec += 1000; | |
4bab9d74 | 209 | bool ret = cache.getRootNXTrust(qname, now, ne, false, false); |
0f84cbf7 PL |
210 | |
211 | BOOST_CHECK_EQUAL(ret, false); | |
0f84cbf7 PL |
212 | } |
213 | ||
42dcf516 OM |
214 | BOOST_AUTO_TEST_CASE(test_add_updated_entry) |
215 | { | |
c8cb955a PL |
216 | DNSName qname("www2.powerdns.com"); |
217 | DNSName auth("powerdns.com"); | |
218 | DNSName auth2("com"); | |
219 | ||
220 | struct timeval now; | |
221 | Utility::gettimeofday(&now, 0); | |
222 | ||
223 | NegCache cache; | |
224 | cache.add(genNegCacheEntry(qname, auth, now)); | |
225 | // Should override the existing entry for www2.powerdns.com | |
226 | cache.add(genNegCacheEntry(qname, auth2, now)); | |
227 | ||
690b86b7 | 228 | BOOST_CHECK_EQUAL(cache.size(), 1U); |
c8cb955a | 229 | |
33433a8a RG |
230 | NegCache::NegCacheEntry ne; |
231 | bool ret = cache.get(qname, QType(1), now, ne); | |
c8cb955a | 232 | |
0d434a13 | 233 | BOOST_CHECK(ret); |
33433a8a RG |
234 | BOOST_CHECK_EQUAL(ne.d_name, qname); |
235 | BOOST_CHECK_EQUAL(ne.d_auth, auth2); | |
c8cb955a PL |
236 | } |
237 | ||
42dcf516 OM |
238 | BOOST_AUTO_TEST_CASE(test_getRootNXTrust) |
239 | { | |
252e9a4c PL |
240 | DNSName qname("www2.powerdns.com"); |
241 | DNSName auth("powerdns.com"); | |
242 | DNSName qname2("com"); | |
243 | DNSName auth2("."); | |
244 | ||
245 | struct timeval now; | |
246 | Utility::gettimeofday(&now, 0); | |
247 | ||
248 | NegCache cache; | |
249 | cache.add(genNegCacheEntry(qname, auth, now)); | |
250 | cache.add(genNegCacheEntry(qname2, auth2, now)); | |
251 | ||
33433a8a | 252 | NegCache::NegCacheEntry ne; |
4bab9d74 | 253 | bool ret = cache.getRootNXTrust(qname, now, ne, false, false); |
252e9a4c PL |
254 | |
255 | BOOST_CHECK(ret); | |
33433a8a RG |
256 | BOOST_CHECK_EQUAL(ne.d_name, qname2); |
257 | BOOST_CHECK_EQUAL(ne.d_auth, auth2); | |
252e9a4c PL |
258 | } |
259 | ||
42dcf516 OM |
260 | BOOST_AUTO_TEST_CASE(test_getRootNXTrust_full_domain_only) |
261 | { | |
252e9a4c PL |
262 | DNSName qname("www2.powerdns.com"); |
263 | DNSName auth("powerdns.com"); | |
264 | DNSName qname2("com"); | |
265 | DNSName auth2("."); | |
266 | ||
267 | struct timeval now; | |
268 | Utility::gettimeofday(&now, 0); | |
269 | ||
270 | NegCache cache; | |
271 | cache.add(genNegCacheEntry(qname, auth, now)); | |
272 | cache.add(genNegCacheEntry(qname2, auth2, now, 1)); // Add the denial for COM|A | |
273 | ||
33433a8a | 274 | NegCache::NegCacheEntry ne; |
4bab9d74 | 275 | bool ret = cache.getRootNXTrust(qname, now, ne, false, false); |
252e9a4c PL |
276 | |
277 | BOOST_CHECK_EQUAL(ret, false); | |
278 | } | |
279 | ||
42dcf516 OM |
280 | BOOST_AUTO_TEST_CASE(test_prune) |
281 | { | |
c8cb955a PL |
282 | string qname(".powerdns.com"); |
283 | DNSName auth("powerdns.com"); | |
284 | ||
285 | struct timeval now; | |
286 | Utility::gettimeofday(&now, 0); | |
287 | ||
5dfa679e OM |
288 | NegCache cache(1); |
289 | NegCache::NegCacheEntry ne; | |
290 | for (int i = 0; i < 400; i++) { | |
291 | ne = genNegCacheEntry(DNSName(std::to_string(i) + qname), auth, now); | |
292 | cache.add(ne); | |
293 | } | |
294 | ||
295 | BOOST_CHECK_EQUAL(cache.size(), 400U); | |
296 | ||
48d02436 | 297 | cache.prune(now.tv_sec, 100); |
5dfa679e OM |
298 | |
299 | BOOST_CHECK_EQUAL(cache.size(), 100U); | |
300 | } | |
301 | ||
302 | BOOST_AUTO_TEST_CASE(test_prune_many_shards) | |
303 | { | |
304 | string qname(".powerdns.com"); | |
305 | DNSName auth("powerdns.com"); | |
306 | ||
307 | struct timeval now; | |
308 | Utility::gettimeofday(&now, 0); | |
309 | ||
c8cb955a PL |
310 | NegCache cache; |
311 | NegCache::NegCacheEntry ne; | |
42dcf516 | 312 | for (int i = 0; i < 400; i++) { |
c8cb955a PL |
313 | ne = genNegCacheEntry(DNSName(std::to_string(i) + qname), auth, now); |
314 | cache.add(ne); | |
315 | } | |
316 | ||
690b86b7 | 317 | BOOST_CHECK_EQUAL(cache.size(), 400U); |
c8cb955a | 318 | |
48d02436 | 319 | cache.prune(now.tv_sec, 100); |
c8cb955a | 320 | |
690b86b7 | 321 | BOOST_CHECK_EQUAL(cache.size(), 100U); |
c8cb955a PL |
322 | } |
323 | ||
42dcf516 OM |
324 | BOOST_AUTO_TEST_CASE(test_prune_valid_entries) |
325 | { | |
1d704752 RG |
326 | DNSName power1("powerdns.com."); |
327 | DNSName power2("powerdns-1.com."); | |
328 | DNSName auth("com."); | |
329 | ||
330 | struct timeval now; | |
331 | Utility::gettimeofday(&now, 0); | |
332 | ||
333 | NegCache cache; | |
334 | NegCache::NegCacheEntry ne; | |
335 | ||
336 | /* insert power1 then power2 */ | |
337 | ne = genNegCacheEntry(power1, auth, now); | |
338 | cache.add(ne); | |
339 | ne = genNegCacheEntry(power2, auth, now); | |
340 | cache.add(ne); | |
341 | ||
690b86b7 | 342 | BOOST_CHECK_EQUAL(cache.size(), 2U); |
1d704752 RG |
343 | |
344 | /* power2 has been inserted more recently, so it should be | |
345 | removed last */ | |
48d02436 | 346 | cache.prune(now.tv_sec, 1); |
690b86b7 | 347 | BOOST_CHECK_EQUAL(cache.size(), 1U); |
1d704752 | 348 | |
33433a8a RG |
349 | NegCache::NegCacheEntry got; |
350 | bool ret = cache.get(power2, QType(1), now, got); | |
1d704752 | 351 | BOOST_REQUIRE(ret); |
33433a8a RG |
352 | BOOST_CHECK_EQUAL(got.d_name, power2); |
353 | BOOST_CHECK_EQUAL(got.d_auth, auth); | |
1d704752 RG |
354 | |
355 | /* insert power1 back */ | |
356 | ne = genNegCacheEntry(power1, auth, now); | |
357 | cache.add(ne); | |
690b86b7 | 358 | BOOST_CHECK_EQUAL(cache.size(), 2U); |
1d704752 RG |
359 | |
360 | /* replace the entry for power2 */ | |
361 | ne = genNegCacheEntry(power2, auth, now); | |
362 | cache.add(ne); | |
363 | ||
690b86b7 | 364 | BOOST_CHECK_EQUAL(cache.size(), 2U); |
1d704752 RG |
365 | |
366 | /* power2 has been updated more recently, so it should be | |
367 | removed last */ | |
48d02436 | 368 | cache.prune(now.tv_sec, 1); |
1d704752 | 369 | |
690b86b7 | 370 | BOOST_CHECK_EQUAL(cache.size(), 1U); |
33433a8a RG |
371 | got = NegCache::NegCacheEntry(); |
372 | ret = cache.get(power2, QType(1), now, got); | |
1d704752 | 373 | BOOST_REQUIRE(ret); |
33433a8a RG |
374 | BOOST_CHECK_EQUAL(got.d_name, power2); |
375 | BOOST_CHECK_EQUAL(got.d_auth, auth); | |
1d704752 RG |
376 | } |
377 | ||
42dcf516 OM |
378 | BOOST_AUTO_TEST_CASE(test_wipe_single) |
379 | { | |
c8cb955a PL |
380 | string qname(".powerdns.com"); |
381 | DNSName auth("powerdns.com"); | |
382 | ||
383 | struct timeval now; | |
384 | Utility::gettimeofday(&now, 0); | |
385 | ||
386 | NegCache cache; | |
387 | NegCache::NegCacheEntry ne; | |
388 | ne = genNegCacheEntry(auth, auth, now); | |
389 | cache.add(ne); | |
390 | ||
42dcf516 | 391 | for (int i = 0; i < 400; i++) { |
c8cb955a PL |
392 | ne = genNegCacheEntry(DNSName(std::to_string(i) + qname), auth, now); |
393 | cache.add(ne); | |
394 | } | |
395 | ||
690b86b7 | 396 | BOOST_CHECK_EQUAL(cache.size(), 401U); |
c8cb955a PL |
397 | |
398 | // Should only wipe the powerdns.com entry | |
399 | cache.wipe(auth); | |
690b86b7 | 400 | BOOST_CHECK_EQUAL(cache.size(), 400U); |
c8cb955a | 401 | |
33433a8a RG |
402 | NegCache::NegCacheEntry ne2; |
403 | bool ret = cache.get(auth, QType(1), now, ne2); | |
c8cb955a PL |
404 | |
405 | BOOST_CHECK_EQUAL(ret, false); | |
c8cb955a PL |
406 | |
407 | cache.wipe(DNSName("1.powerdns.com")); | |
690b86b7 | 408 | BOOST_CHECK_EQUAL(cache.size(), 399U); |
c8cb955a | 409 | |
33433a8a RG |
410 | NegCache::NegCacheEntry ne3; |
411 | ret = cache.get(auth, QType(1), now, ne3); | |
c8cb955a PL |
412 | |
413 | BOOST_CHECK_EQUAL(ret, false); | |
c8cb955a PL |
414 | } |
415 | ||
42dcf516 OM |
416 | BOOST_AUTO_TEST_CASE(test_wipe_subtree) |
417 | { | |
c8cb955a PL |
418 | string qname(".powerdns.com"); |
419 | string qname2("powerdns.org"); | |
420 | DNSName auth("powerdns.com"); | |
421 | ||
422 | struct timeval now; | |
423 | Utility::gettimeofday(&now, 0); | |
424 | ||
425 | NegCache cache; | |
426 | NegCache::NegCacheEntry ne; | |
427 | ne = genNegCacheEntry(auth, auth, now); | |
428 | cache.add(ne); | |
429 | ||
42dcf516 | 430 | for (int i = 0; i < 400; i++) { |
c8cb955a PL |
431 | ne = genNegCacheEntry(DNSName(std::to_string(i) + qname), auth, now); |
432 | cache.add(ne); | |
433 | ne = genNegCacheEntry(DNSName(std::to_string(i) + qname2), auth, now); | |
434 | cache.add(ne); | |
435 | } | |
436 | ||
690b86b7 | 437 | BOOST_CHECK_EQUAL(cache.size(), 801U); |
c8cb955a PL |
438 | |
439 | // Should wipe all the *.powerdns.com and powerdns.com entries | |
440 | cache.wipe(auth, true); | |
690b86b7 | 441 | BOOST_CHECK_EQUAL(cache.size(), 400U); |
c8cb955a PL |
442 | } |
443 | ||
6075405b OM |
444 | BOOST_AUTO_TEST_CASE(test_wipe_typed) |
445 | { | |
446 | string qname(".powerdns.com"); | |
447 | DNSName auth("powerdns.com"); | |
448 | ||
449 | struct timeval now; | |
450 | Utility::gettimeofday(&now, 0); | |
451 | ||
452 | NegCache cache; | |
453 | NegCache::NegCacheEntry ne; | |
454 | ne = genNegCacheEntry(auth, auth, now, QType::A); | |
455 | cache.add(ne); | |
456 | ||
457 | for (int i = 0; i < 400; i++) { | |
458 | ne = genNegCacheEntry(DNSName(std::to_string(i) + qname), auth, now, QType::A); | |
459 | cache.add(ne); | |
460 | } | |
461 | ||
462 | BOOST_CHECK_EQUAL(cache.size(), 401U); | |
463 | ||
464 | // Should only wipe the powerdns.com entry | |
465 | cache.wipeTyped(auth, QType::A); | |
466 | BOOST_CHECK_EQUAL(cache.size(), 400U); | |
467 | ||
468 | NegCache::NegCacheEntry ne2; | |
469 | bool ret = cache.get(auth, QType(1), now, ne2); | |
470 | ||
471 | BOOST_CHECK_EQUAL(ret, false); | |
472 | ||
473 | cache.wipeTyped(DNSName("1.powerdns.com"), QType::A); | |
474 | BOOST_CHECK_EQUAL(cache.size(), 399U); | |
475 | ||
476 | NegCache::NegCacheEntry ne3; | |
477 | ret = cache.get(auth, QType(1), now, ne3); | |
478 | ||
479 | BOOST_CHECK_EQUAL(ret, false); | |
480 | } | |
481 | ||
42dcf516 OM |
482 | BOOST_AUTO_TEST_CASE(test_clear) |
483 | { | |
c8cb955a PL |
484 | string qname(".powerdns.com"); |
485 | DNSName auth("powerdns.com"); | |
486 | ||
487 | struct timeval now; | |
488 | Utility::gettimeofday(&now, 0); | |
489 | ||
490 | NegCache cache; | |
491 | NegCache::NegCacheEntry ne; | |
492 | ||
42dcf516 | 493 | for (int i = 0; i < 400; i++) { |
c8cb955a PL |
494 | ne = genNegCacheEntry(DNSName(std::to_string(i) + qname), auth, now); |
495 | cache.add(ne); | |
496 | } | |
497 | ||
690b86b7 | 498 | BOOST_CHECK_EQUAL(cache.size(), 400U); |
c8cb955a | 499 | cache.clear(); |
690b86b7 | 500 | BOOST_CHECK_EQUAL(cache.size(), 0U); |
c8cb955a PL |
501 | } |
502 | ||
42dcf516 OM |
503 | BOOST_AUTO_TEST_CASE(test_dumpToFile) |
504 | { | |
a0c1d047 | 505 | NegCache cache(1); |
911738a7 OM |
506 | vector<string> expected = { |
507 | "; negcache dump follows\n", | |
508 | ";\n", | |
5dfa679e | 509 | "; negcache shard 0; size 2\n", |
911738a7 OM |
510 | "www1.powerdns.com. 600 IN TYPE0 VIA powerdns.com. ; (Indeterminate) origttl=600 ss=0\n", |
511 | "powerdns.com. 600 IN SOA ns1. hostmaster. 1 2 3 4 5 ; (Indeterminate)\n", | |
512 | "powerdns.com. 600 IN RRSIG SOA 5 3 600 20370101000000 20370101000000 24567 dummy. data ;\n", | |
513 | "powerdns.com. 600 IN NSEC deadbeef. ; (Indeterminate)\n", | |
514 | "powerdns.com. 600 IN RRSIG NSEC 5 3 600 20370101000000 20370101000000 24567 dummy. data ;\n", | |
515 | "www2.powerdns.com. 600 IN TYPE0 VIA powerdns.com. ; (Indeterminate) origttl=600 ss=0\n", | |
516 | "powerdns.com. 600 IN SOA ns1. hostmaster. 1 2 3 4 5 ; (Indeterminate)\n", | |
517 | "powerdns.com. 600 IN RRSIG SOA 5 3 600 20370101000000 20370101000000 24567 dummy. data ;\n", | |
518 | "powerdns.com. 600 IN NSEC deadbeef. ; (Indeterminate)\n", | |
519 | "powerdns.com. 600 IN RRSIG NSEC 5 3 600 20370101000000 20370101000000 24567 dummy. data ;\n", | |
5dfa679e | 520 | "; negcache size: 2/0 shards: 1 min/max shard size: 2/2\n"}; |
c8cb955a PL |
521 | |
522 | struct timeval now; | |
523 | Utility::gettimeofday(&now, 0); | |
524 | ||
525 | cache.add(genNegCacheEntry(DNSName("www1.powerdns.com"), DNSName("powerdns.com"), now)); | |
526 | cache.add(genNegCacheEntry(DNSName("www2.powerdns.com"), DNSName("powerdns.com"), now)); | |
527 | ||
01807c64 RG |
528 | auto fp = pdns::UniqueFilePtr(tmpfile()); |
529 | if (!fp) { | |
c8cb955a | 530 | BOOST_FAIL("Temporary file could not be opened"); |
01807c64 | 531 | } |
c8cb955a | 532 | |
99934d02 | 533 | cache.doDump(fileno(fp.get()), 0, now.tv_sec); |
c8cb955a | 534 | |
e30bc0cf | 535 | rewind(fp.get()); |
42dcf516 | 536 | char* line = nullptr; |
c8cb955a PL |
537 | size_t len = 0; |
538 | ssize_t read; | |
539 | ||
fb63c987 | 540 | for (auto str : expected) { |
e30bc0cf | 541 | read = getline(&line, &len, fp.get()); |
c8cb955a PL |
542 | if (read == -1) |
543 | BOOST_FAIL("Unable to read a line from the temp file"); | |
fb63c987 | 544 | // The clock might have ticked so the 600 becomes 599 |
4bab9d74 | 545 | BOOST_CHECK_EQUAL(line, str); |
c8cb955a | 546 | } |
9a79029f | 547 | |
fb63c987 OM |
548 | /* getline() allocates a buffer when called with a nullptr, |
549 | then reallocates it when needed, but we need to free the | |
550 | last allocation if any. */ | |
551 | free(line); | |
c8cb955a PL |
552 | } |
553 | ||
42dcf516 OM |
554 | BOOST_AUTO_TEST_CASE(test_count) |
555 | { | |
17f7162d PL |
556 | string qname(".powerdns.com"); |
557 | string qname2("powerdns.org"); | |
558 | DNSName auth("powerdns.com"); | |
559 | ||
560 | struct timeval now; | |
561 | Utility::gettimeofday(&now, 0); | |
562 | ||
563 | NegCache cache; | |
564 | NegCache::NegCacheEntry ne; | |
565 | ne = genNegCacheEntry(auth, auth, now); | |
566 | cache.add(ne); | |
567 | ||
42dcf516 | 568 | for (int i = 0; i < 400; i++) { |
17f7162d PL |
569 | ne = genNegCacheEntry(DNSName(std::to_string(i) + qname), auth, now); |
570 | cache.add(ne); | |
571 | ne = genNegCacheEntry(DNSName(std::to_string(i) + qname2), auth, now); | |
572 | cache.add(ne); | |
573 | } | |
574 | ||
575 | uint64_t count; | |
576 | count = cache.count(auth); | |
690b86b7 | 577 | BOOST_CHECK_EQUAL(count, 1U); |
17f7162d | 578 | count = cache.count(auth, QType(1)); |
690b86b7 | 579 | BOOST_CHECK_EQUAL(count, 0U); |
17f7162d PL |
580 | } |
581 | ||
c8cb955a | 582 | BOOST_AUTO_TEST_SUITE_END() |