2016-11-04 17:34:42 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
from Crypto.Cipher import AES
|
|
|
|
import mock
|
|
|
|
from twisted.trial import unittest
|
2017-07-27 06:11:12 +02:00
|
|
|
from twisted.internet import defer
|
2016-11-04 17:34:42 +01:00
|
|
|
|
|
|
|
from lbrynet.core import BlobManager
|
|
|
|
from lbrynet.core import Session
|
|
|
|
from lbrynet.core.server import DHTHashAnnouncer
|
2017-06-26 03:04:04 +02:00
|
|
|
from lbrynet.file_manager import EncryptedFileCreator
|
|
|
|
from lbrynet.file_manager import EncryptedFileManager
|
2016-11-04 17:34:42 +01:00
|
|
|
from tests import mocks
|
2017-07-27 06:11:12 +02:00
|
|
|
from tests.util import mk_db_and_blob_dir, rm_db_and_blob_dir
|
2016-11-04 17:34:42 +01:00
|
|
|
|
|
|
|
MB = 2**20
|
|
|
|
|
|
|
|
def iv_generator():
|
|
|
|
while True:
|
|
|
|
yield '3' * AES.block_size
|
|
|
|
|
|
|
|
|
|
|
|
class CreateEncryptedFileTest(unittest.TestCase):
|
|
|
|
timeout = 5
|
|
|
|
def setUp(self):
|
2017-01-17 04:23:20 +01:00
|
|
|
mocks.mock_conf_settings(self)
|
2017-07-27 06:11:12 +02:00
|
|
|
self.tmp_db_dir, self.tmp_blob_dir = mk_db_and_blob_dir()
|
2016-11-04 17:34:42 +01:00
|
|
|
|
2017-07-27 06:11:12 +02:00
|
|
|
@defer.inlineCallbacks
|
2016-11-04 17:34:42 +01:00
|
|
|
def tearDown(self):
|
2017-07-27 06:11:12 +02:00
|
|
|
yield self.blob_manager.stop()
|
|
|
|
rm_db_and_blob_dir(self.tmp_db_dir, self.tmp_blob_dir)
|
2016-11-04 17:34:42 +01:00
|
|
|
|
2017-09-13 06:15:57 +02:00
|
|
|
@defer.inlineCallbacks
|
2016-11-04 17:34:42 +01:00
|
|
|
def create_file(self, filename):
|
|
|
|
session = mock.Mock(spec=Session.Session)(None, None)
|
2017-02-13 19:53:55 +01:00
|
|
|
hash_announcer = DHTHashAnnouncer.DHTHashAnnouncer(None, None)
|
2017-07-27 06:11:12 +02:00
|
|
|
self.blob_manager = BlobManager.DiskBlobManager(hash_announcer, self.tmp_blob_dir, self.tmp_db_dir)
|
|
|
|
session.blob_manager = self.blob_manager
|
2017-09-13 06:15:57 +02:00
|
|
|
yield session.blob_manager.setup()
|
2017-07-27 06:11:12 +02:00
|
|
|
session.db_dir = self.tmp_db_dir
|
2016-11-04 17:34:42 +01:00
|
|
|
manager = mock.Mock(spec=EncryptedFileManager.EncryptedFileManager)()
|
|
|
|
handle = mocks.GenFile(3*MB, '1')
|
|
|
|
key = '2'*AES.block_size
|
2017-09-13 06:15:57 +02:00
|
|
|
out = yield EncryptedFileCreator.create_lbry_file(
|
2016-11-04 17:34:42 +01:00
|
|
|
session, manager, filename, handle, key, iv_generator())
|
2017-09-13 06:15:57 +02:00
|
|
|
defer.returnValue(out)
|
2016-11-04 17:34:42 +01:00
|
|
|
|
|
|
|
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
|