Open Media Library Platform
This commit is contained in:
commit
411ad5b16f
5849 changed files with 1778641 additions and 0 deletions
|
|
@ -0,0 +1 @@
|
|||
'pair tests'
|
||||
|
|
@ -0,0 +1,224 @@
|
|||
# Copyright (c) Twisted Matrix Laboratories.
|
||||
# See LICENSE for details.
|
||||
from twisted.trial import unittest
|
||||
|
||||
from twisted.python import components
|
||||
from twisted.pair import ethernet, raw
|
||||
from zope.interface import implements
|
||||
|
||||
|
||||
class MyProtocol:
|
||||
implements(raw.IRawPacketProtocol)
|
||||
|
||||
def __init__(self, expecting):
|
||||
self.expecting = list(expecting)
|
||||
|
||||
def datagramReceived(self, data, **kw):
|
||||
assert self.expecting, 'Got a packet when not expecting anymore.'
|
||||
expect = self.expecting.pop(0)
|
||||
assert expect == (data, kw), \
|
||||
"Expected %r, got %r" % (
|
||||
expect, (data, kw),
|
||||
)
|
||||
|
||||
class EthernetTestCase(unittest.TestCase):
|
||||
def testPacketParsing(self):
|
||||
proto = ethernet.EthernetProtocol()
|
||||
p1 = MyProtocol([
|
||||
|
||||
('foobar', {
|
||||
'partial': 0,
|
||||
'dest': "123456",
|
||||
'source': "987654",
|
||||
'protocol': 0x0800,
|
||||
}),
|
||||
|
||||
])
|
||||
proto.addProto(0x0800, p1)
|
||||
|
||||
proto.datagramReceived("123456987654\x08\x00foobar",
|
||||
partial=0)
|
||||
|
||||
assert not p1.expecting, \
|
||||
'Should not expect any more packets, but still want %r' % p1.expecting
|
||||
|
||||
|
||||
def testMultiplePackets(self):
|
||||
proto = ethernet.EthernetProtocol()
|
||||
p1 = MyProtocol([
|
||||
|
||||
('foobar', {
|
||||
'partial': 0,
|
||||
'dest': "123456",
|
||||
'source': "987654",
|
||||
'protocol': 0x0800,
|
||||
}),
|
||||
|
||||
('quux', {
|
||||
'partial': 1,
|
||||
'dest': "012345",
|
||||
'source': "abcdef",
|
||||
'protocol': 0x0800,
|
||||
}),
|
||||
|
||||
])
|
||||
proto.addProto(0x0800, p1)
|
||||
|
||||
proto.datagramReceived("123456987654\x08\x00foobar",
|
||||
partial=0)
|
||||
proto.datagramReceived("012345abcdef\x08\x00quux",
|
||||
partial=1)
|
||||
|
||||
assert not p1.expecting, \
|
||||
'Should not expect any more packets, but still want %r' % p1.expecting
|
||||
|
||||
|
||||
def testMultipleSameProtos(self):
|
||||
proto = ethernet.EthernetProtocol()
|
||||
p1 = MyProtocol([
|
||||
|
||||
('foobar', {
|
||||
'partial': 0,
|
||||
'dest': "123456",
|
||||
'source': "987654",
|
||||
'protocol': 0x0800,
|
||||
}),
|
||||
|
||||
])
|
||||
|
||||
p2 = MyProtocol([
|
||||
|
||||
('foobar', {
|
||||
'partial': 0,
|
||||
'dest': "123456",
|
||||
'source': "987654",
|
||||
'protocol': 0x0800,
|
||||
}),
|
||||
|
||||
])
|
||||
|
||||
proto.addProto(0x0800, p1)
|
||||
proto.addProto(0x0800, p2)
|
||||
|
||||
proto.datagramReceived("123456987654\x08\x00foobar",
|
||||
partial=0)
|
||||
|
||||
assert not p1.expecting, \
|
||||
'Should not expect any more packets, but still want %r' % p1.expecting
|
||||
assert not p2.expecting, \
|
||||
'Should not expect any more packets, but still want %r' % p2.expecting
|
||||
|
||||
def testWrongProtoNotSeen(self):
|
||||
proto = ethernet.EthernetProtocol()
|
||||
p1 = MyProtocol([])
|
||||
proto.addProto(0x0801, p1)
|
||||
|
||||
proto.datagramReceived("123456987654\x08\x00foobar",
|
||||
partial=0)
|
||||
proto.datagramReceived("012345abcdef\x08\x00quux",
|
||||
partial=1)
|
||||
|
||||
def testDemuxing(self):
|
||||
proto = ethernet.EthernetProtocol()
|
||||
p1 = MyProtocol([
|
||||
|
||||
('foobar', {
|
||||
'partial': 0,
|
||||
'dest': "123456",
|
||||
'source': "987654",
|
||||
'protocol': 0x0800,
|
||||
}),
|
||||
|
||||
('quux', {
|
||||
'partial': 1,
|
||||
'dest': "012345",
|
||||
'source': "abcdef",
|
||||
'protocol': 0x0800,
|
||||
}),
|
||||
|
||||
])
|
||||
proto.addProto(0x0800, p1)
|
||||
|
||||
p2 = MyProtocol([
|
||||
|
||||
('quux', {
|
||||
'partial': 1,
|
||||
'dest': "012345",
|
||||
'source': "abcdef",
|
||||
'protocol': 0x0806,
|
||||
}),
|
||||
|
||||
('foobar', {
|
||||
'partial': 0,
|
||||
'dest': "123456",
|
||||
'source': "987654",
|
||||
'protocol': 0x0806,
|
||||
}),
|
||||
|
||||
])
|
||||
proto.addProto(0x0806, p2)
|
||||
|
||||
proto.datagramReceived("123456987654\x08\x00foobar",
|
||||
partial=0)
|
||||
proto.datagramReceived("012345abcdef\x08\x06quux",
|
||||
partial=1)
|
||||
proto.datagramReceived("123456987654\x08\x06foobar",
|
||||
partial=0)
|
||||
proto.datagramReceived("012345abcdef\x08\x00quux",
|
||||
partial=1)
|
||||
|
||||
assert not p1.expecting, \
|
||||
'Should not expect any more packets, but still want %r' % p1.expecting
|
||||
assert not p2.expecting, \
|
||||
'Should not expect any more packets, but still want %r' % p2.expecting
|
||||
|
||||
def testAddingBadProtos_WrongLevel(self):
|
||||
"""Adding a wrong level protocol raises an exception."""
|
||||
e = ethernet.EthernetProtocol()
|
||||
try:
|
||||
e.addProto(42, "silliness")
|
||||
except components.CannotAdapt:
|
||||
pass
|
||||
else:
|
||||
raise AssertionError, 'addProto must raise an exception for bad protocols'
|
||||
|
||||
|
||||
def testAddingBadProtos_TooSmall(self):
|
||||
"""Adding a protocol with a negative number raises an exception."""
|
||||
e = ethernet.EthernetProtocol()
|
||||
try:
|
||||
e.addProto(-1, MyProtocol([]))
|
||||
except TypeError, e:
|
||||
if e.args == ('Added protocol must be positive or zero',):
|
||||
pass
|
||||
else:
|
||||
raise
|
||||
else:
|
||||
raise AssertionError, 'addProto must raise an exception for bad protocols'
|
||||
|
||||
|
||||
def testAddingBadProtos_TooBig(self):
|
||||
"""Adding a protocol with a number >=2**16 raises an exception."""
|
||||
e = ethernet.EthernetProtocol()
|
||||
try:
|
||||
e.addProto(2**16, MyProtocol([]))
|
||||
except TypeError, e:
|
||||
if e.args == ('Added protocol must fit in 16 bits',):
|
||||
pass
|
||||
else:
|
||||
raise
|
||||
else:
|
||||
raise AssertionError, 'addProto must raise an exception for bad protocols'
|
||||
|
||||
def testAddingBadProtos_TooBig2(self):
|
||||
"""Adding a protocol with a number >=2**16 raises an exception."""
|
||||
e = ethernet.EthernetProtocol()
|
||||
try:
|
||||
e.addProto(2**16+1, MyProtocol([]))
|
||||
except TypeError, e:
|
||||
if e.args == ('Added protocol must fit in 16 bits',):
|
||||
pass
|
||||
else:
|
||||
raise
|
||||
else:
|
||||
raise AssertionError, 'addProto must raise an exception for bad protocols'
|
||||
415
Linux/lib/python2.7/site-packages/twisted/pair/test/test_ip.py
Normal file
415
Linux/lib/python2.7/site-packages/twisted/pair/test/test_ip.py
Normal file
|
|
@ -0,0 +1,415 @@
|
|||
# Copyright (c) Twisted Matrix Laboratories.
|
||||
# See LICENSE for details.
|
||||
from twisted.trial import unittest
|
||||
|
||||
from twisted.python import components
|
||||
from twisted.pair import ip, raw
|
||||
from zope import interface
|
||||
|
||||
|
||||
class MyProtocol:
|
||||
interface.implements(raw.IRawDatagramProtocol)
|
||||
|
||||
def __init__(self, expecting):
|
||||
self.expecting = list(expecting)
|
||||
|
||||
def datagramReceived(self, data, **kw):
|
||||
assert self.expecting, 'Got a packet when not expecting anymore.'
|
||||
expectData, expectKw = self.expecting.pop(0)
|
||||
|
||||
expectKwKeys = expectKw.keys(); expectKwKeys.sort()
|
||||
kwKeys = kw.keys(); kwKeys.sort()
|
||||
assert expectKwKeys == kwKeys, "Expected %r, got %r" % (expectKwKeys, kwKeys)
|
||||
|
||||
for k in expectKwKeys:
|
||||
assert expectKw[k] == kw[k], "Expected %s=%r, got %r" % (k, expectKw[k], kw[k])
|
||||
assert expectKw == kw, "Expected %r, got %r" % (expectKw, kw)
|
||||
assert expectData == data, "Expected %r, got %r" % (expectData, data)
|
||||
|
||||
class IPTestCase(unittest.TestCase):
|
||||
def testPacketParsing(self):
|
||||
proto = ip.IPProtocol()
|
||||
p1 = MyProtocol([
|
||||
|
||||
('foobar', {
|
||||
'partial': 0,
|
||||
'dest': '1.2.3.4',
|
||||
'source': '5.6.7.8',
|
||||
'protocol': 0x0F,
|
||||
'version': 4,
|
||||
'ihl': 20,
|
||||
'tos': 7,
|
||||
'tot_len': 20+6,
|
||||
'fragment_id': 0xDEAD,
|
||||
'fragment_offset': 0x1EEF,
|
||||
'dont_fragment': 0,
|
||||
'more_fragments': 1,
|
||||
'ttl': 0xC0,
|
||||
}),
|
||||
|
||||
])
|
||||
proto.addProto(0x0F, p1)
|
||||
|
||||
proto.datagramReceived("\x54" #ihl version
|
||||
+ "\x07" #tos
|
||||
+ "\x00\x1a" #tot_len
|
||||
+ "\xDE\xAD" #id
|
||||
+ "\xBE\xEF" #frag_off
|
||||
+ "\xC0" #ttl
|
||||
+ "\x0F" #protocol
|
||||
+ "FE" #checksum
|
||||
+ "\x05\x06\x07\x08" + "\x01\x02\x03\x04" + "foobar",
|
||||
partial=0,
|
||||
dest='dummy',
|
||||
source='dummy',
|
||||
protocol='dummy',
|
||||
)
|
||||
|
||||
assert not p1.expecting, \
|
||||
'Should not expect any more packets, but still want %r' % p1.expecting
|
||||
|
||||
def testMultiplePackets(self):
|
||||
proto = ip.IPProtocol()
|
||||
p1 = MyProtocol([
|
||||
|
||||
('foobar', {
|
||||
'partial': 0,
|
||||
'dest': '1.2.3.4',
|
||||
'source': '5.6.7.8',
|
||||
'protocol': 0x0F,
|
||||
'version': 4,
|
||||
'ihl': 20,
|
||||
'tos': 7,
|
||||
'tot_len': 20+6,
|
||||
'fragment_id': 0xDEAD,
|
||||
'fragment_offset': 0x1EEF,
|
||||
'dont_fragment': 0,
|
||||
'more_fragments': 1,
|
||||
'ttl': 0xC0,
|
||||
}),
|
||||
|
||||
('quux', {
|
||||
'partial': 1,
|
||||
'dest': '5.4.3.2',
|
||||
'source': '6.7.8.9',
|
||||
'protocol': 0x0F,
|
||||
'version': 4,
|
||||
'ihl': 20,
|
||||
'tos': 7,
|
||||
'tot_len': 20+6,
|
||||
'fragment_id': 0xDEAD,
|
||||
'fragment_offset': 0x1EEF,
|
||||
'dont_fragment': 0,
|
||||
'more_fragments': 1,
|
||||
'ttl': 0xC0,
|
||||
}),
|
||||
|
||||
])
|
||||
proto.addProto(0x0F, p1)
|
||||
proto.datagramReceived("\x54" #ihl version
|
||||
+ "\x07" #tos
|
||||
+ "\x00\x1a" #tot_len
|
||||
+ "\xDE\xAD" #id
|
||||
+ "\xBE\xEF" #frag_off
|
||||
+ "\xC0" #ttl
|
||||
+ "\x0F" #protocol
|
||||
+ "FE" #checksum
|
||||
+ "\x05\x06\x07\x08" + "\x01\x02\x03\x04" + "foobar",
|
||||
partial=0,
|
||||
dest='dummy',
|
||||
source='dummy',
|
||||
protocol='dummy',
|
||||
)
|
||||
proto.datagramReceived("\x54" #ihl version
|
||||
+ "\x07" #tos
|
||||
+ "\x00\x1a" #tot_len
|
||||
+ "\xDE\xAD" #id
|
||||
+ "\xBE\xEF" #frag_off
|
||||
+ "\xC0" #ttl
|
||||
+ "\x0F" #protocol
|
||||
+ "FE" #checksum
|
||||
+ "\x06\x07\x08\x09" + "\x05\x04\x03\x02" + "quux",
|
||||
partial=1,
|
||||
dest='dummy',
|
||||
source='dummy',
|
||||
protocol='dummy',
|
||||
)
|
||||
|
||||
assert not p1.expecting, \
|
||||
'Should not expect any more packets, but still want %r' % p1.expecting
|
||||
|
||||
|
||||
def testMultipleSameProtos(self):
|
||||
proto = ip.IPProtocol()
|
||||
p1 = MyProtocol([
|
||||
|
||||
('foobar', {
|
||||
'partial': 0,
|
||||
'dest': '1.2.3.4',
|
||||
'source': '5.6.7.8',
|
||||
'protocol': 0x0F,
|
||||
'version': 4,
|
||||
'ihl': 20,
|
||||
'tos': 7,
|
||||
'tot_len': 20+6,
|
||||
'fragment_id': 0xDEAD,
|
||||
'fragment_offset': 0x1EEF,
|
||||
'dont_fragment': 0,
|
||||
'more_fragments': 1,
|
||||
'ttl': 0xC0,
|
||||
}),
|
||||
|
||||
])
|
||||
|
||||
p2 = MyProtocol([
|
||||
|
||||
('foobar', {
|
||||
'partial': 0,
|
||||
'dest': '1.2.3.4',
|
||||
'source': '5.6.7.8',
|
||||
'protocol': 0x0F,
|
||||
'version': 4,
|
||||
'ihl': 20,
|
||||
'tos': 7,
|
||||
'tot_len': 20+6,
|
||||
'fragment_id': 0xDEAD,
|
||||
'fragment_offset': 0x1EEF,
|
||||
'dont_fragment': 0,
|
||||
'more_fragments': 1,
|
||||
'ttl': 0xC0,
|
||||
}),
|
||||
|
||||
])
|
||||
|
||||
proto.addProto(0x0F, p1)
|
||||
proto.addProto(0x0F, p2)
|
||||
|
||||
proto.datagramReceived("\x54" #ihl version
|
||||
+ "\x07" #tos
|
||||
+ "\x00\x1a" #tot_len
|
||||
+ "\xDE\xAD" #id
|
||||
+ "\xBE\xEF" #frag_off
|
||||
+ "\xC0" #ttl
|
||||
+ "\x0F" #protocol
|
||||
+ "FE" #checksum
|
||||
+ "\x05\x06\x07\x08" + "\x01\x02\x03\x04" + "foobar",
|
||||
partial=0,
|
||||
dest='dummy',
|
||||
source='dummy',
|
||||
protocol='dummy',
|
||||
)
|
||||
|
||||
assert not p1.expecting, \
|
||||
'Should not expect any more packets, but still want %r' % p1.expecting
|
||||
assert not p2.expecting, \
|
||||
'Should not expect any more packets, but still want %r' % p2.expecting
|
||||
|
||||
def testWrongProtoNotSeen(self):
|
||||
proto = ip.IPProtocol()
|
||||
p1 = MyProtocol([])
|
||||
proto.addProto(1, p1)
|
||||
|
||||
proto.datagramReceived("\x54" #ihl version
|
||||
+ "\x07" #tos
|
||||
+ "\x00\x1a" #tot_len
|
||||
+ "\xDE\xAD" #id
|
||||
+ "\xBE\xEF" #frag_off
|
||||
+ "\xC0" #ttl
|
||||
+ "\x0F" #protocol
|
||||
+ "FE" #checksum
|
||||
+ "\x05\x06\x07\x08" + "\x01\x02\x03\x04" + "foobar",
|
||||
partial=0,
|
||||
dest='dummy',
|
||||
source='dummy',
|
||||
protocol='dummy',
|
||||
)
|
||||
|
||||
def testDemuxing(self):
|
||||
proto = ip.IPProtocol()
|
||||
p1 = MyProtocol([
|
||||
|
||||
('foobar', {
|
||||
'partial': 0,
|
||||
'dest': '1.2.3.4',
|
||||
'source': '5.6.7.8',
|
||||
'protocol': 0x0F,
|
||||
'version': 4,
|
||||
'ihl': 20,
|
||||
'tos': 7,
|
||||
'tot_len': 20+6,
|
||||
'fragment_id': 0xDEAD,
|
||||
'fragment_offset': 0x1EEF,
|
||||
'dont_fragment': 0,
|
||||
'more_fragments': 1,
|
||||
'ttl': 0xC0,
|
||||
}),
|
||||
|
||||
('quux', {
|
||||
'partial': 1,
|
||||
'dest': '5.4.3.2',
|
||||
'source': '6.7.8.9',
|
||||
'protocol': 0x0F,
|
||||
'version': 4,
|
||||
'ihl': 20,
|
||||
'tos': 7,
|
||||
'tot_len': 20+6,
|
||||
'fragment_id': 0xDEAD,
|
||||
'fragment_offset': 0x1EEF,
|
||||
'dont_fragment': 0,
|
||||
'more_fragments': 1,
|
||||
'ttl': 0xC0,
|
||||
}),
|
||||
|
||||
])
|
||||
proto.addProto(0x0F, p1)
|
||||
|
||||
p2 = MyProtocol([
|
||||
|
||||
('quux', {
|
||||
'partial': 1,
|
||||
'dest': '5.4.3.2',
|
||||
'source': '6.7.8.9',
|
||||
'protocol': 0x0A,
|
||||
'version': 4,
|
||||
'ihl': 20,
|
||||
'tos': 7,
|
||||
'tot_len': 20+6,
|
||||
'fragment_id': 0xDEAD,
|
||||
'fragment_offset': 0x1EEF,
|
||||
'dont_fragment': 0,
|
||||
'more_fragments': 1,
|
||||
'ttl': 0xC0,
|
||||
}),
|
||||
|
||||
('foobar', {
|
||||
'partial': 0,
|
||||
'dest': '1.2.3.4',
|
||||
'source': '5.6.7.8',
|
||||
'protocol': 0x0A,
|
||||
'version': 4,
|
||||
'ihl': 20,
|
||||
'tos': 7,
|
||||
'tot_len': 20+6,
|
||||
'fragment_id': 0xDEAD,
|
||||
'fragment_offset': 0x1EEF,
|
||||
'dont_fragment': 0,
|
||||
'more_fragments': 1,
|
||||
'ttl': 0xC0,
|
||||
}),
|
||||
|
||||
|
||||
])
|
||||
proto.addProto(0x0A, p2)
|
||||
|
||||
proto.datagramReceived("\x54" #ihl version
|
||||
+ "\x07" #tos
|
||||
+ "\x00\x1a" #tot_len
|
||||
+ "\xDE\xAD" #id
|
||||
+ "\xBE\xEF" #frag_off
|
||||
+ "\xC0" #ttl
|
||||
+ "\x0A" #protocol
|
||||
+ "FE" #checksum
|
||||
+ "\x06\x07\x08\x09" + "\x05\x04\x03\x02" + "quux",
|
||||
partial=1,
|
||||
dest='dummy',
|
||||
source='dummy',
|
||||
protocol='dummy',
|
||||
)
|
||||
proto.datagramReceived("\x54" #ihl version
|
||||
+ "\x07" #tos
|
||||
+ "\x00\x1a" #tot_len
|
||||
+ "\xDE\xAD" #id
|
||||
+ "\xBE\xEF" #frag_off
|
||||
+ "\xC0" #ttl
|
||||
+ "\x0F" #protocol
|
||||
+ "FE" #checksum
|
||||
+ "\x05\x06\x07\x08" + "\x01\x02\x03\x04" + "foobar",
|
||||
partial=0,
|
||||
dest='dummy',
|
||||
source='dummy',
|
||||
protocol='dummy',
|
||||
)
|
||||
proto.datagramReceived("\x54" #ihl version
|
||||
+ "\x07" #tos
|
||||
+ "\x00\x1a" #tot_len
|
||||
+ "\xDE\xAD" #id
|
||||
+ "\xBE\xEF" #frag_off
|
||||
+ "\xC0" #ttl
|
||||
+ "\x0F" #protocol
|
||||
+ "FE" #checksum
|
||||
+ "\x06\x07\x08\x09" + "\x05\x04\x03\x02" + "quux",
|
||||
partial=1,
|
||||
dest='dummy',
|
||||
source='dummy',
|
||||
protocol='dummy',
|
||||
)
|
||||
proto.datagramReceived("\x54" #ihl version
|
||||
+ "\x07" #tos
|
||||
+ "\x00\x1a" #tot_len
|
||||
+ "\xDE\xAD" #id
|
||||
+ "\xBE\xEF" #frag_off
|
||||
+ "\xC0" #ttl
|
||||
+ "\x0A" #protocol
|
||||
+ "FE" #checksum
|
||||
+ "\x05\x06\x07\x08" + "\x01\x02\x03\x04" + "foobar",
|
||||
partial=0,
|
||||
dest='dummy',
|
||||
source='dummy',
|
||||
protocol='dummy',
|
||||
)
|
||||
|
||||
assert not p1.expecting, \
|
||||
'Should not expect any more packets, but still want %r' % p1.expecting
|
||||
assert not p2.expecting, \
|
||||
'Should not expect any more packets, but still want %r' % p2.expecting
|
||||
|
||||
def testAddingBadProtos_WrongLevel(self):
|
||||
"""Adding a wrong level protocol raises an exception."""
|
||||
e = ip.IPProtocol()
|
||||
try:
|
||||
e.addProto(42, "silliness")
|
||||
except components.CannotAdapt:
|
||||
pass
|
||||
else:
|
||||
raise AssertionError, 'addProto must raise an exception for bad protocols'
|
||||
|
||||
|
||||
def testAddingBadProtos_TooSmall(self):
|
||||
"""Adding a protocol with a negative number raises an exception."""
|
||||
e = ip.IPProtocol()
|
||||
try:
|
||||
e.addProto(-1, MyProtocol([]))
|
||||
except TypeError, e:
|
||||
if e.args == ('Added protocol must be positive or zero',):
|
||||
pass
|
||||
else:
|
||||
raise
|
||||
else:
|
||||
raise AssertionError, 'addProto must raise an exception for bad protocols'
|
||||
|
||||
|
||||
def testAddingBadProtos_TooBig(self):
|
||||
"""Adding a protocol with a number >=2**32 raises an exception."""
|
||||
e = ip.IPProtocol()
|
||||
try:
|
||||
e.addProto(2L**32, MyProtocol([]))
|
||||
except TypeError, e:
|
||||
if e.args == ('Added protocol must fit in 32 bits',):
|
||||
pass
|
||||
else:
|
||||
raise
|
||||
else:
|
||||
raise AssertionError, 'addProto must raise an exception for bad protocols'
|
||||
|
||||
def testAddingBadProtos_TooBig2(self):
|
||||
"""Adding a protocol with a number >=2**32 raises an exception."""
|
||||
e = ip.IPProtocol()
|
||||
try:
|
||||
e.addProto(2L**32+1, MyProtocol([]))
|
||||
except TypeError, e:
|
||||
if e.args == ('Added protocol must fit in 32 bits',):
|
||||
pass
|
||||
else:
|
||||
raise
|
||||
else:
|
||||
raise AssertionError, 'addProto must raise an exception for bad protocols'
|
||||
|
|
@ -0,0 +1,326 @@
|
|||
# Copyright (c) Twisted Matrix Laboratories.
|
||||
# See LICENSE for details.
|
||||
|
||||
#
|
||||
from twisted.trial import unittest
|
||||
|
||||
from twisted.internet import protocol
|
||||
from twisted.pair import rawudp
|
||||
|
||||
class MyProtocol(protocol.DatagramProtocol):
|
||||
def __init__(self, expecting):
|
||||
self.expecting = list(expecting)
|
||||
|
||||
def datagramReceived(self, data, (host, port)):
|
||||
assert self.expecting, 'Got a packet when not expecting anymore.'
|
||||
expectData, expectHost, expectPort = self.expecting.pop(0)
|
||||
|
||||
assert expectData == data, "Expected data %r, got %r" % (expectData, data)
|
||||
assert expectHost == host, "Expected host %r, got %r" % (expectHost, host)
|
||||
assert expectPort == port, "Expected port %d=0x%04x, got %d=0x%04x" % (expectPort, expectPort, port, port)
|
||||
|
||||
class RawUDPTestCase(unittest.TestCase):
|
||||
def testPacketParsing(self):
|
||||
proto = rawudp.RawUDPProtocol()
|
||||
p1 = MyProtocol([
|
||||
|
||||
('foobar', 'testHost', 0x43A2),
|
||||
|
||||
])
|
||||
proto.addProto(0xF00F, p1)
|
||||
|
||||
proto.datagramReceived("\x43\xA2" #source
|
||||
+ "\xf0\x0f" #dest
|
||||
+ "\x00\x06" #len
|
||||
+ "\xDE\xAD" #check
|
||||
+ "foobar",
|
||||
partial=0,
|
||||
dest='dummy',
|
||||
source='testHost',
|
||||
protocol='dummy',
|
||||
version='dummy',
|
||||
ihl='dummy',
|
||||
tos='dummy',
|
||||
tot_len='dummy',
|
||||
fragment_id='dummy',
|
||||
fragment_offset='dummy',
|
||||
dont_fragment='dummy',
|
||||
more_fragments='dummy',
|
||||
ttl='dummy',
|
||||
)
|
||||
|
||||
assert not p1.expecting, \
|
||||
'Should not expect any more packets, but still want %r' % p1.expecting
|
||||
|
||||
def testMultiplePackets(self):
|
||||
proto = rawudp.RawUDPProtocol()
|
||||
p1 = MyProtocol([
|
||||
|
||||
('foobar', 'testHost', 0x43A2),
|
||||
('quux', 'otherHost', 0x33FE),
|
||||
|
||||
])
|
||||
proto.addProto(0xF00F, p1)
|
||||
proto.datagramReceived("\x43\xA2" #source
|
||||
+ "\xf0\x0f" #dest
|
||||
+ "\x00\x06" #len
|
||||
+ "\xDE\xAD" #check
|
||||
+ "foobar",
|
||||
partial=0,
|
||||
dest='dummy',
|
||||
source='testHost',
|
||||
protocol='dummy',
|
||||
version='dummy',
|
||||
ihl='dummy',
|
||||
tos='dummy',
|
||||
tot_len='dummy',
|
||||
fragment_id='dummy',
|
||||
fragment_offset='dummy',
|
||||
dont_fragment='dummy',
|
||||
more_fragments='dummy',
|
||||
ttl='dummy',
|
||||
)
|
||||
proto.datagramReceived("\x33\xFE" #source
|
||||
+ "\xf0\x0f" #dest
|
||||
+ "\x00\x05" #len
|
||||
+ "\xDE\xAD" #check
|
||||
+ "quux",
|
||||
partial=0,
|
||||
dest='dummy',
|
||||
source='otherHost',
|
||||
protocol='dummy',
|
||||
version='dummy',
|
||||
ihl='dummy',
|
||||
tos='dummy',
|
||||
tot_len='dummy',
|
||||
fragment_id='dummy',
|
||||
fragment_offset='dummy',
|
||||
dont_fragment='dummy',
|
||||
more_fragments='dummy',
|
||||
ttl='dummy',
|
||||
)
|
||||
|
||||
assert not p1.expecting, \
|
||||
'Should not expect any more packets, but still want %r' % p1.expecting
|
||||
|
||||
|
||||
def testMultipleSameProtos(self):
|
||||
proto = rawudp.RawUDPProtocol()
|
||||
p1 = MyProtocol([
|
||||
|
||||
('foobar', 'testHost', 0x43A2),
|
||||
|
||||
])
|
||||
|
||||
p2 = MyProtocol([
|
||||
|
||||
('foobar', 'testHost', 0x43A2),
|
||||
|
||||
])
|
||||
|
||||
proto.addProto(0xF00F, p1)
|
||||
proto.addProto(0xF00F, p2)
|
||||
|
||||
proto.datagramReceived("\x43\xA2" #source
|
||||
+ "\xf0\x0f" #dest
|
||||
+ "\x00\x06" #len
|
||||
+ "\xDE\xAD" #check
|
||||
+ "foobar",
|
||||
partial=0,
|
||||
dest='dummy',
|
||||
source='testHost',
|
||||
protocol='dummy',
|
||||
version='dummy',
|
||||
ihl='dummy',
|
||||
tos='dummy',
|
||||
tot_len='dummy',
|
||||
fragment_id='dummy',
|
||||
fragment_offset='dummy',
|
||||
dont_fragment='dummy',
|
||||
more_fragments='dummy',
|
||||
ttl='dummy',
|
||||
)
|
||||
|
||||
assert not p1.expecting, \
|
||||
'Should not expect any more packets, but still want %r' % p1.expecting
|
||||
assert not p2.expecting, \
|
||||
'Should not expect any more packets, but still want %r' % p2.expecting
|
||||
|
||||
def testWrongProtoNotSeen(self):
|
||||
proto = rawudp.RawUDPProtocol()
|
||||
p1 = MyProtocol([])
|
||||
proto.addProto(1, p1)
|
||||
|
||||
proto.datagramReceived("\x43\xA2" #source
|
||||
+ "\xf0\x0f" #dest
|
||||
+ "\x00\x06" #len
|
||||
+ "\xDE\xAD" #check
|
||||
+ "foobar",
|
||||
partial=0,
|
||||
dest='dummy',
|
||||
source='testHost',
|
||||
protocol='dummy',
|
||||
version='dummy',
|
||||
ihl='dummy',
|
||||
tos='dummy',
|
||||
tot_len='dummy',
|
||||
fragment_id='dummy',
|
||||
fragment_offset='dummy',
|
||||
dont_fragment='dummy',
|
||||
more_fragments='dummy',
|
||||
ttl='dummy',
|
||||
)
|
||||
|
||||
def testDemuxing(self):
|
||||
proto = rawudp.RawUDPProtocol()
|
||||
p1 = MyProtocol([
|
||||
|
||||
('foobar', 'testHost', 0x43A2),
|
||||
('quux', 'otherHost', 0x33FE),
|
||||
|
||||
])
|
||||
proto.addProto(0xF00F, p1)
|
||||
|
||||
p2 = MyProtocol([
|
||||
|
||||
('quux', 'otherHost', 0xA401),
|
||||
('foobar', 'testHost', 0xA302),
|
||||
|
||||
])
|
||||
proto.addProto(0xB050, p2)
|
||||
|
||||
proto.datagramReceived("\xA4\x01" #source
|
||||
+ "\xB0\x50" #dest
|
||||
+ "\x00\x05" #len
|
||||
+ "\xDE\xAD" #check
|
||||
+ "quux",
|
||||
partial=0,
|
||||
dest='dummy',
|
||||
source='otherHost',
|
||||
protocol='dummy',
|
||||
version='dummy',
|
||||
ihl='dummy',
|
||||
tos='dummy',
|
||||
tot_len='dummy',
|
||||
fragment_id='dummy',
|
||||
fragment_offset='dummy',
|
||||
dont_fragment='dummy',
|
||||
more_fragments='dummy',
|
||||
ttl='dummy',
|
||||
)
|
||||
proto.datagramReceived("\x43\xA2" #source
|
||||
+ "\xf0\x0f" #dest
|
||||
+ "\x00\x06" #len
|
||||
+ "\xDE\xAD" #check
|
||||
+ "foobar",
|
||||
partial=0,
|
||||
dest='dummy',
|
||||
source='testHost',
|
||||
protocol='dummy',
|
||||
version='dummy',
|
||||
ihl='dummy',
|
||||
tos='dummy',
|
||||
tot_len='dummy',
|
||||
fragment_id='dummy',
|
||||
fragment_offset='dummy',
|
||||
dont_fragment='dummy',
|
||||
more_fragments='dummy',
|
||||
ttl='dummy',
|
||||
)
|
||||
proto.datagramReceived("\x33\xFE" #source
|
||||
+ "\xf0\x0f" #dest
|
||||
+ "\x00\x05" #len
|
||||
+ "\xDE\xAD" #check
|
||||
+ "quux",
|
||||
partial=0,
|
||||
dest='dummy',
|
||||
source='otherHost',
|
||||
protocol='dummy',
|
||||
version='dummy',
|
||||
ihl='dummy',
|
||||
tos='dummy',
|
||||
tot_len='dummy',
|
||||
fragment_id='dummy',
|
||||
fragment_offset='dummy',
|
||||
dont_fragment='dummy',
|
||||
more_fragments='dummy',
|
||||
ttl='dummy',
|
||||
)
|
||||
proto.datagramReceived("\xA3\x02" #source
|
||||
+ "\xB0\x50" #dest
|
||||
+ "\x00\x06" #len
|
||||
+ "\xDE\xAD" #check
|
||||
+ "foobar",
|
||||
partial=0,
|
||||
dest='dummy',
|
||||
source='testHost',
|
||||
protocol='dummy',
|
||||
version='dummy',
|
||||
ihl='dummy',
|
||||
tos='dummy',
|
||||
tot_len='dummy',
|
||||
fragment_id='dummy',
|
||||
fragment_offset='dummy',
|
||||
dont_fragment='dummy',
|
||||
more_fragments='dummy',
|
||||
ttl='dummy',
|
||||
)
|
||||
|
||||
assert not p1.expecting, \
|
||||
'Should not expect any more packets, but still want %r' % p1.expecting
|
||||
assert not p2.expecting, \
|
||||
'Should not expect any more packets, but still want %r' % p2.expecting
|
||||
|
||||
def testAddingBadProtos_WrongLevel(self):
|
||||
"""Adding a wrong level protocol raises an exception."""
|
||||
e = rawudp.RawUDPProtocol()
|
||||
try:
|
||||
e.addProto(42, "silliness")
|
||||
except TypeError, e:
|
||||
if e.args == ('Added protocol must be an instance of DatagramProtocol',):
|
||||
pass
|
||||
else:
|
||||
raise
|
||||
else:
|
||||
raise AssertionError, 'addProto must raise an exception for bad protocols'
|
||||
|
||||
|
||||
def testAddingBadProtos_TooSmall(self):
|
||||
"""Adding a protocol with a negative number raises an exception."""
|
||||
e = rawudp.RawUDPProtocol()
|
||||
try:
|
||||
e.addProto(-1, protocol.DatagramProtocol())
|
||||
except TypeError, e:
|
||||
if e.args == ('Added protocol must be positive or zero',):
|
||||
pass
|
||||
else:
|
||||
raise
|
||||
else:
|
||||
raise AssertionError, 'addProto must raise an exception for bad protocols'
|
||||
|
||||
|
||||
def testAddingBadProtos_TooBig(self):
|
||||
"""Adding a protocol with a number >=2**16 raises an exception."""
|
||||
e = rawudp.RawUDPProtocol()
|
||||
try:
|
||||
e.addProto(2**16, protocol.DatagramProtocol())
|
||||
except TypeError, e:
|
||||
if e.args == ('Added protocol must fit in 16 bits',):
|
||||
pass
|
||||
else:
|
||||
raise
|
||||
else:
|
||||
raise AssertionError, 'addProto must raise an exception for bad protocols'
|
||||
|
||||
def testAddingBadProtos_TooBig2(self):
|
||||
"""Adding a protocol with a number >=2**16 raises an exception."""
|
||||
e = rawudp.RawUDPProtocol()
|
||||
try:
|
||||
e.addProto(2**16+1, protocol.DatagramProtocol())
|
||||
except TypeError, e:
|
||||
if e.args == ('Added protocol must fit in 16 bits',):
|
||||
pass
|
||||
else:
|
||||
raise
|
||||
else:
|
||||
raise AssertionError, 'addProto must raise an exception for bad protocols'
|
||||
1397
Linux/lib/python2.7/site-packages/twisted/pair/test/test_tuntap.py
Normal file
1397
Linux/lib/python2.7/site-packages/twisted/pair/test/test_tuntap.py
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue