| 1 |
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license |
| 2 |
|
| 3 |
import asyncio |
| 4 |
import sys |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
import dns.asyncbackend |
| 9 |
import dns.asyncquery |
| 10 |
import dns.message |
| 11 |
import dns.query |
| 12 |
import dns.tsigkeyring |
| 13 |
import dns.versioned |
| 14 |
import dns.xfr |
| 15 |
|
| 16 |
# Some tests use a "nano nameserver" for testing. It requires trio |
| 17 |
# and threading, so try to import it and if it doesn't work, skip |
| 18 |
# those tests. |
| 19 |
try: |
| 20 |
from .nanonameserver import Server |
| 21 |
|
| 22 |
_nanonameserver_available = True |
| 23 |
except ImportError: |
| 24 |
_nanonameserver_available = False |
| 25 |
|
| 26 |
class Server(object): |
| 27 |
pass |
| 28 |
|
| 29 |
|
| 30 |
axfr = """id 1 |
| 31 |
opcode QUERY |
| 32 |
rcode NOERROR |
| 33 |
flags AA |
| 34 |
;QUESTION |
| 35 |
example. IN AXFR |
| 36 |
;ANSWER |
| 37 |
@ 3600 IN SOA foo bar 1 2 3 4 5 |
| 38 |
@ 3600 IN NS ns1 |
| 39 |
@ 3600 IN NS ns2 |
| 40 |
bar.foo 300 IN MX 0 blaz.foo |
| 41 |
ns1 3600 IN A 10.0.0.1 |
| 42 |
ns2 3600 IN A 10.0.0.2 |
| 43 |
@ 3600 IN SOA foo bar 1 2 3 4 5 |
| 44 |
""" |
| 45 |
|
| 46 |
axfr1 = """id 1 |
| 47 |
opcode QUERY |
| 48 |
rcode NOERROR |
| 49 |
flags AA |
| 50 |
;QUESTION |
| 51 |
example. IN AXFR |
| 52 |
;ANSWER |
| 53 |
@ 3600 IN SOA foo bar 1 2 3 4 5 |
| 54 |
@ 3600 IN NS ns1 |
| 55 |
@ 3600 IN NS ns2 |
| 56 |
""" |
| 57 |
axfr2 = """id 1 |
| 58 |
opcode QUERY |
| 59 |
rcode NOERROR |
| 60 |
flags AA |
| 61 |
;ANSWER |
| 62 |
bar.foo 300 IN MX 0 blaz.foo |
| 63 |
ns1 3600 IN A 10.0.0.1 |
| 64 |
ns2 3600 IN A 10.0.0.2 |
| 65 |
@ 3600 IN SOA foo bar 1 2 3 4 5 |
| 66 |
""" |
| 67 |
|
| 68 |
base = """@ 3600 IN SOA foo bar 1 2 3 4 5 |
| 69 |
@ 3600 IN NS ns1 |
| 70 |
@ 3600 IN NS ns2 |
| 71 |
bar.foo 300 IN MX 0 blaz.foo |
| 72 |
ns1 3600 IN A 10.0.0.1 |
| 73 |
ns2 3600 IN A 10.0.0.2 |
| 74 |
""" |
| 75 |
|
| 76 |
axfr_unexpected_origin = """id 1 |
| 77 |
opcode QUERY |
| 78 |
rcode NOERROR |
| 79 |
flags AA |
| 80 |
;QUESTION |
| 81 |
example. IN AXFR |
| 82 |
;ANSWER |
| 83 |
@ 3600 IN SOA foo bar 1 2 3 4 5 |
| 84 |
@ 3600 IN SOA foo bar 1 2 3 4 7 |
| 85 |
""" |
| 86 |
|
| 87 |
ixfr = """id 1 |
| 88 |
opcode QUERY |
| 89 |
rcode NOERROR |
| 90 |
flags AA |
| 91 |
;QUESTION |
| 92 |
example. IN IXFR |
| 93 |
;ANSWER |
| 94 |
@ 3600 IN SOA foo bar 4 2 3 4 5 |
| 95 |
@ 3600 IN SOA foo bar 1 2 3 4 5 |
| 96 |
bar.foo 300 IN MX 0 blaz.foo |
| 97 |
ns2 3600 IN A 10.0.0.2 |
| 98 |
@ 3600 IN SOA foo bar 2 2 3 4 5 |
| 99 |
ns2 3600 IN A 10.0.0.4 |
| 100 |
@ 3600 IN SOA foo bar 2 2 3 4 5 |
| 101 |
@ 3600 IN SOA foo bar 3 2 3 4 5 |
| 102 |
ns3 3600 IN A 10.0.0.3 |
| 103 |
@ 3600 IN SOA foo bar 3 2 3 4 5 |
| 104 |
@ 3600 IN NS ns2 |
| 105 |
@ 3600 IN SOA foo bar 4 2 3 4 5 |
| 106 |
@ 3600 IN SOA foo bar 4 2 3 4 5 |
| 107 |
""" |
| 108 |
|
| 109 |
compressed_ixfr = """id 1 |
| 110 |
opcode QUERY |
| 111 |
rcode NOERROR |
| 112 |
flags AA |
| 113 |
;QUESTION |
| 114 |
example. IN IXFR |
| 115 |
;ANSWER |
| 116 |
@ 3600 IN SOA foo bar 4 2 3 4 5 |
| 117 |
@ 3600 IN SOA foo bar 1 2 3 4 5 |
| 118 |
bar.foo 300 IN MX 0 blaz.foo |
| 119 |
ns2 3600 IN A 10.0.0.2 |
| 120 |
@ 3600 IN NS ns2 |
| 121 |
@ 3600 IN SOA foo bar 4 2 3 4 5 |
| 122 |
ns2 3600 IN A 10.0.0.4 |
| 123 |
ns3 3600 IN A 10.0.0.3 |
| 124 |
@ 3600 IN SOA foo bar 4 2 3 4 5 |
| 125 |
""" |
| 126 |
|
| 127 |
ixfr_expected = """@ 3600 IN SOA foo bar 4 2 3 4 5 |
| 128 |
@ 3600 IN NS ns1 |
| 129 |
ns1 3600 IN A 10.0.0.1 |
| 130 |
ns2 3600 IN A 10.0.0.4 |
| 131 |
ns3 3600 IN A 10.0.0.3 |
| 132 |
""" |
| 133 |
|
| 134 |
ixfr_first_message = """id 1 |
| 135 |
opcode QUERY |
| 136 |
rcode NOERROR |
| 137 |
flags AA |
| 138 |
;QUESTION |
| 139 |
example. IN IXFR |
| 140 |
;ANSWER |
| 141 |
@ 3600 IN SOA foo bar 4 2 3 4 5 |
| 142 |
""" |
| 143 |
|
| 144 |
ixfr_header = """id 1 |
| 145 |
opcode QUERY |
| 146 |
rcode NOERROR |
| 147 |
flags AA |
| 148 |
;ANSWER |
| 149 |
""" |
| 150 |
|
| 151 |
ixfr_body = [ |
| 152 |
"@ 3600 IN SOA foo bar 1 2 3 4 5", |
| 153 |
"bar.foo 300 IN MX 0 blaz.foo", |
| 154 |
"ns2 3600 IN A 10.0.0.2", |
| 155 |
"@ 3600 IN SOA foo bar 2 2 3 4 5", |
| 156 |
"ns2 3600 IN A 10.0.0.4", |
| 157 |
"@ 3600 IN SOA foo bar 2 2 3 4 5", |
| 158 |
"@ 3600 IN SOA foo bar 3 2 3 4 5", |
| 159 |
"ns3 3600 IN A 10.0.0.3", |
| 160 |
"@ 3600 IN SOA foo bar 3 2 3 4 5", |
| 161 |
"@ 3600 IN NS ns2", |
| 162 |
"@ 3600 IN SOA foo bar 4 2 3 4 5", |
| 163 |
"@ 3600 IN SOA foo bar 4 2 3 4 5", |
| 164 |
] |
| 165 |
|
| 166 |
ixfrs = [ixfr_first_message] |
| 167 |
ixfrs.extend([ixfr_header + l for l in ixfr_body]) |
| 168 |
|
| 169 |
good_empty_ixfr = """id 1 |
| 170 |
opcode QUERY |
| 171 |
rcode NOERROR |
| 172 |
flags AA |
| 173 |
;QUESTION |
| 174 |
example. IN IXFR |
| 175 |
;ANSWER |
| 176 |
@ 3600 IN SOA foo bar 1 2 3 4 5 |
| 177 |
""" |
| 178 |
|
| 179 |
retry_tcp_ixfr = """id 1 |
| 180 |
opcode QUERY |
| 181 |
rcode NOERROR |
| 182 |
flags AA |
| 183 |
;QUESTION |
| 184 |
example. IN IXFR |
| 185 |
;ANSWER |
| 186 |
@ 3600 IN SOA foo bar 5 2 3 4 5 |
| 187 |
""" |
| 188 |
|
| 189 |
bad_empty_ixfr = """id 1 |
| 190 |
opcode QUERY |
| 191 |
rcode NOERROR |
| 192 |
flags AA |
| 193 |
;QUESTION |
| 194 |
example. IN IXFR |
| 195 |
;ANSWER |
| 196 |
@ 3600 IN SOA foo bar 4 2 3 4 5 |
| 197 |
@ 3600 IN SOA foo bar 4 2 3 4 5 |
| 198 |
""" |
| 199 |
|
| 200 |
unexpected_end_ixfr = """id 1 |
| 201 |
opcode QUERY |
| 202 |
rcode NOERROR |
| 203 |
flags AA |
| 204 |
;QUESTION |
| 205 |
example. IN IXFR |
| 206 |
;ANSWER |
| 207 |
@ 3600 IN SOA foo bar 4 2 3 4 5 |
| 208 |
@ 3600 IN SOA foo bar 1 2 3 4 5 |
| 209 |
bar.foo 300 IN MX 0 blaz.foo |
| 210 |
ns2 3600 IN A 10.0.0.2 |
| 211 |
@ 3600 IN NS ns2 |
| 212 |
@ 3600 IN SOA foo bar 3 2 3 4 5 |
| 213 |
ns2 3600 IN A 10.0.0.4 |
| 214 |
ns3 3600 IN A 10.0.0.3 |
| 215 |
@ 3600 IN SOA foo bar 4 2 3 4 5 |
| 216 |
""" |
| 217 |
|
| 218 |
unexpected_end_ixfr_2 = """id 1 |
| 219 |
opcode QUERY |
| 220 |
rcode NOERROR |
| 221 |
flags AA |
| 222 |
;QUESTION |
| 223 |
example. IN IXFR |
| 224 |
;ANSWER |
| 225 |
@ 3600 IN SOA foo bar 4 2 3 4 5 |
| 226 |
@ 3600 IN SOA foo bar 1 2 3 4 5 |
| 227 |
bar.foo 300 IN MX 0 blaz.foo |
| 228 |
ns2 3600 IN A 10.0.0.2 |
| 229 |
@ 3600 IN NS ns2 |
| 230 |
""" |
| 231 |
|
| 232 |
bad_serial_ixfr = """id 1 |
| 233 |
opcode QUERY |
| 234 |
rcode NOERROR |
| 235 |
flags AA |
| 236 |
;QUESTION |
| 237 |
example. IN IXFR |
| 238 |
;ANSWER |
| 239 |
@ 3600 IN SOA foo bar 4 2 3 4 5 |
| 240 |
@ 3600 IN SOA foo bar 2 2 3 4 5 |
| 241 |
bar.foo 300 IN MX 0 blaz.foo |
| 242 |
ns2 3600 IN A 10.0.0.2 |
| 243 |
@ 3600 IN NS ns2 |
| 244 |
@ 3600 IN SOA foo bar 4 2 3 4 5 |
| 245 |
ns2 3600 IN A 10.0.0.4 |
| 246 |
ns3 3600 IN A 10.0.0.3 |
| 247 |
@ 3600 IN SOA foo bar 4 2 3 4 5 |
| 248 |
""" |
| 249 |
|
| 250 |
ixfr_axfr = """id 1 |
| 251 |
opcode QUERY |
| 252 |
rcode NOERROR |
| 253 |
flags AA |
| 254 |
;QUESTION |
| 255 |
example. IN IXFR |
| 256 |
;ANSWER |
| 257 |
@ 3600 IN SOA foo bar 1 2 3 4 5 |
| 258 |
@ 3600 IN NS ns1 |
| 259 |
@ 3600 IN NS ns2 |
| 260 |
bar.foo 300 IN MX 0 blaz.foo |
| 261 |
ns1 3600 IN A 10.0.0.1 |
| 262 |
ns2 3600 IN A 10.0.0.2 |
| 263 |
@ 3600 IN SOA foo bar 1 2 3 4 5 |
| 264 |
""" |
| 265 |
|
| 266 |
ixfr_axfr1 = """id 1 |
| 267 |
opcode QUERY |
| 268 |
rcode NOERROR |
| 269 |
flags AA |
| 270 |
;QUESTION |
| 271 |
example. IN IXFR |
| 272 |
;ANSWER |
| 273 |
@ 3600 IN SOA foo bar 1 2 3 4 5 |
| 274 |
@ 3600 IN NS ns1 |
| 275 |
@ 3600 IN NS ns2 |
| 276 |
""" |
| 277 |
ixfr_axfr2 = """id 1 |
| 278 |
opcode QUERY |
| 279 |
rcode NOERROR |
| 280 |
flags AA |
| 281 |
;QUESTION |
| 282 |
example. IN IXFR |
| 283 |
;ANSWER |
| 284 |
bar.foo 300 IN MX 0 blaz.foo |
| 285 |
ns1 3600 IN A 10.0.0.1 |
| 286 |
ns2 3600 IN A 10.0.0.2 |
| 287 |
@ 3600 IN SOA foo bar 1 2 3 4 5 |
| 288 |
""" |
| 289 |
|
| 290 |
|
| 291 |
def test_basic_axfr(): |
| 292 |
z = dns.versioned.Zone("example.") |
| 293 |
m = dns.message.from_text(axfr, origin=z.origin, one_rr_per_rrset=True) |
| 294 |
with dns.xfr.Inbound(z, dns.rdatatype.AXFR) as xfr: |
| 295 |
done = xfr.process_message(m) |
| 296 |
assert done |
| 297 |
ez = dns.zone.from_text(base, "example.") |
| 298 |
assert z == ez |
| 299 |
|
| 300 |
|
| 301 |
def test_basic_axfr_unversioned(): |
| 302 |
z = dns.zone.Zone("example.") |
| 303 |
m = dns.message.from_text(axfr, origin=z.origin, one_rr_per_rrset=True) |
| 304 |
with dns.xfr.Inbound(z, dns.rdatatype.AXFR) as xfr: |
| 305 |
done = xfr.process_message(m) |
| 306 |
assert done |
| 307 |
ez = dns.zone.from_text(base, "example.") |
| 308 |
assert z == ez |
| 309 |
|
| 310 |
|
| 311 |
def test_basic_axfr_two_parts(): |
| 312 |
z = dns.versioned.Zone("example.") |
| 313 |
m1 = dns.message.from_text(axfr1, origin=z.origin, one_rr_per_rrset=True) |
| 314 |
m2 = dns.message.from_text(axfr2, origin=z.origin, one_rr_per_rrset=True) |
| 315 |
with dns.xfr.Inbound(z, dns.rdatatype.AXFR) as xfr: |
| 316 |
done = xfr.process_message(m1) |
| 317 |
assert not done |
| 318 |
done = xfr.process_message(m2) |
| 319 |
assert done |
| 320 |
ez = dns.zone.from_text(base, "example.") |
| 321 |
assert z == ez |
| 322 |
|
| 323 |
|
| 324 |
def test_axfr_unexpected_origin(): |
| 325 |
z = dns.versioned.Zone("example.") |
| 326 |
m = dns.message.from_text( |
| 327 |
axfr_unexpected_origin, origin=z.origin, one_rr_per_rrset=True |
| 328 |
) |
| 329 |
with dns.xfr.Inbound(z, dns.rdatatype.AXFR) as xfr: |
| 330 |
with pytest.raises(dns.exception.FormError): |
| 331 |
xfr.process_message(m) |
| 332 |
|
| 333 |
|
| 334 |
def test_basic_ixfr(): |
| 335 |
z = dns.zone.from_text(base, "example.", zone_factory=dns.versioned.Zone) |
| 336 |
m = dns.message.from_text(ixfr, origin=z.origin, one_rr_per_rrset=True) |
| 337 |
with dns.xfr.Inbound(z, dns.rdatatype.IXFR, serial=1) as xfr: |
| 338 |
done = xfr.process_message(m) |
| 339 |
assert done |
| 340 |
ez = dns.zone.from_text(ixfr_expected, "example.") |
| 341 |
assert z == ez |
| 342 |
|
| 343 |
|
| 344 |
def test_basic_ixfr_unversioned(): |
| 345 |
z = dns.zone.from_text(base, "example.") |
| 346 |
m = dns.message.from_text(ixfr, origin=z.origin, one_rr_per_rrset=True) |
| 347 |
with dns.xfr.Inbound(z, dns.rdatatype.IXFR, serial=1) as xfr: |
| 348 |
done = xfr.process_message(m) |
| 349 |
assert done |
| 350 |
ez = dns.zone.from_text(ixfr_expected, "example.") |
| 351 |
assert z == ez |
| 352 |
|
| 353 |
|
| 354 |
def test_compressed_ixfr(): |
| 355 |
z = dns.zone.from_text(base, "example.", zone_factory=dns.versioned.Zone) |
| 356 |
m = dns.message.from_text(compressed_ixfr, origin=z.origin, one_rr_per_rrset=True) |
| 357 |
with dns.xfr.Inbound(z, dns.rdatatype.IXFR, serial=1) as xfr: |
| 358 |
done = xfr.process_message(m) |
| 359 |
assert done |
| 360 |
ez = dns.zone.from_text(ixfr_expected, "example.") |
| 361 |
assert z == ez |
| 362 |
|
| 363 |
|
| 364 |
def test_basic_ixfr_many_parts(): |
| 365 |
z = dns.zone.from_text(base, "example.", zone_factory=dns.versioned.Zone) |
| 366 |
with dns.xfr.Inbound(z, dns.rdatatype.IXFR, serial=1) as xfr: |
| 367 |
done = False |
| 368 |
for text in ixfrs: |
| 369 |
assert not done |
| 370 |
m = dns.message.from_text(text, origin=z.origin, one_rr_per_rrset=True) |
| 371 |
done = xfr.process_message(m) |
| 372 |
assert done |
| 373 |
ez = dns.zone.from_text(ixfr_expected, "example.") |
| 374 |
assert z == ez |
| 375 |
|
| 376 |
|
| 377 |
def test_good_empty_ixfr(): |
| 378 |
z = dns.zone.from_text(ixfr_expected, "example.", zone_factory=dns.versioned.Zone) |
| 379 |
m = dns.message.from_text(good_empty_ixfr, origin=z.origin, one_rr_per_rrset=True) |
| 380 |
with dns.xfr.Inbound(z, dns.rdatatype.IXFR, serial=1) as xfr: |
| 381 |
done = xfr.process_message(m) |
| 382 |
assert done |
| 383 |
ez = dns.zone.from_text(ixfr_expected, "example.") |
| 384 |
assert z == ez |
| 385 |
|
| 386 |
|
| 387 |
def test_retry_tcp_ixfr(): |
| 388 |
z = dns.zone.from_text(ixfr_expected, "example.", zone_factory=dns.versioned.Zone) |
| 389 |
m = dns.message.from_text(retry_tcp_ixfr, origin=z.origin, one_rr_per_rrset=True) |
| 390 |
with dns.xfr.Inbound(z, dns.rdatatype.IXFR, serial=1, is_udp=True) as xfr: |
| 391 |
with pytest.raises(dns.xfr.UseTCP): |
| 392 |
xfr.process_message(m) |
| 393 |
|
| 394 |
|
| 395 |
def test_bad_empty_ixfr(): |
| 396 |
z = dns.zone.from_text(ixfr_expected, "example.", zone_factory=dns.versioned.Zone) |
| 397 |
m = dns.message.from_text(bad_empty_ixfr, origin=z.origin, one_rr_per_rrset=True) |
| 398 |
with dns.xfr.Inbound(z, dns.rdatatype.IXFR, serial=3) as xfr: |
| 399 |
with pytest.raises(dns.exception.FormError): |
| 400 |
xfr.process_message(m) |
| 401 |
|
| 402 |
|
| 403 |
def test_serial_went_backwards_ixfr(): |
| 404 |
z = dns.zone.from_text(ixfr_expected, "example.", zone_factory=dns.versioned.Zone) |
| 405 |
m = dns.message.from_text(bad_empty_ixfr, origin=z.origin, one_rr_per_rrset=True) |
| 406 |
with dns.xfr.Inbound(z, dns.rdatatype.IXFR, serial=5) as xfr: |
| 407 |
with pytest.raises(dns.xfr.SerialWentBackwards): |
| 408 |
xfr.process_message(m) |
| 409 |
|
| 410 |
|
| 411 |
def test_ixfr_is_axfr(): |
| 412 |
z = dns.zone.from_text(base, "example.", zone_factory=dns.versioned.Zone) |
| 413 |
m = dns.message.from_text(ixfr_axfr, origin=z.origin, one_rr_per_rrset=True) |
| 414 |
with dns.xfr.Inbound(z, dns.rdatatype.IXFR, serial=0xFFFFFFFF) as xfr: |
| 415 |
done = xfr.process_message(m) |
| 416 |
assert done |
| 417 |
ez = dns.zone.from_text(base, "example.") |
| 418 |
assert z == ez |
| 419 |
|
| 420 |
|
| 421 |
def test_ixfr_is_axfr_two_parts(): |
| 422 |
z = dns.versioned.Zone("example.") |
| 423 |
m1 = dns.message.from_text(ixfr_axfr1, origin=z.origin, one_rr_per_rrset=True) |
| 424 |
m2 = dns.message.from_text(ixfr_axfr2, origin=z.origin, one_rr_per_rrset=True) |
| 425 |
with dns.xfr.Inbound(z, dns.rdatatype.IXFR, serial=0xFFFFFFFF) as xfr: |
| 426 |
done = xfr.process_message(m1) |
| 427 |
assert not done |
| 428 |
done = xfr.process_message(m2) |
| 429 |
assert done |
| 430 |
ez = dns.zone.from_text(base, "example.") |
| 431 |
assert z == ez |
| 432 |
|
| 433 |
|
| 434 |
def test_ixfr_requires_serial(): |
| 435 |
z = dns.zone.from_text(base, "example.", zone_factory=dns.versioned.Zone) |
| 436 |
with pytest.raises(ValueError): |
| 437 |
dns.xfr.Inbound(z, dns.rdatatype.IXFR) |
| 438 |
|
| 439 |
|
| 440 |
def test_ixfr_unexpected_end_bad_diff_sequence(): |
| 441 |
# This is where we get the end serial, but haven't seen all of |
| 442 |
# the expected diffs |
| 443 |
z = dns.zone.from_text(base, "example.", zone_factory=dns.versioned.Zone) |
| 444 |
m = dns.message.from_text( |
| 445 |
unexpected_end_ixfr, origin=z.origin, one_rr_per_rrset=True |
| 446 |
) |
| 447 |
with dns.xfr.Inbound(z, dns.rdatatype.IXFR, serial=1) as xfr: |
| 448 |
with pytest.raises(dns.exception.FormError): |
| 449 |
xfr.process_message(m) |
| 450 |
|
| 451 |
|
| 452 |
def test_udp_ixfr_unexpected_end_just_stops(): |
| 453 |
# This is where everything looks good, but the IXFR just stops |
| 454 |
# in the middle. |
| 455 |
z = dns.zone.from_text(base, "example.", zone_factory=dns.versioned.Zone) |
| 456 |
m = dns.message.from_text( |
| 457 |
unexpected_end_ixfr_2, origin=z.origin, one_rr_per_rrset=True |
| 458 |
) |
| 459 |
with dns.xfr.Inbound(z, dns.rdatatype.IXFR, serial=1, is_udp=True) as xfr: |
| 460 |
with pytest.raises(dns.exception.FormError): |
| 461 |
xfr.process_message(m) |
| 462 |
|
| 463 |
|
| 464 |
def test_ixfr_bad_serial(): |
| 465 |
z = dns.zone.from_text(base, "example.", zone_factory=dns.versioned.Zone) |
| 466 |
m = dns.message.from_text(bad_serial_ixfr, origin=z.origin, one_rr_per_rrset=True) |
| 467 |
with dns.xfr.Inbound(z, dns.rdatatype.IXFR, serial=1) as xfr: |
| 468 |
with pytest.raises(dns.exception.FormError): |
| 469 |
xfr.process_message(m) |
| 470 |
|
| 471 |
|
| 472 |
def test_no_udp_with_axfr(): |
| 473 |
z = dns.versioned.Zone("example.") |
| 474 |
with pytest.raises(ValueError): |
| 475 |
with dns.xfr.Inbound(z, dns.rdatatype.AXFR, is_udp=True) as xfr: |
| 476 |
pass |
| 477 |
|
| 478 |
|
| 479 |
refused = """id 1 |
| 480 |
opcode QUERY |
| 481 |
rcode REFUSED |
| 482 |
flags AA |
| 483 |
;QUESTION |
| 484 |
example. IN AXFR |
| 485 |
""" |
| 486 |
|
| 487 |
bad_qname = """id 1 |
| 488 |
opcode QUERY |
| 489 |
rcode NOERROR |
| 490 |
flags AA |
| 491 |
;QUESTION |
| 492 |
not-example. IN IXFR |
| 493 |
""" |
| 494 |
|
| 495 |
bad_qtype = """id 1 |
| 496 |
opcode QUERY |
| 497 |
rcode NOERROR |
| 498 |
flags AA |
| 499 |
;QUESTION |
| 500 |
example. IN AXFR |
| 501 |
""" |
| 502 |
|
| 503 |
soa_not_first = """id 1 |
| 504 |
opcode QUERY |
| 505 |
rcode NOERROR |
| 506 |
flags AA |
| 507 |
;QUESTION |
| 508 |
example. IN IXFR |
| 509 |
;ANSWER |
| 510 |
bar.foo 300 IN MX 0 blaz.foo |
| 511 |
""" |
| 512 |
|
| 513 |
soa_not_first_2 = """id 1 |
| 514 |
opcode QUERY |
| 515 |
rcode NOERROR |
| 516 |
flags AA |
| 517 |
;QUESTION |
| 518 |
example. IN IXFR |
| 519 |
;ANSWER |
| 520 |
@ 300 IN MX 0 blaz.foo |
| 521 |
""" |
| 522 |
|
| 523 |
no_answer = """id 1 |
| 524 |
opcode QUERY |
| 525 |
rcode NOERROR |
| 526 |
flags AA |
| 527 |
;QUESTION |
| 528 |
example. IN IXFR |
| 529 |
;ADDITIONAL |
| 530 |
bar.foo 300 IN MX 0 blaz.foo |
| 531 |
""" |
| 532 |
|
| 533 |
axfr_answers_after_final_soa = """id 1 |
| 534 |
opcode QUERY |
| 535 |
rcode NOERROR |
| 536 |
flags AA |
| 537 |
;QUESTION |
| 538 |
example. IN AXFR |
| 539 |
;ANSWER |
| 540 |
@ 3600 IN SOA foo bar 1 2 3 4 5 |
| 541 |
@ 3600 IN NS ns1 |
| 542 |
@ 3600 IN NS ns2 |
| 543 |
bar.foo 300 IN MX 0 blaz.foo |
| 544 |
ns1 3600 IN A 10.0.0.1 |
| 545 |
ns2 3600 IN A 10.0.0.2 |
| 546 |
@ 3600 IN SOA foo bar 1 2 3 4 5 |
| 547 |
ns3 3600 IN A 10.0.0.3 |
| 548 |
""" |
| 549 |
|
| 550 |
|
| 551 |
def test_refused(): |
| 552 |
z = dns.zone.from_text(base, "example.", zone_factory=dns.versioned.Zone) |
| 553 |
m = dns.message.from_text(refused, origin=z.origin, one_rr_per_rrset=True) |
| 554 |
with dns.xfr.Inbound(z, dns.rdatatype.IXFR, serial=1) as xfr: |
| 555 |
with pytest.raises(dns.xfr.TransferError): |
| 556 |
xfr.process_message(m) |
| 557 |
|
| 558 |
|
| 559 |
def test_bad_qname(): |
| 560 |
z = dns.zone.from_text(base, "example.", zone_factory=dns.versioned.Zone) |
| 561 |
m = dns.message.from_text(bad_qname, origin=z.origin, one_rr_per_rrset=True) |
| 562 |
with dns.xfr.Inbound(z, dns.rdatatype.IXFR, serial=1) as xfr: |
| 563 |
with pytest.raises(dns.exception.FormError): |
| 564 |
xfr.process_message(m) |
| 565 |
|
| 566 |
|
| 567 |
def test_bad_qtype(): |
| 568 |
z = dns.zone.from_text(base, "example.", zone_factory=dns.versioned.Zone) |
| 569 |
m = dns.message.from_text(bad_qtype, origin=z.origin, one_rr_per_rrset=True) |
| 570 |
with dns.xfr.Inbound(z, dns.rdatatype.IXFR, serial=1) as xfr: |
| 571 |
with pytest.raises(dns.exception.FormError): |
| 572 |
xfr.process_message(m) |
| 573 |
|
| 574 |
|
| 575 |
def test_soa_not_first(): |
| 576 |
z = dns.zone.from_text(base, "example.", zone_factory=dns.versioned.Zone) |
| 577 |
m = dns.message.from_text(soa_not_first, origin=z.origin, one_rr_per_rrset=True) |
| 578 |
with dns.xfr.Inbound(z, dns.rdatatype.IXFR, serial=1) as xfr: |
| 579 |
with pytest.raises(dns.exception.FormError): |
| 580 |
xfr.process_message(m) |
| 581 |
m = dns.message.from_text(soa_not_first_2, origin=z.origin, one_rr_per_rrset=True) |
| 582 |
with dns.xfr.Inbound(z, dns.rdatatype.IXFR, serial=1) as xfr: |
| 583 |
with pytest.raises(dns.exception.FormError): |
| 584 |
xfr.process_message(m) |
| 585 |
|
| 586 |
|
| 587 |
def test_no_answer(): |
| 588 |
z = dns.zone.from_text(base, "example.", zone_factory=dns.versioned.Zone) |
| 589 |
m = dns.message.from_text(no_answer, origin=z.origin, one_rr_per_rrset=True) |
| 590 |
with dns.xfr.Inbound(z, dns.rdatatype.IXFR, serial=1) as xfr: |
| 591 |
with pytest.raises(dns.exception.FormError): |
| 592 |
xfr.process_message(m) |
| 593 |
|
| 594 |
|
| 595 |
def test_axfr_answers_after_final_soa(): |
| 596 |
z = dns.versioned.Zone("example.") |
| 597 |
m = dns.message.from_text( |
| 598 |
axfr_answers_after_final_soa, origin=z.origin, one_rr_per_rrset=True |
| 599 |
) |
| 600 |
with dns.xfr.Inbound(z, dns.rdatatype.AXFR) as xfr: |
| 601 |
with pytest.raises(dns.exception.FormError): |
| 602 |
xfr.process_message(m) |
| 603 |
|
| 604 |
|
| 605 |
keyring = dns.tsigkeyring.from_text({"keyname.": "NjHwPsMKjdN++dOfE5iAiQ=="}) |
| 606 |
|
| 607 |
keyname = dns.name.from_text("keyname") |
| 608 |
|
| 609 |
|
| 610 |
def test_make_query_basic(): |
| 611 |
z = dns.versioned.Zone("example.") |
| 612 |
(q, s) = dns.xfr.make_query(z) |
| 613 |
assert q.question[0].rdtype == dns.rdatatype.AXFR |
| 614 |
assert s is None |
| 615 |
(q, s) = dns.xfr.make_query(z, serial=None) |
| 616 |
assert q.question[0].rdtype == dns.rdatatype.AXFR |
| 617 |
assert s is None |
| 618 |
(q, s) = dns.xfr.make_query(z, serial=10) |
| 619 |
assert q.question[0].rdtype == dns.rdatatype.IXFR |
| 620 |
assert q.authority[0].rdtype == dns.rdatatype.SOA |
| 621 |
assert q.authority[0][0].serial == 10 |
| 622 |
assert s == 10 |
| 623 |
with z.writer() as txn: |
| 624 |
txn.add("@", 300, dns.rdata.from_text("in", "soa", ". . 1 2 3 4 5")) |
| 625 |
(q, s) = dns.xfr.make_query(z) |
| 626 |
assert q.question[0].rdtype == dns.rdatatype.IXFR |
| 627 |
assert q.authority[0].rdtype == dns.rdatatype.SOA |
| 628 |
assert q.authority[0][0].serial == 1 |
| 629 |
assert s == 1 |
| 630 |
(q, s) = dns.xfr.make_query(z, keyring=keyring, keyname=keyname) |
| 631 |
assert q.question[0].rdtype == dns.rdatatype.IXFR |
| 632 |
assert q.authority[0].rdtype == dns.rdatatype.SOA |
| 633 |
assert q.authority[0][0].serial == 1 |
| 634 |
assert s == 1 |
| 635 |
assert q.keyname == keyname |
| 636 |
|
| 637 |
|
| 638 |
def test_make_query_bad_serial(): |
| 639 |
z = dns.versioned.Zone("example.") |
| 640 |
with pytest.raises(ValueError): |
| 641 |
dns.xfr.make_query(z, serial="hi") |
| 642 |
with pytest.raises(ValueError): |
| 643 |
dns.xfr.make_query(z, serial=-1) |
| 644 |
with pytest.raises(ValueError): |
| 645 |
dns.xfr.make_query(z, serial=4294967296) |
| 646 |
|
| 647 |
|
| 648 |
def test_extract_serial_from_query(): |
| 649 |
z = dns.versioned.Zone("example.") |
| 650 |
(q, s) = dns.xfr.make_query(z) |
| 651 |
xs = dns.xfr.extract_serial_from_query(q) |
| 652 |
assert s is None |
| 653 |
assert s == xs |
| 654 |
(q, s) = dns.xfr.make_query(z, serial=10) |
| 655 |
xs = dns.xfr.extract_serial_from_query(q) |
| 656 |
assert s == 10 |
| 657 |
assert s == xs |
| 658 |
q = dns.message.make_query("example", "a") |
| 659 |
with pytest.raises(ValueError): |
| 660 |
dns.xfr.extract_serial_from_query(q) |
| 661 |
|
| 662 |
|
| 663 |
class XFRNanoNameserver(Server): |
| 664 |
def __init__(self): |
| 665 |
super().__init__(origin=dns.name.from_text("example")) |
| 666 |
|
| 667 |
def handle(self, request): |
| 668 |
try: |
| 669 |
if request.message.question[0].rdtype == dns.rdatatype.IXFR: |
| 670 |
text = ixfr |
| 671 |
else: |
| 672 |
text = axfr |
| 673 |
r = dns.message.from_text(text, one_rr_per_rrset=True, origin=self.origin) |
| 674 |
r.id = request.message.id |
| 675 |
return r |
| 676 |
except Exception: |
| 677 |
pass |
| 678 |
|
| 679 |
|
| 680 |
@pytest.mark.skipif(not _nanonameserver_available, reason="requires nanonameserver") |
| 681 |
def test_sync_inbound_xfr(): |
| 682 |
with XFRNanoNameserver() as ns: |
| 683 |
zone = dns.versioned.Zone("example") |
| 684 |
dns.query.inbound_xfr( |
| 685 |
ns.tcp_address[0], |
| 686 |
zone, |
| 687 |
port=ns.tcp_address[1], |
| 688 |
udp_mode=dns.query.UDPMode.TRY_FIRST, |
| 689 |
) |
| 690 |
dns.query.inbound_xfr( |
| 691 |
ns.tcp_address[0], |
| 692 |
zone, |
| 693 |
port=ns.tcp_address[1], |
| 694 |
udp_mode=dns.query.UDPMode.TRY_FIRST, |
| 695 |
) |
| 696 |
expected = dns.zone.from_text(ixfr_expected, "example") |
| 697 |
assert zone == expected |
| 698 |
|
| 699 |
|
| 700 |
async def async_inbound_xfr(): |
| 701 |
with XFRNanoNameserver() as ns: |
| 702 |
zone = dns.versioned.Zone("example") |
| 703 |
await dns.asyncquery.inbound_xfr( |
| 704 |
ns.tcp_address[0], |
| 705 |
zone, |
| 706 |
port=ns.tcp_address[1], |
| 707 |
udp_mode=dns.query.UDPMode.TRY_FIRST, |
| 708 |
) |
| 709 |
await dns.asyncquery.inbound_xfr( |
| 710 |
ns.tcp_address[0], |
| 711 |
zone, |
| 712 |
port=ns.tcp_address[1], |
| 713 |
udp_mode=dns.query.UDPMode.TRY_FIRST, |
| 714 |
) |
| 715 |
expected = dns.zone.from_text(ixfr_expected, "example") |
| 716 |
assert zone == expected |
| 717 |
|
| 718 |
|
| 719 |
@pytest.mark.skipif(not _nanonameserver_available, reason="requires nanonameserver") |
| 720 |
def test_asyncio_inbound_xfr(): |
| 721 |
dns.asyncbackend.set_default_backend("asyncio") |
| 722 |
|
| 723 |
async def run(): |
| 724 |
await async_inbound_xfr() |
| 725 |
|
| 726 |
asyncio.run(run()) |
| 727 |
|
| 728 |
|
| 729 |
# |
| 730 |
# We don't need to do this as it's all generic code, but |
| 731 |
# just for extra caution we do it for each backend. |
| 732 |
# |
| 733 |
|
| 734 |
try: |
| 735 |
import trio |
| 736 |
|
| 737 |
@pytest.mark.skipif(not _nanonameserver_available, reason="requires nanonameserver") |
| 738 |
def test_trio_inbound_xfr(): |
| 739 |
dns.asyncbackend.set_default_backend("trio") |
| 740 |
|
| 741 |
async def run(): |
| 742 |
await async_inbound_xfr() |
| 743 |
|
| 744 |
trio.run(run) |
| 745 |
|
| 746 |
except ImportError: |
| 747 |
pass |
| 748 |
|
| 749 |
|
| 750 |
class UDPXFRNanoNameserver(Server): |
| 751 |
def __init__(self): |
| 752 |
super().__init__(origin=dns.name.from_text("example")) |
| 753 |
self.did_truncation = False |
| 754 |
|
| 755 |
def handle(self, request): |
| 756 |
try: |
| 757 |
if request.message.question[0].rdtype == dns.rdatatype.IXFR: |
| 758 |
if self.did_truncation: |
| 759 |
text = ixfr |
| 760 |
else: |
| 761 |
text = retry_tcp_ixfr |
| 762 |
self.did_truncation = True |
| 763 |
else: |
| 764 |
text = axfr |
| 765 |
r = dns.message.from_text(text, one_rr_per_rrset=True, origin=self.origin) |
| 766 |
r.id = request.message.id |
| 767 |
return r |
| 768 |
except Exception: |
| 769 |
pass |
| 770 |
|
| 771 |
|
| 772 |
@pytest.mark.skipif(not _nanonameserver_available, reason="requires nanonameserver") |
| 773 |
def test_sync_retry_tcp_inbound_xfr(): |
| 774 |
with UDPXFRNanoNameserver() as ns: |
| 775 |
zone = dns.versioned.Zone("example") |
| 776 |
dns.query.inbound_xfr( |
| 777 |
ns.tcp_address[0], |
| 778 |
zone, |
| 779 |
port=ns.tcp_address[1], |
| 780 |
udp_mode=dns.query.UDPMode.TRY_FIRST, |
| 781 |
) |
| 782 |
dns.query.inbound_xfr( |
| 783 |
ns.tcp_address[0], |
| 784 |
zone, |
| 785 |
port=ns.tcp_address[1], |
| 786 |
udp_mode=dns.query.UDPMode.TRY_FIRST, |
| 787 |
) |
| 788 |
expected = dns.zone.from_text(ixfr_expected, "example") |
| 789 |
assert zone == expected |
| 790 |
|
| 791 |
|
| 792 |
async def udp_async_inbound_xfr(): |
| 793 |
with UDPXFRNanoNameserver() as ns: |
| 794 |
zone = dns.versioned.Zone("example") |
| 795 |
await dns.asyncquery.inbound_xfr( |
| 796 |
ns.tcp_address[0], |
| 797 |
zone, |
| 798 |
port=ns.tcp_address[1], |
| 799 |
udp_mode=dns.query.UDPMode.TRY_FIRST, |
| 800 |
) |
| 801 |
await dns.asyncquery.inbound_xfr( |
| 802 |
ns.tcp_address[0], |
| 803 |
zone, |
| 804 |
port=ns.tcp_address[1], |
| 805 |
udp_mode=dns.query.UDPMode.TRY_FIRST, |
| 806 |
) |
| 807 |
expected = dns.zone.from_text(ixfr_expected, "example") |
| 808 |
assert zone == expected |
| 809 |
|
| 810 |
|
| 811 |
@pytest.mark.skipif(not _nanonameserver_available, reason="requires nanonameserver") |
| 812 |
def test_asyncio_retry_tcp_inbound_xfr(): |
| 813 |
dns.asyncbackend.set_default_backend("asyncio") |
| 814 |
|
| 815 |
async def run(): |
| 816 |
await udp_async_inbound_xfr() |
| 817 |
|
| 818 |
try: |
| 819 |
runner = asyncio.run |
| 820 |
except AttributeError: |
| 821 |
|
| 822 |
def old_runner(awaitable): |
| 823 |
loop = asyncio.get_event_loop() |
| 824 |
return loop.run_until_complete(awaitable) |
| 825 |
|
| 826 |
runner = old_runner |
| 827 |
runner(run()) |