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, components
9
from twisted.pair import ethernet, raw
10
from zope.interface import implements
13
implements(raw.IRawPacketProtocol)
15
def __init__(self, expecting):
16
self.expecting = list(expecting)
18
def datagramReceived(self, data, **kw):
19
assert self.expecting, 'Got a packet when not expecting anymore.'
20
expect = self.expecting.pop(0)
21
assert expect == (data, kw), \
22
"Expected %r, got %r" % (
26
class EthernetTestCase(unittest.TestCase):
27
def testPacketParsing(self):
28
proto = ethernet.EthernetProtocol()
39
proto.addProto(0x0800, p1)
41
proto.datagramReceived("123456987654\x08\x00foobar",
44
assert not p1.expecting, \
45
'Should not expect any more packets, but still want %r' % p1.expecting
48
def testMultiplePackets(self):
49
proto = ethernet.EthernetProtocol()
67
proto.addProto(0x0800, p1)
69
proto.datagramReceived("123456987654\x08\x00foobar",
71
proto.datagramReceived("012345abcdef\x08\x00quux",
74
assert not p1.expecting, \
75
'Should not expect any more packets, but still want %r' % p1.expecting
78
def testMultipleSameProtos(self):
79
proto = ethernet.EthernetProtocol()
102
proto.addProto(0x0800, p1)
103
proto.addProto(0x0800, p2)
105
proto.datagramReceived("123456987654\x08\x00foobar",
108
assert not p1.expecting, \
109
'Should not expect any more packets, but still want %r' % p1.expecting
110
assert not p2.expecting, \
111
'Should not expect any more packets, but still want %r' % p2.expecting
113
def testWrongProtoNotSeen(self):
114
proto = ethernet.EthernetProtocol()
116
proto.addProto(0x0801, p1)
118
proto.datagramReceived("123456987654\x08\x00foobar",
120
proto.datagramReceived("012345abcdef\x08\x00quux",
123
def testDemuxing(self):
124
proto = ethernet.EthernetProtocol()
142
proto.addProto(0x0800, p1)
161
proto.addProto(0x0806, p2)
163
proto.datagramReceived("123456987654\x08\x00foobar",
165
proto.datagramReceived("012345abcdef\x08\x06quux",
167
proto.datagramReceived("123456987654\x08\x06foobar",
169
proto.datagramReceived("012345abcdef\x08\x00quux",
172
assert not p1.expecting, \
173
'Should not expect any more packets, but still want %r' % p1.expecting
174
assert not p2.expecting, \
175
'Should not expect any more packets, but still want %r' % p2.expecting
177
def testAddingBadProtos_WrongLevel(self):
178
"""Adding a wrong level protocol raises an exception."""
179
e = ethernet.EthernetProtocol()
181
e.addProto(42, "silliness")
182
except components.CannotAdapt:
185
raise AssertionError, 'addProto must raise an exception for bad protocols'
188
def testAddingBadProtos_TooSmall(self):
189
"""Adding a protocol with a negative number raises an exception."""
190
e = ethernet.EthernetProtocol()
192
e.addProto(-1, MyProtocol([]))
194
if e.args == ('Added protocol must be positive or zero',):
199
raise AssertionError, 'addProto must raise an exception for bad protocols'
202
def testAddingBadProtos_TooBig(self):
203
"""Adding a protocol with a number >=2**16 raises an exception."""
204
e = ethernet.EthernetProtocol()
206
e.addProto(2**16, MyProtocol([]))
208
if e.args == ('Added protocol must fit in 16 bits',):
213
raise AssertionError, 'addProto must raise an exception for bad protocols'
215
def testAddingBadProtos_TooBig2(self):
216
"""Adding a protocol with a number >=2**16 raises an exception."""
217
e = ethernet.EthernetProtocol()
219
e.addProto(2**16+1, MyProtocol([]))
221
if e.args == ('Added protocol must fit in 16 bits',):
226
raise AssertionError, 'addProto must raise an exception for bad protocols'