diff options
Diffstat (limited to 'tests/integration/test_udp.py')
| -rw-r--r-- | tests/integration/test_udp.py | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/tests/integration/test_udp.py b/tests/integration/test_udp.py new file mode 100644 index 0000000..1cb2389 --- /dev/null +++ b/tests/integration/test_udp.py | |||
| @@ -0,0 +1,168 @@ | |||
| 1 | """The UDP tracker protocol, BEP 15. | ||
| 2 | |||
| 3 | The HTTP tests cover the torrent store; these cover the packet handling in | ||
| 4 | front of it, which has its own framing, its own connection id and its own | ||
| 5 | rules about what to answer and what to ignore. | ||
| 6 | """ | ||
| 7 | |||
| 8 | import socket | ||
| 9 | import struct | ||
| 10 | import unittest | ||
| 11 | |||
| 12 | from harness import TrackerTestCase | ||
| 13 | |||
| 14 | MAGIC = 0x41727101980 | ||
| 15 | |||
| 16 | |||
| 17 | class UdpProtocol(TrackerTestCase): | ||
| 18 | def test_connect_echoes_the_transaction_id(self): | ||
| 19 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
| 20 | sock.settimeout(5) | ||
| 21 | try: | ||
| 22 | sock.sendto(struct.pack(">QII", MAGIC, 0, 0xDEADBEEF), ("127.0.0.1", self.tracker.port)) | ||
| 23 | action, transaction, connection_id = struct.unpack(">IIQ", sock.recv(2048)[:16]) | ||
| 24 | self.assertEqual(action, 0) | ||
| 25 | self.assertEqual(transaction, 0xDEADBEEF) | ||
| 26 | self.assertNotEqual(connection_id, 0) | ||
| 27 | finally: | ||
| 28 | sock.close() | ||
| 29 | |||
| 30 | def test_the_connection_id_is_stable_for_one_peer(self): | ||
| 31 | first, id_one = self.tracker.udp_connect() | ||
| 32 | second, id_two = self.tracker.udp_connect() | ||
| 33 | try: | ||
| 34 | self.assertEqual(id_one, id_two) | ||
| 35 | finally: | ||
| 36 | first.close() | ||
| 37 | second.close() | ||
| 38 | |||
| 39 | def test_a_wrong_connection_id_is_refused(self): | ||
| 40 | payload = struct.pack(">QII20s20sQQQIIIiH", 0, 1, 2, self.hash_for("udpbad"), b"p" * 20, 0, 0, 0, 0, 0, 0, -1, 6881) | ||
| 41 | answer = self.tracker.udp_raw(payload) | ||
| 42 | self.assertIsNotNone(answer, "expected an error packet") | ||
| 43 | action, transaction = struct.unpack(">II", answer[:8]) | ||
| 44 | self.assertEqual(action, 3, "action 3 is the error packet") | ||
| 45 | self.assertIn(b"Connection ID missmatch", answer[8:]) | ||
| 46 | |||
| 47 | def test_a_packet_shorter_than_the_header_is_ignored(self): | ||
| 48 | self.assertIsNone(self.tracker.udp_raw(b"\x00" * 15)) | ||
| 49 | |||
| 50 | def test_an_unknown_action_is_ignored(self): | ||
| 51 | self.assertIsNone(self.tracker.udp_raw(struct.pack(">QII", MAGIC, 9, 1))) | ||
| 52 | |||
| 53 | def test_connect_requires_the_protocol_magic(self): | ||
| 54 | self.assertIsNone(self.tracker.udp_raw(struct.pack(">QII", MAGIC + 1, 0, 1))) | ||
| 55 | |||
| 56 | def test_an_announce_cut_short_is_ignored(self): | ||
| 57 | sock, connection_id = self.tracker.udp_connect() | ||
| 58 | try: | ||
| 59 | self.assertIsNone(self.tracker.udp_raw(struct.pack(">QII", connection_id, 1, 1) + b"\0" * 40)) | ||
| 60 | finally: | ||
| 61 | sock.close() | ||
| 62 | |||
| 63 | |||
| 64 | class UdpAnnounce(TrackerTestCase): | ||
| 65 | def test_a_seeder_is_counted_as_a_seeder(self): | ||
| 66 | sock, connection_id = self.tracker.udp_connect() | ||
| 67 | try: | ||
| 68 | answer = self.tracker.udp_announce(sock, connection_id, self.hash_for("udpseed"), left=0) | ||
| 69 | self.assertEqual(answer["seeders"], 1) | ||
| 70 | self.assertEqual(answer["leechers"], 0) | ||
| 71 | self.assertGreater(answer["interval"], 0) | ||
| 72 | finally: | ||
| 73 | sock.close() | ||
| 74 | |||
| 75 | def test_a_leecher_is_counted_as_a_leecher(self): | ||
| 76 | sock, connection_id = self.tracker.udp_connect() | ||
| 77 | try: | ||
| 78 | answer = self.tracker.udp_announce(sock, connection_id, self.hash_for("udpleech"), left=1024) | ||
| 79 | self.assertEqual(answer["seeders"], 0) | ||
| 80 | self.assertEqual(answer["leechers"], 1) | ||
| 81 | finally: | ||
| 82 | sock.close() | ||
| 83 | |||
| 84 | def test_stopped_removes_the_peer(self): | ||
| 85 | info_hash = self.hash_for("udpstop") | ||
| 86 | sock, connection_id = self.tracker.udp_connect() | ||
| 87 | try: | ||
| 88 | self.tracker.udp_announce(sock, connection_id, info_hash, left=0) | ||
| 89 | self.tracker.udp_announce(sock, connection_id, info_hash, left=0, event=3) | ||
| 90 | answer = self.tracker.udp_announce(sock, connection_id, info_hash, left=1024) | ||
| 91 | self.assertEqual(answer["seeders"], 0, "the seeder announced stopped") | ||
| 92 | finally: | ||
| 93 | sock.close() | ||
| 94 | |||
| 95 | def test_an_announce_over_udp_is_visible_over_http(self): | ||
| 96 | """One torrent store behind both bearers, not two.""" | ||
| 97 | info_hash = self.hash_for("udpcross") | ||
| 98 | sock, connection_id = self.tracker.udp_connect() | ||
| 99 | try: | ||
| 100 | self.tracker.udp_announce(sock, connection_id, info_hash, left=0) | ||
| 101 | finally: | ||
| 102 | sock.close() | ||
| 103 | scrape = self.tracker.scrape(info_hash) | ||
| 104 | self.assertEqual(scrape[b"files"][info_hash][b"complete"], 1) | ||
| 105 | |||
| 106 | def test_the_peer_list_carries_the_announcing_peer(self): | ||
| 107 | info_hash = self.hash_for("udppeers") | ||
| 108 | sock, connection_id = self.tracker.udp_connect() | ||
| 109 | try: | ||
| 110 | answer = self.tracker.udp_announce(sock, connection_id, info_hash, left=0, port=6881) | ||
| 111 | self.assertEqual(len(answer["peers"]) % 6, 0, "v4 peers are six bytes each") | ||
| 112 | self.assertGreaterEqual(len(answer["peers"]), 6) | ||
| 113 | self.assertEqual(struct.unpack(">H", answer["peers"][4:6])[0], 6881) | ||
| 114 | finally: | ||
| 115 | sock.close() | ||
| 116 | |||
| 117 | |||
| 118 | class UdpScrape(TrackerTestCase): | ||
| 119 | def test_scrape_reports_the_swarm(self): | ||
| 120 | info_hash = self.hash_for("udpscrape") | ||
| 121 | sock, connection_id = self.tracker.udp_connect() | ||
| 122 | try: | ||
| 123 | self.tracker.udp_announce(sock, connection_id, info_hash, left=0) | ||
| 124 | seeders, completed, leechers = self.tracker.udp_scrape(sock, connection_id, [info_hash])[0] | ||
| 125 | self.assertEqual(seeders, 1) | ||
| 126 | self.assertEqual(leechers, 0) | ||
| 127 | self.assertEqual(completed, 0) | ||
| 128 | finally: | ||
| 129 | sock.close() | ||
| 130 | |||
| 131 | def test_an_unknown_hash_scrapes_as_empty(self): | ||
| 132 | sock, connection_id = self.tracker.udp_connect() | ||
| 133 | try: | ||
| 134 | self.assertEqual(self.tracker.udp_scrape(sock, connection_id, [self.hash_for("udpnone")])[0], (0, 0, 0)) | ||
| 135 | finally: | ||
| 136 | sock.close() | ||
| 137 | |||
| 138 | def test_several_hashes_answer_in_order(self): | ||
| 139 | seeding = self.hash_for("udpmultiA") | ||
| 140 | leeching = self.hash_for("udpmultiB") | ||
| 141 | sock, connection_id = self.tracker.udp_connect() | ||
| 142 | try: | ||
| 143 | self.tracker.udp_announce(sock, connection_id, seeding, left=0) | ||
| 144 | self.tracker.udp_announce(sock, connection_id, leeching, left=1024) | ||
| 145 | answers = self.tracker.udp_scrape(sock, connection_id, [seeding, leeching]) | ||
| 146 | self.assertEqual(answers[0][0], 1, "first hash is the seeded one") | ||
| 147 | self.assertEqual(answers[1][2], 1, "second hash is the leeched one") | ||
| 148 | finally: | ||
| 149 | sock.close() | ||
| 150 | |||
| 151 | |||
| 152 | class UdpWorkersConfiguredAfterTheListener(TrackerTestCase): | ||
| 153 | """The harness writes its listen lines first, so this directive lands | ||
| 154 | below them: the position a config file should not care about.""" | ||
| 155 | |||
| 156 | config = "listen.udp.workers 4\n" | ||
| 157 | |||
| 158 | def test_the_tracker_starts_and_still_answers_udp(self): | ||
| 159 | sock, connection_id = self.tracker.udp_connect() | ||
| 160 | try: | ||
| 161 | answer = self.tracker.udp_announce(sock, connection_id, self.hash_for("udpworkers"), left=0) | ||
| 162 | self.assertEqual(answer["seeders"], 1) | ||
| 163 | finally: | ||
| 164 | sock.close() | ||
| 165 | |||
| 166 | |||
| 167 | if __name__ == "__main__": | ||
| 168 | unittest.main() | ||
