2018-09-30 14:12:02 +00:00
|
|
|
import os
|
|
|
|
import hashlib
|
2018-10-06 15:01:03 +00:00
|
|
|
import asyncio
|
|
|
|
import uvloop
|
2018-10-07 16:23:27 +00:00
|
|
|
from p2pfs import Peer, Tracker
|
2018-10-06 15:01:03 +00:00
|
|
|
|
|
|
|
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
|
2018-09-30 14:12:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def fmd5(fname):
|
|
|
|
""" calculate the md5 value of a file
|
|
|
|
:param fname: file name
|
|
|
|
:return: md5 value.
|
|
|
|
"""
|
|
|
|
hash_md5 = hashlib.md5()
|
|
|
|
with open(fname, "rb") as f:
|
|
|
|
for chunk in iter(lambda: f.read(4096), b""):
|
|
|
|
hash_md5.update(chunk)
|
|
|
|
return hash_md5.hexdigest()
|
|
|
|
|
|
|
|
|
2018-10-02 01:47:53 +00:00
|
|
|
def test_server_refused():
|
|
|
|
peer = Peer('localhost', 0, 'localhost', 8880)
|
2018-10-06 15:01:03 +00:00
|
|
|
started = asyncio.get_event_loop().run_until_complete(peer.start())
|
2018-10-02 01:47:53 +00:00
|
|
|
assert not started
|
|
|
|
|
|
|
|
|
2018-09-30 14:12:02 +00:00
|
|
|
def test_main():
|
2018-10-08 03:19:37 +00:00
|
|
|
peers = tuple(Peer('localhost', 0, 'localhost', 8880) for _ in range(3))
|
2018-09-30 14:12:02 +00:00
|
|
|
tracker = Tracker('localhost', 8880)
|
|
|
|
with open('test_small_file', 'wb') as fout:
|
|
|
|
fout.write(os.urandom(1000))
|
|
|
|
|
|
|
|
with open('test_big_file', 'wb') as fout:
|
|
|
|
# write 500MB random data into the file
|
|
|
|
for _ in range(500):
|
|
|
|
fout.write(os.urandom(1000 * 1000))
|
2018-10-06 18:18:23 +00:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
tracker_started = loop.run_until_complete(tracker.start())
|
2018-10-08 03:27:22 +00:00
|
|
|
# spawn peers concurrently
|
2018-10-08 03:19:37 +00:00
|
|
|
peers_started = \
|
2018-10-08 14:41:25 +00:00
|
|
|
loop.run_until_complete(asyncio.gather(*[peer.start() for peer in peers]))
|
2018-10-08 03:19:37 +00:00
|
|
|
assert tracker_started and all(peers_started)
|
2018-10-01 19:17:53 +00:00
|
|
|
|
2018-09-30 14:12:02 +00:00
|
|
|
# peer1 publish small file and peer2 downloads it
|
2018-10-08 03:19:37 +00:00
|
|
|
loop.run_until_complete(peers[0].publish('test_small_file'))
|
2018-09-30 14:12:02 +00:00
|
|
|
file_list = tracker.file_list()
|
|
|
|
assert 'test_small_file' in file_list
|
|
|
|
assert file_list['test_small_file']['size'] == 1000
|
2018-10-08 03:19:37 +00:00
|
|
|
file_list = loop.run_until_complete(peers[1].list_file())
|
2018-09-30 14:12:02 +00:00
|
|
|
assert 'test_small_file' in file_list
|
2018-10-04 04:01:22 +00:00
|
|
|
|
|
|
|
def reporthook(chunk_num, chunk_size, total_size):
|
|
|
|
reporthook.value = (chunk_num, total_size)
|
|
|
|
|
2018-10-08 03:27:22 +00:00
|
|
|
# download small file
|
2018-10-06 18:18:23 +00:00
|
|
|
result, msg = loop.run_until_complete(
|
2018-10-08 03:19:37 +00:00
|
|
|
peers[1].download('test_small_file', 'downloaded_small_file', reporthook=reporthook)
|
2018-10-06 18:18:23 +00:00
|
|
|
)
|
2018-09-30 14:12:02 +00:00
|
|
|
assert result is True
|
|
|
|
assert os.path.exists('downloaded_small_file')
|
|
|
|
assert fmd5('test_small_file') == fmd5('downloaded_small_file')
|
2018-10-04 04:01:22 +00:00
|
|
|
assert reporthook.value == (1, 1000)
|
2018-09-30 14:12:02 +00:00
|
|
|
|
2018-10-08 03:27:22 +00:00
|
|
|
# publish big file
|
2018-10-08 03:19:37 +00:00
|
|
|
loop.run_until_complete(peers[1].publish('test_big_file'))
|
2018-09-30 14:12:02 +00:00
|
|
|
file_list = tracker.file_list()
|
|
|
|
assert 'test_big_file' in file_list and 'test_small_file' in file_list
|
|
|
|
assert file_list['test_big_file']['size'] == 500 * 1000 * 1000
|
2018-10-08 03:19:37 +00:00
|
|
|
file_list = loop.run_until_complete(peers[0].list_file())
|
2018-09-30 14:12:02 +00:00
|
|
|
assert 'test_big_file' in file_list and 'test_small_file' in file_list
|
2018-10-08 03:27:22 +00:00
|
|
|
|
|
|
|
# download from single peer
|
2018-10-08 03:19:37 +00:00
|
|
|
result, msg = loop.run_until_complete(peers[0].download('test_big_file', 'downloaded_big_file'))
|
2018-09-30 14:12:02 +00:00
|
|
|
assert result is True
|
|
|
|
assert os.path.exists('downloaded_big_file')
|
|
|
|
assert fmd5('test_big_file') == fmd5('downloaded_big_file')
|
2018-10-08 03:27:22 +00:00
|
|
|
|
|
|
|
# test download from multiple peers
|
|
|
|
result, msg = loop.run_until_complete(peers[2].download('test_big_file', 'downloaded_big_file_from_multiple_peers'))
|
|
|
|
assert result is True
|
|
|
|
assert os.path.exists('downloaded_big_file_from_multiple_peers')
|
|
|
|
assert fmd5('test_big_file') == fmd5('downloaded_big_file_from_multiple_peers')
|
2018-09-30 14:12:02 +00:00
|
|
|
os.remove('test_small_file')
|
|
|
|
os.remove('test_big_file')
|
|
|
|
os.remove('downloaded_small_file')
|
|
|
|
os.remove('downloaded_big_file')
|
2018-10-08 03:27:22 +00:00
|
|
|
os.remove('downloaded_big_file_from_multiple_peers')
|
2018-10-08 03:19:37 +00:00
|
|
|
loop.run_until_complete(asyncio.wait({peer.stop() for peer in peers}))
|
2018-10-07 16:07:18 +00:00
|
|
|
loop.run_until_complete(tracker.stop())
|
2018-10-06 18:18:23 +00:00
|
|
|
loop.close()
|