diff options
| -rw-r--r-- | tests/accept_filter_probe.py | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/tests/accept_filter_probe.py b/tests/accept_filter_probe.py new file mode 100644 index 0000000..22f585a --- /dev/null +++ b/tests/accept_filter_probe.py | |||
| @@ -0,0 +1,110 @@ | |||
| 1 | """What a deferred accept does to connections that never send anything. | ||
| 2 | |||
| 3 | Those connections never reach opentracker, so its own client timeout cannot | ||
| 4 | clean them up: whatever bound exists is the kernel's. This measures it, and | ||
| 5 | then asks the question that follows from it -- whether enough of them keep | ||
| 6 | the tracker from answering anybody else. | ||
| 7 | |||
| 8 | make opentracker | ||
| 9 | python3 tests/accept_filter_probe.py | ||
| 10 | |||
| 11 | On FreeBSD the filter does nothing unless accf_data is loaded, so run it both | ||
| 12 | ways to see whether the numbers differ: | ||
| 13 | |||
| 14 | kldload accf_data | ||
| 15 | """ | ||
| 16 | |||
| 17 | import os | ||
| 18 | import socket | ||
| 19 | import signal | ||
| 20 | import subprocess | ||
| 21 | import sys | ||
| 22 | import tempfile | ||
| 23 | import time | ||
| 24 | import urllib.error | ||
| 25 | import urllib.request | ||
| 26 | |||
| 27 | TRACKER = os.environ.get("OPENTRACKER", "./opentracker") | ||
| 28 | IDLE = int(os.environ.get("IDLE", "300")) | ||
| 29 | PATIENCE = float(os.environ.get("PATIENCE", "90")) | ||
| 30 | HASH = "".join("%%%02x" % b for b in range(1, 21)) | ||
| 31 | |||
| 32 | |||
| 33 | def start(port): | ||
| 34 | directory = tempfile.mkdtemp(prefix="ot-accf-") | ||
| 35 | with open(os.path.join(directory, "conf"), "w") as handle: | ||
| 36 | handle.write("listen.tcp 127.0.0.1:%d\nlisten.udp 127.0.0.1:%d\n" % (port, port)) | ||
| 37 | log = open(os.path.join(directory, "log"), "w+b") | ||
| 38 | tracker = subprocess.Popen([TRACKER, "-f", os.path.join(directory, "conf")], stdout=log, stderr=log) | ||
| 39 | for _ in range(300): | ||
| 40 | try: | ||
| 41 | socket.create_connection(("127.0.0.1", port), timeout=0.2).close() | ||
| 42 | return tracker, log | ||
| 43 | except OSError: | ||
| 44 | time.sleep(0.02) | ||
| 45 | tracker.kill() | ||
| 46 | raise SystemExit("tracker never listened on %d" % port) | ||
| 47 | |||
| 48 | |||
| 49 | def announce(port, timeout=5): | ||
| 50 | """One ordinary request, so we can see whether the tracker still serves.""" | ||
| 51 | started = time.time() | ||
| 52 | try: | ||
| 53 | urllib.request.urlopen( | ||
| 54 | "http://127.0.0.1:%d/announce?info_hash=%s&peer_id=-OT0001-probe0000001&port=6881&left=0" % (port, HASH), | ||
| 55 | timeout=timeout, | ||
| 56 | ).read() | ||
| 57 | return time.time() - started, None | ||
| 58 | except Exception as problem: # noqa: BLE001 - reported, not swallowed | ||
| 59 | return time.time() - started, problem | ||
| 60 | |||
| 61 | |||
| 62 | def main(): | ||
| 63 | port = int(os.environ.get("PORT", "28000")) | ||
| 64 | tracker, log = start(port) | ||
| 65 | try: | ||
| 66 | print(" how long a connection that says nothing is held") | ||
| 67 | quiet = socket.create_connection(("127.0.0.1", port), timeout=5) | ||
| 68 | quiet.settimeout(PATIENCE) | ||
| 69 | started = time.time() | ||
| 70 | try: | ||
| 71 | held = "dropped after %.1f s" % (time.time() - started) if quiet.recv(1) == b"" else "sent us something" | ||
| 72 | except socket.timeout: | ||
| 73 | held = "still open after %.0f s" % PATIENCE | ||
| 74 | except OSError as problem: | ||
| 75 | held = "ended after %.1f s: %s" % (time.time() - started, problem) | ||
| 76 | quiet.close() | ||
| 77 | print(" %s" % held) | ||
| 78 | |||
| 79 | print(" and whether %d of them keep anybody else out" % IDLE) | ||
| 80 | before, problem = announce(port) | ||
| 81 | print(" a request before: %.0f ms%s" % (before * 1000, "" if problem is None else " (%s)" % problem)) | ||
| 82 | |||
| 83 | idlers = [] | ||
| 84 | for _ in range(IDLE): | ||
| 85 | try: | ||
| 86 | idlers.append(socket.create_connection(("127.0.0.1", port), timeout=2)) | ||
| 87 | except OSError as problem: | ||
| 88 | print(" could only open %d of them: %s" % (len(idlers), problem)) | ||
| 89 | break | ||
| 90 | print(" holding %d connections that have sent nothing" % len(idlers)) | ||
| 91 | |||
| 92 | during, problem = announce(port, timeout=10) | ||
| 93 | print(" a request during: %.0f ms%s" % (during * 1000, "" if problem is None else " REFUSED: %s" % problem)) | ||
| 94 | |||
| 95 | for idler in idlers: | ||
| 96 | idler.close() | ||
| 97 | after, problem = announce(port) | ||
| 98 | print(" a request after: %.0f ms%s" % (after * 1000, "" if problem is None else " (%s)" % problem)) | ||
| 99 | finally: | ||
| 100 | tracker.send_signal(signal.SIGINT) | ||
| 101 | try: | ||
| 102 | tracker.wait(timeout=10) | ||
| 103 | except subprocess.TimeoutExpired: | ||
| 104 | tracker.kill() | ||
| 105 | tracker.wait() | ||
| 106 | log.close() | ||
| 107 | |||
| 108 | |||
| 109 | if __name__ == "__main__": | ||
| 110 | main() | ||
