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.py128
1 files changed, 128 insertions, 0 deletions
diff --git a/tests/integration/test_accesslist.py b/tests/integration/test_accesslist.py
new file mode 100644
index 0000000..3ed8ddf
--- /dev/null
+++ b/tests/integration/test_accesslist.py
@@ -0,0 +1,128 @@
1"""The blacklist, loaded from a file at startup.
2
3A blacklisted info hash must be refused at both the announce and the scrape
4entry, 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.
6"""
7
8import os
9import struct
10import time
11import unittest
12
13from harness import Tracker, TrackerTestCase
14
15BLOCKED = b"\xb1" * 20
16ALLOWED = b"\xa1" * 20
17
18
19def listing(*hashes):
20 return b"".join(h.hex().upper().encode() + b"\n" for h in hashes)
21
22
23class Blacklist(TrackerTestCase):
24 config = "access.blacklist {dir}/blacklist\n"
25 files = {"blacklist": listing(BLOCKED)}
26
27 def test_a_blacklisted_hash_is_refused(self):
28 answer = self.tracker.announce(BLOCKED, port=6881, left=0)
29 self.assertIn(b"failure reason", answer)
30 self.assertIn(b"not authorized", answer[b"failure reason"])
31
32 def test_a_blacklisted_hash_never_becomes_a_torrent(self):
33 before = self.tracker.torrent_count()
34 self.tracker.announce(BLOCKED, port=6881, left=0)
35 self.assertEqual(self.tracker.torrent_count(), before, "the refused hash was stored anyway")
36
37 def test_any_other_hash_is_served(self):
38 answer = self.tracker.announce(ALLOWED, port=6881, left=0)
39 self.assertNotIn(b"failure reason", answer)
40 self.assertEqual(answer[b"complete"], 1)
41
42 def test_the_block_is_an_empty_announce_over_udp(self):
43 """An empty swarm with a long interval, not an error. BEP 15 binds a
44 client to the interval and says nothing about what to do with an
45 error, so an error puts the client on its own retry schedule, which
46 starts in the tens of seconds."""
47 sock, connection_id = self.tracker.udp_connect()
48 sock.close()
49 request = struct.pack(
50 ">QII20s20sQQQIIIiH", connection_id, 1, 7, BLOCKED, b"p" * 20, 0, 0, 0, 0, 0, 0, -1, 6881
51 )
52 answer = self.tracker.udp_raw(request)
53 self.assertIsNotNone(answer)
54 self.assertEqual(len(answer), 20, "header and counts, no peers behind them")
55 action, transaction, interval, leechers, seeders = struct.unpack(">IIIII", answer)
56 self.assertEqual(action, 1, "action 1 is the announce packet")
57 self.assertEqual(transaction, 7)
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")
61
62 def test_a_blocked_hash_is_not_stored_when_it_arrives_over_udp(self):
63 sock, connection_id = self.tracker.udp_connect()
64 sock.close()
65 self.tracker.udp_raw(
66 struct.pack(">QII20s20sQQQIIIiH", connection_id, 1, 8, BLOCKED, b"p" * 20, 0, 0, 0, 0, 0, 0, -1, 6881)
67 )
68 self.assertEqual(self.tracker.torrent_count(), 0)
69
70
71class BlacklistWithoutTrailingNewline(TrackerTestCase):
72 """A single hash with no newline after it used to size the list at zero
73 and then write into it, which took the tracker down before it listened."""
74
75 config = "access.blacklist {dir}/blacklist\n"
76 files = {"blacklist": BLOCKED.hex().upper().encode()}
77
78 def test_the_last_line_counts_even_without_its_newline(self):
79 answer = self.tracker.announce(BLOCKED, port=6881, left=0)
80 self.assertIn(b"failure reason", answer)
81
82 def test_and_the_tracker_still_serves_everything_else(self):
83 self.assertEqual(self.tracker.announce(ALLOWED, port=6881, left=0)[b"complete"], 1)
84
85
86class EmptyBlacklist(TrackerTestCase):
87 config = "access.blacklist {dir}/blacklist\n"
88 files = {"blacklist": b""}
89
90 def test_an_empty_file_blocks_nothing_and_starts_cleanly(self):
91 self.assertEqual(self.tracker.announce(BLOCKED, port=6881, left=0)[b"complete"], 1)
92 self.assertEqual(self.tracker.announce(ALLOWED, port=6882, left=0)[b"complete"], 1)
93
94
95class MissingBlacklist(TrackerTestCase):
96 """The file is named but absent: the tracker warns and serves everything,
97 because it may be created later."""
98
99 config = "access.blacklist {dir}/absent\n"
100
101 def test_a_missing_file_blocks_nothing(self):
102 self.assertEqual(self.tracker.announce(BLOCKED, port=6881, left=0)[b"complete"], 1)
103
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
127if __name__ == "__main__":
128 unittest.main()