diff options
Diffstat (limited to 'tests/integration')
| -rw-r--r-- | tests/integration/test_stats.py | 34 |
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 | |||
| 5 | the thing it counts happens. | 5 | the thing it counts happens. |
| 6 | """ | 6 | """ |
| 7 | 7 | ||
| 8 | import ipaddress | ||
| 8 | import re | 9 | import re |
| 10 | import socket | ||
| 9 | import unittest | 11 | import unittest |
| 10 | import urllib.error | 12 | import urllib.error |
| 11 | import xml.etree.ElementTree | 13 | import xml.etree.ElementTree |
| @@ -131,15 +133,37 @@ class StatsTop(TrackerTestCase): | |||
| 131 | 133 | ||
| 132 | 134 | ||
| 133 | class StatsNetworks(TrackerTestCase): | 135 | class 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 | ||
| 145 | if __name__ == "__main__": | 169 | if __name__ == "__main__": |
