summaryrefslogtreecommitdiff
path: root/tests/integration/test_accesslist.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/integration/test_accesslist.py')
-rw-r--r--tests/integration/test_accesslist.py44
1 files changed, 36 insertions, 8 deletions
diff --git a/tests/integration/test_accesslist.py b/tests/integration/test_accesslist.py
index 41ff654..3ed8ddf 100644
--- a/tests/integration/test_accesslist.py
+++ b/tests/integration/test_accesslist.py
@@ -5,10 +5,12 @@ entry, and the file parser has to cope with the shapes an operator actually
5produces: an empty file, and a last line with no newline after it. 5produces: an empty file, and a last line with no newline after it.
6""" 6"""
7 7
8import os
8import struct 9import struct
10import time
9import unittest 11import unittest
10 12
11from harness import TrackerTestCase 13from harness import Tracker, TrackerTestCase
12 14
13BLOCKED = b"\xb1" * 20 15BLOCKED = b"\xb1" * 20
14ALLOWED = b"\xa1" * 20 16ALLOWED = b"\xa1" * 20
@@ -37,10 +39,11 @@ class Blacklist(TrackerTestCase):
37 self.assertNotIn(b"failure reason", answer) 39 self.assertNotIn(b"failure reason", answer)
38 self.assertEqual(answer[b"complete"], 1) 40 self.assertEqual(answer[b"complete"], 1)
39 41
40 def test_the_block_is_an_error_packet_over_udp(self): 42 def test_the_block_is_an_empty_announce_over_udp(self):
41 """BEP 15 has an error action, which is what a refusal should use. 43 """An empty swarm with a long interval, not an error. BEP 15 binds a
42 It used to answer the announce header with no body behind it, a 44 client to the interval and says nothing about what to do with an
43 packet too short for any client to read.""" 45 error, so an error puts the client on its own retry schedule, which
46 starts in the tens of seconds."""
44 sock, connection_id = self.tracker.udp_connect() 47 sock, connection_id = self.tracker.udp_connect()
45 sock.close() 48 sock.close()
46 request = struct.pack( 49 request = struct.pack(
@@ -48,10 +51,13 @@ class Blacklist(TrackerTestCase):
48 ) 51 )
49 answer = self.tracker.udp_raw(request) 52 answer = self.tracker.udp_raw(request)
50 self.assertIsNotNone(answer) 53 self.assertIsNotNone(answer)
51 action, transaction = struct.unpack(">II", answer[:8]) 54 self.assertEqual(len(answer), 20, "header and counts, no peers behind them")
52 self.assertEqual(action, 3, "action 3 is the error packet") 55 action, transaction, interval, leechers, seeders = struct.unpack(">IIIII", answer)
56 self.assertEqual(action, 1, "action 1 is the announce packet")
53 self.assertEqual(transaction, 7) 57 self.assertEqual(transaction, 7)
54 self.assertIn(b"not authorized", answer[8:]) 58 self.assertEqual((leechers, seeders), (0, 0))
59 self.assertGreaterEqual(interval, 60 * 60, "far enough out to be worth the trouble")
60 self.assertLessEqual(interval, 60 * 60 * 6, "not so far out that a client clamps it")
55 61
56 def test_a_blocked_hash_is_not_stored_when_it_arrives_over_udp(self): 62 def test_a_blocked_hash_is_not_stored_when_it_arrives_over_udp(self):
57 sock, connection_id = self.tracker.udp_connect() 63 sock, connection_id = self.tracker.udp_connect()
@@ -96,5 +102,27 @@ class MissingBlacklist(TrackerTestCase):
96 self.assertEqual(self.tracker.announce(BLOCKED, port=6881, left=0)[b"complete"], 1) 102 self.assertEqual(self.tracker.announce(BLOCKED, port=6881, left=0)[b"complete"], 1)
97 103
98 104
105class ListIsInPlaceBeforeTheStateFile(TrackerTestCase):
106 """The loader used to run in a thread of its own, so a state file read at
107 startup was checked against a list that was still empty."""
108
109 config = "access.blacklist {dir}/blacklist\n"
110 files = {"blacklist": listing(BLOCKED)}
111
112 def test_a_blacklisted_torrent_in_a_state_file_is_not_restored(self):
113 touched = int(time.time()) // 60 - 5
114 path = os.path.join(self.tracker.dir, "state")
115 with open(path, "w") as handle:
116 handle.write("%s:%d:7\n%s:%d:7\n" % (BLOCKED.hex(), touched, ALLOWED.hex(), touched))
117
118 restarted = Tracker(self.config, self.files, extra_args=["-l", path])
119 try:
120 self.assertNotIn(BLOCKED, restarted.scrape(BLOCKED)[b"files"], "the blacklist was not in place when the state file was read")
121 self.assertIn(ALLOWED, restarted.scrape(ALLOWED)[b"files"], "the state file was not read at all")
122 restarted.stop()
123 finally:
124 restarted._cleanup()
125
126
99if __name__ == "__main__": 127if __name__ == "__main__":
100 unittest.main() 128 unittest.main()