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;
::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;
std::unique_ptr<SyncRes> sr;
initSR(sr);
+ BOOST_CHECK_EQUAL(SyncRes::getNSSpeedsSize(), 0U);
+
primeHints();
const DNSName target("powerdns.com.");
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;