From 83cd3bb631131a116f024cfacfd687e8c4855fd1 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Fri, 6 Nov 2026 08:28:16 +0100 Subject: Bound what load_state will accept for an age --- tests/integration/test_statefile.py | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'tests') diff --git a/tests/integration/test_statefile.py b/tests/integration/test_statefile.py index 9f6db01..21eefae 100644 --- a/tests/integration/test_statefile.py +++ b/tests/integration/test_statefile.py @@ -52,3 +52,43 @@ class StateFileRoundTrip(TrackerTestCase): restarted.stop() finally: restarted._cleanup() + + +class StateFileWithNonsenseAges(TrackerTestCase): + """The age column is read as unsigned and handed on as a signed ot_time. + A value at or above 2^63 arrived negative, cleared the timeout check, and + put the torrent's base in the future, where the cleaner never reaches it. + At exactly 2^63 the subtraction itself overflowed.""" + + AGES = { + "overflow": 2**63, + "wrapped": 2**63 + 60 * 24, + "minus-one": 2**64 - 1, + "int64-max": 2**63 - 1, + "past-timeout": 60 * 24 + 1, + } + + def test_no_age_outside_the_timeout_builds_a_torrent(self): + lines = ["%s:%d:7" % (self.hash_for(name).hex(), age) for name, age in self.AGES.items()] + lines.append("%s:5:7" % self.hash_for("honest").hex()) + + path = os.path.join(self.tracker.dir, "nonsense") + with open(path, "w") as handle: + handle.write("\n".join(lines) + "\n") + + restarted = Tracker(extra_args=["-l", path]) + try: + for name in self.AGES: + self.assertNotIn(self.hash_for(name), restarted.scrape(self.hash_for(name))[b"files"], + "the line with age %d was loaded" % self.AGES[name]) + self.assertIn(self.hash_for("honest"), restarted.scrape(self.hash_for("honest"))[b"files"], + "the state file was not read at all") + self.assertEqual(restarted.torrent_count(), 1) + complaints = restarted.stop() + self.assertEqual(complaints, [], "the tracker reported:\n" + "\n".join(complaints)) + finally: + restarted._cleanup() + + +if __name__ == "__main__": + unittest.main() -- cgit v1.3