1
# Copyright (c) 2001-2005 Twisted Matrix Laboratories.
2
# See LICENSE for details.
5
from twisted.trial import unittest
7
from twisted.words.protocols import toc
8
from twisted.internet import protocol, main
9
from twisted.python import failure
12
from struct import pack,unpack
14
class StringIOWithoutClosing(StringIO.StringIO):
18
class DummyTOC(toc.TOC):
20
used to override authentication, now overrides printing.
22
def _debug(self,data):
28
send=send+pack("!BHH",type,SEQID,len(data))
33
if data=="": return [None,""]
34
null,type,seqid,length=unpack("!BBHH",data[:6])
36
return [[type,val],data[6+length:]]
38
class TOCGeneralTestCase(unittest.TestCase):
40
general testing of TOC functions.
47
data[0]=("FLAPON\r\n\r\n",\
48
flap(1,"\000\000\000\001\000\001\000\004test"),\
49
flap(2,"toc_signon localhost 9999 test 0x100000 english \"penguin 0.1\"\000"),\
50
flap(2,"toc_add_buddy test\000"),\
51
flap(2,"toc_init_done\000"),\
52
flap(2,"toc_send_im test \"hi\"\000"),\
53
flap(2,"toc_send_im test2 \"hello\"\000"),\
54
flap(2,"toc_set_away \"not here\"\000"),\
55
flap(2,"toc_set_idle 602\000"),\
56
flap(2,"toc_set_idle 0\000"),\
57
flap(2,"toc_set_away\000"),\
58
flap(2,"toc_evil test norm\000"),\
59
flap(2,"toc_chat_join 4 \"Test Chat\"\000"),\
60
flap(2,"toc_chat_send 0 \"hello\"\000"),\
61
#flap(2,"toc_chat_leave 0\000")) #,\
62
flap(2,"toc_chat_invite 0 \"come\" ooga\000"),\
63
#flap(2,"toc_chat_accept 0\000"),\
65
flap(2,"toc_chat_whisper 0 ooga \"boo ga\"\000"),\
66
flap(2,"toc_chat_leave 0"),\
68
data[1]=("FLAPON\r\n\r\n",\
69
flap(1,"\000\000\000\001\000\001\000\004ooga"),\
70
flap(2,"toc_signon localhost 9999 ooga 0x100000 english \"penguin 0.1\"\000"),\
71
flap(2,"toc_add_buddy test\000"),\
72
flap(2,"toc_init_done\000"),\
86
flap(2,"toc_chat_accept 0\000"),\
87
flap(2,"toc_chat_send 0 \"hi test\"\000"),\
89
flap(2,"toc_chat_leave 0\000"))
92
strings[i]=StringIOWithoutClosing()
98
dummy[i].makeConnection(protocol.FileWrapper(strings[i]))
99
while reduce(lambda x,y:x+y,map(lambda x:x==(),data))!=USERS:
100
for i in range(USERS):
105
dummy[i].dataReceived(j) # test by doing a character at a time
107
dummy[i].connectionLost(failure.Failure(main.CONNECTION_DONE))
110
values[i]=strings[i].getvalue()
111
flaps=map(lambda x:[],range(USERS))
113
i=values.index(value)
114
f,value=readFlap(value)
117
f,value=readFlap(value)
120
ts[t]=dummy[t].signontime
121
shouldequal=range(USERS)
123
[1,"\000\000\000\001"],\
124
[2,"SIGN_ON:TOC1.0\000"],\
125
[2,"NICK:test\000"],\
127
[2,"UPDATE_BUDDY:test:T:0:%s:0: O\000"%ts[0]],\
128
[2,"IM_IN:test:F:hi\000"],\
129
[2,"ERROR:901:test2\000"],\
130
#[2,"UPDATE_BUDDY:test:T:0:%s:0: O\000"%ts[0]],\
131
[2,"UPDATE_BUDDY:test:T:0:%s:0: OU\000"%ts[0]],\
132
[2,"UPDATE_BUDDY:test:T:0:%s:10: OU\000"%ts[0]],\
133
[2,"UPDATE_BUDDY:test:T:0:%s:0: OU\000"%ts[0]],\
134
[2,"UPDATE_BUDDY:test:T:0:%s:0: O\000"%ts[0]],\
135
[2,"EVILED:10:test\000"],\
136
[2,"UPDATE_BUDDY:test:T:10:%s:0: O\000"%ts[0]],\
137
[2,"CHAT_JOIN:0:Test Chat\000"],\
138
[2,"CHAT_UPDATE_BUDDY:0:T:test\000"],\
139
[2,"CHAT_IN:0:test:F:hello\000"],\
140
[2,"CHAT_UPDATE_BUDDY:0:T:ooga\000"],\
141
[2,"CHAT_IN:0:ooga:F:hi test\000"],\
142
[2,"CHAT_LEFT:0\000"]]
144
[1,"\000\000\000\001"],\
145
[2,"SIGN_ON:TOC1.0\000"],\
146
[2,"NICK:ooga\000"],\
148
#[2,"UPDATE_BUDDY:test:T:0:%s:0: O\000"%ts[0]],\
149
[2,"UPDATE_BUDDY:test:T:0:%s:0: OU\000"%ts[0]],\
150
[2,"UPDATE_BUDDY:test:T:0:%s:10: OU\000"%ts[0]],\
151
[2,"UPDATE_BUDDY:test:T:0:%s:0: OU\000"%ts[0]],\
152
[2,"UPDATE_BUDDY:test:T:0:%s:0: O\000"%ts[0]],\
153
[2,"UPDATE_BUDDY:test:T:10:%s:0: O\000"%ts[0]],\
154
[2,"CHAT_INVITE:Test Chat:0:test:come\000"],\
155
[2,"CHAT_JOIN:0:Test Chat\000"],\
156
[2,"CHAT_UPDATE_BUDDY:0:T:test:ooga\000"],\
157
[2,"CHAT_IN:0:ooga:F:hi test\000"],\
158
[2,"CHAT_IN:0:test:T:boo ga\000"],\
159
[2,"CHAT_UPDATE_BUDDY:0:F:test\000"],\
160
[2,"CHAT_LEFT:0\000"]]
161
if flaps!=shouldequal:
162
for i in range(len(shouldequal)):
163
for j in range(len(shouldequal[i])):
164
if shouldequal[i][j]!=flaps[i][j]:
165
raise AssertionError("GeneralTest Failed!\nUser %s Line %s\nactual:%s\nshould be:%s"%(i,j,flaps[i][j],shouldequal[i][j]))
166
raise AssertionError("GeneralTest Failed with incorrect lengths!")
167
class TOCMultiPacketTestCase(unittest.TestCase):
169
i saw this problem when using GAIM. It only read the flaps onces per dataReceived, and would basically block if it ever received two packets together in one dataReceived. this tests for that occurance.
174
packets=["FLAPON\r\n\r\n",\
175
flap(1,"\000\000\000\001\000\001\000\004test"),\
176
flap(2,"toc_signon null 9999 test 0x100000 english \"penguin 0.1\"\000"),\
177
flap(2,"toc_init_done\000"),\
178
flap(2,"toc_send_im test hi\000")]
179
shouldbe=[[1,"\000\000\000\001"],\
180
[2,"SIGN_ON:TOC1.0\000"],\
181
[2,"NICK:test\000"],\
183
[2,"IM_IN:test:F:hi\000"]]
187
s=StringIOWithoutClosing()
191
d.makeConnection(protocol.FileWrapper(s))
193
d.connectionLost(failure.Failure(main.CONNECTION_DONE))
196
f,value=readFlap(value)
199
f,value=readFlap(value)
201
for i in range(len(flaps)):
202
if flaps[i]!=shouldbe[i]:raise AssertionError("MultiPacketTest Failed!\nactual:%s\nshould be:%s"%(flaps[i],shouldbe[i]))
203
raise AssertionError("MultiPacketTest Failed with incorrect length!, printing both lists\nactual:%s\nshould be:%s"%(flaps,shouldbe))
204
class TOCSavedValuesTestCase(unittest.TestCase):
208
password1=toc.roast("test pass")
209
password2=toc.roast("pass test")
212
flap(1,"\000\000\000\001\000\001\000\004test"),\
213
flap(2,"toc_signon localhost 9999 test %s english \"penguin 0.1\"\000"%password1),\
214
flap(2,"toc_init_done\000"),\
215
flap(2,"toc_set_config \"{m 4}\"\000"),\
216
flap(2,"toc_format_nickname BOOGA\000"),\
217
flap(2,"toc_format_nickname \"T E S T\"\000"),\
218
flap(2,"toc_change_passwd \"testpass\" \"pass test\"\000"),\
219
flap(2,"toc_change_passwd \"test pass\" \"pass test\"\000")]
221
[1,"\000\000\000\001"],\
222
[2,"SIGN_ON:TOC1.0\000"],\
223
[2,"NICK:test\000"],\
225
[2,"ERROR:911\000"],\
226
[2,"ADMIN_NICK_STATUS:0\000"],\
227
[2,"ERROR:911\000"],\
228
[2,"ADMIN_PASSWD_STATUS:0\000"]]
231
flap(1,"\000\000\000\001\000\001\000\004test"),\
232
flap(2,"toc_signon localhost 9999 test 0x1000 english \"penguin 0.1\"\000"),\
233
flap(2,"toc_init_done")]
235
[1,"\000\00\000\001"],\
239
flap(1,"\000\000\000\001\000\001\000\004test"),\
240
flap(2,"toc_signon localhost 9999 test %s english \"penguin 0.1\"\000"%password2),\
241
flap(2,"toc_init_done")]
243
[1,"\000\000\000\001"],\
244
[2,"SIGN_ON:TOC1.0\000"],\
245
[2,"NICK:T E S T\000"],\
246
[2,"CONFIG:{m 4}\000"]]
250
s=StringIOWithoutClosing()
251
d.makeConnection(protocol.FileWrapper(s))
254
d.connectionLost(failure.Failure(main.CONNECTION_DONE))
261
if flaps!=beforeexpect:
262
for i in range(len(flaps)):
263
if flaps[i]!=beforeexpect[i]:
264
raise AssertionError("SavedValuesTest Before Failed!\nactual:%s\nshould be:%s"%(flaps[i],beforeexpect[i]))
265
raise AssertionError("SavedValuesTest Before Failed with incorrect length!\nactual:%s\nshould be:%s"%(flaps,beforeexpect))
268
s=StringIOWithoutClosing()
269
d.makeConnection(protocol.FileWrapper(s))
270
for i in badpasssend:
272
d.connectionLost(failure.Failure(main.CONNECTION_DONE))
279
if flaps!=badpassexpect:
280
for i in range(len(flaps)):
281
if flaps[i]!=badpassexpect[i]:
282
raise AssertionError("SavedValuesTest BadPass Failed!\nactual:%s\nshould be:%s"%(flaps[i],badpassexpect[i]))
283
raise AssertionError("SavedValuesTest BadPass Failed with incorrect length!\nactual:%s\nshould be:%s"%(flaps,badpassexpect))
286
s=StringIOWithoutClosing()
287
d.makeConnection(protocol.FileWrapper(s))
288
for i in goodpasssend:
290
d.connectionLost(failure.Failure(main.CONNECTION_DONE))
297
if flaps!=goodpassexpect:
298
for i in range(len(flaps)):
299
if flaps[i]!=goodpassexpect[i]:
300
raise AssertionError("SavedValuesTest GoodPass Failed!\nactual:%s\nshould be:%s"%(flaps[i],goodpassexpect[i]))
301
raise AssertionError("SavedValuesTest GoodPass Failed with incorrect length!\nactual:%s\nshould be:%s"%(flaps,beforeexpect))
302
class TOCPrivacyTestCase(unittest.TestCase):
304
sends=["FLAPON\r\n\r\n",\
305
flap(1,"\000\000\000\001\000\001\000\004test"),\
306
flap(2,"toc_signon localhost 9999 test 0x00 english penguin\000"),\
307
flap(2,"toc_init_done\000"),\
308
flap(2,"toc_add_deny\000"),\
309
flap(2,"toc_send_im test 1\000"),\
310
flap(2,"toc_add_deny test\000"),\
311
flap(2,"toc_send_im test 2\000"),\
312
flap(2,"toc_add_permit\000"),\
313
flap(2,"toc_send_im test 3\000"),\
314
flap(2,"toc_add_permit test\000"),\
315
flap(2,"toc_send_im test 4\000")]
316
expect=[[1,"\000\000\000\001"],\
317
[2,"SIGN_ON:TOC1.0\000"],\
318
[2,"NICK:test\000"],\
320
[2,"IM_IN:test:F:1\000"],\
321
[2,"ERROR:901:test\000"],\
322
[2,"ERROR:901:test\000"],\
323
[2,"IM_IN:test:F:4\000"]]
325
d.factory=toc.TOCFactory()
326
s=StringIOWithoutClosing()
327
d.makeConnection(protocol.FileWrapper(s))
330
d.connectionLost(failure.Failure(main.CONNECTION_DONE))
338
for i in range(len(flaps)):
339
if flaps[i]!=expect[i]:
340
raise AssertionError("PrivacyTest Before Failed!\nactual:%s\nshould be:%s"%(flaps[i],expect[i]))
341
raise AssertionError("PrivacyTest Before Failed with incorrect length!\nactual:%s\nshould be:%s"%(flaps,expect))
342
testCases=[TOCGeneralTestCase,TOCMultiPacketTestCase,TOCSavedValuesTestCase,TOCPrivacyTestCase]