summaryrefslogtreecommitdiff
path: root/tests/integration/test_livesync.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/integration/test_livesync.py')
-rw-r--r--tests/integration/test_livesync.py240
1 files changed, 240 insertions, 0 deletions
diff --git a/tests/integration/test_livesync.py b/tests/integration/test_livesync.py
new file mode 100644
index 0000000..e146d93
--- /dev/null
+++ b/tests/integration/test_livesync.py
@@ -0,0 +1,240 @@
1"""Live sync, driven over the multicast group a cluster would use.
2
3Live sync sits behind WANT_SYNC_LIVE, so these only run against a binary
4built with it: make check-livesync. Against any other build they skip, which
5is what the feature list in -v is for.
6"""
7
8import socket
9import struct
10import subprocess
11import unittest
12
13from harness import TRACKER, Tracker, TrackerTestCase
14
15# enum { OT_SYNC_PEER4, OT_SYNC_PEER6 } in src/ot_livesync.c
16SYNC_PEER4 = 0
17SYNC_PEER6 = 1
18
19# src/trackerlogic.h
20FLAG_SEEDING = 0x80
21FLAG_COMPLETED = 0x40
22FLAG_STOPPED = 0x20
23
24
25def has_livesync():
26 done = subprocess.run([TRACKER, "-v"], capture_output=True, timeout=30)
27 return b"WANT_SYNC_LIVE" in done.stderr
28
29
30def peer4(address, port, flags):
31 """Four bytes of address, two of port, one of flags, one the tracker
32 overwrites with its own idea of the time."""
33 return socket.inet_aton(address) + struct.pack("!H", port) + bytes([flags, 0])
34
35
36def peer6(address, port, flags):
37 return socket.inet_pton(socket.AF_INET6, address) + struct.pack("!H", port) + bytes([flags, 0])
38
39
40def packet(kind, entries, tracker_id=0xDEADBEEF):
41 body = b"".join(info_hash + peer for info_hash, peer in entries)
42 return struct.pack("!I", tracker_id) + struct.pack("!I", kind) + body
43
44
45@unittest.skipUnless(has_livesync(), "this build has no WANT_SYNC_LIVE")
46class LiveSyncIn(TrackerTestCase):
47 config = "livesync.cluster.listen 127.0.0.1:{livesync_port}\nlivesync.cluster.node_ip 127.0.0.1\n"
48
49 def counts(self, info_hash):
50 """Seeders and leechers a scrape reports for one torrent."""
51 files = self.tracker.scrape(info_hash)[b"files"]
52 if info_hash not in files:
53 return None
54 entry = files[info_hash]
55 return entry[b"complete"], entry[b"incomplete"]
56
57 def test_a_synced_seeder_arrives(self):
58 info_hash = self.hash_for("sync-seed")
59 self.tracker.livesync_send(packet(SYNC_PEER4, [(info_hash, peer4("10.1.2.3", 6881, FLAG_SEEDING))]))
60 self.assertTrue(self.tracker.wait_until(lambda: self.counts(info_hash) == (1, 0)),
61 "scrape says %r" % (self.counts(info_hash),))
62
63 def test_a_synced_leecher_arrives(self):
64 info_hash = self.hash_for("sync-leech")
65 self.tracker.livesync_send(packet(SYNC_PEER4, [(info_hash, peer4("10.1.2.4", 6881, 0))]))
66 self.assertTrue(self.tracker.wait_until(lambda: self.counts(info_hash) == (0, 1)),
67 "scrape says %r" % (self.counts(info_hash),))
68
69 def test_a_v6_peer_arrives(self):
70 info_hash = self.hash_for("sync-v6")
71 self.tracker.livesync_send(packet(SYNC_PEER6, [(info_hash, peer6("2001:db8::1", 6881, FLAG_SEEDING))]))
72 self.assertTrue(self.tracker.wait_until(lambda: self.counts(info_hash) == (1, 0)),
73 "scrape says %r" % (self.counts(info_hash),))
74
75 def test_many_v6_announces_do_not_overflow_the_send_buffer(self):
76 """The outgoing peer buffer is filled by announces to a clustered
77 tracker. The flush watermark used sizeof(ot_peer) (== 1), so a v6
78 record could be appended past the 1480-byte buffer onto the adjacent
79 fill field, which then drove an oversized stack copy. Well past the
80 old overflow point (~37 v6 peers) the tracker must still be alive."""
81 last = None
82 for i in range(80):
83 last = self.tracker.announce(self.hash_for("flood-%d" % i),
84 ip="2001:db8::%x" % (i + 1), port=7000 + i, left=1)
85 self.assertIn(b"complete", last, "the tracker stopped answering mid-flood")
86 # tearDown's stop() surfaces any sanitizer complaint on the way out.
87
88 def test_one_packet_carries_several_torrents(self):
89 hashes = [self.hash_for("sync-many-%d" % i) for i in range(4)]
90 self.tracker.livesync_send(packet(SYNC_PEER4, [(h, peer4("10.1.2.%d" % (5 + i), 6881, FLAG_SEEDING))
91 for i, h in enumerate(hashes)]))
92 self.assertTrue(self.tracker.wait_until(lambda: all(self.counts(h) == (1, 0) for h in hashes)))
93
94 def test_a_stopped_peer_is_taken_away_again(self):
95 info_hash = self.hash_for("sync-stop")
96 peer = peer4("10.1.2.9", 6881, FLAG_SEEDING)
97 self.tracker.livesync_send(packet(SYNC_PEER4, [(info_hash, peer)]))
98 self.assertTrue(self.tracker.wait_until(lambda: self.counts(info_hash) == (1, 0)))
99
100 self.tracker.livesync_send(packet(SYNC_PEER4, [(info_hash, peer4("10.1.2.9", 6881, FLAG_STOPPED))]))
101 self.assertTrue(self.tracker.wait_until(lambda: self.counts(info_hash) in (None, (0, 0))),
102 "scrape still says %r" % (self.counts(info_hash),))
103
104 def test_a_truncated_entry_is_dropped(self):
105 """The loop takes whole hash and peer pairs and leaves a short tail
106 where it is, so the complete entry before it still lands."""
107 good, short = self.hash_for("sync-good"), self.hash_for("sync-short")
108 payload = packet(SYNC_PEER4, [(good, peer4("10.1.2.10", 6881, FLAG_SEEDING))]) + short + b"\x0a\x01"
109 self.tracker.livesync_send(payload)
110 self.assertTrue(self.tracker.wait_until(lambda: self.counts(good) == (1, 0)))
111 self.assertIsNone(self.counts(short))
112
113 def test_an_unknown_packet_type_is_ignored(self):
114 info_hash = self.hash_for("sync-unknown")
115 self.tracker.livesync_send(packet(99, [(info_hash, peer4("10.1.2.11", 6881, FLAG_SEEDING))]))
116 self.assertFalse(self.tracker.wait_until(lambda: self.counts(info_hash) is not None, timeout=1))
117
118 def test_synced_peers_are_counted(self):
119 """The live sync worker keeps its own tally like every other worker,
120 and ?mode=syncs is the sum of them."""
121 before = int(self.tracker.stats("syncs").split()[0])
122 hashes = [self.hash_for("sync-count-%d" % i) for i in range(3)]
123 self.tracker.livesync_send(packet(SYNC_PEER4, [(h, peer4("10.1.2.%d" % (20 + i), 6881, FLAG_SEEDING))
124 for i, h in enumerate(hashes)]))
125 self.assertTrue(self.tracker.wait_until(
126 lambda: int(self.tracker.stats("syncs").split()[0]) == before + 3),
127 "syncs says %s" % self.tracker.stats("syncs").split()[0])
128
129 def test_a_packet_with_nothing_but_a_header_is_ignored(self):
130 before = self.tracker.torrent_count()
131 self.tracker.livesync_send(struct.pack("!I", 0xDEADBEEF) + struct.pack("!I", SYNC_PEER4))
132 self.assertFalse(self.tracker.wait_until(lambda: self.tracker.torrent_count() != before, timeout=1))
133
134
135@unittest.skipUnless(has_livesync(), "this build has no WANT_SYNC_LIVE")
136class LiveSyncAccess(TrackerTestCase):
137 """Nothing blesses 127.0.0.1 here, so the tracker should take nothing
138 from it. Source address checking is all live sync has."""
139
140 config = "livesync.cluster.listen 127.0.0.1:{livesync_port}\nlivesync.cluster.node_ip 10.0.0.1\n"
141
142 def test_an_unblessed_sender_is_ignored(self):
143 info_hash = self.hash_for("sync-unblessed")
144 self.tracker.livesync_send(packet(SYNC_PEER4, [(info_hash, peer4("10.1.2.12", 6881, FLAG_SEEDING))]))
145 self.assertFalse(self.tracker.wait_until(lambda: self.tracker.torrent_count() != 0, timeout=1))
146
147
148@unittest.skipUnless(has_livesync(), "this build has no WANT_SYNC_LIVE")
149class LiveSyncUnconfigured(TrackerTestCase):
150 """A binary that has the feature but no cluster to join. Somebody whose
151 packager turned WANT_SYNC_LIVE on did not ask for live sync and should
152 not be able to tell that it is there."""
153
154 config = ""
155
156 def test_the_tracker_runs_anyway(self):
157 info_hash = self.hash_for("no-cluster")
158 self.tracker.announce(info_hash, port=6881, left=0)
159 self.assertEqual(self.tracker.torrent_count(), 1)
160
161 def test_nothing_is_said_about_the_missing_cluster(self):
162 self.assertNotIn("sync", self.tracker.output().lower())
163
164 def test_no_socket_is_bound_for_it(self):
165 """A configured tracker holds the wildcard address on that port, so
166 taking it here proves nothing was started."""
167 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
168 try:
169 sock.bind(("0.0.0.0", self.tracker.livesync_port))
170 finally:
171 sock.close()
172
173 def test_announcing_stays_clean(self):
174 """Enough announces to pass the watermark that would flush an
175 outgoing buffer, if there were one to flush. The sanitizers in this
176 build turn anything the attempt touched into a failure."""
177 for i in range(200):
178 self.tracker.announce(self.hash_for("no-cluster-%d" % i), port=6881 + i, left=0)
179 self.assertEqual(self.tracker.torrent_count(), 200)
180
181
182# RFC 2365 sets 239.192.0.0/14 aside for exactly this kind of use
183OWN_GROUP = "239.192.0.7"
184# A group nothing in this suite ever joins. Deliberately not the default one:
185# other trackers here do join that, on ports the harness picks at random, and
186# a socket bound to the wildcard address shares a port's multicast traffic
187# with anything else on the host that has joined the group it is addressed
188# to. Two clusters sharing a host and a port is pathological in the field but
189# routine in a test suite.
190UNJOINED_GROUP = "239.192.0.99"
191
192
193@unittest.skipUnless(has_livesync(), "this build has no WANT_SYNC_LIVE")
194class LiveSyncGroup(TrackerTestCase):
195 config = ("livesync.cluster.group %s\n"
196 "livesync.cluster.listen 127.0.0.1:{livesync_port}\n"
197 "livesync.cluster.node_ip 127.0.0.1\n" % OWN_GROUP)
198
199 def test_the_configured_group_is_joined(self):
200 info_hash = self.hash_for("own-group")
201 self.tracker.livesync_send(packet(SYNC_PEER4, [(info_hash, peer4("10.1.3.1", 6881, FLAG_SEEDING))]),
202 group=OWN_GROUP)
203 self.assertTrue(self.tracker.wait_until(lambda: self.tracker.torrent_count() == 1))
204
205 def test_another_group_is_left_alone(self):
206 """A cluster that picked its own group hears nothing from the others."""
207 info_hash = self.hash_for("not-our-group")
208 self.tracker.livesync_send(packet(SYNC_PEER4, [(info_hash, peer4("10.1.3.2", 6881, FLAG_SEEDING))]),
209 group=UNJOINED_GROUP)
210 self.assertFalse(self.tracker.wait_until(lambda: self.tracker.torrent_count() != 0, timeout=1))
211
212
213@unittest.skipUnless(has_livesync(), "this build has no WANT_SYNC_LIVE")
214class LiveSyncGroupRefused(unittest.TestCase):
215 """A cluster that asked for a group of its own must not quietly end up on
216 the one every other opentracker is on, so a line that does not name a
217 group stops the tracker rather than falling back."""
218
219 def start_with(self, group):
220 return Tracker("livesync.cluster.group %s\n"
221 "livesync.cluster.listen 127.0.0.1:{livesync_port}\n"
222 "livesync.cluster.node_ip 127.0.0.1\n" % group)
223
224 def test_a_unicast_address_stops_the_tracker(self):
225 with self.assertRaises(AssertionError) as caught:
226 self.start_with("10.0.0.1")
227 self.assertIn("multicast address", str(caught.exception))
228
229 def test_so_does_something_that_is_not_an_address(self):
230 with self.assertRaises(AssertionError) as caught:
231 self.start_with("not-an-address")
232 self.assertIn("multicast address", str(caught.exception))
233
234 def test_a_group_at_either_end_of_the_range_is_taken(self):
235 for group in ("224.0.0.1", "239.255.255.255"):
236 tracker = self.start_with(group)
237 try:
238 self.assertEqual(tracker.torrent_count(), 0)
239 finally:
240 tracker.stop()