Skip to content

Commit

Permalink
Fix TorrentDef.get_files_with_length
Browse files Browse the repository at this point in the history
  • Loading branch information
egbertbouman committed Jul 1, 2020
1 parent 1079f92 commit 601f7e4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from tribler_core.tests.tools.common import TESTS_DATA_DIR, TORRENT_UBUNTU_FILE
from tribler_core.tests.tools.test_as_server import BaseTestCase
from tribler_core.tests.tools.tools import timeout
from tribler_core.utilities.path_util import mkdtemp
from tribler_core.utilities.path_util import Path, mkdtemp
from tribler_core.utilities.utilities import bdecode_compat

TRACKER = 'http://www.tribler.org/announce'
Expand Down Expand Up @@ -278,3 +278,26 @@ def test_get_name_as_unicode(self):
self.assertEqual(t.get_name_as_unicode(), name_unicode)
t.metainfo = {b'info': {b'name': b'test\xff' + name_bytes}}
self.assertEqual(t.get_name_as_unicode(), 'test?????????????')

def test_get_files_with_length(self):
name_bytes = b'\xe8\xaf\xad\xe8\xa8\x80\xe5\xa4\x84\xe7\x90\x86'
name_unicode = name_bytes.decode()
t = TorrentDef()
t.metainfo = {b'info': {b'files': [{b'path.utf-8': [name_bytes], b'length': 123},
{b'path.utf-8': [b'file.txt'], b'length': 456}]}}
self.assertEqual(t.get_files_with_length(), [(Path(name_unicode), 123),
(Path('file.txt'), 456)])

t.metainfo = {b'info': {b'files': [{b'path': [name_bytes], b'length': 123},
{b'path': [b'file.txt'], b'length': 456}]}}
self.assertEqual(t.get_files_with_length(), [(Path(name_unicode), 123),
(Path('file.txt'), 456)])

t.metainfo = {b'info': {b'files': [{b'path': [b'test\xff' + name_bytes], b'length': 123},
{b'path': [b'file.txt'], b'length': 456}]}}
self.assertEqual(t.get_files_with_length(), [(Path('test?????????????'), 123),
(Path('file.txt'), 456)])

t.metainfo = {b'info': {b'files': [{b'path.utf-8': [b'test\xff' + name_bytes], b'length': 123},
{b'path': [b'file.txt'], b'length': 456}]}}
self.assertEqual(t.get_files_with_length(), [(Path('file.txt'), 456)])
30 changes: 10 additions & 20 deletions src/tribler-core/tribler_core/modules/libtorrent/torrentdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,10 @@ def filter_characters(name):
def filter_character(char):
if 0 < char < 128:
return chr(char)
self._logger.debug("Bad character %s", bytes(char))
return u"?"
return u"".join([filter_character(char) for char in name])
return str(filter_characters(self.metainfo[b"info"][b"name"]))
self._logger.debug("Bad character 0x%X", char)
return "?"
return "".join([filter_character(char) for char in name])
return filter_characters(self.metainfo[b"info"][b"name"])
except UnicodeError:
pass

Expand Down Expand Up @@ -381,28 +381,18 @@ def _get_all_files_as_unicode_with_length(self):
except UnicodeError:
pass

# Try to convert the names in path to unicode,
# without specifying the encoding
try:
yield join(*[str(element) for element in file_dict[b"path"]]), file_dict[b"length"]
continue
except UnicodeError:
pass

# Convert the names in path to unicode by
# replacing out all characters that may -even
# remotely- cause problems with the '?' character
try:
def filter_characters(name):
def filter_character(char):
if 0 < ord(char) < 128:
return char
else:
self._logger.debug(
"Bad character filter %s, isalnum? %s", ord(char), char.isalnum())
return u"?"
return u"".join([filter_character(char) for char in name])
yield (join(*[str(filter_characters(element)) for element in file_dict[b"path"]]),
if 0 < char < 128:
return chr(char)
self._logger.debug("Bad character 0x%X", char)
return "?"
return "".join([filter_character(char) for char in name])
yield (join(*[filter_characters(element) for element in file_dict[b"path"]]),
file_dict[b"length"])
continue
except UnicodeError:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ async def test_check_torrent_health(self):
self.session.dlmgr.dht_health_manager = MockObject()
dht_health_dict = {"infohash": hexlify(infohash), "seeders": 1, "leechers": 2}
self.session.dlmgr.dht_health_manager.get_health = lambda *_, **__: succeed({"DHT": [dht_health_dict]})
self.session.dlmgr.get_channel_downloads = lambda: []

# Left for compatibility with other tests in this object
await self.udp_tracker.start()
Expand Down

0 comments on commit 601f7e4

Please sign in to comment.