]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Add test for TTL perc bug found while working on issue #13266
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Tue, 19 Sep 2023 09:03:43 +0000 (11:03 +0200)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Wed, 20 Sep 2023 11:11:19 +0000 (13:11 +0200)
pdns/recursordist/test-recpacketcache_cc.cc
pdns/recursordist/test-syncres_cc.cc

index e0494fe18ac994f28b07fee883fd941c7b0525fb..8b0787d7f52c0a538878b7f4b244bbe8bcf9859a 100644 (file)
@@ -10,6 +10,8 @@
 #include "dns_random.hh"
 #include "iputils.hh"
 #include "recpacketcache.hh"
+#include "taskqueue.hh"
+#include "rec-taskqueue.hh"
 #include <utility>
 
 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<uint8_t> 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;
index 179d6b0269c0cc6c984c8b37eea065d4c352c2cd..044af55332290c2443533054eb0e9a06fd7613da 100644 (file)
@@ -7,6 +7,7 @@
 #include "root-dnssec.hh"
 #include "rec-taskqueue.hh"
 #include "test-syncres_cc.hh"
+#include "recpacketcache.hh"
 
 GlobalStateHolder<LuaConfigItems> g_luaconfs;
 GlobalStateHolder<SuffixMatchNode> 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<MemRecursorCache>();