1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2010 The University of Melbourne
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
from nose.tools import assert_equal, raises
30
SIMPLESTRING = "Hello world!"
31
SIMPLENETSTRING = "12:Hello world!,"
34
class TestChat(object):
36
"""Creates a socket pair for testing"""
37
self.s1, self.s2 = socket.socketpair()
38
self.s1.settimeout(TIMEOUT)
39
self.s2.settimeout(TIMEOUT)
42
"""Closes the socket pair"""
46
def test_send_null_netstring(self):
47
"""Check that we construct a empty Netstring correctly"""
48
ivle.chat.send_netstring(self.s1, "")
49
assert_equal(self.s2.recv(1024), NULLNETSTRING)
51
def test_send_simple_netstring(self):
52
"""Check that we construct a simple Netstring correctly"""
53
ivle.chat.send_netstring(self.s1, SIMPLESTRING)
54
assert_equal(self.s2.recv(1024), SIMPLENETSTRING)
56
def test_recv_null_netstring(self):
57
"""Check that we can decode a null Netstring"""
58
self.s1.sendall(NULLNETSTRING)
59
assert_equal(ivle.chat.recv_netstring(self.s2), "")
61
def test_recv_null_netstring(self):
62
"""Check that we can decode a simple Netstring"""
63
self.s1.sendall(SIMPLENETSTRING)
64
assert_equal(ivle.chat.recv_netstring(self.s2), SIMPLESTRING)
66
@raises(socket.timeout)
67
def test_invalid_short_netstring(self):
68
self.s1.sendall("1234:not that long!,")
69
assert ivle.chat.recv_netstring(self.s2) is None
71
@raises(ivle.chat.ProtocolError)
72
def test_invalid_long_netstring(self):
73
self.s1.sendall("5:not that short!,")
74
assert ivle.chat.recv_netstring(self.s2) is None
76
def test_long_netstring(self):
77
# XXX: send() may block if this is too big
78
msg = os.urandom(50000)
79
ivle.chat.send_netstring(self.s1, msg)
80
assert ivle.chat.recv_netstring(self.s2) == msg
82
def test_multiple_netstrings(self):
85
message = os.urandom(random.randint(0,20))
86
messages.append(message)
87
ivle.chat.send_netstring(self.s1, message)
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)