forked from abesto/python_ircd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration_tests.py
157 lines (132 loc) · 5.52 KB
/
integration_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import unittest
import socket
import time
import errno
from config import config
class Client(object):
test_case = None
timeout = 0.2
timeout_step = 0.001
def __init__(self, name):
self.name = name
self.socket = socket.socket()
self.socket.connect((config.get('server', 'listen_host'), config.getint('server', 'listen_port')))
self.socket.setblocking(0)
self.socket_file = self.socket.makefile()
self.responses = []
def write(self, msg):
self.responses.append([])
print '<- [%s] %s' % (self.name, msg)
self.socket_file.write(msg + '\r\n')
self.socket_file.flush()
def expect(self, line):
got = None
timeout = self.timeout
while got != line and timeout > 0:
try:
got = self.socket_file.readline().strip()
self.responses[-1].append(got)
print '-> [%s] %s' % (self.name, got)
except socket.error, e:
if e.errno != errno.EAGAIN:
raise
time.sleep(self.timeout_step)
timeout -= self.timeout_step
self.test_case.assertEqual(got, line)
def __str__(self):
return self.name
class ServerClientTests(unittest.TestCase):
n = 0
@classmethod
def setUpClass(cls):
Client.test_case = None
@classmethod
def _increment_n(cls):
cls.n += 1
def setUp(self):
self._increment_n()
self.c1 = Client('al-%s' % self.n)
self.c2 = Client('bob-%s' % self.n)
self.c3 = Client('clair-%s' % self.n)
Client.test_case = self
def test_login_nick_first(self, c=None):
if c is None: c = self.c1
c.write('NICK %s' % c.name)
c.write('USER %s %s %s %s' % (c.name, c.name, socket.getfqdn(), c.name))
c.expect(':localhost 376 %s :End of MOTD command' % c.name)
def test_login_user_first(self, c=None):
if c is None: c = self.c2
c.write('USER %s %s %s %s' % (c.name, c.name, socket.getfqdn(), c.name))
c.write('NICK %s' % c.name)
c.expect(':localhost 376 %s :End of MOTD command' % c.name)
def test_join(self, c=None, channel=None, users=None):
if c is None: c = self.c1
if channel is None: channel = '#ch-%s' % self.n
if users is None: users = []
users.append(c)
expected_username_list = ' '.join([c.name for c in users])
if len(users) > 1:
expected_username_list = ':' + expected_username_list
self.test_login_nick_first(c)
c.write('JOIN %s' % channel)
c.expect(':localhost 353 %s = %s %s' % (c.name, channel, expected_username_list))
c.expect(':localhost 366 %s :End of NAMES list' % c.name)
def test_user_list_after_join(self):
self.test_join(self.c1)
self.test_join(self.c2, users=[self.c1])
def test_message_to_channel(self, c=None, rc=None, channel=None, msg='test message'):
if c is None: c = self.c1
if channel is None: channel = '#ch-%s' % self.n
if rc is None:
if c is self.c1: rc = self.c2
else: rc = self.c1
self.test_user_list_after_join()
c.write('PRIVMSG %s :%s' % (channel, msg))
rc.expect(':{c}!{c}@localhost. PRIVMSG {channel} :{msg}'.format(
c=c, channel=channel, msg=msg))
def test_login_quit_login(self):
"""QUITting releases the nick"""
self.test_login_nick_first(self.c1)
self.c1.write('QUIT leaving')
self.c1.expect(':localhost ERROR')
self.c1 = Client(self.c1.name)
self.test_login_nick_first(self.c1)
def test_direct_message(self):
self.test_login_nick_first(self.c1)
self.test_login_nick_first(self.c2)
msg = 'test direct message'
self.c1.write('PRIVMSG %s :%s' % (self.c2.name, msg))
self.c2.expect(':{c}!{c}@localhost. PRIVMSG {rc} :{msg}'.format(
c=self.c1, rc=self.c2, msg=msg))
def test_nick_change(self):
self.test_login_nick_first(self.c1)
self.c1.write('NICK foo')
self.c1.expect(':{c}!{c}@localhost. NICK foo'.format(c=self.c1))
def test_nick_change_taken(self):
self.test_login_nick_first(self.c1)
self.test_login_nick_first(self.c2)
self.c1.write('NICK %s' % self.c2.name)
self.c1.expect(':localhost 433 %s %s :Nickname is already in use' % (self.c1.name, self.c2.name))
def test_topic(self):
channel = '#ch-%s' % self.n
topic = ':This is the old shit'
self.test_join(self.c1, channel)
self.test_join(self.c2, channel, [self.c1])
self.c1.write('TOPIC %s %s' % (channel, topic))
self.c2.expect(':{c}!{c}@localhost. TOPIC {ch} {topic}'.format(
c=self.c1, ch=channel, topic=topic))
self.test_join(self.c3, channel, [self.c1, self.c2])
self.c3.expect(':localhost 332 {c} {ch} {topic}'.format(
c=self.c3, ch=channel, topic=topic))
def test_clear_topic(self):
channel = '#ch-%s' % self.n
self.test_join(self.c1, channel)
self.c1.write('TOPIC %s %s' % (channel, ':old topic'))
self.c1.write('TOPIC %s :' % channel)
self.c1.write('TOPIC %s' % channel)
self.c1.expect(':localhost 331 {c} {ch} :No topic is set'.format(
c=self.c1.name, ch=channel))
def test_change_topic_no_such_channel(self):
self.test_login_nick_first(self.c3)
self.c3.write('TOPIC #foo baz')
self.c3.expect(':localhost 401 {c} #foo :No such channel'.format(c=self.c3.name))