2018-07-31 19:20:25 +02:00
|
|
|
import json
|
2016-11-04 17:34:42 +01:00
|
|
|
from twisted.trial import unittest
|
2018-03-27 23:35:31 +02:00
|
|
|
from twisted.internet import defer
|
2016-11-04 17:34:42 +01:00
|
|
|
|
2018-07-31 19:20:25 +02:00
|
|
|
from cryptography.hazmat.primitives.ciphers.algorithms import AES
|
2018-11-04 20:06:29 +01:00
|
|
|
from lbrynet.p2p.StreamDescriptor import get_sd_info, BlobStreamDescriptorReader
|
|
|
|
from lbrynet.p2p.StreamDescriptor import StreamDescriptorIdentifier
|
|
|
|
from lbrynet.p2p.BlobManager import DiskBlobManager
|
|
|
|
from lbrynet.p2p.PeerManager import PeerManager
|
|
|
|
from lbrynet.p2p.RateLimiter import DummyRateLimiter
|
|
|
|
from lbrynet.p2p.PaymentRateManager import OnlyFreePaymentsManager
|
2018-11-04 20:00:57 +01:00
|
|
|
from lbrynet.extras.daemon.storage import SQLiteStorage
|
2018-11-04 20:55:01 +01:00
|
|
|
from lbrynet.blob import EncryptedFileCreator
|
|
|
|
from lbrynet.blob.EncryptedFileManager import EncryptedFileManager
|
2018-11-04 20:06:29 +01:00
|
|
|
from lbrynet.p2p.StreamDescriptor import JSONBytesEncoder
|
2018-07-06 22:16:58 +02:00
|
|
|
from tests import mocks
|
|
|
|
from tests.util import mk_db_and_blob_dir, rm_db_and_blob_dir
|
2016-11-04 17:34:42 +01:00
|
|
|
|
2018-08-02 20:34:02 +02:00
|
|
|
|
|
|
|
FakeNode = mocks.Node
|
|
|
|
FakeWallet = mocks.Wallet
|
|
|
|
FakePeerFinder = mocks.PeerFinder
|
|
|
|
FakeAnnouncer = mocks.Announcer
|
|
|
|
GenFile = mocks.GenFile
|
|
|
|
test_create_stream_sd_file = mocks.create_stream_sd_file
|
|
|
|
DummyBlobAvailabilityTracker = mocks.BlobAvailabilityTracker
|
|
|
|
|
2016-11-04 17:34:42 +01:00
|
|
|
MB = 2**20
|
|
|
|
|
2018-02-21 22:41:15 +01:00
|
|
|
|
2016-11-04 17:34:42 +01:00
|
|
|
def iv_generator():
|
|
|
|
while True:
|
2018-07-31 19:20:25 +02:00
|
|
|
yield b'3' * (AES.block_size // 8)
|
2016-11-04 17:34:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
class CreateEncryptedFileTest(unittest.TestCase):
|
|
|
|
timeout = 5
|
2018-02-21 22:41:15 +01:00
|
|
|
|
2016-11-04 17:34:42 +01:00
|
|
|
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()
|
2018-08-02 20:34:02 +02:00
|
|
|
self.wallet = FakeWallet()
|
|
|
|
self.peer_manager = PeerManager()
|
|
|
|
self.peer_finder = FakePeerFinder(5553, self.peer_manager, 2)
|
|
|
|
self.rate_limiter = DummyRateLimiter()
|
|
|
|
self.sd_identifier = StreamDescriptorIdentifier()
|
|
|
|
self.storage = SQLiteStorage(self.tmp_db_dir)
|
|
|
|
self.blob_manager = DiskBlobManager(self.tmp_blob_dir, self.storage)
|
|
|
|
self.prm = OnlyFreePaymentsManager()
|
|
|
|
self.lbry_file_manager = EncryptedFileManager(self.peer_finder, self.rate_limiter, self.blob_manager,
|
|
|
|
self.wallet, self.prm, self.storage, self.sd_identifier)
|
|
|
|
d = self.storage.setup()
|
|
|
|
d.addCallback(lambda _: self.lbry_file_manager.setup())
|
|
|
|
return d
|
2018-02-12 20:19:15 +01:00
|
|
|
|
2017-07-27 06:11:12 +02:00
|
|
|
@defer.inlineCallbacks
|
2016-11-04 17:34:42 +01:00
|
|
|
def tearDown(self):
|
2018-08-02 20:34:02 +02:00
|
|
|
yield self.lbry_file_manager.stop()
|
2017-07-27 06:11:12 +02:00
|
|
|
yield self.blob_manager.stop()
|
2018-08-02 20:34:02 +02:00
|
|
|
yield self.storage.stop()
|
2017-07-27 06:11:12 +02:00
|
|
|
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):
|
2018-08-10 06:55:28 +02:00
|
|
|
handle = mocks.GenFile(3*MB, b'1')
|
2018-07-13 06:21:45 +02:00
|
|
|
key = b'2' * (AES.block_size // 8)
|
2018-08-03 15:36:03 +02:00
|
|
|
out = yield EncryptedFileCreator.create_lbry_file(
|
|
|
|
self.blob_manager, self.storage, self.prm, self.lbry_file_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
|
|
|
|
2017-10-12 17:14:03 +02:00
|
|
|
@defer.inlineCallbacks
|
2016-11-04 17:34:42 +01:00
|
|
|
def test_can_create_file(self):
|
2018-08-10 05:50:11 +02:00
|
|
|
expected_stream_hash = "41e6b247d923d191b154fb6f1b8529d6ddd6a73d65c35" \
|
|
|
|
"7b1acb742dd83151fb66393a7709e9f346260a4f4db6de10c25"
|
2018-07-06 22:16:58 +02:00
|
|
|
expected_sd_hash = "40c485432daec586c1a2d247e6c08d137640a5af6e81f3f652" \
|
|
|
|
"3e62e81a2e8945b0db7c94f1852e70e371d917b994352c"
|
2016-11-04 17:34:42 +01:00
|
|
|
filename = 'test.file'
|
2018-02-12 20:19:15 +01:00
|
|
|
lbry_file = yield self.create_file(filename)
|
2018-08-02 20:34:02 +02:00
|
|
|
sd_hash = yield self.storage.get_sd_blob_hash_for_stream(lbry_file.stream_hash)
|
2018-02-12 20:19:15 +01:00
|
|
|
|
2018-02-21 22:41:15 +01:00
|
|
|
# read the sd blob file
|
|
|
|
sd_blob = self.blob_manager.blobs[sd_hash]
|
|
|
|
sd_reader = BlobStreamDescriptorReader(sd_blob)
|
|
|
|
sd_file_info = yield sd_reader.get_info()
|
|
|
|
|
|
|
|
# this comes from the database, the blobs returned are sorted
|
2018-08-02 20:34:02 +02:00
|
|
|
sd_info = yield get_sd_info(self.storage, lbry_file.stream_hash, include_blobs=True)
|
2018-07-13 06:21:45 +02:00
|
|
|
self.maxDiff = None
|
2018-07-31 19:20:25 +02:00
|
|
|
unicode_sd_info = json.loads(json.dumps(sd_info, sort_keys=True, cls=JSONBytesEncoder))
|
|
|
|
self.assertDictEqual(unicode_sd_info, sd_file_info)
|
2018-02-21 22:41:15 +01:00
|
|
|
self.assertEqual(sd_info['stream_hash'], expected_stream_hash)
|
|
|
|
self.assertEqual(len(sd_info['blobs']), 3)
|
|
|
|
self.assertNotEqual(sd_info['blobs'][0]['length'], 0)
|
|
|
|
self.assertNotEqual(sd_info['blobs'][1]['length'], 0)
|
|
|
|
self.assertEqual(sd_info['blobs'][2]['length'], 0)
|
2018-02-12 20:19:15 +01:00
|
|
|
self.assertEqual(expected_stream_hash, lbry_file.stream_hash)
|
|
|
|
self.assertEqual(sd_hash, lbry_file.sd_hash)
|
|
|
|
self.assertEqual(sd_hash, expected_sd_hash)
|
2017-10-12 17:14:03 +02:00
|
|
|
blobs = yield self.blob_manager.get_all_verified_blobs()
|
2018-02-12 20:19:15 +01:00
|
|
|
self.assertEqual(3, len(blobs))
|
2017-10-12 17:14:03 +02:00
|
|
|
num_should_announce_blobs = yield self.blob_manager.count_should_announce_blobs()
|
2018-02-12 20:19:15 +01:00
|
|
|
self.assertEqual(2, num_should_announce_blobs)
|
2016-11-04 17:34:42 +01:00
|
|
|
|
2017-10-12 17:14:03 +02:00
|
|
|
@defer.inlineCallbacks
|
2016-11-04 17:34:42 +01:00
|
|
|
def test_can_create_file_with_unicode_filename(self):
|
2018-08-10 05:50:11 +02:00
|
|
|
expected_stream_hash = ('d1da4258f3ce12edb91d7e8e160d091d3ab1432c2e55a6352dce0'
|
|
|
|
'2fd5adb86fe144e93e110075b5865fff8617776c6c0')
|
2018-10-18 12:42:45 +02:00
|
|
|
filename = '☃.file'
|
2018-02-12 20:19:15 +01:00
|
|
|
lbry_file = yield self.create_file(filename)
|
|
|
|
self.assertEqual(expected_stream_hash, lbry_file.stream_hash)
|