From d06c670a128899b0cbbbadec56b250c9e46fb462 Mon Sep 17 00:00:00 2001 From: Job Evers-Meltzer Date: Fri, 4 Nov 2016 11:34:42 -0500 Subject: [PATCH] Allow publishing with unicode filenames encodes unicode into a string before hashing. Also adds tests to verify that the fix works and that normal files can be made into EncryptedFiles --- .../lbryfilemanager/EncryptedFileCreator.py | 20 +++++-- tests/unit/lbryfilemanager/__init__.py | 0 .../test_EncryptedFileCreator.py | 59 +++++++++++++++++++ 3 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 tests/unit/lbryfilemanager/__init__.py create mode 100644 tests/unit/lbryfilemanager/test_EncryptedFileCreator.py diff --git a/lbrynet/lbryfilemanager/EncryptedFileCreator.py b/lbrynet/lbryfilemanager/EncryptedFileCreator.py index c9feaccf3..516259c82 100644 --- a/lbrynet/lbryfilemanager/EncryptedFileCreator.py +++ b/lbrynet/lbryfilemanager/EncryptedFileCreator.py @@ -37,9 +37,9 @@ class EncryptedFileStreamCreator(CryptStreamCreator): def _save_stream_info(self): stream_info_manager = self.lbry_file_manager.stream_info_manager - d = stream_info_manager.save_stream(self.stream_hash, binascii.hexlify(self.name), - binascii.hexlify(self.key), - binascii.hexlify(self.suggested_file_name), + d = stream_info_manager.save_stream(self.stream_hash, hexlify(self.name), + hexlify(self.key), + hexlify(self.suggested_file_name), self.blob_infos) return d @@ -68,9 +68,9 @@ class EncryptedFileStreamCreator(CryptStreamCreator): def _make_stream_hash(self): hashsum = get_lbry_hash_obj() - hashsum.update(binascii.hexlify(self.name)) - hashsum.update(binascii.hexlify(self.key)) - hashsum.update(binascii.hexlify(self.suggested_file_name)) + hashsum.update(hexlify(self.name)) + hashsum.update(hexlify(self.key)) + hashsum.update(hexlify(self.suggested_file_name)) hashsum.update(self._get_blobs_hashsum()) self.stream_hash = hashsum.hexdigest() @@ -155,3 +155,11 @@ def create_lbry_file(session, lbry_file_manager, file_name, file_handle, key=Non d = lbry_file_creator.setup() d.addCallback(lambda _: start_stream()) return d + + +def hexlify(str_or_unicode): + if isinstance(str_or_unicode, unicode): + strng = str_or_unicode.encode('utf-8') + else: + strng = str_or_unicode + return binascii.hexlify(strng) diff --git a/tests/unit/lbryfilemanager/__init__.py b/tests/unit/lbryfilemanager/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/unit/lbryfilemanager/test_EncryptedFileCreator.py b/tests/unit/lbryfilemanager/test_EncryptedFileCreator.py new file mode 100644 index 000000000..ae2efccc2 --- /dev/null +++ b/tests/unit/lbryfilemanager/test_EncryptedFileCreator.py @@ -0,0 +1,59 @@ +# -*- coding: utf-8 -*- +import shutil +import tempfile + +from Crypto.Cipher import AES +import mock +from twisted.trial import unittest + +from lbrynet.core import BlobManager +from lbrynet.core import Session +from lbrynet.core.server import DHTHashAnnouncer +from lbrynet.lbryfilemanager import EncryptedFileCreator +from lbrynet.lbryfilemanager import EncryptedFileManager + +from tests import mocks + + +MB = 2**20 + + +def iv_generator(): + while True: + yield '3' * AES.block_size + + +class CreateEncryptedFileTest(unittest.TestCase): + timeout = 5 + def setUp(self): + self.tmp_dir = tempfile.mkdtemp() + + def tearDown(self): + shutil.rmtree(self.tmp_dir) + + def create_file(self, filename): + session = mock.Mock(spec=Session.Session)(None, None) + hash_announcer = mock.Mock(spec=DHTHashAnnouncer.DHTHashAnnouncer)(None, None) + session.blob_manager = BlobManager.TempBlobManager(hash_announcer) + session.db_dir = self.tmp_dir + manager = mock.Mock(spec=EncryptedFileManager.EncryptedFileManager)() + handle = mocks.GenFile(3*MB, '1') + key = '2'*AES.block_size + return EncryptedFileCreator.create_lbry_file( + session, manager, filename, handle, key, iv_generator()) + + def test_can_create_file(self): + expected_stream_hash = ('41e6b247d923d191b154fb6f1b8529d6ddd6a73d65c357b1acb7' + '42dd83151fb66393a7709e9f346260a4f4db6de10c25') + filename = 'test.file' + d = self.create_file(filename) + d.addCallback(self.assertEqual, expected_stream_hash) + return d + + def test_can_create_file_with_unicode_filename(self): + expected_stream_hash = ('d1da4258f3ce12edb91d7e8e160d091d3ab1432c2e55a6352dce0' + '2fd5adb86fe144e93e110075b5865fff8617776c6c0') + filename = u'☃.file' + d = self.create_file(filename) + d.addCallback(self.assertEqual, expected_stream_hash) + return d