Simple tests for Connections, SiteManager delete test, ZeroName updater allow domain names starting with numbers, check subdomains validity

This commit is contained in:
HelloZeroNet 2015-09-22 12:16:29 +02:00
parent 54c367cac8
commit 891c5cc34a
5 changed files with 57 additions and 4 deletions

View file

@ -29,7 +29,7 @@ def processNameOp(domain, value):
if not isinstance(data["zeronet"], dict): if not isinstance(data["zeronet"], dict):
print "Not dict: ", data["zeronet"] print "Not dict: ", data["zeronet"]
return False return False
if not re.match("^[a-z]([a-z0-9-]{0,62}[a-z0-9])?$", domain): if not re.match("^[a-z0-9]([a-z0-9-]{0,62}[a-z0-9])?$", domain):
print "Invalid domain: ", domain print "Invalid domain: ", domain
return False return False
@ -45,7 +45,10 @@ def processNameOp(domain, value):
address = re.sub("[^A-Za-z0-9]", "", address) address = re.sub("[^A-Za-z0-9]", "", address)
print subdomain, domain, "->", address print subdomain, domain, "->", address
if subdomain: if subdomain:
names["%s.%s.bit" % (subdomain, domain)] = address if re.match("^[a-z0-9]([a-z0-9-]{0,62}[a-z0-9])?$", subdomain):
names["%s.%s.bit" % (subdomain, domain)] = address
else:
print "Invalid subdomain:", domain, subdomain
else: else:
names["%s.bit" % domain] = address names["%s.bit" % domain] = address
@ -153,4 +156,4 @@ while 1:
processBlock(block_id) processBlock(block_id)
config["lastprocessed"] = last_block config["lastprocessed"] = last_block
open(config_path, "w").write(json.dumps(config, indent=2)) open(config_path, "w").write(json.dumps(config, indent=1))

View file

@ -0,0 +1,31 @@
import time
from Crypt import CryptConnection
class TestConnection:
def testSslConnection(self, connection_server):
server = connection_server
assert server.running
# Connect to myself
connection = server.getConnection("127.0.0.1", 1544)
assert connection.handshake
assert connection.crypt
# Close connection
connection.close()
time.sleep(0.01)
assert len(server.connections) == 0
def testRawConnection(self, connection_server):
server = connection_server
crypt_supported_bk = CryptConnection.manager.crypt_supported
CryptConnection.manager.crypt_supported = []
connection = server.getConnection("127.0.0.1", 1544)
assert not connection.crypt
# Close connection
connection.close()
time.sleep(0.01)
assert len(server.connections) == 0

View file

@ -2,6 +2,7 @@ import shutil
import os import os
import pytest import pytest
from Site import SiteManager
@pytest.mark.usefixtures("resetSettings") @pytest.mark.usefixtures("resetSettings")
@ -56,3 +57,8 @@ class TestSite:
# Delete created files # Delete created files
new_site.storage.deleteFiles() new_site.storage.deleteFiles()
assert not os.path.isdir("src/Test/testdata/159EGD5srUsMP97UpcLy8AtKQbQLK2AbbL") assert not os.path.isdir("src/Test/testdata/159EGD5srUsMP97UpcLy8AtKQbQLK2AbbL")
# Delete from site registry
assert new_site.address in SiteManager.site_manager.sites
SiteManager.site_manager.delete(new_site.address)
assert new_site.address not in SiteManager.site_manager.sites

View file

@ -1,6 +1,7 @@
import os import os
import sys import sys
import urllib import urllib
import time
import pytest import pytest
@ -22,6 +23,11 @@ config.data_dir = "src/Test/testdata" # Use test data for unittests
from Site import Site from Site import Site
from User import UserManager from User import UserManager
from Connection import ConnectionServer
from Crypt import CryptConnection
import gevent
from gevent import monkey
monkey.patch_all(thread=False)
@pytest.fixture(scope="session") @pytest.fixture(scope="session")
def resetSettings(request): def resetSettings(request):
@ -70,3 +76,10 @@ def site_url():
except Exception, err: except Exception, err:
raise pytest.skip("Test requires zeronet client running: %s" % err) raise pytest.skip("Test requires zeronet client running: %s" % err)
return SITE_URL return SITE_URL
@pytest.fixture(scope="session")
def connection_server():
connection_server = ConnectionServer("127.0.0.1", 1544)
gevent.spawn(connection_server.start)
time.sleep(0) # Port opening
return connection_server

View file

@ -1,5 +1,5 @@
[pytest] [pytest]
python_files = Test*.py python_files = Test*.py
addopts = -rsxX -v addopts = -rsxX -v --durations=6
markers = markers =
webtest: mark a test as a webtest. webtest: mark a test as a webtest.