1637
by William Grant
Add ivle.tests, move ivle.test_chat into there, and be slightly more idiomatic. |
1 |
# IVLE - Informatics Virtual Learning Environment
|
2 |
# Copyright (C) 2007-2010 The University of Melbourne
|
|
3 |
#
|
|
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.
|
|
8 |
#
|
|
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.
|
|
13 |
#
|
|
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
|
|
17 |
||
18 |
import socket |
|
1651
by David Coles
A few more tests for chat. Testing the chat and start_server functions will likey require a little bit of refactoring. |
19 |
import os |
20 |
import random |
|
1637
by William Grant
Add ivle.tests, move ivle.test_chat into there, and be slightly more idiomatic. |
21 |
|
1801.1.1
by William Grant
Replace cjson with json, or simplejson if json is not available (Python <2.6) |
22 |
try: |
23 |
import json |
|
24 |
except ImportError: |
|
25 |
import simplejson as json |
|
26 |
||
1636
by David Coles
Testcase for chat protocol |
27 |
from nose.tools import assert_equal, raises |
28 |
||
1637
by William Grant
Add ivle.tests, move ivle.test_chat into there, and be slightly more idiomatic. |
29 |
import ivle.chat |
30 |
||
1636
by David Coles
Testcase for chat protocol |
31 |
|
32 |
TIMEOUT = 0.1 |
|
33 |
NULLNETSTRING = "0:," |
|
34 |
SIMPLESTRING = "Hello world!" |
|
35 |
SIMPLENETSTRING = "12:Hello world!," |
|
36 |
||
1637
by William Grant
Add ivle.tests, move ivle.test_chat into there, and be slightly more idiomatic. |
37 |
|
38 |
class TestChat(object): |
|
39 |
def setUp(self): |
|
1636
by David Coles
Testcase for chat protocol |
40 |
"""Creates a socket pair for testing"""
|
41 |
self.s1, self.s2 = socket.socketpair() |
|
42 |
self.s1.settimeout(TIMEOUT) |
|
43 |
self.s2.settimeout(TIMEOUT) |
|
44 |
||
1637
by William Grant
Add ivle.tests, move ivle.test_chat into there, and be slightly more idiomatic. |
45 |
def tearDown(self): |
1636
by David Coles
Testcase for chat protocol |
46 |
"""Closes the socket pair"""
|
47 |
self.s1.close() |
|
48 |
self.s2.close() |
|
49 |
||
50 |
def test_send_null_netstring(self): |
|
51 |
"""Check that we construct a empty Netstring correctly"""
|
|
1637
by William Grant
Add ivle.tests, move ivle.test_chat into there, and be slightly more idiomatic. |
52 |
ivle.chat.send_netstring(self.s1, "") |
1636
by David Coles
Testcase for chat protocol |
53 |
assert_equal(self.s2.recv(1024), NULLNETSTRING) |
54 |
||
55 |
def test_send_simple_netstring(self): |
|
56 |
"""Check that we construct a simple Netstring correctly"""
|
|
1637
by William Grant
Add ivle.tests, move ivle.test_chat into there, and be slightly more idiomatic. |
57 |
ivle.chat.send_netstring(self.s1, SIMPLESTRING) |
1636
by David Coles
Testcase for chat protocol |
58 |
assert_equal(self.s2.recv(1024), SIMPLENETSTRING) |
59 |
||
60 |
def test_recv_null_netstring(self): |
|
61 |
"""Check that we can decode a null Netstring"""
|
|
62 |
self.s1.sendall(NULLNETSTRING) |
|
1637
by William Grant
Add ivle.tests, move ivle.test_chat into there, and be slightly more idiomatic. |
63 |
assert_equal(ivle.chat.recv_netstring(self.s2), "") |
1636
by David Coles
Testcase for chat protocol |
64 |
|
65 |
def test_recv_null_netstring(self): |
|
66 |
"""Check that we can decode a simple Netstring"""
|
|
67 |
self.s1.sendall(SIMPLENETSTRING) |
|
1637
by William Grant
Add ivle.tests, move ivle.test_chat into there, and be slightly more idiomatic. |
68 |
assert_equal(ivle.chat.recv_netstring(self.s2), SIMPLESTRING) |
1636
by David Coles
Testcase for chat protocol |
69 |
|
70 |
@raises(socket.timeout) |
|
1651
by David Coles
A few more tests for chat. Testing the chat and start_server functions will likey require a little bit of refactoring. |
71 |
def test_invalid_short_netstring(self): |
1636
by David Coles
Testcase for chat protocol |
72 |
self.s1.sendall("1234:not that long!,") |
1637
by William Grant
Add ivle.tests, move ivle.test_chat into there, and be slightly more idiomatic. |
73 |
assert ivle.chat.recv_netstring(self.s2) is None |
1636
by David Coles
Testcase for chat protocol |
74 |
|
1637
by William Grant
Add ivle.tests, move ivle.test_chat into there, and be slightly more idiomatic. |
75 |
@raises(ivle.chat.ProtocolError) |
1651
by David Coles
A few more tests for chat. Testing the chat and start_server functions will likey require a little bit of refactoring. |
76 |
def test_invalid_long_netstring(self): |
77 |
self.s1.sendall("5:not that short!,") |
|
78 |
assert ivle.chat.recv_netstring(self.s2) is None |
|
79 |
||
1636
by David Coles
Testcase for chat protocol |
80 |
def test_long_netstring(self): |
1651
by David Coles
A few more tests for chat. Testing the chat and start_server functions will likey require a little bit of refactoring. |
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 |
|
85 |
||
86 |
def test_multiple_netstrings(self): |
|
87 |
messages = [] |
|
88 |
for i in range(10): |
|
89 |
message = os.urandom(random.randint(0,20)) |
|
90 |
messages.append(message) |
|
91 |
ivle.chat.send_netstring(self.s1, message) |
|
92 |
for i in range(10): |
|
93 |
assert_equal(ivle.chat.recv_netstring(self.s2), messages[i]) |
|
1636
by David Coles
Testcase for chat protocol |
94 |
|
1697
by David Coles
chat: Factor out the encoding and decoding functions so we can test them and possibly reuse elsewhere |
95 |
def test_encode(self): |
96 |
"""Check that we correctly encode a basic object
|
|
97 |
"""
|
|
98 |
MESSAGE = {} |
|
99 |
MAGIC = "3EE" |
|
1801.1.1
by William Grant
Replace cjson with json, or simplejson if json is not available (Python <2.6) |
100 |
content = json.dumps(MESSAGE) |
1697
by David Coles
chat: Factor out the encoding and decoding functions so we can test them and possibly reuse elsewhere |
101 |
# Digest can be formed with `echo -n "${content}${MAGIC}" | md5sum`
|
102 |
DIGEST = '2b59b68e1ac0852b87fb7e64946f2658' |
|
103 |
expected = {'digest': DIGEST, |
|
104 |
'content': content} |
|
105 |
encoded = ivle.chat.encode(MESSAGE, MAGIC) |
|
1801.1.1
by William Grant
Replace cjson with json, or simplejson if json is not available (Python <2.6) |
106 |
assert_equal(json.loads(encoded), expected) |
1697
by David Coles
chat: Factor out the encoding and decoding functions so we can test them and possibly reuse elsewhere |
107 |
|
108 |
def test_encode_decode(self): |
|
109 |
"""Check that a round trip encoding and decoding works
|
|
110 |
"""
|
|
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) |
|
116 |
||
117 |
@raises(ivle.chat.ProtocolError) |
|
118 |
def test_decode_bad_magic(self): |
|
119 |
"""Check that a bad digest causes a ProtocolError to be raised
|
|
120 |
"""
|
|
121 |
CHATMESSAGE = ('{"content": "{\\"a\\": \\"b\\"}", "digest": ' + |
|
122 |
'"eb860a5fe8fdbef19ffb79e3a5c47113"}') |
|
123 |
CORRECTMAGIC = "AEIOU" |
|
124 |
INCORRECTMAGIC = "ABCDE" |
|
125 |
||
126 |
# Check our "correct" string decodes without a ProtocolError
|
|
127 |
try: |
|
128 |
ivle.chat.decode(CHATMESSAGE, CORRECTMAGIC) |
|
129 |
except ivle.chat.ProtocolError: |
|
130 |
raise AssertionError("ProtocolError with 'correct' magic") |
|
131 |
||
132 |
# This should raise the ProtocolError
|
|
133 |
ivle.chat.decode(CHATMESSAGE, INCORRECTMAGIC) |