~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/tests/test_chat.py

  • Committer: William Grant
  • Date: 2010-02-23 08:55:42 UTC
  • mto: This revision was merged to the branch mainline in revision 1674.
  • Revision ID: grantw@unimelb.edu.au-20100223085542-r8xw14bxxoraza51
Permit underscores in all names.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
19
import os
 
20
import random
 
21
 
 
22
from nose.tools import assert_equal, raises
 
23
 
 
24
import ivle.chat
 
25
 
 
26
 
 
27
TIMEOUT = 0.1
 
28
NULLNETSTRING = "0:,"
 
29
SIMPLESTRING = "Hello world!"
 
30
SIMPLENETSTRING = "12:Hello world!,"
 
31
 
 
32
 
 
33
class TestChat(object):
 
34
    def setUp(self):
 
35
        """Creates a socket pair for testing"""
 
36
        self.s1, self.s2 = socket.socketpair()
 
37
        self.s1.settimeout(TIMEOUT)
 
38
        self.s2.settimeout(TIMEOUT)
 
39
 
 
40
    def tearDown(self):
 
41
        """Closes the socket pair"""
 
42
        self.s1.close()
 
43
        self.s2.close()
 
44
 
 
45
    def test_send_null_netstring(self):
 
46
        """Check that we construct a empty Netstring correctly"""
 
47
        ivle.chat.send_netstring(self.s1, "")
 
48
        assert_equal(self.s2.recv(1024), NULLNETSTRING)
 
49
 
 
50
    def test_send_simple_netstring(self):
 
51
        """Check that we construct a simple Netstring correctly"""
 
52
        ivle.chat.send_netstring(self.s1, SIMPLESTRING)
 
53
        assert_equal(self.s2.recv(1024), SIMPLENETSTRING)
 
54
 
 
55
    def test_recv_null_netstring(self):
 
56
        """Check that we can decode a null Netstring"""
 
57
        self.s1.sendall(NULLNETSTRING)
 
58
        assert_equal(ivle.chat.recv_netstring(self.s2), "")
 
59
 
 
60
    def test_recv_null_netstring(self):
 
61
        """Check that we can decode a simple Netstring"""
 
62
        self.s1.sendall(SIMPLENETSTRING)
 
63
        assert_equal(ivle.chat.recv_netstring(self.s2), SIMPLESTRING)
 
64
 
 
65
    @raises(socket.timeout)
 
66
    def test_invalid_short_netstring(self):
 
67
        self.s1.sendall("1234:not that long!,")
 
68
        assert ivle.chat.recv_netstring(self.s2) is None
 
69
 
 
70
    @raises(ivle.chat.ProtocolError)
 
71
    def test_invalid_long_netstring(self):
 
72
        self.s1.sendall("5:not that short!,")
 
73
        assert ivle.chat.recv_netstring(self.s2) is None
 
74
 
 
75
    def test_long_netstring(self):
 
76
        # XXX: send() may block if this is too big
 
77
        msg = os.urandom(50000)
 
78
        ivle.chat.send_netstring(self.s1, msg)
 
79
        assert ivle.chat.recv_netstring(self.s2) == msg
 
80
 
 
81
    def test_multiple_netstrings(self):
 
82
        messages = []
 
83
        for i in range(10):
 
84
            message = os.urandom(random.randint(0,20))
 
85
            messages.append(message)
 
86
            ivle.chat.send_netstring(self.s1, message)
 
87
        for i in range(10):
 
88
            assert_equal(ivle.chat.recv_netstring(self.s2), messages[i])
 
89