Skip to content
Permalink
Browse files

Fixes broken test_tcps_tests

* Added secure mode to echoserver and telnet examples

* [WIP]: Implemented a synchronous test client to work-around test_tcps_basic
  • Loading branch information...
prologic committed Apr 2, 2016
1 parent 884b071 commit 4869430931a2caf1b42b220da8e7dca3c062e585
Showing with 125 additions and 40 deletions.
  1. +37 −0 examples/cert.pem
  2. +1 −1 examples/echoserver.py
  3. +7 −1 examples/telnet.py
  4. +1 −3 tests/net/server.py
  5. +79 −35 tests/net/test_tcp.py
@@ -0,0 +1,37 @@
-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgQC9B7hV67bzhLA//STrZUIi0iHD4WFtftOhvj+xiHRNnYw0+r+4
WdQv1YiL+ab03pn/J9R1SQuOGwYDVPQvYX+qEFVRUFP9yvXIQl7PG40HQzfs8lJz
hnmI+64HJT//oJ9e7PiyDHLfFH1FuCqSy9RlvzOd4hmydX9J3VxFFzrpJQIDAQAB
AoGAHhGxT/Gb+6a6xqMFEXDdEV7twhQDBIDtN0hlJ192aLZMDE1q2+9mImnMO7/t
v/v88Sqr0DBbZzKDRVppMXRH80ZtnmMu3/3kUCtA3WAbKxyFpIiXGv/NAUHZe5Gz
rua+z3lUvmt6CmwMm2ReB70Q61zxr9q4HjrjYI82dtJ4M40CQQDnGujxdBdbPmiR
oc8mcShfmPNP7igQrUkf/DpB0GWnLWdA97mmXLw4jHXpHy9gm3wGc+9uOi0Ex8Ml
1t9xAGFjAkEA0WSGwG45d5dmYV8Oa/9UsY5/F6hAlYAAI1TxRsKcl2YTqaQastac
glV1GSUrgGw/8UBvdKKS2REF7cpAkiQV1wJAPtQhCiuOgf7YfOcpowDWgg7Z7xwH
Bmml3K08xVG7oRSF4rK2ZRUHErSVBbi1r6T1tedk62mjfY41bp8ZBeadkwJAD7AG
YHhhmdIf+3+Rpwm0ILFaWD1kyU6TtBHzGagO71DYfEctMOTfSOx6H24nejGiAMMh
Fo3vjo+18ADNIaXOdQJAYdm+dUdyVaW2IDUi7ew0shyKT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BFhlhZG1p
bkBzaG9ydGNpcmN1aXQubmV0LmF1ggkA3SazIvkSX+MwDAYDVR0TBAUwAwEB/zAN
BgkqhkiG9w0BAQUFAAOBgQAokSGDpbFV2osC8nM8K12vheeDBVDHGxOaENXGVIm8
SWPXsaIUsm6JQx0wm/eouWRPbNJkOBwBrNCls1oMmdxdxG8mBh+kAMWUkdVeuT2H
lCo9BRJnhUr4L6poJ7ORzL2oUilGZNwONpHGY0cWzFG8/tOoRJsfKZm23bwXbIxv
Hw==
-----END CERTIFICATE-----
@@ -32,6 +32,6 @@ def on_read(self, sock, data):

# Start and "run" the system.
# Bind to port 0.0.0.0:8000
app = EchoServer(("0.0.0.0", 8000))
app = EchoServer(("0.0.0.0", 8000), secure=True, certfile="cert.pem")
Debugger().register(app)
app.run()
@@ -42,6 +42,12 @@
def parse_options():
parser = OptionParser(usage=USAGE, version=VERSION)

parser.add_option(
"-s", "--secure",
action="store_true", default=False, dest="secure",
help="Enable secure mode"
)

