summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2026-10-27 01:25:41 +0100
committerDirk Engling <erdgeist@erdgeist.org>2026-10-27 01:25:41 +0100
commit0d6e0044a104176d6d6f2111fda1d7c1f160a583 (patch)
treeac0725d6fcf99c16a5ce5a57fc6f70ce4481d873 /tests
parent9d21cb476d07ab5583c99b1418d4ffb54fbb2a10 (diff)
retrieve the address of the sending socket for comparison. In jails it might be forced to its primary address
Diffstat (limited to 'tests')
-rw-r--r--tests/integration/test_stats.py34
1 files changed, 29 insertions, 5 deletions
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
5the thing it counts happens. 5the thing it counts happens.
6""" 6"""
7 7
8import ipaddress
8import re 9import re
10import socket
9import unittest 11import unittest
10import urllib.error 12import urllib.error
11import xml.etree.ElementTree 13import xml.etree.ElementTree
@@ -131,15 +133,37 @@ class StatsTop(TrackerTestCase):
131 133
132 134
133class StatsNetworks(TrackerTestCase): 135class StatsNetworks(TrackerTestCase):
134 def test_the_slash24_report_names_the_loopback_network(self): 136 def source_address(self):
137 """Which address the tracker sees us from is not ours to choose: a
138 FreeBSD jail relocates a loopback connection to the jail's own
139 address, so ask the socket rather than assume 127.0.0.1."""
140 probe = socket.create_connection(("127.0.0.1", self.tracker.port), timeout=5)
141 try:
142 return probe.getsockname()[0]
143 finally:
144 probe.close()
145
146 def test_the_slash24_report_names_the_announcing_network(self):
147 source = self.source_address()
135 self.tracker.announce(self.hash_for("s24"), port=6001, left=0) 148 self.tracker.announce(self.hash_for("s24"), port=6001, left=0)
136 body = self.tracker.stats("s24s") 149 body = self.tracker.stats("s24s")
137 self.assertIn(b"Networks, limit /28:", body) 150 self.assertIn(b"Networks, limit /28:", body)
138 self.assertIn(b"Networks, limit /24:", body) 151 self.assertIn(b"Networks, limit /24:", body)
139 # The prefix is masked out of a v4 peer's four address bytes and then 152
140 # printed through the v6 formatter, so 127.0.0.0/24 reads as "7f00::" 153 # The prefix is the first three address bytes of a v4 peer, left
141 # rather than as an address anyone would recognise. Pinned as it is. 154 # aligned in a sixteen byte buffer and then printed by the v6
142 self.assertIn(b"7f00::", body) 155 # formatter, so 127.0.0.0/24 reads as "7f00::" rather than as the
156 # network an operator asked about. Pinned as it is, not blessed.
157 prefix = socket.inet_aton(source)[:3] + b"\0" * 13
158 self.assertIn(str(ipaddress.IPv6Address(prefix)).encode(), body)
159
160 def test_the_report_counts_one_network_per_announcing_address(self):
161 for port in (6001, 6002, 6003):
162 self.tracker.announce(self.hash_for("s24count"), port=port, left=0)
163 lines = [line for line in self.tracker.stats("s24s").splitlines() if b":" in line and b"limit" not in line]
164 self.assertEqual(len(lines), 2, "one line under each of the two limits")
165 for line in lines:
166 self.assertEqual(int(line.split(b":")[0]), 3, "three peers from the one network")
143 167
144 168
145if __name__ == "__main__": 169if __name__ == "__main__":