From: Otto Moerbeek Date: Tue, 19 Sep 2023 09:03:43 +0000 (+0200) Subject: Add test for TTL perc bug found while working on issue #13266 X-Git-Tag: rec-5.0.0-alpha2~52^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1e325a929a3dd6e1eca1f52cb854bbb3eeb57009;p=thirdparty%2Fpdns.git Add test for TTL perc bug found while working on issue #13266 --- diff --git a/pdns/recursordist/test-recpacketcache_cc.cc b/pdns/recursordist/test-recpacketcache_cc.cc index e0494fe18a..8b0787d7f5 100644 --- a/pdns/recursordist/test-recpacketcache_cc.cc +++ b/pdns/recursordist/test-recpacketcache_cc.cc @@ -10,6 +10,8 @@ #include "dns_random.hh" #include "iputils.hh" #include "recpacketcache.hh" +#include "taskqueue.hh" +#include "rec-taskqueue.hh" #include BOOST_AUTO_TEST_SUITE(test_recpacketcache_cc) @@ -80,6 +82,66 @@ BOOST_AUTO_TEST_CASE(test_recPacketCacheSimple) BOOST_CHECK_EQUAL(rpc.size(), 0U); } +BOOST_AUTO_TEST_CASE(test_recPacketCacheSimpleWithRefresh) +{ + RecursorPacketCache::s_refresh_ttlperc = 30; + RecursorPacketCache rpc(1000); + string fpacket; + unsigned int tag = 0; + uint32_t age = 0; + uint32_t qhash = 0; + uint32_t ttd = 3600; + BOOST_CHECK_EQUAL(rpc.size(), 0U); + + DNSName qname("www.powerdns.com"); + vector packet; + DNSPacketWriter pw(packet, qname, QType::A); + pw.getHeader()->rd = true; + pw.getHeader()->qr = false; + pw.getHeader()->id = dns_random_uint16(); + string qpacket((const char*)&packet[0], packet.size()); + pw.startRecord(qname, QType::A, ttd); + + time_t now = time(nullptr); + + BOOST_CHECK_EQUAL(rpc.getResponsePacket(tag, qpacket, qname, QType::A, QClass::IN, now, &fpacket, &age, &qhash), false); + + ARecordContent ar("127.0.0.1"); + ar.toPacket(pw); + pw.commit(); + string rpacket((const char*)&packet[0], packet.size()); + + rpc.insertResponsePacket(tag, qhash, string(qpacket), qname, QType::A, QClass::IN, string(rpacket), now, ttd, vState::Indeterminate, boost::none, false); + BOOST_CHECK_EQUAL(rpc.size(), 1U); + uint32_t qhash2 = 0; + bool found = rpc.getResponsePacket(tag, qpacket, qname, QType::A, QClass::IN, now, &fpacket, &age, &qhash2); + BOOST_CHECK_EQUAL(found, true); + BOOST_CHECK_EQUAL(qhash, qhash2); + BOOST_CHECK_EQUAL(fpacket, rpacket); + + BOOST_REQUIRE_EQUAL(getTaskSize(), 0U); + + // Half of time has passed, no task should have been submitted + now += ttd / 2; + found = rpc.getResponsePacket(tag, qpacket, qname, QType::A, QClass::IN, now, &fpacket, &age, &qhash2); + BOOST_CHECK_EQUAL(found, true); + BOOST_CHECK_EQUAL(qhash, qhash2); + BOOST_CHECK_EQUAL(fpacket, rpacket); + + BOOST_REQUIRE_EQUAL(getTaskSize(), 0U); + + // 75% of time has passed, task should have been submitted as refresh perc is 30 and (100-75) = 25 <= 30 + now += ttd / 4; + found = rpc.getResponsePacket(tag, qpacket, qname, QType::A, QClass::IN, now, &fpacket, &age, &qhash2); + BOOST_CHECK_EQUAL(found, true); + BOOST_CHECK_EQUAL(qhash, qhash2); + BOOST_CHECK_EQUAL(fpacket, rpacket); + + BOOST_REQUIRE_EQUAL(getTaskSize(), 1U); + auto task = taskQueuePop(); + BOOST_CHECK_EQUAL(task.d_qname, qname); +} + BOOST_AUTO_TEST_CASE(test_recPacketCacheSimplePost2038) { RecursorPacketCache rpc(1000); @@ -275,7 +337,7 @@ BOOST_AUTO_TEST_CASE(test_recPacketCache_Tags) BOOST_AUTO_TEST_CASE(test_recPacketCache_TCP) { /* Insert a response with UDP, the exact same query with a TCP flag - should lead to a miss. + should lead to a miss. */ RecursorPacketCache rpc(1000); string fpacket; diff --git a/pdns/recursordist/test-syncres_cc.cc b/pdns/recursordist/test-syncres_cc.cc index 179d6b0269..044af55332 100644 --- a/pdns/recursordist/test-syncres_cc.cc +++ b/pdns/recursordist/test-syncres_cc.cc @@ -7,6 +7,7 @@ #include "root-dnssec.hh" #include "rec-taskqueue.hh" #include "test-syncres_cc.hh" +#include "recpacketcache.hh" GlobalStateHolder g_luaconfs; GlobalStateHolder g_xdnssec; @@ -139,6 +140,7 @@ void initSR(bool debug) g_log.toConsole(Logger::Error); } + RecursorPacketCache::s_refresh_ttlperc = 0; MemRecursorCache::s_maxServedStaleExtensions = 0; NegCache::s_maxServedStaleExtensions = 0; g_recCache = std::make_unique();