2016-10-20 18:23:39 +02:00
|
|
|
import os
|
|
|
|
import shutil
|
2018-02-12 20:19:39 +01:00
|
|
|
import tempfile
|
2018-05-05 03:12:43 +02:00
|
|
|
from hashlib import md5
|
2016-10-20 18:23:39 +02:00
|
|
|
from twisted.trial.unittest import TestCase
|
2018-02-13 18:45:22 +01:00
|
|
|
from twisted.internet import defer, threads
|
2019-01-21 21:55:50 +01:00
|
|
|
from lbrynet.conf import Config
|
2018-11-04 20:06:29 +01:00
|
|
|
from lbrynet.p2p.StreamDescriptor import StreamDescriptorIdentifier
|
|
|
|
from lbrynet.p2p.BlobManager import DiskBlobManager
|
|
|
|
from lbrynet.p2p.StreamDescriptor import get_sd_info
|
|
|
|
from lbrynet.p2p.RateLimiter import DummyRateLimiter
|
2019-01-07 08:52:53 +01:00
|
|
|
from lbrynet.extras.compat import f2d
|
2018-11-07 21:15:05 +01:00
|
|
|
from lbrynet.extras.daemon.PeerManager import PeerManager
|
2018-11-04 20:00:57 +01:00
|
|
|
from lbrynet.extras.daemon.storage import SQLiteStorage
|
2018-11-04 20:06:29 +01:00
|
|
|
from lbrynet.p2p.PaymentRateManager import OnlyFreePaymentsManager
|
2018-11-04 20:55:01 +01:00
|
|
|
from lbrynet.blob.EncryptedFileCreator import create_lbry_file
|
|
|
|
from lbrynet.blob.EncryptedFileManager import EncryptedFileManager
|
2018-07-07 00:34:33 +02:00
|
|
|
from tests import mocks
|
2016-10-20 18:23:39 +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
|
|
|
|
|
|
|
|
|
|
|
|
class TestStreamify(TestCase):
|
2018-02-12 20:19:39 +01:00
|
|
|
maxDiff = 5000
|
2018-07-24 18:45:32 +02:00
|
|
|
|
2019-01-07 08:52:53 +01:00
|
|
|
@defer.inlineCallbacks
|
2016-10-20 18:23:39 +02:00
|
|
|
def setUp(self):
|
|
|
|
self.session = None
|
|
|
|
self.lbry_file_manager = None
|
|
|
|
self.is_generous = True
|
2018-02-12 20:19:39 +01:00
|
|
|
self.db_dir = tempfile.mkdtemp()
|
|
|
|
self.blob_dir = os.path.join(self.db_dir, "blobfiles")
|
2019-01-21 21:55:50 +01:00
|
|
|
conf = Config(data_dir=self.blob_dir)
|
2018-08-02 20:34:02 +02:00
|
|
|
os.mkdir(self.blob_dir)
|
2018-07-24 18:45:32 +02:00
|
|
|
self.dht_node = FakeNode()
|
|
|
|
self.wallet = FakeWallet()
|
|
|
|
self.peer_manager = PeerManager()
|
|
|
|
self.peer_finder = FakePeerFinder(5553, self.peer_manager, 2)
|
|
|
|
self.rate_limiter = DummyRateLimiter()
|
|
|
|
self.sd_identifier = StreamDescriptorIdentifier()
|
2019-01-21 21:55:50 +01:00
|
|
|
self.storage = SQLiteStorage(conf, ':memory:')
|
2018-08-02 20:34:02 +02:00
|
|
|
self.blob_manager = DiskBlobManager(self.blob_dir, self.storage, self.dht_node._dataStore)
|
|
|
|
self.prm = OnlyFreePaymentsManager()
|
2018-08-03 15:36:03 +02:00
|
|
|
self.lbry_file_manager = EncryptedFileManager(
|
2019-01-21 21:55:50 +01:00
|
|
|
conf, self.peer_finder, self.rate_limiter, self.blob_manager, self.wallet, self.prm, self.storage,
|
2018-08-03 15:36:03 +02:00
|
|
|
self.sd_identifier
|
|
|
|
)
|
2019-01-07 08:52:53 +01:00
|
|
|
yield f2d(self.storage.open())
|
|
|
|
yield f2d(self.lbry_file_manager.setup())
|
2018-02-12 20:19:39 +01:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def tearDown(self):
|
|
|
|
lbry_files = self.lbry_file_manager.lbry_files
|
|
|
|
for lbry_file in lbry_files:
|
|
|
|
yield self.lbry_file_manager.delete_lbry_file(lbry_file)
|
2018-08-02 20:34:02 +02:00
|
|
|
yield self.lbry_file_manager.stop()
|
2019-01-07 08:52:53 +01:00
|
|
|
yield f2d(self.storage.close())
|
|
|
|
shutil.rmtree(self.db_dir, ignore_errors=True)
|
2018-02-12 20:19:39 +01:00
|
|
|
if os.path.exists("test_file"):
|
|
|
|
os.remove("test_file")
|
2016-10-20 18:23:39 +02:00
|
|
|
|
|
|
|
def test_create_stream(self):
|
|
|
|
|
|
|
|
def verify_equal(sd_info):
|
|
|
|
self.assertEqual(sd_info, test_create_stream_sd_file)
|
|
|
|
|
|
|
|
def verify_stream_descriptor_file(stream_hash):
|
2019-01-07 08:52:53 +01:00
|
|
|
d = f2d(get_sd_info(self.storage, stream_hash, True))
|
2016-10-20 18:23:39 +02:00
|
|
|
d.addCallback(verify_equal)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def iv_generator():
|
|
|
|
iv = 0
|
|
|
|
while 1:
|
|
|
|
iv += 1
|
2018-08-10 05:50:11 +02:00
|
|
|
yield b"%016d" % iv
|
2016-10-20 18:23:39 +02:00
|
|
|
|
|
|
|
def create_stream():
|
2018-08-10 06:55:28 +02:00
|
|
|
test_file = GenFile(5209343, bytes((i + 3) for i in range(0, 64, 6)))
|
2018-08-03 15:36:03 +02:00
|
|
|
d = create_lbry_file(
|
|
|
|
self.blob_manager, self.storage, self.prm, self.lbry_file_manager, "test_file", test_file,
|
2018-08-10 05:50:11 +02:00
|
|
|
key=b'0123456701234567', iv_generator=iv_generator()
|
2018-08-03 15:36:03 +02:00
|
|
|
)
|
2018-02-12 20:19:39 +01:00
|
|
|
d.addCallback(lambda lbry_file: lbry_file.stream_hash)
|
2016-10-20 18:23:39 +02:00
|
|
|
return d
|
|
|
|
|
2018-08-02 20:34:02 +02:00
|
|
|
d = create_stream()
|
2016-10-20 18:23:39 +02:00
|
|
|
d.addCallback(verify_stream_descriptor_file)
|
|
|
|
return d
|
|
|
|
|
2018-08-02 20:34:02 +02:00
|
|
|
@defer.inlineCallbacks
|
2016-10-20 18:23:39 +02:00
|
|
|
def test_create_and_combine_stream(self):
|
2018-08-10 06:55:28 +02:00
|
|
|
test_file = GenFile(53209343, bytes((i + 5) for i in range(0, 64, 6)))
|
2018-08-02 20:34:02 +02:00
|
|
|
lbry_file = yield create_lbry_file(self.blob_manager, self.storage, self.prm, self.lbry_file_manager,
|
|
|
|
"test_file", test_file)
|
2019-01-07 08:52:53 +01:00
|
|
|
sd_hash = yield f2d(self.storage.get_sd_blob_hash_for_stream(lbry_file.stream_hash))
|
2018-08-02 20:34:02 +02:00
|
|
|
self.assertTrue(lbry_file.sd_hash, sd_hash)
|
|
|
|
yield lbry_file.start()
|
2018-08-10 05:50:11 +02:00
|
|
|
f = open('test_file', 'rb')
|
2018-08-02 20:34:02 +02:00
|
|
|
hashsum = md5()
|
|
|
|
hashsum.update(f.read())
|
|
|
|
self.assertEqual(hashsum.hexdigest(), "68959747edc73df45e45db6379dd7b3b")
|