diff options
Diffstat (limited to 'tests/integration/test_dynamic_accesslist.py')
| -rw-r--r-- | tests/integration/test_dynamic_accesslist.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/tests/integration/test_dynamic_accesslist.py b/tests/integration/test_dynamic_accesslist.py new file mode 100644 index 0000000..2072f5e --- /dev/null +++ b/tests/integration/test_dynamic_accesslist.py | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | """Hashes added to and removed from the blacklist while the tracker runs. | ||
| 2 | |||
| 3 | The two FIFOs are read by threads of their own, which is also what makes | ||
| 4 | this the test that drives those threads through a shutdown. | ||
| 5 | """ | ||
| 6 | |||
| 7 | import os | ||
| 8 | import time | ||
| 9 | import unittest | ||
| 10 | |||
| 11 | from harness import TrackerTestCase | ||
| 12 | |||
| 13 | BLOCKED = bytes(range(1, 21)) | ||
| 14 | |||
| 15 | |||
| 16 | def as_hex(info_hash): | ||
| 17 | return "".join("%02x" % b for b in info_hash) | ||
| 18 | |||
| 19 | |||
| 20 | class DynamicAccesslist(TrackerTestCase): | ||
| 21 | config = ("access.blacklist {dir}/blacklist\n" | ||
| 22 | "access.fifo_add {dir}/add.fifo\n" | ||
| 23 | "access.fifo_delete {dir}/delete.fifo\n") | ||
| 24 | files = {"blacklist": b""} | ||
| 25 | |||
| 26 | def feed(self, name, info_hash): | ||
| 27 | """Writing to the FIFO blocks until the reader has it, so this also | ||
| 28 | tells us the thread is alive.""" | ||
| 29 | with open(os.path.join(self.tracker.dir, name), "w") as fifo: | ||
| 30 | fifo.write("%s added by a test\n" % as_hex(info_hash)) | ||
| 31 | |||
| 32 | def wait_for(self, info_hash, blocked, patience=5.0): | ||
| 33 | """The reader thread picks the hash up on its own schedule, so this | ||
| 34 | waits for the answer to change rather than assuming it already has.""" | ||
| 35 | deadline = time.time() + patience | ||
| 36 | while time.time() < deadline: | ||
| 37 | answer = self.tracker.announce(info_hash) | ||
| 38 | if (b"failure reason" in answer) == blocked: | ||
| 39 | return True | ||
| 40 | time.sleep(0.05) | ||
| 41 | return False | ||
| 42 | |||
| 43 | def test_a_hash_added_after_a_reload_is_blocked(self): | ||
| 44 | """A reload leaves the add changeset an empty list rather than an | ||
| 45 | absent one. Adding against it walked a null array; the hash must be | ||
| 46 | blocked and the tracker must stay up.""" | ||
| 47 | OTHER = bytes(range(21, 41)) | ||
| 48 | self.feed("add.fifo", BLOCKED) | ||
| 49 | self.assertTrue(self.wait_for(BLOCKED, blocked=True), "the first hash was never blocked") | ||
| 50 | |||
| 51 | # The reload empties the add changeset, which unblocks the first hash. | ||
| 52 | # Wait for that before adding the second, so it lands on the empty list | ||
| 53 | # the bug walked, not on the changeset before the reload has cleared it. | ||
| 54 | self.tracker.reload() | ||
| 55 | self.assertTrue(self.wait_for(BLOCKED, blocked=False), "the reload never emptied the changeset") | ||
| 56 | |||
| 57 | self.feed("add.fifo", OTHER) | ||
| 58 | self.assertTrue(self.wait_for(OTHER, blocked=True), "the hash added after the reload was never blocked") | ||
| 59 | # tearDown's stop() surfaces any sanitizer complaint on the way out. | ||
| 60 | |||
| 61 | def test_a_hash_can_be_blocked_and_released_while_running(self): | ||
| 62 | self.assertNotIn(b"failure reason", self.tracker.announce(BLOCKED)) | ||
| 63 | |||
| 64 | self.feed("add.fifo", BLOCKED) | ||
| 65 | self.assertTrue(self.wait_for(BLOCKED, blocked=True), "the hash was never blocked") | ||
| 66 | |||
| 67 | self.feed("delete.fifo", BLOCKED) | ||
| 68 | self.assertTrue(self.wait_for(BLOCKED, blocked=False), "the hash was never released") | ||