parser.add_option(
"-u", "--udp",
action="store_true", default=False, dest="udp",
@@ -98,7 +104,7 @@ def __init__(self, *args, **opts):
print("Trying %s ..." % host)

if not opts["udp"]:
self.fire(connect(*dest))
self.fire(connect(*dest, secure=opts["secure"]))
else:
self.fire(write((host, port), b"\x00"))

@@ -6,9 +6,7 @@ class Server(Component):

channel = "server"

def __init__(self):
super(Server, self).__init__()

def init(self):
self.data = ""
self.host = None
self.port = None
@@ -7,14 +7,17 @@
import select
import os.path
from socket import error as SocketError
from ssl import wrap_socket as sslsocket
from socket import EAI_NODATA, EAI_NONAME
from socket import socket, AF_INET, AF_INET6, SOCK_STREAM, has_ipv6


from circuits import Manager, Debugger
from circuits.net.events import close, connect, write
from circuits.core.pollers import Select, Poll, EPoll, KQueue
from circuits.net.sockets import TCPServer, TCP6Server, TCPClient, TCP6Client


from .client import Client
from .server import Server
from tests.conftest import WaitEvent
@@ -23,6 +26,57 @@
CERT_FILE = os.path.join(os.path.dirname(__file__), "cert.pem")


class TestClient(object):

def __init__(self, ipv6=False):
self._sockname = None

self.sock = socket(
AF_INET6 if ipv6
else AF_INET,
SOCK_STREAM
)

self.ssock = sslsocket(self.sock)

@property
def sockname(self):
return self._sockname

def connect(self, host, port):
self.ssock.connect_ex((host, port))
self._sockname = self.ssock.getsockname()

def send(self, data):
self.ssock.send(data)

def recv(self, buflen=4069):
return self.ssock.recv(buflen)

def disconnect(self):
try:
self.ssock.shutdown(2)
except SocketError:
pass

try:
self.ssock.close()
except SockerError:
pass


@pytest.fixture
def client(request, ipv6):
client = TestClient(ipv6=ipv6)

def finalizer():
client.disconnect()

request.addfinalizer(finalizer)

return client


def wait_host(server):
def checker(obj, attr):
return all((getattr(obj, a) for a in attr))
@@ -89,46 +143,37 @@ def test_tcp_basic(Poller, ipv6):
m.stop()


def test_tcps_basic(Poller, ipv6):
from circuits import Debugger
m = Manager() + Debugger() + Poller()
def test_tcps_basic(manager, watcher, client, Poller, ipv6):
poller = Poller().register(manager)

if ipv6:
tcp_server = TCP6Server(("::1", 0), secure=True, certfile=CERT_FILE)
tcp_client = TCP6Client()
else:
tcp_server = TCPServer(0, secure=True, certfile=CERT_FILE)
tcp_client = TCPClient()
server = Server() + tcp_server
client = Client() + tcp_client

server.register(m)
client.register(m)
server = Server() + tcp_server

m.start()
server.register(manager)

try:
assert pytest.wait_for(client, "ready")
assert pytest.wait_for(server, "ready")
wait_host(server)
watcher.wait("ready", "server")

client.fire(connect(server.host, server.port, secure=True))
assert pytest.wait_for(client, "connected")
assert pytest.wait_for(server, "connected")
assert pytest.wait_for(client, "data", b"Ready")
client.connect(server.host, server.port)
assert watcher.wait("connect", "server")
assert client.recv() == b"Ready"

client.fire(write(b"foo"))
assert pytest.wait_for(server, "data", b"foo")
assert pytest.wait_for(client, "data", b"foo")
client.send(b"foo")
assert watcher.wait("read", "server")
assert client.recv() == b"foo"

client.fire(close())
assert pytest.wait_for(client, "disconnected")
assert pytest.wait_for(server, "disconnected")
client.disconnect()
assert watcher.wait("disconnect", "server")

server.fire(close())
assert pytest.wait_for(server, "closed")
assert watcher.wait("closed", "server")
finally:
m.stop()
poller.unregister()
server.unregister()


def test_tcp_reconnect(Poller, ipv6):
@@ -281,28 +326,27 @@ def test_tcp_bind(Poller, ipv6):
m.stop()


def test_tcp_lookup_failure(Poller, ipv6):
m = Manager() + Poller()
def test_tcp_lookup_failure(manager, watcher, Poller, ipv6):
poller = Poller().register(manager)

if ipv6:
tcp_client = TCP6Client()
else:
tcp_client = TCPClient()
client = Client() + tcp_client

client.register(m)

m.start()
client = Client() + tcp_client
client.register(manager)

try:
assert pytest.wait_for(client, "ready")
assert watcher.wait("ready", "client")

client.fire(connect("foo", 1234))
assert pytest.wait_for(
client, "error", lambda obj, attr: isinstance(getattr(obj, attr), SocketError))
assert watcher.wait("error", "client")

if pytest.PLATFORM == "win32":
assert client.error.errno == 11004
else:
assert client.error.errno in (EAI_NODATA, EAI_NONAME,)
finally:
m.stop()
poller.unregister()
client.unregister()

0 comments on commit 4869430

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.