summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjvoisin2018-06-10 00:56:55 +0200
committerjvoisin2018-06-10 00:56:55 +0200
commit87bdcd1a9501e398bccb03fce606cf6925a75d16 (patch)
tree5b2687defbad65f8720b938fe7a658524228974c
parent3c56fa32375ff8df087d15e8649b661682bbb150 (diff)
Improve a bit our coverage wrt. torrent files handling
-rw-r--r--libmat2/torrent.py10
-rw-r--r--tests/test_libmat2.py14
2 files changed, 22 insertions, 2 deletions
diff --git a/libmat2/torrent.py b/libmat2/torrent.py
index f5935e6..b598065 100644
--- a/libmat2/torrent.py
+++ b/libmat2/torrent.py
@@ -1,4 +1,6 @@
1import logging
1from typing import Union, Tuple, Dict 2from typing import Union, Tuple, Dict
3
2from . import abstract 4from . import abstract
3 5
4 6
@@ -58,6 +60,8 @@ class _BencodeHandler(object):
58 def __decode_int(s: bytes) -> Tuple[int, bytes]: 60 def __decode_int(s: bytes) -> Tuple[int, bytes]:
59 s = s[1:] 61 s = s[1:]
60 next_idx = s.index(b'e') 62 next_idx = s.index(b'e')
63 if next_idx is None:
64 raise ValueError # missing suffix
61 if s.startswith(b'-0'): 65 if s.startswith(b'-0'):
62 raise ValueError # negative zero doesn't exist 66 raise ValueError # negative zero doesn't exist
63 elif s.startswith(b'0') and next_idx != 1: 67 elif s.startswith(b'0') and next_idx != 1:
@@ -67,6 +71,8 @@ class _BencodeHandler(object):
67 @staticmethod 71 @staticmethod
68 def __decode_string(s: bytes) -> Tuple[bytes, bytes]: 72 def __decode_string(s: bytes) -> Tuple[bytes, bytes]:
69 sep = s.index(b':') 73 sep = s.index(b':')
74 if set is None:
75 raise ValueError # missing suffix
70 str_len = int(s[:sep]) 76 str_len = int(s[:sep])
71 if str_len < 0: 77 if str_len < 0:
72 raise ValueError 78 raise ValueError
@@ -119,9 +125,9 @@ class _BencodeHandler(object):
119 try: 125 try:
120 r, l = self.__decode_func[s[0]](s) 126 r, l = self.__decode_func[s[0]](s)
121 except (IndexError, KeyError, ValueError) as e: 127 except (IndexError, KeyError, ValueError) as e:
122 print("not a valid bencoded string: %s" % e) 128 logging.debug("Not a valid bencoded string: %s" % e)
123 return None 129 return None
124 if l != b'': 130 if l != b'':
125 print("invalid bencoded value (data after valid prefix)") 131 logging.debug("Invalid bencoded value (data after valid prefix)")
126 return None 132 return None
127 return r 133 return r
diff --git a/tests/test_libmat2.py b/tests/test_libmat2.py
index 3d08dea..7deeadc 100644
--- a/tests/test_libmat2.py
+++ b/tests/test_libmat2.py
@@ -57,6 +57,20 @@ class TestCorruptedFiles(unittest.TestCase):
57 images.PNGParser('./tests/data/clean.pdf') 57 images.PNGParser('./tests/data/clean.pdf')
58 os.remove('./tests/data/clean.pdf') 58 os.remove('./tests/data/clean.pdf')
59 59
60 def test_torrent(self):
61 shutil.copy('./tests/data/dirty.png', './tests/data/clean.torrent')
62 p = torrent.TorrentParser('./tests/data/clean.torrent')
63 self.assertFalse(p.remove_all())
64 expected = {'Unknown meta': 'Unable to parse torrent file "./tests/data/clean.torrent".'}
65 self.assertEqual(p.get_meta(), expected)
66
67 with open("./tests/data/clean.torrent", "a") as f:
68 f.write("trailing garbage")
69 p = torrent.TorrentParser('./tests/data/clean.torrent')
70 self.assertEqual(p.get_meta(), expected)
71
72 os.remove('./tests/data/clean.torrent')
73
60class TestGetMeta(unittest.TestCase): 74class TestGetMeta(unittest.TestCase):
61 def test_pdf(self): 75 def test_pdf(self):
62 p = pdf.PDFParser('./tests/data/dirty.pdf') 76 p = pdf.PDFParser('./tests/data/dirty.pdf')