From 0d6e0044a104176d6d6f2111fda1d7c1f160a583 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Tue, 27 Oct 2026 01:25:41 +0100 Subject: retrieve the address of the sending socket for comparison. In jails it might be forced to its primary address --- tests/integration/test_stats.py | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) (limited to 'tests') diff --git a/tests/integration/test_stats.py b/tests/integration/test_stats.py index 920d199..7ff9a38 100644 --- a/tests/integration/test_stats.py +++ b/tests/integration/test_stats.py @@ -5,7 +5,9 @@ every one of them and, where the number means something, that it moves when the thing it counts happens. """ +import ipaddress import re +import socket import unittest import urllib.error import xml.etree.ElementTree @@ -131,15 +133,37 @@ class StatsTop(TrackerTestCase): class StatsNetworks(TrackerTestCase): - def test_the_slash24_report_names_the_loopback_network(self): + def source_address(self): + """Which address the tracker sees us from is not ours to choose: a + FreeBSD jail relocates a loopback connection to the jail's own + address, so ask the socket rather than assume 127.0.0.1.""" + probe = socket.create_connection(("127.0.0.1", self.tracker.port), timeout=5) + try: + return probe.getsockname()[0] + finally: + probe.close() + + def test_the_slash24_report_names_the_announcing_network(self): + source = self.source_address() self.tracker.announce(self.hash_for("s24"), port=6001, left=0) body = self.tracker.stats("s24s") self.assertIn(b"Networks, limit /28:", body) self.assertIn(b"Networks, limit /24:", body) - # The prefix is masked out of a v4 peer's four address bytes and then - # printed through the v6 formatter, so 127.0.0.0/24 reads as "7f00::" - # rather than as an address anyone would recognise. Pinned as it is. - self.assertIn(b"7f00::", body) + + # The prefix is the first three address bytes of a v4 peer, left + # aligned in a sixteen byte buffer and then printed by the v6 + # formatter, so 127.0.0.0/24 reads as "7f00::" rather than as the + # network an operator asked about. Pinned as it is, not blessed. + prefix = socket.inet_aton(source)[:3] + b"\0" * 13 + self.assertIn(str(ipaddress.IPv6Address(prefix)).encode(), body) + + def test_the_report_counts_one_network_per_announcing_address(self): + for port in (6001, 6002, 6003): + self.tracker.announce(self.hash_for("s24count"), port=port, left=0) + lines = [line for line in self.tracker.stats("s24s").splitlines() if b":" in line and b"limit" not in line] + self.assertEqual(len(lines), 2, "one line under each of the two limits") + for line in lines: + self.assertEqual(int(line.split(b":")[0]), 3, "three peers from the one network") if __name__ == "__main__": -- cgit v1.3