1
# Copyright (c) 2001-2009 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
10
from twisted.test.proto_helpers import StringIOWithoutClosing
12
from struct import pack,unpack
15
class DummyTOC(toc.TOC):
17
used to override authentication, now overrides printing.
19
def _debug(self,data):
25
send=send+pack("!BHH",type,SEQID,len(data))
30
if data=="": return [None,""]
31
null,type,seqid,length=unpack("!BBHH",data[:6])
33
return [[type,val],data[6+length:]]
35
class TOCGeneralTestCase(unittest.TestCase):
37
general testing of TOC functions.
44
data[0]=("FLAPON\r\n\r\n",\
45
flap(1,"\000\000\000\001\000\001\000\004test"),\
46
flap(2,"toc_signon localhost 9999 test 0x100000 english \"penguin 0.1\"\000"),\
47
flap(2,"toc_add_buddy test\000"),\
48
flap(2,"toc_init_done\000"),\
49
flap(2,"toc_send_im test \"hi\"\000"),\
50
flap(2,"toc_send_im test2 \"hello\"\000"),\
51
flap(2,"toc_set_away \"not here\"\000"),\
52
flap(2,"toc_set_idle 602\000"),\
53
flap(2,"toc_set_idle 0\000"),\
54
flap(2,"toc_set_away\000"),\
55
flap(2,"toc_evil test norm\000"),\
56
flap(2,"toc_chat_join 4 \"Test Chat\"\000"),\
57
flap(2,"toc_chat_send 0 \"hello\"\000"),\
58
#flap(2,"toc_chat_leave 0\000")) #,\
59
flap(2,"toc_chat_invite 0 \"come\" ooga\000"),\
60
#flap(2,"toc_chat_accept 0\000"),\
62
flap(2,"toc_chat_whisper 0 ooga \"boo ga\"\000"),\
63
flap(2,"toc_chat_leave 0"),\
65
data[1]=("FLAPON\r\n\r\n",\
66
flap(1,"\000\000\000\001\000\001\000\004ooga"),\
67
flap(2,"toc_signon localhost 9999 ooga 0x100000 english \"penguin 0.1\"\000"),\
68
flap(2,"toc_add_buddy test\000"),\
69
flap(2,"toc_init_done\000"),\
83
flap(2,"toc_chat_accept 0\000"),\
84
flap(2,"toc_chat_send 0 \"hi test\"\000"),\
86
flap(2,"toc_chat_leave 0\000"))
89
strings[i]=StringIOWithoutClosing()
95
dummy[i].makeConnection(protocol.FileWrapper(strings[i]))
96
while sum(map(lambda x: x == (), data)) != USERS:
97
for i in range(USERS):
102
dummy[i].dataReceived(j) # test by doing a character at a time
104
dummy[i].connectionLost(failure.Failure(main.CONNECTION_DONE))
107
values[i]=strings[i].getvalue()
108
flaps=map(lambda x:[],range(USERS))
110
i=values.index(value)
111
f,value=readFlap(value)
114
f,value=readFlap(value)
117
ts[t]=dummy[t].signontime
118
shouldequal=range(USERS)
120
[1,"\000\000\000\001"],\
121
[2,"SIGN_ON:TOC1.0\000"],\
122
[2,"NICK:test\000"],\
124
[2,"UPDATE_BUDDY:test:T:0:%s:0: O\000"%ts[0]],\
125
[2,"IM_IN:test:F:hi\000"],\
126
[2,"ERROR:901:test2\000"],\
127
#[2,"UPDATE_BUDDY:test:T:0:%s:0: O\000"%ts[0]],\
128
[2,"UPDATE_BUDDY:test:T:0:%s:0: OU\000"%ts[0]],\
129
[2,"UPDATE_BUDDY:test:T:0:%s:10: OU\000"%ts[0]],\
130
[2,"UPDATE_BUDDY:test:T:0:%s:0: OU\000"%ts[0]],\
131
[2,"UPDATE_BUDDY:test:T:0:%s:0: O\000"%ts[0]],\
132
[2,"EVILED:10:test\000"],\
133
[2,"UPDATE_BUDDY:test:T:10:%s:0: O\000"%ts[0]],\
134
[2,"CHAT_JOIN:0:Test Chat\000"],\
135
[2,"CHAT_UPDATE_BUDDY:0:T:test\000"],\
136
[2,"CHAT_IN:0:test:F:hello\000"],\
137
[2,"CHAT_UPDATE_BUDDY:0:T:ooga\000"],\
138
[2,"CHAT_IN:0:ooga:F:hi test\000"],\
139
[2,"CHAT_LEFT:0\000"]]
141
[1,"\000\000\000\001"],\
142
[2,"SIGN_ON:TOC1.0\000"],\
143
[2,"NICK:ooga\000"],\
145
#[2,"UPDATE_BUDDY:test:T:0:%s:0: O\000"%ts[0]],\
146
[2,"UPDATE_BUDDY:test:T:0:%s:0: OU\000"%ts[0]],\
147
[2,"UPDATE_BUDDY:test:T:0:%s:10: OU\000"%ts[0]],\
148
[2,"UPDATE_BUDDY:test:T:0:%s:0: OU\000"%ts[0]],\
149
[2,"UPDATE_BUDDY:test:T:0:%s:0: O\000"%ts[0]],\
150
[2,"UPDATE_BUDDY:test:T:10:%s:0: O\000"%ts[0]],\
151
[2,"CHAT_INVITE:Test Chat:0:test:come\000"],\
152
[2,"CHAT_JOIN:0:Test Chat\000"],\
153
[2,"CHAT_UPDATE_BUDDY:0:T:test:ooga\000"],\
154
[2,"CHAT_IN:0:ooga:F:hi test\000"],\
155
[2,"CHAT_IN:0:test:T:boo ga\000"],\
156
[2,"CHAT_UPDATE_BUDDY:0:F:test\000"],\
157
[2,"CHAT_LEFT:0\000"]]
158
if flaps!=shouldequal:
159
for i in range(len(shouldequal)):
160
for j in range(len(shouldequal[i])):
161
if shouldequal[i][j]!=flaps[i][j]:
162
raise AssertionError("GeneralTest Failed!\nUser %s Line %s\nactual:%s\nshould be:%s"%(i,j,flaps[i][j],shouldequal[i][j]))
163
raise AssertionError("GeneralTest Failed with incorrect lengths!")
164
class TOCMultiPacketTestCase(unittest.TestCase):
166
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.
171
packets=["FLAPON\r\n\r\n",\
172
flap(1,"\000\000\000\001\000\001\000\004test"),\
173
flap(2,"toc_signon null 9999 test 0x100000 english \"penguin 0.1\"\000"),\
174
flap(2,"toc_init_done\000"),\
175
flap(2,"toc_send_im test hi\000")]
176
shouldbe=[[1,"\000\000\000\001"],\
177
[2,"SIGN_ON:TOC1.0\000"],\
178
[2,"NICK:test\000"],\
180
[2,"IM_IN:test:F:hi\000"]]
184
s=StringIOWithoutClosing()
188
d.makeConnection(protocol.FileWrapper(s))
190
d.connectionLost(failure.Failure(main.CONNECTION_DONE))
193
f,value=readFlap(value)
196
f,value=readFlap(value)
198
for i in range(len(flaps)):
199
if flaps[i]!=shouldbe[i]:raise AssertionError("MultiPacketTest Failed!\nactual:%s\nshould be:%s"%(flaps[i],shouldbe[i]))
200
raise AssertionError("MultiPacketTest Failed with incorrect length!, printing both lists\nactual:%s\nshould be:%s"%(flaps,shouldbe))
201
class TOCSavedValuesTestCase(unittest.TestCase):
205
password1=toc.roast("test pass")
206
password2=toc.roast("pass test")
209
flap(1,"\000\000\000\001\000\001\000\004test"),\
210
flap(2,"toc_signon localhost 9999 test %s english \"penguin 0.1\"\000"%password1),\
211
flap(2,"toc_init_done\000"),\
212
flap(2,"toc_set_config \"{m 4}\"\000"),\
213
flap(2,"toc_format_nickname BOOGA\000"),\
214
flap(2,"toc_format_nickname \"T E S T\"\000"),\
215
flap(2,"toc_change_passwd \"testpass\" \"pass test\"\000"),\
216
flap(2,"toc_change_passwd \"test pass\" \"pass test\"\000")]
218
[1,"\000\000\000\001"],\
219
[2,"SIGN_ON:TOC1.0\000"],\
220
[2,"NICK:test\000"],\
222
[2,"ERROR:911\000"],\
223
[2,"ADMIN_NICK_STATUS:0\000"],\
224
[2,"ERROR:911\000"],\
225
[2,"ADMIN_PASSWD_STATUS:0\000"]]
228
flap(1,"\000\000\000\001\000\001\000\004test"),\
229
flap(2,"toc_signon localhost 9999 test 0x1000 english \"penguin 0.1\"\000"),\
230
flap(2,"toc_init_done")]
232
[1,"\000\00\000\001"],\
236
flap(1,"\000\000\000\001\000\001\000\004test"),\
237
flap(2,"toc_signon localhost 9999 test %s english \"penguin 0.1\"\000"%password2),\
238
flap(2,"toc_init_done")]
240
[1,"\000\000\000\001"],\
241
[2,"SIGN_ON:TOC1.0\000"],\
242
[2,"NICK:T E S T\000"],\
243
[2,"CONFIG:{m 4}\000"]]
247
s=StringIOWithoutClosing()
248
d.makeConnection(protocol.FileWrapper(s))
251
d.connectionLost(failure.Failure(main.CONNECTION_DONE))
258
if flaps!=beforeexpect:
259
for i in range(len(flaps)):
260
if flaps[i]!=beforeexpect[i]:
261
raise AssertionError("SavedValuesTest Before Failed!\nactual:%s\nshould be:%s"%(flaps[i],beforeexpect[i]))
262
raise AssertionError("SavedValuesTest Before Failed with incorrect length!\nactual:%s\nshould be:%s"%(flaps,beforeexpect))
265
s=StringIOWithoutClosing()
266
d.makeConnection(protocol.FileWrapper(s))
267
for i in badpasssend:
269
d.connectionLost(failure.Failure(main.CONNECTION_DONE))
276
if flaps!=badpassexpect:
277
for i in range(len(flaps)):
278
if flaps[i]!=badpassexpect[i]:
279
raise AssertionError("SavedValuesTest BadPass Failed!\nactual:%s\nshould be:%s"%(flaps[i],badpassexpect[i]))
280
raise AssertionError("SavedValuesTest BadPass Failed with incorrect length!\nactual:%s\nshould be:%s"%(flaps,badpassexpect))
283
s=StringIOWithoutClosing()
284
d.makeConnection(protocol.FileWrapper(s))
285
for i in goodpasssend:
287
d.connectionLost(failure.Failure(main.CONNECTION_DONE))
294
if flaps!=goodpassexpect:
295
for i in range(len(flaps)):
296
if flaps[i]!=goodpassexpect[i]:
297
raise AssertionError("SavedValuesTest GoodPass Failed!\nactual:%s\nshould be:%s"%(flaps[i],goodpassexpect[i]))
298
raise AssertionError("SavedValuesTest GoodPass Failed with incorrect length!\nactual:%s\nshould be:%s"%(flaps,beforeexpect))
299
class TOCPrivacyTestCase(unittest.TestCase):
301
sends=["FLAPON\r\n\r\n",\
302
flap(1,"\000\000\000\001\000\001\000\004test"),\
303
flap(2,"toc_signon localhost 9999 test 0x00 english penguin\000"),\
304
flap(2,"toc_init_done\000"),\
305
flap(2,"toc_add_deny\000"),\
306
flap(2,"toc_send_im test 1\000"),\
307
flap(2,"toc_add_deny test\000"),\
308
flap(2,"toc_send_im test 2\000"),\
309
flap(2,"toc_add_permit\000"),\
310
flap(2,"toc_send_im test 3\000"),\
311
flap(2,"toc_add_permit test\000"),\
312
flap(2,"toc_send_im test 4\000")]
313
expect=[[1,"\000\000\000\001"],\
314
[2,"SIGN_ON:TOC1.0\000"],\
315
[2,"NICK:test\000"],\
317
[2,"IM_IN:test:F:1\000"],\
318
[2,"ERROR:901:test\000"],\
319
[2,"ERROR:901:test\000"],\
320
[2,"IM_IN:test:F:4\000"]]
322
d.factory=toc.TOCFactory()
323
s=StringIOWithoutClosing()
324
d.makeConnection(protocol.FileWrapper(s))
327
d.connectionLost(failure.Failure(main.CONNECTION_DONE))
335
for i in range(len(flaps)):
336
if flaps[i]!=expect[i]:
337
raise AssertionError("PrivacyTest Before Failed!\nactual:%s\nshould be:%s"%(flaps[i],expect[i]))
338
raise AssertionError("PrivacyTest Before Failed with incorrect length!\nactual:%s\nshould be:%s"%(flaps,expect))
339
testCases=[TOCGeneralTestCase,TOCMultiPacketTestCase,TOCSavedValuesTestCase,TOCPrivacyTestCase]