diff options
Diffstat (limited to 'tests/integration/test_statefile.py')
| -rw-r--r-- | tests/integration/test_statefile.py | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/tests/integration/test_statefile.py b/tests/integration/test_statefile.py new file mode 100644 index 0000000..a3c8d59 --- /dev/null +++ b/tests/integration/test_statefile.py | |||
| @@ -0,0 +1,97 @@ | |||
| 1 | """The state dump and the -l switch that reads it back. | ||
| 2 | |||
| 3 | The second column is the minute a torrent was last touched, by the wall | ||
| 4 | clock, which is what the file has recorded for years and the only clock two | ||
| 5 | runs of the tracker agree on. | ||
| 6 | """ | ||
| 7 | |||
| 8 | import os | ||
| 9 | import time | ||
| 10 | import unittest | ||
| 11 | |||
| 12 | from harness import Tracker, TrackerTestCase | ||
| 13 | |||
| 14 | |||
| 15 | class StateFileRoundTrip(TrackerTestCase): | ||
| 16 | def dump(self): | ||
| 17 | return self.tracker.get("/stats?mode=statedump") | ||
| 18 | |||
| 19 | def test_a_dumped_torrent_comes_back_with_its_completed_count(self): | ||
| 20 | info_hash = self.hash_for("state-rt") | ||
| 21 | self.tracker.announce(info_hash, port=6001, left=0, event="completed") | ||
| 22 | self.assertEqual(self.tracker.scrape(info_hash)[b"files"][info_hash][b"downloaded"], 1) | ||
| 23 | |||
| 24 | path = os.path.join(self.tracker.dir, "state") | ||
| 25 | with open(path, "wb") as handle: | ||
| 26 | handle.write(self.dump()) | ||
| 27 | |||
| 28 | restarted = Tracker(extra_args=["-l", path]) | ||
| 29 | try: | ||
| 30 | files = restarted.scrape(info_hash)[b"files"] | ||
| 31 | self.assertIn(info_hash, files, "the torrent did not survive the restart") | ||
| 32 | entry = files[info_hash] | ||
| 33 | self.assertEqual(entry[b"downloaded"], 1, "the completed count did not survive") | ||
| 34 | self.assertEqual(entry[b"complete"], 0, "no peers are carried over, only the torrent") | ||
| 35 | self.assertEqual(entry[b"incomplete"], 0) | ||
| 36 | complaints = restarted.stop() | ||
| 37 | self.assertEqual(complaints, [], "the tracker reported:\n" + "\n".join(complaints)) | ||
| 38 | finally: | ||
| 39 | restarted._cleanup() | ||
| 40 | |||
| 41 | def test_a_torrent_older_than_the_tracker_would_keep_is_not_loaded(self): | ||
| 42 | """An age past the torrent timeout would be cleaned away on the first | ||
| 43 | sweep, so it is never built in the first place.""" | ||
| 44 | info_hash = self.hash_for("state-stale") | ||
| 45 | path = os.path.join(self.tracker.dir, "stale") | ||
| 46 | with open(path, "w") as handle: | ||
| 47 | handle.write("%s:%d:7\n" % (info_hash.hex(), 24 * 60 + 1)) | ||
| 48 | |||
| 49 | restarted = Tracker(extra_args=["-l", path]) | ||
| 50 | try: | ||
| 51 | self.assertNotIn(info_hash, restarted.scrape(info_hash)[b"files"]) | ||
| 52 | restarted.stop() | ||
| 53 | finally: | ||
| 54 | restarted._cleanup() | ||
| 55 | |||
| 56 | |||
| 57 | class StateFileWithNonsenseStamps(TrackerTestCase): | ||
| 58 | """The column is read as unsigned and handed on as a signed ot_time. A | ||
| 59 | value at or above 2^63 arrived negative, cleared the timeout check, and | ||
| 60 | put the torrent's base in the future, where the cleaner never reaches it. | ||
| 61 | At exactly 2^63 the subtraction itself overflowed.""" | ||
| 62 | |||
| 63 | def stamps(self): | ||
| 64 | now = int(time.time()) // 60 | ||
| 65 | return { | ||
| 66 | "overflow": 2**63, | ||
| 67 | "minus-one": 2**64 - 1, | ||
| 68 | "int64-max": 2**63 - 1, | ||
| 69 | "in-the-future": now + 60, | ||
| 70 | "past-timeout": now - (24 * 60 + 1), | ||
| 71 | } | ||
| 72 | |||
| 73 | def test_no_stamp_outside_the_timeout_builds_a_torrent(self): | ||
| 74 | stamps = self.stamps() | ||
| 75 | lines = ["%s:%d:7" % (self.hash_for(name).hex(), stamp) for name, stamp in stamps.items()] | ||
| 76 | lines.append("%s:%d:7" % (self.hash_for("honest").hex(), int(time.time()) // 60 - 5)) | ||
| 77 | |||
| 78 | path = os.path.join(self.tracker.dir, "nonsense") | ||
| 79 | with open(path, "w") as handle: | ||
| 80 | handle.write("\n".join(lines) + "\n") | ||
| 81 | |||
| 82 | restarted = Tracker(extra_args=["-l", path]) | ||
| 83 | try: | ||
| 84 | for name, stamp in stamps.items(): | ||
| 85 | self.assertNotIn(self.hash_for(name), restarted.scrape(self.hash_for(name))[b"files"], | ||
| 86 | "the line stamped %d was loaded" % stamp) | ||
| 87 | self.assertIn(self.hash_for("honest"), restarted.scrape(self.hash_for("honest"))[b"files"], | ||
| 88 | "the state file was not read at all") | ||
| 89 | self.assertEqual(restarted.torrent_count(), 1) | ||
| 90 | complaints = restarted.stop() | ||
| 91 | self.assertEqual(complaints, [], "the tracker reported:\n" + "\n".join(complaints)) | ||
| 92 | finally: | ||
| 93 | restarted._cleanup() | ||
| 94 | |||
| 95 | |||
| 96 | if __name__ == "__main__": | ||
| 97 | unittest.main() | ||
