87
88
for i in range(10):
88
89
assert_equal(ivle.chat.recv_netstring(self.s2), messages[i])
91
def test_encode(self):
92
"""Check that we correctly encode a basic object
96
content = cjson.encode(MESSAGE)
97
# Digest can be formed with `echo -n "${content}${MAGIC}" | md5sum`
98
DIGEST = '2b59b68e1ac0852b87fb7e64946f2658'
99
expected = {'digest': DIGEST,
101
encoded = ivle.chat.encode(MESSAGE, MAGIC)
102
assert_equal(cjson.decode(encoded), expected)
104
def test_encode_decode(self):
105
"""Check that a round trip encoding and decoding works
107
MESSAGE = {'message': 'Hello, world'}
108
MAGIC = "MagicString"
109
encoded = ivle.chat.encode(MESSAGE, MAGIC)
110
decoded = ivle.chat.decode(encoded, MAGIC)
111
assert_equal(decoded, MESSAGE)
113
@raises(ivle.chat.ProtocolError)
114
def test_decode_bad_magic(self):
115
"""Check that a bad digest causes a ProtocolError to be raised
117
CHATMESSAGE = ('{"content": "{\\"a\\": \\"b\\"}", "digest": ' +
118
'"eb860a5fe8fdbef19ffb79e3a5c47113"}')
119
CORRECTMAGIC = "AEIOU"
120
INCORRECTMAGIC = "ABCDE"
122
# Check our "correct" string decodes without a ProtocolError
124
ivle.chat.decode(CHATMESSAGE, CORRECTMAGIC)
125
except ivle.chat.ProtocolError:
126
raise AssertionError("ProtocolError with 'correct' magic")
128
# This should raise the ProtocolError
129
ivle.chat.decode(CHATMESSAGE, INCORRECTMAGIC)