1
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
2
# See LICENSE for details.
5
from twisted.trial import unittest
7
from twisted.internet import protocol, reactor, error
8
from twisted.python import failure
9
from twisted.pair import rawudp
11
class MyProtocol(protocol.DatagramProtocol):
12
def __init__(self, expecting):
13
self.expecting = list(expecting)
15
def datagramReceived(self, data, (host, port)):
16
assert self.expecting, 'Got a packet when not expecting anymore.'
17
expectData, expectHost, expectPort = self.expecting.pop(0)
19
assert expectData == data, "Expected data %r, got %r" % (expectData, data)
20
assert expectHost == host, "Expected host %r, got %r" % (expectHost, host)
21
assert expectPort == port, "Expected port %d=0x%04x, got %d=0x%04x" % (expectPort, expectPort, port, port)
23
class RawUDPTestCase(unittest.TestCase):
24
def testPacketParsing(self):
25
proto = rawudp.RawUDPProtocol()
28
('foobar', 'testHost', 0x43A2),
31
proto.addProto(0xF00F, p1)
33
proto.datagramReceived("\x43\xA2" #source
47
fragment_offset='dummy',
48
dont_fragment='dummy',
49
more_fragments='dummy',
53
assert not p1.expecting, \
54
'Should not expect any more packets, but still want %r' % p1.expecting
56
def testMultiplePackets(self):
57
proto = rawudp.RawUDPProtocol()
60
('foobar', 'testHost', 0x43A2),
61
('quux', 'otherHost', 0x33FE),
64
proto.addProto(0xF00F, p1)
65
proto.datagramReceived("\x43\xA2" #source
79
fragment_offset='dummy',
80
dont_fragment='dummy',
81
more_fragments='dummy',
84
proto.datagramReceived("\x33\xFE" #source
98
fragment_offset='dummy',
99
dont_fragment='dummy',
100
more_fragments='dummy',
104
assert not p1.expecting, \
105
'Should not expect any more packets, but still want %r' % p1.expecting
108
def testMultipleSameProtos(self):
109
proto = rawudp.RawUDPProtocol()
112
('foobar', 'testHost', 0x43A2),
118
('foobar', 'testHost', 0x43A2),
122
proto.addProto(0xF00F, p1)
123
proto.addProto(0xF00F, p2)
125
proto.datagramReceived("\x43\xA2" #source
139
fragment_offset='dummy',
140
dont_fragment='dummy',
141
more_fragments='dummy',
145
assert not p1.expecting, \
146
'Should not expect any more packets, but still want %r' % p1.expecting
147
assert not p2.expecting, \
148
'Should not expect any more packets, but still want %r' % p2.expecting
150
def testWrongProtoNotSeen(self):
151
proto = rawudp.RawUDPProtocol()
153
proto.addProto(1, p1)
155
proto.datagramReceived("\x43\xA2" #source
169
fragment_offset='dummy',
170
dont_fragment='dummy',
171
more_fragments='dummy',
175
def testDemuxing(self):
176
proto = rawudp.RawUDPProtocol()
179
('foobar', 'testHost', 0x43A2),
180
('quux', 'otherHost', 0x33FE),
183
proto.addProto(0xF00F, p1)
187
('quux', 'otherHost', 0xA401),
188
('foobar', 'testHost', 0xA302),
191
proto.addProto(0xB050, p2)
193
proto.datagramReceived("\xA4\x01" #source
207
fragment_offset='dummy',
208
dont_fragment='dummy',
209
more_fragments='dummy',
212
proto.datagramReceived("\x43\xA2" #source
226
fragment_offset='dummy',
227
dont_fragment='dummy',
228
more_fragments='dummy',
231
proto.datagramReceived("\x33\xFE" #source
245
fragment_offset='dummy',
246
dont_fragment='dummy',
247
more_fragments='dummy',
250
proto.datagramReceived("\xA3\x02" #source
264
fragment_offset='dummy',
265
dont_fragment='dummy',
266
more_fragments='dummy',
270
assert not p1.expecting, \
271
'Should not expect any more packets, but still want %r' % p1.expecting
272
assert not p2.expecting, \
273
'Should not expect any more packets, but still want %r' % p2.expecting
275
def testAddingBadProtos_WrongLevel(self):
276
"""Adding a wrong level protocol raises an exception."""
277
e = rawudp.RawUDPProtocol()
279
e.addProto(42, "silliness")
281
if e.args == ('Added protocol must be an instance of DatagramProtocol',):
286
raise AssertionError, 'addProto must raise an exception for bad protocols'
289
def testAddingBadProtos_TooSmall(self):
290
"""Adding a protocol with a negative number raises an exception."""
291
e = rawudp.RawUDPProtocol()
293
e.addProto(-1, protocol.DatagramProtocol())
295
if e.args == ('Added protocol must be positive or zero',):
300
raise AssertionError, 'addProto must raise an exception for bad protocols'
303
def testAddingBadProtos_TooBig(self):
304
"""Adding a protocol with a number >=2**16 raises an exception."""
305
e = rawudp.RawUDPProtocol()
307
e.addProto(2**16, protocol.DatagramProtocol())
309
if e.args == ('Added protocol must fit in 16 bits',):
314
raise AssertionError, 'addProto must raise an exception for bad protocols'
316
def testAddingBadProtos_TooBig2(self):
317
"""Adding a protocol with a number >=2**16 raises an exception."""
318
e = rawudp.RawUDPProtocol()
320
e.addProto(2**16+1, protocol.DatagramProtocol())
322
if e.args == ('Added protocol must fit in 16 bits',):
327
raise AssertionError, 'addProto must raise an exception for bad protocols'