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

« back to all changes in this revision

Viewing changes to ivle/test_chat.py

  • Committer: David Coles
  • Date: 2010-02-18 03:42:12 UTC
  • Revision ID: coles.david@gmail.com-20100218034212-80t0afyb6qnv1e6h
Testcase for chat protocol

Show diffs side-by-side

added added

removed removed

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