From ec5729bee3d1aca84ed6136ffdf3d842feeee6c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0t=C4=9Bp=C3=A1n=20Bal=C3=A1=C5=BEik?= Date: Wed, 18 Jun 2025 12:13:37 +0200 Subject: [PATCH] Use isctest.asyncserver in the "zero" test The original `ans.pl` server was based on a copy of the one in `fetchlimit`, so there are some changes: - The server now only responds with A replies (which is the only thing needed). - The incrementing of the IP address goes beyond the least significant octet (so, after 192.0.2.255 it will yield 192.0.3.0). --- bin/tests/system/zero/ans5/ans.pl | 81 ------------------------------- bin/tests/system/zero/ans5/ans.py | 62 +++++++++++++++++++++++ 2 files changed, 62 insertions(+), 81 deletions(-) delete mode 100644 bin/tests/system/zero/ans5/ans.pl create mode 100644 bin/tests/system/zero/ans5/ans.py diff --git a/bin/tests/system/zero/ans5/ans.pl b/bin/tests/system/zero/ans5/ans.pl deleted file mode 100644 index 3ca1083ad5..0000000000 --- a/bin/tests/system/zero/ans5/ans.pl +++ /dev/null @@ -1,81 +0,0 @@ -#!/usr/bin/perl -w - -# Copyright (C) Internet Systems Consortium, Inc. ("ISC") -# -# SPDX-License-Identifier: MPL-2.0 -# -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, you can obtain one at https://mozilla.org/MPL/2.0/. -# -# See the COPYRIGHT file distributed with this work for additional -# information regarding copyright ownership. - -# -# Don't respond if the "norespond" file exists; otherwise respond to -# any A or AAAA query. -# - -use IO::File; -use IO::Socket; -use Net::DNS; -use Net::DNS::Packet; - -my $localport = int($ENV{'PORT'}); -if (!$localport) { $localport = 5300; } - -my $sock = IO::Socket::INET->new(LocalAddr => "10.53.0.5", - LocalPort => $localport, Proto => "udp") or die "$!"; - -my $pidf = new IO::File "ans.pid", "w" or die "cannot open pid file: $!"; -print $pidf "$$\n" or die "cannot write pid file: $!"; -$pidf->close or die "cannot close pid file: $!"; -sub rmpid { unlink "ans.pid"; exit 1; }; - -$SIG{INT} = \&rmpid; -$SIG{TERM} = \&rmpid; - -my $octet = 0; - -for (;;) { - $sock->recv($buf, 512); - - print "**** request from " , $sock->peerhost, " port ", $sock->peerport, "\n"; - - my $packet; - - if ($Net::DNS::VERSION > 0.68) { - $packet = new Net::DNS::Packet(\$buf, 0); - $@ and die $@; - } else { - my $err; - ($packet, $err) = new Net::DNS::Packet(\$buf, 0); - $err and die $err; - } - - print "REQUEST:\n"; - $packet->print; - - $packet->header->qr(1); - - my @questions = $packet->question; - my $qname = $questions[0]->qname; - my $qtype = $questions[0]->qtype; - - $packet->header->aa(1); - if ($qtype eq "A") { - $packet->push("answer", - new Net::DNS::RR($qname . - " 0 A 192.0.2." . $octet)); - $octet = $octet + 1; - } elsif ($qtype eq "AAAA") { - $packet->push("answer", - new Net::DNS::RR($qname . - " 300 AAAA 2001:db8:beef::1")); - } - - $sock->send($packet->data); - print "RESPONSE:\n"; - $packet->print; - print "\n"; -} diff --git a/bin/tests/system/zero/ans5/ans.py b/bin/tests/system/zero/ans5/ans.py new file mode 100644 index 0000000000..47b4da0ad0 --- /dev/null +++ b/bin/tests/system/zero/ans5/ans.py @@ -0,0 +1,62 @@ +""" +Copyright (C) Internet Systems Consortium, Inc. ("ISC") + +SPDX-License-Identifier: MPL-2.0 + +This Source Code Form is subject to the terms of the Mozilla Public +License, v. 2.0. If a copy of the MPL was not distributed with this +file, you can obtain one at https://mozilla.org/MPL/2.0/. + +See the COPYRIGHT file distributed with this work for additional +information regarding copyright ownership. +""" + +import ipaddress +from typing import AsyncGenerator + +import dns.flags +import dns.message +import dns.rdata +import dns.rdataclass +import dns.rdatatype +import dns.rrset + +from isctest.asyncserver import ( + AsyncDnsServer, + DnsResponseSend, + QueryContext, + ResponseHandler, +) + + +class IncrementARecordHandler(ResponseHandler): + """ + To test the TTL=0 behavior, increment the IPv4 address by one every + time we get queried. + """ + + def __init__(self): + self._ip_address = ipaddress.ip_address("192.0.2.0") + + async def get_responses( + self, qctx: QueryContext + ) -> AsyncGenerator[DnsResponseSend, None]: + if qctx.qtype == dns.rdatatype.A: + rrset = dns.rrset.from_text( + qctx.qname, 0, qctx.qclass, dns.rdatatype.A, str(self._ip_address) + ) + qctx.response.answer.append(rrset) + self._ip_address += 1 + + qctx.response.set_rcode(dns.rcode.NOERROR) + yield DnsResponseSend(qctx.response, authoritative=True) + + +def main() -> None: + server = AsyncDnsServer() + server.install_response_handler(IncrementARecordHandler()) + server.run() + + +if __name__ == "__main__": + main()