RAHHH
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"pair tests"
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,255 @@
|
||||
# Copyright (c) Twisted Matrix Laboratories.
|
||||
# See LICENSE for details.
|
||||
from zope.interface import implementer
|
||||
|
||||
from twisted.pair import ethernet, raw
|
||||
from twisted.python import components
|
||||
from twisted.trial import unittest
|
||||
|
||||
|
||||
@implementer(raw.IRawPacketProtocol)
|
||||
class MyProtocol:
|
||||
def __init__(self, expecting):
|
||||
self.expecting = list(expecting)
|
||||
|
||||
def addProto(self, num, proto):
|
||||
"""
|
||||
Not implemented
|
||||
"""
|
||||
|
||||
def datagramReceived(self, data, partial, dest, source, protocol):
|
||||
assert self.expecting, "Got a packet when not expecting anymore."
|
||||
expect = self.expecting.pop(0)
|
||||
localVariables = locals()
|
||||
params = {
|
||||
"partial": partial,
|
||||
"dest": dest,
|
||||
"source": source,
|
||||
"protocol": protocol,
|
||||
}
|
||||
assert expect == (data, params), "Expected {!r}, got {!r}".format(
|
||||
expect, (data, params)
|
||||
)
|
||||
|
||||
|
||||
class EthernetTests(unittest.TestCase):
|
||||
def testPacketParsing(self):
|
||||
proto = ethernet.EthernetProtocol()
|
||||
p1 = MyProtocol(
|
||||
[
|
||||
(
|
||||
b"foobar",
|
||||
{
|
||||
"partial": 0,
|
||||
"dest": b"123456",
|
||||
"source": b"987654",
|
||||
"protocol": 0x0800,
|
||||
},
|
||||
),
|
||||
]
|
||||
)
|
||||
proto.addProto(0x0800, p1)
|
||||
|
||||
proto.datagramReceived(b"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(
|
||||
[
|
||||
(
|
||||
b"foobar",
|
||||
{
|
||||
"partial": 0,
|
||||
"dest": b"123456",
|
||||
"source": b"987654",
|
||||
"protocol": 0x0800,
|
||||
},
|
||||
),
|
||||
(
|
||||
b"quux",
|
||||
{
|
||||
"partial": 1,
|
||||
"dest": b"012345",
|
||||
"source": b"abcdef",
|
||||
"protocol": 0x0800,
|
||||
},
|
||||
),
|
||||
]
|
||||
)
|
||||
proto.addProto(0x0800, p1)
|
||||
|
||||
proto.datagramReceived(b"123456987654\x08\x00foobar", partial=0)
|
||||
proto.datagramReceived(b"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(
|
||||
[
|
||||
(
|
||||
b"foobar",
|
||||
{
|
||||
"partial": 0,
|
||||
"dest": b"123456",
|
||||
"source": b"987654",
|
||||
"protocol": 0x0800,
|
||||
},
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
p2 = MyProtocol(
|
||||
[
|
||||
(
|
||||
b"foobar",
|
||||
{
|
||||
"partial": 0,
|
||||
"dest": b"123456",
|
||||
"source": b"987654",
|
||||
"protocol": 0x0800,
|
||||
},
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
proto.addProto(0x0800, p1)
|
||||
proto.addProto(0x0800, p2)
|
||||
|
||||
proto.datagramReceived(b"123456987654\x08\x00foobar", partial=0)
|
||||
|
||||
assert (
|
||||
not p1.expecting
|
||||
), "Should not expect any more packets, " "but still want {!r}".format(
|
||||
p1.expecting
|
||||
)
|
||||
assert (
|
||||
not p2.expecting
|
||||
), "Should not expect any more packets," " but still want {!r}".format(
|
||||
p2.expecting
|
||||
)
|
||||
|
||||
def testWrongProtoNotSeen(self):
|
||||
proto = ethernet.EthernetProtocol()
|
||||
p1 = MyProtocol([])
|
||||
proto.addProto(0x0801, p1)
|
||||
|
||||
proto.datagramReceived(b"123456987654\x08\x00foobar", partial=0)
|
||||
proto.datagramReceived(b"012345abcdef\x08\x00quux", partial=1)
|
||||
|
||||
def testDemuxing(self):
|
||||
proto = ethernet.EthernetProtocol()
|
||||
p1 = MyProtocol(
|
||||
[
|
||||
(
|
||||
b"foobar",
|
||||
{
|
||||
"partial": 0,
|
||||
"dest": b"123456",
|
||||
"source": b"987654",
|
||||
"protocol": 0x0800,
|
||||
},
|
||||
),
|
||||
(
|
||||
b"quux",
|
||||
{
|
||||
"partial": 1,
|
||||
"dest": b"012345",
|
||||
"source": b"abcdef",
|
||||
"protocol": 0x0800,
|
||||
},
|
||||
),
|
||||
]
|
||||
)
|
||||
proto.addProto(0x0800, p1)
|
||||
|
||||
p2 = MyProtocol(
|
||||
[
|
||||
(
|
||||
b"quux",
|
||||
{
|
||||
"partial": 1,
|
||||
"dest": b"012345",
|
||||
"source": b"abcdef",
|
||||
"protocol": 0x0806,
|
||||
},
|
||||
),
|
||||
(
|
||||
b"foobar",
|
||||
{
|
||||
"partial": 0,
|
||||
"dest": b"123456",
|
||||
"source": b"987654",
|
||||
"protocol": 0x0806,
|
||||
},
|
||||
),
|
||||
]
|
||||
)
|
||||
proto.addProto(0x0806, p2)
|
||||
|
||||
proto.datagramReceived(b"123456987654\x08\x00foobar", partial=0)
|
||||
proto.datagramReceived(b"012345abcdef\x08\x06quux", partial=1)
|
||||
proto.datagramReceived(b"123456987654\x08\x06foobar", partial=0)
|
||||
proto.datagramReceived(b"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 as 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 as 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 as 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")
|
||||
@@ -0,0 +1,490 @@
|
||||
# Copyright (c) Twisted Matrix Laboratories.
|
||||
# See LICENSE for details.
|
||||
from __future__ import annotations
|
||||
|
||||
from zope import interface
|
||||
|
||||
from twisted.pair import ip, raw
|
||||
from twisted.python import components
|
||||
from twisted.trial import unittest
|
||||
|
||||
|
||||
@interface.implementer(raw.IRawDatagramProtocol)
|
||||
class MyProtocol:
|
||||
def __init__(self, expecting: list[tuple[bytes, dict[str, str | int]]]) -> None:
|
||||
self.expecting = list(expecting)
|
||||
|
||||
def datagramReceived(
|
||||
self,
|
||||
data: bytes,
|
||||
partial: int,
|
||||
source: str,
|
||||
dest: str,
|
||||
protocol: int,
|
||||
version: int,
|
||||
ihl: int,
|
||||
tos: int,
|
||||
tot_len: int,
|
||||
fragment_id: int,
|
||||
fragment_offset: int,
|
||||
dont_fragment: int,
|
||||
more_fragments: int,
|
||||
ttl: int,
|
||||
) -> None:
|
||||
assert self.expecting, "Got a packet when not expecting anymore."
|
||||
expectData, expectKw = self.expecting.pop(0)
|
||||
|
||||
expectKwKeys = list(sorted(expectKw.keys()))
|
||||
localVariables = locals()
|
||||
|
||||
for k in expectKwKeys:
|
||||
assert (
|
||||
expectKw[k] == localVariables[k]
|
||||
), f"Expected {k}={expectKw[k]!r}, got {localVariables[k]!r}"
|
||||
assert expectData == data, f"Expected {expectData!r}, got {data!r}"
|
||||
|
||||
def addProto(self, num: object, proto: object) -> None:
|
||||
# IRawDatagramProtocol.addProto
|
||||
pass
|
||||
|
||||
|
||||
class IPTests(unittest.TestCase):
|
||||
def testPacketParsing(self) -> None:
|
||||
proto = ip.IPProtocol()
|
||||
p1 = MyProtocol(
|
||||
[
|
||||
(
|
||||
b"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(
|
||||
b"\x54" # ihl version
|
||||
+ b"\x07" # tos
|
||||
+ b"\x00\x1a" # tot_len
|
||||
+ b"\xDE\xAD" # id
|
||||
+ b"\xBE\xEF" # frag_off
|
||||
+ b"\xC0" # ttl
|
||||
+ b"\x0F" # protocol
|
||||
+ b"FE" # checksum
|
||||
+ b"\x05\x06\x07\x08"
|
||||
+ b"\x01\x02\x03\x04"
|
||||
+ b"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) -> None:
|
||||
proto = ip.IPProtocol()
|
||||
p1 = MyProtocol(
|
||||
[
|
||||
(
|
||||
b"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,
|
||||
},
|
||||
),
|
||||
(
|
||||
b"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(
|
||||
b"\x54" # ihl version
|
||||
+ b"\x07" # tos
|
||||
+ b"\x00\x1a" # tot_len
|
||||
+ b"\xDE\xAD" # id
|
||||
+ b"\xBE\xEF" # frag_off
|
||||
+ b"\xC0" # ttl
|
||||
+ b"\x0F" # protocol
|
||||
+ b"FE" # checksum
|
||||
+ b"\x05\x06\x07\x08"
|
||||
+ b"\x01\x02\x03\x04"
|
||||
+ b"foobar",
|
||||
partial=0,
|
||||
dest="dummy",
|
||||
source="dummy",
|
||||
protocol="dummy",
|
||||
)
|
||||
proto.datagramReceived(
|
||||
b"\x54" # ihl version
|
||||
+ b"\x07" # tos
|
||||
+ b"\x00\x1a" # tot_len
|
||||
+ b"\xDE\xAD" # id
|
||||
+ b"\xBE\xEF" # frag_off
|
||||
+ b"\xC0" # ttl
|
||||
+ b"\x0F" # protocol
|
||||
+ b"FE" # checksum
|
||||
+ b"\x06\x07\x08\x09"
|
||||
+ b"\x05\x04\x03\x02"
|
||||
+ b"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) -> None:
|
||||
proto = ip.IPProtocol()
|
||||
p1 = MyProtocol(
|
||||
[
|
||||
(
|
||||
b"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(
|
||||
[
|
||||
(
|
||||
b"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(
|
||||
b"\x54" # ihl version
|
||||
+ b"\x07" # tos
|
||||
+ b"\x00\x1a" # tot_len
|
||||
+ b"\xDE\xAD" # id
|
||||
+ b"\xBE\xEF" # frag_off
|
||||
+ b"\xC0" # ttl
|
||||
+ b"\x0F" # protocol
|
||||
+ b"FE" # checksum
|
||||
+ b"\x05\x06\x07\x08"
|
||||
+ b"\x01\x02\x03\x04"
|
||||
+ b"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) -> None:
|
||||
proto = ip.IPProtocol()
|
||||
p1 = MyProtocol([])
|
||||
proto.addProto(1, p1)
|
||||
|
||||
proto.datagramReceived(
|
||||
b"\x54" # ihl version
|
||||
+ b"\x07" # tos
|
||||
+ b"\x00\x1a" # tot_len
|
||||
+ b"\xDE\xAD" # id
|
||||
+ b"\xBE\xEF" # frag_off
|
||||
+ b"\xC0" # ttl
|
||||
+ b"\x0F" # protocol
|
||||
+ b"FE" # checksum
|
||||
+ b"\x05\x06\x07\x08"
|
||||
+ b"\x01\x02\x03\x04"
|
||||
+ b"foobar",
|
||||
partial=0,
|
||||
dest="dummy",
|
||||
source="dummy",
|
||||
protocol="dummy",
|
||||
)
|
||||
|
||||
def testDemuxing(self) -> None:
|
||||
proto = ip.IPProtocol()
|
||||
p1 = MyProtocol(
|
||||
[
|
||||
(
|
||||
b"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,
|
||||
},
|
||||
),
|
||||
(
|
||||
b"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(
|
||||
[
|
||||
(
|
||||
b"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,
|
||||
},
|
||||
),
|
||||
(
|
||||
b"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(
|
||||
b"\x54" # ihl version
|
||||
+ b"\x07" # tos
|
||||
+ b"\x00\x1a" # tot_len
|
||||
+ b"\xDE\xAD" # id
|
||||
+ b"\xBE\xEF" # frag_off
|
||||
+ b"\xC0" # ttl
|
||||
+ b"\x0A" # protocol
|
||||
+ b"FE" # checksum
|
||||
+ b"\x06\x07\x08\x09"
|
||||
+ b"\x05\x04\x03\x02"
|
||||
+ b"quux",
|
||||
partial=1,
|
||||
dest="dummy",
|
||||
source="dummy",
|
||||
protocol="dummy",
|
||||
)
|
||||
proto.datagramReceived(
|
||||
b"\x54" # ihl version
|
||||
+ b"\x07" # tos
|
||||
+ b"\x00\x1a" # tot_len
|
||||
+ b"\xDE\xAD" # id
|
||||
+ b"\xBE\xEF" # frag_off
|
||||
+ b"\xC0" # ttl
|
||||
+ b"\x0F" # protocol
|
||||
+ b"FE" # checksum
|
||||
+ b"\x05\x06\x07\x08"
|
||||
+ b"\x01\x02\x03\x04"
|
||||
+ b"foobar",
|
||||
partial=0,
|
||||
dest="dummy",
|
||||
source="dummy",
|
||||
protocol="dummy",
|
||||
)
|
||||
proto.datagramReceived(
|
||||
b"\x54" # ihl version
|
||||
+ b"\x07" # tos
|
||||
+ b"\x00\x1a" # tot_len
|
||||
+ b"\xDE\xAD" # id
|
||||
+ b"\xBE\xEF" # frag_off
|
||||
+ b"\xC0" # ttl
|
||||
+ b"\x0F" # protocol
|
||||
+ b"FE" # checksum
|
||||
+ b"\x06\x07\x08\x09"
|
||||
+ b"\x05\x04\x03\x02"
|
||||
+ b"quux",
|
||||
partial=1,
|
||||
dest="dummy",
|
||||
source="dummy",
|
||||
protocol="dummy",
|
||||
)
|
||||
proto.datagramReceived(
|
||||
b"\x54" # ihl version
|
||||
+ b"\x07" # tos
|
||||
+ b"\x00\x1a" # tot_len
|
||||
+ b"\xDE\xAD" # id
|
||||
+ b"\xBE\xEF" # frag_off
|
||||
+ b"\xC0" # ttl
|
||||
+ b"\x0A" # protocol
|
||||
+ b"FE" # checksum
|
||||
+ b"\x05\x06\x07\x08"
|
||||
+ b"\x01\x02\x03\x04"
|
||||
+ b"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) -> None:
|
||||
"""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) -> None:
|
||||
"""Adding a protocol with a negative number raises an exception."""
|
||||
e = ip.IPProtocol()
|
||||
try:
|
||||
e.addProto(-1, MyProtocol([]))
|
||||
except TypeError as 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) -> None:
|
||||
"""Adding a protocol with a number >=2**32 raises an exception."""
|
||||
e = ip.IPProtocol()
|
||||
try:
|
||||
e.addProto(2**32, MyProtocol([]))
|
||||
except TypeError as 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) -> None:
|
||||
"""Adding a protocol with a number >=2**32 raises an exception."""
|
||||
e = ip.IPProtocol()
|
||||
try:
|
||||
e.addProto(2**32 + 1, MyProtocol([]))
|
||||
except TypeError as 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,351 @@
|
||||
# Copyright (c) Twisted Matrix Laboratories.
|
||||
# See LICENSE for details.
|
||||
from __future__ import annotations
|
||||
|
||||
from twisted.internet import protocol
|
||||
from twisted.pair import rawudp
|
||||
|
||||
#
|
||||
from twisted.trial import unittest
|
||||
|
||||
|
||||
class MyProtocol(protocol.DatagramProtocol):
|
||||
def __init__(self, expecting: list[tuple[bytes, bytes, int]]) -> None:
|
||||
self.expecting = list(expecting)
|
||||
|
||||
def datagramReceived(self, data: bytes, peer: tuple[bytes, int]) -> None:
|
||||
(host, port) = peer
|
||||
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}".format(
|
||||
expectData, data
|
||||
)
|
||||
assert expectHost == host, "Expected host {!r}, got {!r}".format(
|
||||
expectHost, host
|
||||
)
|
||||
assert expectPort == port, "Expected port %d=0x%04x, got %d=0x%04x" % (
|
||||
expectPort,
|
||||
expectPort,
|
||||
port,
|
||||
port,
|
||||
)
|
||||
|
||||
|
||||
class RawUDPTests(unittest.TestCase):
|
||||
def testPacketParsing(self) -> None:
|
||||
proto = rawudp.RawUDPProtocol()
|
||||
p1 = MyProtocol(
|
||||
[
|
||||
(b"foobar", b"testHost", 0x43A2),
|
||||
]
|
||||
)
|
||||
proto.addProto(0xF00F, p1)
|
||||
|
||||
proto.datagramReceived(
|
||||
b"\x43\xA2" # source
|
||||
b"\xf0\x0f" # dest
|
||||
b"\x00\x06" # len
|
||||
b"\xDE\xAD" # check
|
||||
b"foobar",
|
||||
partial=0,
|
||||
dest=b"dummy",
|
||||
source=b"testHost",
|
||||
protocol=b"dummy",
|
||||
version=b"dummy",
|
||||
ihl=b"dummy",
|
||||
tos=b"dummy",
|
||||
tot_len=b"dummy",
|
||||
fragment_id=b"dummy",
|
||||
fragment_offset=b"dummy",
|
||||
dont_fragment=b"dummy",
|
||||
more_fragments=b"dummy",
|
||||
ttl=b"dummy",
|
||||
)
|
||||
|
||||
assert not p1.expecting, (
|
||||
"Should not expect any more packets, but still want %r" % p1.expecting
|
||||
)
|
||||
|
||||
def testMultiplePackets(self) -> None:
|
||||
proto = rawudp.RawUDPProtocol()
|
||||
p1 = MyProtocol(
|
||||
[
|
||||
(b"foobar", b"testHost", 0x43A2),
|
||||
(b"quux", b"otherHost", 0x33FE),
|
||||
]
|
||||
)
|
||||
proto.addProto(0xF00F, p1)
|
||||
proto.datagramReceived(
|
||||
b"\x43\xA2" # source
|
||||
b"\xf0\x0f" # dest
|
||||
b"\x00\x06" # len
|
||||
b"\xDE\xAD" # check
|
||||
b"foobar",
|
||||
partial=0,
|
||||
dest=b"dummy",
|
||||
source=b"testHost",
|
||||
protocol=b"dummy",
|
||||
version=b"dummy",
|
||||
ihl=b"dummy",
|
||||
tos=b"dummy",
|
||||
tot_len=b"dummy",
|
||||
fragment_id=b"dummy",
|
||||
fragment_offset=b"dummy",
|
||||
dont_fragment=b"dummy",
|
||||
more_fragments=b"dummy",
|
||||
ttl=b"dummy",
|
||||
)
|
||||
proto.datagramReceived(
|
||||
b"\x33\xFE" # source
|
||||
b"\xf0\x0f" # dest
|
||||
b"\x00\x05" # len
|
||||
b"\xDE\xAD" # check
|
||||
b"quux",
|
||||
partial=0,
|
||||
dest=b"dummy",
|
||||
source=b"otherHost",
|
||||
protocol=b"dummy",
|
||||
version=b"dummy",
|
||||
ihl=b"dummy",
|
||||
tos=b"dummy",
|
||||
tot_len=b"dummy",
|
||||
fragment_id=b"dummy",
|
||||
fragment_offset=b"dummy",
|
||||
dont_fragment=b"dummy",
|
||||
more_fragments=b"dummy",
|
||||
ttl=b"dummy",
|
||||
)
|
||||
|
||||
assert not p1.expecting, (
|
||||
"Should not expect any more packets, but still want %r" % p1.expecting
|
||||
)
|
||||
|
||||
def testMultipleSameProtos(self) -> None:
|
||||
proto = rawudp.RawUDPProtocol()
|
||||
p1 = MyProtocol(
|
||||
[
|
||||
(b"foobar", b"testHost", 0x43A2),
|
||||
]
|
||||
)
|
||||
|
||||
p2 = MyProtocol(
|
||||
[
|
||||
(b"foobar", b"testHost", 0x43A2),
|
||||
]
|
||||
)
|
||||
|
||||
proto.addProto(0xF00F, p1)
|
||||
proto.addProto(0xF00F, p2)
|
||||
|
||||
proto.datagramReceived(
|
||||
b"\x43\xA2" # source
|
||||
b"\xf0\x0f" # dest
|
||||
b"\x00\x06" # len
|
||||
b"\xDE\xAD" # check
|
||||
b"foobar",
|
||||
partial=0,
|
||||
dest=b"dummy",
|
||||
source=b"testHost",
|
||||
protocol=b"dummy",
|
||||
version=b"dummy",
|
||||
ihl=b"dummy",
|
||||
tos=b"dummy",
|
||||
tot_len=b"dummy",
|
||||
fragment_id=b"dummy",
|
||||
fragment_offset=b"dummy",
|
||||
dont_fragment=b"dummy",
|
||||
more_fragments=b"dummy",
|
||||
ttl=b"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) -> None:
|
||||
proto = rawudp.RawUDPProtocol()
|
||||
p1 = MyProtocol([])
|
||||
proto.addProto(1, p1)
|
||||
|
||||
proto.datagramReceived(
|
||||
b"\x43\xA2" # source
|
||||
b"\xf0\x0f" # dest
|
||||
b"\x00\x06" # len
|
||||
b"\xDE\xAD" # check
|
||||
b"foobar",
|
||||
partial=0,
|
||||
dest=b"dummy",
|
||||
source=b"testHost",
|
||||
protocol=b"dummy",
|
||||
version=b"dummy",
|
||||
ihl=b"dummy",
|
||||
tos=b"dummy",
|
||||
tot_len=b"dummy",
|
||||
fragment_id=b"dummy",
|
||||
fragment_offset=b"dummy",
|
||||
dont_fragment=b"dummy",
|
||||
more_fragments=b"dummy",
|
||||
ttl=b"dummy",
|
||||
)
|
||||
|
||||
def testDemuxing(self) -> None:
|
||||
proto = rawudp.RawUDPProtocol()
|
||||
p1 = MyProtocol(
|
||||
[
|
||||
(b"foobar", b"testHost", 0x43A2),
|
||||
(b"quux", b"otherHost", 0x33FE),
|
||||
]
|
||||
)
|
||||
proto.addProto(0xF00F, p1)
|
||||
|
||||
p2 = MyProtocol(
|
||||
[
|
||||
(b"quux", b"otherHost", 0xA401),
|
||||
(b"foobar", b"testHost", 0xA302),
|
||||
]
|
||||
)
|
||||
proto.addProto(0xB050, p2)
|
||||
|
||||
proto.datagramReceived(
|
||||
b"\xA4\x01" # source
|
||||
b"\xB0\x50" # dest
|
||||
b"\x00\x05" # len
|
||||
b"\xDE\xAD" # check
|
||||
b"quux",
|
||||
partial=0,
|
||||
dest=b"dummy",
|
||||
source=b"otherHost",
|
||||
protocol=b"dummy",
|
||||
version=b"dummy",
|
||||
ihl=b"dummy",
|
||||
tos=b"dummy",
|
||||
tot_len=b"dummy",
|
||||
fragment_id=b"dummy",
|
||||
fragment_offset=b"dummy",
|
||||
dont_fragment=b"dummy",
|
||||
more_fragments=b"dummy",
|
||||
ttl=b"dummy",
|
||||
)
|
||||
proto.datagramReceived(
|
||||
b"\x43\xA2" # source
|
||||
b"\xf0\x0f" # dest
|
||||
b"\x00\x06" # len
|
||||
b"\xDE\xAD" # check
|
||||
b"foobar",
|
||||
partial=0,
|
||||
dest=b"dummy",
|
||||
source=b"testHost",
|
||||
protocol=b"dummy",
|
||||
version=b"dummy",
|
||||
ihl=b"dummy",
|
||||
tos=b"dummy",
|
||||
tot_len=b"dummy",
|
||||
fragment_id=b"dummy",
|
||||
fragment_offset=b"dummy",
|
||||
dont_fragment=b"dummy",
|
||||
more_fragments=b"dummy",
|
||||
ttl=b"dummy",
|
||||
)
|
||||
proto.datagramReceived(
|
||||
b"\x33\xFE" # source
|
||||
b"\xf0\x0f" # dest
|
||||
b"\x00\x05" # len
|
||||
b"\xDE\xAD" # check
|
||||
b"quux",
|
||||
partial=0,
|
||||
dest=b"dummy",
|
||||
source=b"otherHost",
|
||||
protocol=b"dummy",
|
||||
version=b"dummy",
|
||||
ihl=b"dummy",
|
||||
tos=b"dummy",
|
||||
tot_len=b"dummy",
|
||||
fragment_id=b"dummy",
|
||||
fragment_offset=b"dummy",
|
||||
dont_fragment=b"dummy",
|
||||
more_fragments=b"dummy",
|
||||
ttl=b"dummy",
|
||||
)
|
||||
proto.datagramReceived(
|
||||
b"\xA3\x02" # source
|
||||
b"\xB0\x50" # dest
|
||||
b"\x00\x06" # len
|
||||
b"\xDE\xAD" # check
|
||||
b"foobar",
|
||||
partial=0,
|
||||
dest=b"dummy",
|
||||
source=b"testHost",
|
||||
protocol=b"dummy",
|
||||
version=b"dummy",
|
||||
ihl=b"dummy",
|
||||
tos=b"dummy",
|
||||
tot_len=b"dummy",
|
||||
fragment_id=b"dummy",
|
||||
fragment_offset=b"dummy",
|
||||
dont_fragment=b"dummy",
|
||||
more_fragments=b"dummy",
|
||||
ttl=b"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) -> None:
|
||||
"""Adding a wrong level protocol raises an exception."""
|
||||
e = rawudp.RawUDPProtocol()
|
||||
try:
|
||||
e.addProto(42, "silliness")
|
||||
except TypeError as 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) -> None:
|
||||
"""Adding a protocol with a negative number raises an exception."""
|
||||
e = rawudp.RawUDPProtocol()
|
||||
try:
|
||||
e.addProto(-1, protocol.DatagramProtocol())
|
||||
except TypeError as 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) -> None:
|
||||
"""Adding a protocol with a number >=2**16 raises an exception."""
|
||||
e = rawudp.RawUDPProtocol()
|
||||
try:
|
||||
e.addProto(2**16, protocol.DatagramProtocol())
|
||||
except TypeError as 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) -> None:
|
||||
"""Adding a protocol with a number >=2**16 raises an exception."""
|
||||
e = rawudp.RawUDPProtocol()
|
||||
try:
|
||||
e.addProto(2**16 + 1, protocol.DatagramProtocol())
|
||||
except TypeError as 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")
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user