diff options
| author | jvoisin | 2018-07-08 15:13:03 +0200 |
|---|---|---|
| committer | jvoisin | 2018-07-08 15:13:03 +0200 |
| commit | 3cd4f9111f4a050ff365bbe103993b3a89fdc29d (patch) | |
| tree | 89d2cb1d9e5ba5838d517e728bcfe82f682e871a | |
| parent | b5fcddd6a68b7e18d1e5521e7363046ca0e68667 (diff) | |
Bump coverage for torrent handling
Diffstat (limited to '')
| -rw-r--r-- | libmat2/torrent.py | 15 | ||||
| -rw-r--r-- | tests/test_corrupted_files.py | 20 |
2 files changed, 25 insertions, 10 deletions
diff --git a/libmat2/torrent.py b/libmat2/torrent.py index d614136..925ac55 100644 --- a/libmat2/torrent.py +++ b/libmat2/torrent.py | |||
| @@ -60,8 +60,6 @@ class _BencodeHandler(object): | |||
| 60 | def __decode_int(s: bytes) -> Tuple[int, bytes]: | 60 | def __decode_int(s: bytes) -> Tuple[int, bytes]: |
| 61 | s = s[1:] | 61 | s = s[1:] |
| 62 | next_idx = s.index(b'e') | 62 | next_idx = s.index(b'e') |
| 63 | if next_idx is None: | ||
| 64 | raise ValueError # missing suffix | ||
| 65 | if s.startswith(b'-0'): | 63 | if s.startswith(b'-0'): |
| 66 | raise ValueError # negative zero doesn't exist | 64 | raise ValueError # negative zero doesn't exist |
| 67 | elif s.startswith(b'0') and next_idx != 1: | 65 | elif s.startswith(b'0') and next_idx != 1: |
| @@ -70,16 +68,13 @@ class _BencodeHandler(object): | |||
| 70 | 68 | ||
| 71 | @staticmethod | 69 | @staticmethod |
| 72 | def __decode_string(s: bytes) -> Tuple[bytes, bytes]: | 70 | def __decode_string(s: bytes) -> Tuple[bytes, bytes]: |
| 73 | sep = s.index(b':') | 71 | colon = s.index(b':') |
| 74 | if set is None: | 72 | str_len = int(s[:colon]) |
| 75 | raise ValueError # missing suffix | 73 | print('S: %s' % s) |
| 76 | str_len = int(s[:sep]) | 74 | if s[0] == '0' and colon != 1: |
| 77 | if str_len < 0: | ||
| 78 | raise ValueError | ||
| 79 | elif s[0] == b'0' and sep != 1: | ||
| 80 | raise ValueError | 75 | raise ValueError |
| 81 | s = s[1:] | 76 | s = s[1:] |
| 82 | return s[sep:sep+str_len], s[sep+str_len:] | 77 | return s[colon:colon+str_len], s[colon+str_len:] |
| 83 | 78 | ||
| 84 | def __decode_list(self, s: bytes) -> Tuple[list, bytes]: | 79 | def __decode_list(self, s: bytes) -> Tuple[list, bytes]: |
| 85 | r = list() | 80 | r = list() |
diff --git a/tests/test_corrupted_files.py b/tests/test_corrupted_files.py index 776b0e9..a77acbc 100644 --- a/tests/test_corrupted_files.py +++ b/tests/test_corrupted_files.py | |||
| @@ -54,6 +54,26 @@ class TestCorruptedFiles(unittest.TestCase): | |||
| 54 | with self.assertRaises(ValueError): | 54 | with self.assertRaises(ValueError): |
| 55 | torrent.TorrentParser('./tests/data/clean.torrent') | 55 | torrent.TorrentParser('./tests/data/clean.torrent') |
| 56 | 56 | ||
| 57 | with open("./tests/data/clean.torrent", "w") as f: | ||
| 58 | f.write("i-0e") | ||
| 59 | with self.assertRaises(ValueError): | ||
| 60 | torrent.TorrentParser('./tests/data/clean.torrent') | ||
| 61 | |||
| 62 | with open("./tests/data/clean.torrent", "w") as f: | ||
| 63 | f.write("i00e") | ||
| 64 | with self.assertRaises(ValueError): | ||
| 65 | torrent.TorrentParser('./tests/data/clean.torrent') | ||
| 66 | |||
| 67 | with open("./tests/data/clean.torrent", "w") as f: | ||
| 68 | f.write("d01:AAAAAAAAA") | ||
| 69 | with self.assertRaises(ValueError): | ||
| 70 | torrent.TorrentParser('./tests/data/clean.torrent') | ||
| 71 | |||
| 72 | with open("./tests/data/clean.torrent", "w") as f: | ||
| 73 | f.write("1:aaa") | ||
| 74 | with self.assertRaises(ValueError): | ||
| 75 | torrent.TorrentParser('./tests/data/clean.torrent') | ||
| 76 | |||
| 57 | os.remove('./tests/data/clean.torrent') | 77 | os.remove('./tests/data/clean.torrent') |
| 58 | 78 | ||
| 59 | def test_odg(self): | 79 | def test_odg(self): |
