~cbehrens/nova/lp844160-build-works-with-zones

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/twisted/words/test/test_toc.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2001-2009 Twisted Matrix Laboratories.
 
2
# See LICENSE for details.
 
3
 
 
4
 
 
5
from twisted.trial import unittest
 
6
 
 
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
 
11
 
 
12
from struct import pack,unpack
 
13
 
 
14
 
 
15
class DummyTOC(toc.TOC):
 
16
    """
 
17
    used to override authentication, now overrides printing.
 
18
    """
 
19
    def _debug(self,data):
 
20
        pass
 
21
SEQID=1001
 
22
def flap(type,data):
 
23
    global SEQID
 
24
    send="*"
 
25
    send=send+pack("!BHH",type,SEQID,len(data))
 
26
    send=send+data
 
27
    SEQID=SEQID+1
 
28
    return send
 
29
def readFlap(data):
 
30
    if data=="": return [None,""]
 
31
    null,type,seqid,length=unpack("!BBHH",data[:6])
 
32
    val=data[6:6+length]
 
33
    return [[type,val],data[6+length:]]
 
34
    
 
35
class TOCGeneralTestCase(unittest.TestCase):
 
36
    """
 
37
    general testing of TOC functions.
 
38
    """
 
39
    def testTOC(self):
 
40
        self.runTest()
 
41
    def runTest(self):
 
42
        USERS=2
 
43
        data=range(USERS)
 
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"),\
 
61
        flap(5,"\000"),\
 
62
        flap(2,"toc_chat_whisper 0 ooga \"boo ga\"\000"),\
 
63
        flap(2,"toc_chat_leave 0"),\
 
64
        flap(5,"\000"))
 
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"),\
 
70
        flap(5,"\000"),\
 
71
        flap(5,"\000"),\
 
72
        #flap(5,"\000"),\
 
73
        #flap(5,"\000"),\
 
74
        #flap(5,"\000"),\
 
75
        flap(5,"\000"),\
 
76
        flap(5,"\000"),\
 
77
        flap(5,"\000"),\
 
78
        flap(5,"\000"),\
 
79
        flap(5,"\000"),\
 
80
        flap(5,"\000"),\
 
81
        flap(5,"\000"),\
 
82
        #flap(5,"\000"),\
 
83
        flap(2,"toc_chat_accept 0\000"),\
 
84
        flap(2,"toc_chat_send 0 \"hi test\"\000"),\
 
85
        flap(5,"\000"),\
 
86
        flap(2,"toc_chat_leave 0\000"))
 
87
        strings=range(USERS)
 
88
        for i in strings:
 
89
            strings[i]=StringIOWithoutClosing()
 
90
        fac=toc.TOCFactory()
 
91
        dummy=range(USERS)
 
92
        for i in dummy:
 
93
            dummy[i]=DummyTOC()
 
94
            dummy[i].factory=fac
 
95
            dummy[i].makeConnection(protocol.FileWrapper(strings[i]))
 
96
        while sum(map(lambda x: x == (), data)) != USERS:
 
97
            for i in range(USERS):
 
98
                d=data[i]
 
99
                if len(d)>0:
 
100
                    k,data[i]=d[0],d[1:]
 
101
                    for j in k:
 
102
                        dummy[i].dataReceived(j) # test by doing a character at a time
 
103
                else:
 
104
                    dummy[i].connectionLost(failure.Failure(main.CONNECTION_DONE))
 
105
        values=range(USERS)
 
106
        for i in values:
 
107
            values[i]=strings[i].getvalue()
 
108
        flaps=map(lambda x:[],range(USERS))
 
109
        for value in values:
 
110
            i=values.index(value)
 
111
            f,value=readFlap(value)
 
112
            while f:
 
113
                flaps[i].append(f)
 
114
                f,value=readFlap(value)
 
115
        ts=range(USERS)
 
116
        for t in ts:
 
117
            ts[t]=dummy[t].signontime
 
118
        shouldequal=range(USERS)
 
