Skip to content
Snippets Groups Projects
Commit 3cd4f911 authored by Julien (jvoisin) Voisin's avatar Julien (jvoisin) Voisin
Browse files

Bump coverage for torrent handling

parent b5fcddd6
No related branches found
No related tags found
No related merge requests found
...@@ -60,8 +60,6 @@ class _BencodeHandler(object): ...@@ -60,8 +60,6 @@ class _BencodeHandler(object):
def __decode_int(s: bytes) -> Tuple[int, bytes]: def __decode_int(s: bytes) -> Tuple[int, bytes]:
s = s[1:] s = s[1:]
next_idx = s.index(b'e') next_idx = s.index(b'e')
if next_idx is None:
raise ValueError # missing suffix
if s.startswith(b'-0'): if s.startswith(b'-0'):
raise ValueError # negative zero doesn't exist raise ValueError # negative zero doesn't exist
elif s.startswith(b'0') and next_idx != 1: elif s.startswith(b'0') and next_idx != 1:
...@@ -70,16 +68,13 @@ class _BencodeHandler(object): ...@@ -70,16 +68,13 @@ class _BencodeHandler(object):
@staticmethod @staticmethod
def __decode_string(s: bytes) -> Tuple[bytes, bytes]: def __decode_string(s: bytes) -> Tuple[bytes, bytes]:
sep = s.index(b':') colon = s.index(b':')
if set is None: str_len = int(s[:colon])
raise ValueError # missing suffix print('S: %s' % s)
str_len = int(s[:sep]) if s[0] == '0' and colon != 1:
if str_len < 0:
raise ValueError
elif s[0] == b'0' and sep != 1:
raise ValueError raise ValueError
s = s[1:] s = s[1:]
return s[sep:sep+str_len], s[sep+str_len:] return s[colon:colon+str_len], s[colon+str_len:]
def __decode_list(self, s: bytes) -> Tuple[list, bytes]: def __decode_list(self, s: bytes) -> Tuple[list, bytes]:
r = list() r = list()
......
...@@ -54,6 +54,26 @@ class TestCorruptedFiles(unittest.TestCase): ...@@ -54,6 +54,26 @@ class TestCorruptedFiles(unittest.TestCase):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
torrent.TorrentParser('./tests/data/clean.torrent') torrent.TorrentParser('./tests/data/clean.torrent')
with open("./tests/data/clean.torrent", "w") as f:
f.write("i-0e")
with self.assertRaises(ValueError):
torrent.TorrentParser('./tests/data/clean.torrent')
with open("./tests/data/clean.torrent", "w") as f:
f.write("i00e")
with self.assertRaises(ValueError):
torrent.TorrentParser('./tests/data/clean.torrent')
with open("./tests/data/clean.torrent", "w") as f:
f.write("d01:AAAAAAAAA")
with self.assertRaises(ValueError):
torrent.TorrentParser('./tests/data/clean.torrent')
with open("./tests/data/clean.torrent", "w") as f:
f.write("1:aaa")
with self.assertRaises(ValueError):
torrent.TorrentParser('./tests/data/clean.torrent')
os.remove('./tests/data/clean.torrent') os.remove('./tests/data/clean.torrent')
def test_odg(self): def test_odg(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment