]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Add test
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Mon, 7 Jul 2025 10:28:12 +0000 (12:28 +0200)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Wed, 9 Jul 2025 09:17:19 +0000 (11:17 +0200)
Signed-off-by: Otto Moerbeek <otto.moerbeek@open-xchange.com>
pdns/recursordist/rec-nsspeeds.cc
pdns/recursordist/test-reczones-helpers.cc
pdns/recursordist/test-syncres_cc2.cc

index 8e02a6e7f1e2a141521517f36a22d5da610057a7..12c7c408e7549a20160c5f277d48e874e4b0b644 100644 (file)
@@ -89,7 +89,7 @@ size_t nsspeeds_t::getPB(const string& serverID, size_t maxSize, std::string& re
   for (const auto& entry : *this) {
     protozero::pbf_builder<PBNSSpeedEntry> message(full, PBNSSpeedDump::repeated_message_nsspeedEntry);
     getPBEntry(message, entry);
-    if (ret.size() > maxSize) {
+    if (maxSize > 0 && ret.size() > maxSize) {
       message.rollback();
       log->info(Logr::Info, "Produced nsspeed dump (max size reached)", "size", Logging::Loggable(ret.size()), "count", Logging::Loggable(count));
       return count;
index 7b0d4845acebdb07c0b9ca806651678a41d8b21c..30d7b20e71b875e6796c9292075523b7286ea272 100644 (file)
@@ -277,20 +277,21 @@ BOOST_AUTO_TEST_CASE(test_UserHints)
 
   ::arg().set("max-generate-steps") = "0";
   ::arg().set("max-include-depth") = "0";
-  char temp[] = "/tmp/hintsXXXXXXXXXX";
-  int fd = mkstemp(temp);
-  BOOST_REQUIRE(fd > 0);
-  FILE* fp = fdopen(fd, "w");
-  BOOST_REQUIRE(fp != nullptr);
-  size_t written = fwrite(hints.data(), 1, hints.length(), fp);
+  string temp{"/tmp/hintsXXXXXXXXXX"};
+  int fileDesc = mkstemp(temp.data());
+  BOOST_REQUIRE(fileDesc > 0);
+  FILE* filePointer = fdopen(fileDesc, "w");
+  BOOST_REQUIRE(filePointer != nullptr);
+  size_t written = fwrite(hints.data(), 1, hints.length(), filePointer);
   BOOST_REQUIRE(written == hints.length());
-  BOOST_REQUIRE(fclose(fp) == 0);
+  BOOST_REQUIRE(fclose(filePointer) == 0); // NOLINT
 
   time_t now = time(nullptr);
   std::vector<DNSRecord> nsvec;
 
-  auto ok = readHintsIntoCache(now, std::string(temp), nsvec);
-  BOOST_CHECK(ok);
+  auto readOK = readHintsIntoCache(now, std::string(temp), nsvec);
+  unlink(temp.data());
+  BOOST_CHECK(readOK);
   BOOST_CHECK_EQUAL(nsvec.size(), 2U);
 
   const MemRecursorCache::Flags flags = 0;
index c478b758d9bfc30eff1d350375cf9b0e54c72dca..ca28f3d87a455642637249e5021e7bf4c701cfca 100644 (file)
@@ -1366,6 +1366,8 @@ BOOST_AUTO_TEST_CASE(test_ns_speed)
   std::unique_ptr<SyncRes> sr;
   initSR(sr);
 
+  BOOST_CHECK_EQUAL(SyncRes::getNSSpeedsSize(), 0U);
+
   primeHints();
 
   const DNSName target("powerdns.com.");
@@ -1430,8 +1432,53 @@ BOOST_AUTO_TEST_CASE(test_ns_speed)
   BOOST_CHECK_EQUAL(nsCounts[ComboAddress("192.0.2.1:53")], 1U);
   BOOST_CHECK_EQUAL(nsCounts[ComboAddress("192.0.2.2:53")], 1U);
   BOOST_CHECK_EQUAL(nsCounts[ComboAddress("[2001:DB8::2]:53")], 1U);
+
+
+  // read PB representation back and forth, compare using the text dump
+  std::string temp1{"/tmp/speedDump1XXXXXX"};
+  std::string temp2{"/tmp/speedDump2XXXXXX"};
+  auto fd1 = FDWrapper(mkstemp(temp1.data()));
+  auto fd2 = FDWrapper(mkstemp(temp2.data()));
+  auto count = SyncRes::doDumpNSSpeeds(fd1);
+  fd1.reset();
+  std::string pbDump;
+  auto records = SyncRes::getNSSpeedTable(0, pbDump);
+  BOOST_CHECK_EQUAL(records, count);
+
+  SyncRes::clearNSSpeeds();
+  BOOST_CHECK_EQUAL(SyncRes::getNSSpeedsSize(), 0U);
+
+  // Put PB dump back
+  count = SyncRes::putIntoNSSpeedTable(pbDump);
+  BOOST_CHECK_EQUAL(records, count);
+  count = SyncRes::doDumpNSSpeeds(fd2);
+  fd2.reset();
+  BOOST_CHECK_EQUAL(records, count);
+
+  // NS speed table is a hashed unique table, which not neccesarily stable wrt recreation
+  // So we read the lines, sort them and compare
+  std::ifstream file1(temp1);
+  std::ifstream file2(temp2);
+  std::vector<std::string> lines1;
+  std::vector<std::string> lines2;
+  while (file1.good()) {
+    std::string line;
+    std::getline(file1, line);
+    lines1.emplace_back(line);
+  }
+  while (file2.good()) {
+    std::string line;
+    std::getline(file2, line);
+    lines2.emplace_back(line);
+  }
+  unlink(temp1.data());
+  unlink(temp2.data());
+  std::sort(lines1.begin(), lines1.end());
+  std::sort(lines2.begin(), lines2.end());
+  BOOST_CHECK(lines1 == lines2);
 }
 
+
 BOOST_AUTO_TEST_CASE(test_flawed_nsset)
 {
   std::unique_ptr<SyncRes> sr;