119
        shouldequal[0]=[ \
 
120
        [1,"\000\000\000\001"],\
 
121
        [2,"SIGN_ON:TOC1.0\000"],\
 
122
        [2,"NICK:test\000"],\
 
123
        [2,"CONFIG:\00"],\
 
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"]]
 
140
        shouldequal[1]=[ \
 
141
        [1,"\000\000\000\001"],\
 
142
        [2,"SIGN_ON:TOC1.0\000"],\
 
143
        [2,"NICK:ooga\000"],\
 
144
        [2,"CONFIG:\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):
 
165
    """
 
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.
 
167
    """
 
168
    def testTOC(self):
 
169
        self.runTest()
 
170
    def runTest(self):
 
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"],\
 
179
         [2,"CONFIG:\000"],\
 
180
         [2,"IM_IN:test:F:hi\000"]]
 
181
        data=""
 
182
        for i in packets:
 
183
            data=data+i
 
184
        s=StringIOWithoutClosing()
 
185
        d=DummyTOC()
 
186
        fac=toc.TOCFactory()
 
187
        d.factory=fac
 
188
        d.makeConnection(protocol.FileWrapper(s))
 
189
        d.dataReceived(data)
 
190
        d.connectionLost(failure.Failure(main.CONNECTION_DONE))
 
191
        value=s.getvalue()
 
192
        flaps=[]
 
193
        f,value=readFlap(value)
 
194
        while f:
 
195
            flaps.append(f)
 
196
            f,value=readFlap(value)
 
197
        if flaps!=shouldbe:
 
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):
 
202
    def testTOC(self):
 
203
        self.runTest()
 
204
    def runTest(self):
 
205
        password1=toc.roast("test pass")
 
206
        password2=toc.roast("pass test")
 
207
        beforesend=[\
 
208
         "FLAPON\r\n\r\n",\
 
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")]
 
217
        beforeexpect=[\
 
218
         [1,"\000\000\000\001"],\
 
219
         [2,"SIGN_ON:TOC1.0\000"],\
 
220
         [2,"NICK:test\000"],\
 
221
         [2,"CONFIG:\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"]]
 
226
        badpasssend=[\
 
227
         "FLAPON\r\n\r\n",\
 
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")]
 
231
        badpassexpect=[\
 
232
         [1,"\000\00\000\001"],\
 
233
         [2,"ERROR:980\000"]]
 
234
        goodpasssend=[\
 
235
         "FLAPON\r\n\r\n",\
 
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")]
 
239
        goodpassexpect=[\
 
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"]]
 
244
        fac=toc.TOCFactory()
 
245
        d=DummyTOC()
 
246
        d.factory=fac
 
247
        s=StringIOWithoutClosing()
 
248
        d.makeConnection(protocol.FileWrapper(s))
 
249
        for i in beforesend:
 
250
            d.dataReceived(i)
 
251
        d.connectionLost(failure.Failure(main.CONNECTION_DONE))
 
252
        v=s.getvalue()
 
253
        flaps=[]
 
254
        f,v=readFlap(v)
 
255
        while f:
 
256
            flaps.append(f)
 
257
            f,v=readFlap(v)
 
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))
 
263
        d=DummyTOC()
 
264
        d.factory=fac
 
265
        s=StringIOWithoutClosing()
 
266
        d.makeConnection(protocol.FileWrapper(s))
 
267
        for i in badpasssend:
 
268
            d.dataReceived(i)
 
269
        d.connectionLost(failure.Failure(main.CONNECTION_DONE))
 
270
        v=s.getvalue()
 
271
        flaps=[]
 
272
        f,v=readFlap(v)
 
273
        while f:
 
274
            flaps.append(f)
 
275
            f,v=readFlap(v)
 
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))
 
281
        d=DummyTOC()
 
282
        d.factory=fac
 
283
        s=StringIOWithoutClosing()
 
284
        d.makeConnection(protocol.FileWrapper(s))
 
285
        for i in goodpasssend:
 
286
            d.dataReceived(i)
 
287
        d.connectionLost(failure.Failure(main.CONNECTION_DONE))
 
288
        v=s.getvalue()
 
289
        flaps=[]
 
290
        f,v=readFlap(v)
 
291
        while f:
 
292
            flaps.append(f)
 
293
            f,v=readFlap(v)
 
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):
 
300
    def runTest(self):
 
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"],\
 
316
         [2,"CONFIG:\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"]]
 
321
        d=DummyTOC()
 
322
        d.factory=toc.TOCFactory()
 
323
        s=StringIOWithoutClosing()
 
324
        d.makeConnection(protocol.FileWrapper(s))
 
325
        for i in sends:
 
326
            d.dataReceived(i)
 
327
        d.connectionLost(failure.Failure(main.CONNECTION_DONE))
 
328
        v=s.getvalue()
 
329
        flaps=[]
 
330
        f,v=readFlap(v)
 
331
        while f:
 
332
            flaps.append(f)
 
333
            f,v=readFlap(v)
 
334
        if flaps!=expect:
 
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]
 
340