~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-18 06:43:57 UTC
  • Revision ID: grantw@unimelb.edu.au-20100218064357-a43eajvau1gpzw29
Add ivle.tests, move ivle.test_chat into there, and be slightly more idiomatic.

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
 
1
20
from nose import with_setup
2
21
from nose.tools import assert_equal, raises
3
22
 
4
 
import chat
5
 
import socket
 
23
import ivle.chat
 
24
 
6
25
 
7
26
TIMEOUT = 0.1
8
27
NULLNETSTRING = "0:,"
9
28
SIMPLESTRING = "Hello world!"
10
29
SIMPLENETSTRING = "12:Hello world!,"
11
30
 
12
 
class TestCases(object):
13
 
    def setup(self):
 
31
 
 
32
class TestChat(object):
 
33
    def setUp(self):
14
34
        """Creates a socket pair for testing"""
15
35
        self.s1, self.s2 = socket.socketpair()
16
36
        self.s1.settimeout(TIMEOUT)
17
37
        self.s2.settimeout(TIMEOUT)
18
38
 
19
 
    def teardown(self):
 
39
    def tearDown(self):
20
40
        """Closes the socket pair"""
21
41
        self.s1.close()
22
42
        self.s2.close()
23
43
 
24
 
    @with_setup(setup, teardown)
25
44
    def test_send_null_netstring(self):
26
45
        """Check that we construct a empty Netstring correctly"""
27
 
        chat.send_netstring(self.s1, "")
 
46
        ivle.chat.send_netstring(self.s1, "")
28
47
        assert_equal(self.s2.recv(1024), NULLNETSTRING)
29
48
 
30
 
    @with_setup(setup, teardown)
31
49
    def test_send_simple_netstring(self):
32
50
        """Check that we construct a simple Netstring correctly"""
33
 
        chat.send_netstring(self.s1, SIMPLESTRING)
 
51
        ivle.chat.send_netstring(self.s1, SIMPLESTRING)
34
52
        assert_equal(self.s2.recv(1024), SIMPLENETSTRING)
35
53
 
36
 
    @with_setup(setup, teardown)
37
54
    def test_recv_null_netstring(self):
38
55
        """Check that we can decode a null Netstring"""
39
56
        self.s1.sendall(NULLNETSTRING)
40
 
        assert_equal(chat.recv_netstring(self.s2), "")
 
57
        assert_equal(ivle.chat.recv_netstring(self.s2), "")
41
58
 
42
 
    @with_setup(setup, teardown)
43
59
    def test_recv_null_netstring(self):
44
60
        """Check that we can decode a simple Netstring"""
45
61
        self.s1.sendall(SIMPLENETSTRING)
46
 
        assert_equal(chat.recv_netstring(self.s2), SIMPLESTRING)
 
62
        assert_equal(ivle.chat.recv_netstring(self.s2), SIMPLESTRING)
47
63
 
48
 
    @with_setup(setup, teardown)
49
64
    @raises(socket.timeout)
50
65
    def test_short_netstring(self):
51
66
        self.s1.sendall("1234:not that long!,")
52
 
        assert chat.recv_netstring(self.s2) is None
 
67
        assert ivle.chat.recv_netstring(self.s2) is None
53
68
 
54
 
    @with_setup(setup, teardown)
55
 
    @raises(chat.ProtocolError)
 
69
    @raises(ivle.chat.ProtocolError)
56
70
    def test_long_netstring(self):
57
71
        self.s1.sendall("5:not that short!,")
58
 
        assert chat.recv_netstring(self.s2) is None
 
72
        assert ivle.chat.recv_netstring(self.s2) is None
59
73