]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dns: Add unit tests for the RCode/ERCode/Opcode helpers
authorRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 11 Jul 2025 08:05:04 +0000 (10:05 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 11 Jul 2025 08:05:04 +0000 (10:05 +0200)
meson.build
pdns/Makefile.am
pdns/dnsdistdist/Makefile.am
pdns/dnsdistdist/meson.build
pdns/dnsdistdist/test-dns_cc.cc [new symlink]
pdns/test-dns_cc.cc [new file with mode: 0644]

index 3d8590c13ddf650402a8c80754257b9b9883781c..4b6aa8b85007f8910fed188daf58224987ce7e63 100644 (file)
@@ -869,6 +869,7 @@ if get_option('unit-tests')
       src_dir / 'test-credentials_cc.cc',
       src_dir / 'test-digests_hh.cc',
       src_dir / 'test-distributor_hh.cc',
+      src_dir / 'test-dns_cc.cc',
       src_dir / 'test-dns_random_hh.cc',
       src_dir / 'test-dnsname_cc.cc',
       src_dir / 'test-dnsparser_cc.cc',
index 7c1945db2bc9418d3058c906649323e7ad8d7a2c..8dd8e5c3f70f97ade600b0652e621ca1cf77e722 100644 (file)
@@ -1399,6 +1399,7 @@ testrunner_SOURCES = \
        test-credentials_cc.cc \
        test-digests_hh.cc \
        test-distributor_hh.cc \
+       test-dns_cc.cc \
        test-dns_random_hh.cc \
        test-dnsname_cc.cc \
        test-dnsparser_cc.cc \
index 91c7ba40f4f9350afbb30ed35702dbc9994a7e42..a74d51394e8dc997ac544adf504f1d7ed1bc79c1 100644 (file)
@@ -392,6 +392,7 @@ testrunner_SOURCES = \
        test-connectionmanagement_hh.cc \
        test-credentials_cc.cc \
        test-delaypipe_hh.cc \
+       test-dns_cc.cc \
        test-dnscrypt_cc.cc \
        test-dnsdist-connections-cache.cc \
        test-dnsdist-dnsparser.cc \
index 92ba36f633cce14e1c524f3fb63f9e69e82ad02d..30f0d76436ea0702a5ede9a9bd70e477be13ed9c 100644 (file)
@@ -517,6 +517,7 @@ test_sources += files(
   src_dir / 'test-connectionmanagement_hh.cc',
   src_dir / 'test-credentials_cc.cc',
   src_dir / 'test-delaypipe_hh.cc',
+  src_dir / 'test-dns_cc.cc',
   src_dir / 'test-dnscrypt_cc.cc',
   src_dir / 'test-dnsdistasync.cc',
   src_dir / 'test-dnsdistbackend_cc.cc',
diff --git a/pdns/dnsdistdist/test-dns_cc.cc b/pdns/dnsdistdist/test-dns_cc.cc
new file mode 120000 (symlink)
index 0000000..e14fc8a
--- /dev/null
@@ -0,0 +1 @@
+../test-dns_cc.cc
\ No newline at end of file
diff --git a/pdns/test-dns_cc.cc b/pdns/test-dns_cc.cc
new file mode 100644 (file)
index 0000000..45eccfa
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * This file is part of PowerDNS or dnsdist.
+ * Copyright -- PowerDNS.COM B.V. and its contributors
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * In addition, for the avoidance of any doubt, permission is granted to
+ * link this program with OpenSSL and to (re)distribute the binaries
+ * produced as the result of such linking.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#ifndef BOOST_TEST_DYN_LINK
+#define BOOST_TEST_DYN_LINK
+#endif
+
+#define BOOST_TEST_NO_MAIN
+
+#include <boost/test/unit_test.hpp>
+
+#include "dns.hh"
+
+BOOST_AUTO_TEST_SUITE(test_dnscc)
+
+BOOST_AUTO_TEST_CASE(test_rcode)
+{
+  BOOST_CHECK_EQUAL(RCode::to_s(255), "ErrOutOfRange");
+
+  for (uint8_t idx = 0; idx <= RCode::NotZone; idx++) {
+    auto long_s = RCode::to_s(idx);
+    BOOST_CHECK(long_s.size() > 0);
+    auto short_s = RCode::to_short_s(idx);
+    auto rcode = RCode::from_short(short_s);
+    BOOST_CHECK(rcode);
+    BOOST_CHECK_EQUAL(*rcode, idx);
+  }
+
+  BOOST_CHECK_EQUAL(RCode::to_s(RCode::NotZone + 1), "Err#11");
+}
+
+BOOST_AUTO_TEST_CASE(test_ercode)
+{
+  for (uint16_t idx = ERCode::BADVERS; idx <= ERCode::BADCOOKIE; idx++) {
+    auto long_s = ERCode::to_s(idx);
+    BOOST_CHECK(long_s.size() > 0);
+  }
+
+  BOOST_CHECK_EQUAL(ERCode::to_s(ERCode::BADCOOKIE + 1), "Err#24");
+}
+
+BOOST_AUTO_TEST_CASE(test_opcode)
+{
+  for (uint8_t idx = Opcode::Query; idx <= Opcode::Update; idx++) {
+    auto long_s = Opcode::to_s(idx);
+    BOOST_CHECK(long_s.size() > 0);
+  }
+
+  BOOST_CHECK_EQUAL(Opcode::to_s(Opcode::Update + 1), std::to_string(Opcode::Update + 1));
+}
+
+BOOST_AUTO_TEST_CASE(test_resource_record_place)
+{
+  for (uint8_t idx = DNSResourceRecord::Place::QUESTION; idx <= DNSResourceRecord::Place::ADDITIONAL; idx++) {
+    auto long_s = DNSResourceRecord::placeString(idx);
+    BOOST_CHECK(long_s.size() > 0);
+  }
+
+  BOOST_CHECK_EQUAL(DNSResourceRecord::placeString(DNSResourceRecord::Place::ADDITIONAL + 1), "?");
+}
+
+BOOST_AUTO_TEST_SUITE_END()