32 lines
1.3 KiB
Python
32 lines
1.3 KiB
Python
"""Obtain four real DNS failure/absence results, including a silent UDP endpoint."""
|
|
import socket
|
|
import subprocess
|
|
|
|
|
|
def query(server, name, kind="A", port=53):
|
|
command = ["dig", "@" + server, "-p", str(port), name, kind,
|
|
"+tries=1", "+time=2", "+noall", "+comments", "+answer", "+authority"]
|
|
print("\n$ " + " ".join(command), flush=True)
|
|
result = subprocess.run(command, capture_output=True, text=True)
|
|
print(result.stdout, end="", flush=True)
|
|
return result
|
|
|
|
|
|
def main():
|
|
nx = query("resolver", "missing.lab.test")
|
|
nodata = query("resolver", "orders.lab.test", "AAAA")
|
|
failed = query("resolver", "name.broken.test")
|
|
# A bound UDP socket receives packets but sends nothing back: no ICMP port-unreachable.
|
|
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as silent:
|
|
silent.bind(("127.0.0.1", 0))
|
|
timeout = query("127.0.0.1", "orders.lab.test", port=silent.getsockname()[1])
|
|
assert "status: NXDOMAIN" in nx.stdout
|
|
assert "status: NOERROR" in nodata.stdout and "ANSWER: 0" in nodata.stdout
|
|
assert "status: SERVFAIL" in failed.stdout
|
|
assert timeout.returncode == 9 and "timed out" in timeout.stdout
|
|
print("PASS: NXDOMAIN / NODATA / SERVFAIL / timeout")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|