Debian Codegraph @

bin/tests/system/ixfr_nonminimal/ans2/ans.py

raw
1 """
2 Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
4 SPDX-License-Identifier: MPL-2.0
5
6 This Source Code Form is subject to the terms of the Mozilla Public
7 License, v. 2.0. If a copy of the MPL was not distributed with this
8 file, you can obtain one at https://mozilla.org/MPL/2.0/.
9
10 See the COPYRIGHT file distributed with this work for additional
11 information regarding copyright ownership.
12 """
13
14 from collections.abc import AsyncGenerator, Collection, Iterable
15
16 import abc
17
18 import dns.rcode
19 import dns.rdataclass
20 import dns.rdatatype
21 import dns.rrset
22
23 from isctest.asyncserver import (
24 ControllableAsyncDnsServer,
25 DnsResponseSend,
26 QueryContext,
27 ResponseHandler,
28 SwitchControlCommand,
29 )
30
31
32 def rrset(owner: str, rdtype: dns.rdatatype.RdataType, rdata: str) -> dns.rrset.RRset:
33 return dns.rrset.from_text(
34 owner,
35 300,
36 dns.rdataclass.IN,
37 rdtype,
38 rdata,
39 )
40
41
42 def soa(serial: int, *, owner: str = "nil.") -> dns.rrset.RRset:
43 return rrset(
44 owner,
45 dns.rdatatype.SOA,
46 f"ns.nil. root.nil. {serial} 300 300 604800 300",
47 )
48
49
50 def ns() -> dns.rrset.RRset:
51 return rrset(
52 "nil.",
53 dns.rdatatype.NS,
54 "ns.nil.",
55 )
56
57
58 def a(address: str, *, owner: str) -> dns.rrset.RRset:
59 return rrset(
60 owner,
61 dns.rdatatype.A,
62 address,
63 )
64
65
66 def txt(data: str, *, owner: str = "nil.") -> dns.rrset.RRset:
67 return rrset(
68 owner,
69 dns.rdatatype.TXT,
70 f'"{data}"',
71 )
72
73
74 class SoaHandler(ResponseHandler):
75 def __init__(self, serial: int):
76 self._serial = serial
77
78 def match(self, qctx: QueryContext) -> bool:
79 return qctx.qtype == dns.rdatatype.SOA
80
81 async def get_responses(
82 self, qctx: QueryContext
83 ) -> AsyncGenerator[DnsResponseSend, None]:
84 qctx.response.answer.append(soa(self._serial))
85 yield DnsResponseSend(qctx.response)
86
87
88 class AxfrHandler(ResponseHandler):
89 @property
90 @abc.abstractmethod
91 def answers(self) -> Iterable[Collection[dns.rrset.RRset]]:
92 """
93 Answer sections of response packets sent in response to
94 AXFR queries.
95 """
96 raise NotImplementedError
97
98 def match(self, qctx: QueryContext) -> bool:
99 return qctx.qtype == dns.rdatatype.AXFR
100
101 async def get_responses(
102 self, qctx: QueryContext
103 ) -> AsyncGenerator[DnsResponseSend, None]:
104 for answer in self.answers:
105 response = qctx.prepare_new_response()
106 for rrset_ in answer:
107 response.answer.append(rrset_)
108 yield DnsResponseSend(response)
109
110
111 class IxfrHandler(ResponseHandler):
112 @property
113 @abc.abstractmethod
114 def answer(self) -> Collection[dns.rrset.RRset]:
115 """
116 Answer section of a response packet sent in response to
117 IXFR queries.
118 """
119 raise NotImplementedError
120
121 def match(self, qctx: QueryContext) -> bool:
122 return qctx.qtype == dns.rdatatype.IXFR
123
124 async def get_responses(
125 self, qctx: QueryContext
126 ) -> AsyncGenerator[DnsResponseSend, None]:
127 for rrset_ in self.answer:
128 qctx.response.answer.append(rrset_)
129 yield DnsResponseSend(qctx.response)
130
131
132 class InitialAxfrHandler(AxfrHandler):
133 answers = (
134 (soa(1),),
135 (
136 ns(),
137 txt("initial AXFR"),
138 a("10.0.0.61", owner="a.nil."),
139 a("10.0.0.62", owner="b.nil."),
140 ),
141 (soa(1),),
142 )
143
144
145 class InitialIxfrHandler(IxfrHandler):
146 answer = (
147 soa(1),
148 ns(),
149 txt("initial AXFR"),
150 a("10.0.0.61", owner="a.nil."),
151 a("10.0.0.62", owner="b.nil."),
152 soa(1),
153 )
154
155
156 class UnchangedIxfrHandler(IxfrHandler):
157 """
158 IXFR from serial 1 -> 2.
159
160 The diff deletes nothing for the A rrset at a.nil., but re-adds
161 "a.nil. A 10.0.0.61" which already exists. This causes the merge
162 to find no new records, triggering DNS_R_UNCHANGED.
163
164 We also add a new TXT record so the IXFR has at least one real
165 change (the SOA serial bump + TXT addition).
166 """
167
168 answer = (
169 soa(2),
170 soa(1),
171 soa(2),
172 txt("unchanged ixfr test"),
173 a("10.0.0.61", owner="a.nil."), # already exists -> DNS_R_UNCHANGED
174 soa(2),
175 )
176
177
178 def main() -> None:
179 server = ControllableAsyncDnsServer(
180 default_aa=True, default_rcode=dns.rcode.NOERROR
181 )
182 switch_command = SwitchControlCommand(
183 {
184 "initial_axfr": (
185 SoaHandler(1),
186 InitialIxfrHandler(),
187 InitialAxfrHandler(),
188 ),
189 "unchanged_ixfr": (
190 SoaHandler(2),
191 UnchangedIxfrHandler(),
192 ),
193 }
194 )
195 server.install_control_command(switch_command)
196 server.run()
197
198
199 if __name__ == "__main__":
200 main()