1
from nose import with_setup
2
from nose.tools import assert_equal, raises
9
SIMPLESTRING = "Hello world!"
10
SIMPLENETSTRING = "12:Hello world!,"
12
class TestCases(object):
14
"""Creates a socket pair for testing"""
15
self.s1, self.s2 = socket.socketpair()
16
self.s1.settimeout(TIMEOUT)
17
self.s2.settimeout(TIMEOUT)
20
"""Closes the socket pair"""
24
@with_setup(setup, teardown)
25
def test_send_null_netstring(self):
26
"""Check that we construct a empty Netstring correctly"""
27
chat.send_netstring(self.s1, "")
28
assert_equal(self.s2.recv(1024), NULLNETSTRING)
30
@with_setup(setup, teardown)
31
def test_send_simple_netstring(self):
32
"""Check that we construct a simple Netstring correctly"""
33
chat.send_netstring(self.s1, SIMPLESTRING)
34
assert_equal(self.s2.recv(1024), SIMPLENETSTRING)
36
@with_setup(setup, teardown)
37
def test_recv_null_netstring(self):
38
"""Check that we can decode a null Netstring"""
39
self.s1.sendall(NULLNETSTRING)
40
assert_equal(chat.recv_netstring(self.s2), "")
42
@with_setup(setup, teardown)
43
def test_recv_null_netstring(self):
44
"""Check that we can decode a simple Netstring"""
45
self.s1.sendall(SIMPLENETSTRING)
46
assert_equal(chat.recv_netstring(self.s2), SIMPLESTRING)
48
@with_setup(setup, teardown)
49
@raises(socket.timeout)
50
def test_short_netstring(self):
51
self.s1.sendall("1234:not that long!,")
52
assert chat.recv_netstring(self.s2) is None
54
@with_setup(setup, teardown)
55
@raises(chat.ProtocolError)
56
def test_long_netstring(self):
57
self.s1.sendall("5:not that short!,")
58
assert chat.recv_netstring(self.s2) is None