68
62
assert_equal(ivle.chat.recv_netstring(self.s2), SIMPLESTRING)
70
64
@raises(socket.timeout)
71
def test_invalid_short_netstring(self):
65
def test_short_netstring(self):
72
66
self.s1.sendall("1234:not that long!,")
73
67
assert ivle.chat.recv_netstring(self.s2) is None
75
69
@raises(ivle.chat.ProtocolError)
76
def test_invalid_long_netstring(self):
70
def test_long_netstring(self):
77
71
self.s1.sendall("5:not that short!,")
78
72
assert ivle.chat.recv_netstring(self.s2) is None
80
def test_long_netstring(self):
81
# XXX: send() may block if this is too big
82
msg = os.urandom(50000)
83
ivle.chat.send_netstring(self.s1, msg)
84
assert ivle.chat.recv_netstring(self.s2) == msg
86
def test_multiple_netstrings(self):
89
message = os.urandom(random.randint(0,20))
90
messages.append(message)
91
ivle.chat.send_netstring(self.s1, message)
93
assert_equal(ivle.chat.recv_netstring(self.s2), messages[i])
95
def test_encode(self):
96
"""Check that we correctly encode a basic object
100
content = json.dumps(MESSAGE)
101
# Digest can be formed with `echo -n "${content}${MAGIC}" | md5sum`
102
DIGEST = '2b59b68e1ac0852b87fb7e64946f2658'
103
expected = {'digest': DIGEST,
105
encoded = ivle.chat.encode(MESSAGE, MAGIC)
106
assert_equal(json.loads(encoded), expected)
108
def test_encode_decode(self):
109
"""Check that a round trip encoding and decoding works
111
MESSAGE = {'message': 'Hello, world'}
112
MAGIC = "MagicString"
113
encoded = ivle.chat.encode(MESSAGE, MAGIC)
114
decoded = ivle.chat.decode(encoded, MAGIC)
115
assert_equal(decoded, MESSAGE)
117
@raises(ivle.chat.ProtocolError)
118
def test_decode_bad_magic(self):
119
"""Check that a bad digest causes a ProtocolError to be raised
121
CHATMESSAGE = ('{"content": "{\\"a\\": \\"b\\"}", "digest": ' +
122
'"eb860a5fe8fdbef19ffb79e3a5c47113"}')
123
CORRECTMAGIC = "AEIOU"
124
INCORRECTMAGIC = "ABCDE"
126
# Check our "correct" string decodes without a ProtocolError
128
ivle.chat.decode(CHATMESSAGE, CORRECTMAGIC)
129
except ivle.chat.ProtocolError:
130
raise AssertionError("ProtocolError with 'correct' magic")
132
# This should raise the ProtocolError
133
ivle.chat.decode(CHATMESSAGE, INCORRECTMAGIC)