2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-30 14:07:59 +00:00

chg: test: Use isctest.asyncserver in the "zero" test

The original `ans.pl` server was 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).

Merge branch 'stepan/zero-asyncserver' into 'main'

See merge request isc-projects/bind9!10597
This commit is contained in:
Štěpán Balážik
2025-07-23 11:23:38 +00:00
2 changed files with 62 additions and 81 deletions

View File

@@ -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";
}

View File

@@ -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()