From: Remi Gacogne Date: Fri, 11 Jul 2025 08:05:04 +0000 (+0200) Subject: dns: Add unit tests for the RCode/ERCode/Opcode helpers X-Git-Tag: rec-5.4.0-alpha0~36^2~4 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d147b9dbd5df717c4dd299d05037b6e7a78b1061;p=thirdparty%2Fpdns.git dns: Add unit tests for the RCode/ERCode/Opcode helpers --- diff --git a/meson.build b/meson.build index 3d8590c13d..4b6aa8b850 100644 --- a/meson.build +++ b/meson.build @@ -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', diff --git a/pdns/Makefile.am b/pdns/Makefile.am index 7c1945db2b..8dd8e5c3f7 100644 --- a/pdns/Makefile.am +++ b/pdns/Makefile.am @@ -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 \ diff --git a/pdns/dnsdistdist/Makefile.am b/pdns/dnsdistdist/Makefile.am index 91c7ba40f4..a74d51394e 100644 --- a/pdns/dnsdistdist/Makefile.am +++ b/pdns/dnsdistdist/Makefile.am @@ -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 \ diff --git a/pdns/dnsdistdist/meson.build b/pdns/dnsdistdist/meson.build index 92ba36f633..30f0d76436 100644 --- a/pdns/dnsdistdist/meson.build +++ b/pdns/dnsdistdist/meson.build @@ -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 index 0000000000..e14fc8afac --- /dev/null +++ b/pdns/dnsdistdist/test-dns_cc.cc @@ -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 index 0000000000..45eccfa40f --- /dev/null +++ b/pdns/test-dns_cc.cc @@ -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 + +#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()