Rev452, Auto hide no peers found info message, Raise error on invalid file location, Test PEX, Test invalid file downloads, Test peer download and ping

This commit is contained in:
HelloZeroNet 2015-09-28 00:22:27 +02:00
parent 3173313ca2
commit 39413b9755
7 changed files with 128 additions and 7 deletions

View file

@ -1,11 +1,15 @@
import cStringIO as StringIO
import pytest
import time
from Connection import ConnectionServer
from Connection import Connection
from File import FileServer
@pytest.mark.usefixtures("resetSettings")
@pytest.mark.usefixtures("resetTempSettings")
class TestFileRequest:
def testGetFile(self, file_server, site):
file_server.ip_incoming = {} # Reset flood protection
@ -17,6 +21,18 @@ class TestFileRequest:
response = connection.request("getFile", {"site": site.address, "inner_path": "content.json", "location": 0})
assert "sign" in response["body"]
# Invalid file
response = connection.request("getFile", {"site": site.address, "inner_path": "invalid.file", "location": 0})
assert "No such file or directory" in response["error"]
# Location over size
response = connection.request("getFile", {"site": site.address, "inner_path": "content.json", "location": 1024 * 1024})
assert "File read error" in response["error"]
# Stream from parent dir
response = connection.request("getFile", {"site": site.address, "inner_path": "../users.json", "location": 0})
assert "File not allowed" in response["error"]
connection.close()
client.stop()
@ -31,5 +47,46 @@ class TestFileRequest:
assert "stream_bytes" in response
assert "sign" in buff.getvalue()
# Invalid file
buff = StringIO.StringIO()
response = connection.request("streamFile", {"site": site.address, "inner_path": "invalid.file", "location": 0}, buff)
assert "No such file or directory" in response["error"]
# Location over size
buff = StringIO.StringIO()
response = connection.request(
"streamFile", {"site": site.address, "inner_path": "content.json", "location": 1024 * 1024}, buff
)
assert "File read error" in response["error"]
# Stream from parent dir
buff = StringIO.StringIO()
response = connection.request("streamFile", {"site": site.address, "inner_path": "../users.json", "location": 0}, buff)
assert "File not allowed" in response["error"]
connection.close()
client.stop()
def testPex(self, file_server, site, site_temp):
file_server.sites[site.address] = site
client = FileServer("127.0.0.1", 1545)
client.sites[site_temp.address] = site_temp
site_temp.connection_server = client
connection = client.getConnection("127.0.0.1", 1544)
# Add new fake peer to site
fake_peer = site.addPeer("1.2.3.4", 11337, return_peer=True)
# Add fake connection to it
fake_peer.connection = Connection(file_server, "1.2.3.4", 11337)
fake_peer.connection.last_recv_time = time.time()
assert fake_peer in site.getConnectablePeers()
# Add file_server as peer to client
peer_file_server = site_temp.addPeer("127.0.0.1", 1544)
assert "1.2.3.4:11337" not in site_temp.peers
assert peer_file_server.pex()
assert "1.2.3.4:11337" in site_temp.peers
connection.close()
client.stop()

54
src/Test/TestPeer.py Normal file
View file

@ -0,0 +1,54 @@
import cStringIO as StringIO
import pytest
import time
from Connection import ConnectionServer
from Connection import Connection
from File import FileServer
@pytest.mark.usefixtures("resetSettings")
@pytest.mark.usefixtures("resetTempSettings")
class TestFileRequest:
def testPing(self, file_server, site, site_temp):
file_server.ip_incoming = {} # Reset flood protection
file_server.sites[site.address] = site
client = FileServer("127.0.0.1", 1545)
client.sites[site_temp.address] = site_temp
site_temp.connection_server = client
connection = client.getConnection("127.0.0.1", 1544)
# Add file_server as peer to client
peer_file_server = site_temp.addPeer("127.0.0.1", 1544)
assert peer_file_server.ping()
assert peer_file_server in site_temp.peers.values()
peer_file_server.remove()
assert peer_file_server not in site_temp.peers.values()
connection.close()
client.stop()
def testDownloadFile(self, file_server, site, site_temp):
file_server.ip_incoming = {} # Reset flood protection
file_server.sites[site.address] = site
client = FileServer("127.0.0.1", 1545)
client.sites[site_temp.address] = site_temp
site_temp.connection_server = client
connection = client.getConnection("127.0.0.1", 1544)
# Add file_server as peer to client
peer_file_server = site_temp.addPeer("127.0.0.1", 1544)
# Testing streamFile
buff = peer_file_server.streamFile(site_temp.address, "content.json")
assert "sign" in buff.getvalue()
# Testing getFile
buff = peer_file_server.getFile(site_temp.address, "content.json")
assert "sign" in buff.getvalue()
connection.close()
client.stop()

View file

@ -127,5 +127,8 @@ def file_server(request):
gevent.spawn(lambda: ConnectionServer.start(file_server))
time.sleep(0) # Port opening
assert file_server.running
def stop():
file_server.stop()
request.addfinalizer(stop)
return file_server