From: Otto Moerbeek Date: Mon, 7 Jul 2025 10:28:12 +0000 (+0200) Subject: Add test X-Git-Tag: rec-5.4.0-alpha0~41^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d7eb5d39463fa126a69d4efad3a2e317c489b934;p=thirdparty%2Fpdns.git Add test Signed-off-by: Otto Moerbeek --- diff --git a/pdns/recursordist/rec-nsspeeds.cc b/pdns/recursordist/rec-nsspeeds.cc index 8e02a6e7f1..12c7c408e7 100644 --- a/pdns/recursordist/rec-nsspeeds.cc +++ b/pdns/recursordist/rec-nsspeeds.cc @@ -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 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; diff --git a/pdns/recursordist/test-reczones-helpers.cc b/pdns/recursordist/test-reczones-helpers.cc index 7b0d4845ac..30d7b20e71 100644 --- a/pdns/recursordist/test-reczones-helpers.cc +++ b/pdns/recursordist/test-reczones-helpers.cc @@ -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 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; diff --git a/pdns/recursordist/test-syncres_cc2.cc b/pdns/recursordist/test-syncres_cc2.cc index c478b758d9..ca28f3d87a 100644 --- a/pdns/recursordist/test-syncres_cc2.cc +++ b/pdns/recursordist/test-syncres_cc2.cc @@ -1366,6 +1366,8 @@ BOOST_AUTO_TEST_CASE(test_ns_speed) std::unique_ptr 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 lines1; + std::vector 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 sr;