Skip to content
This repository was archived by the owner on Apr 22, 2024. It is now read-only.

Commit bcdf6fc

Browse files
committed
Add some more tests and fix comments in IPv4 class
Changes due to PR review on github.
1 parent de38e5c commit bcdf6fc

2 files changed

Lines changed: 57 additions & 19 deletions

File tree

pyof/foundation/network_types.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -145,33 +145,38 @@ class IPv4(GenericStruct):
145145
Contains all fields of an IP version 4 packet header, plus the upper layer
146146
content as binary data.
147147
Some of the fields were merged together because of their size being
148-
inferior to 8 bits.
148+
inferior to 8 bits. They are represented as a single class attribute, but
149+
pack/unpack methods will take into account the values in individual
150+
instance attributes.
149151
"""
150152

151-
# IP protocol version + Internet Header Length (words)
153+
#: _version_ihl (:class:`UBInt8`): IP protocol version + Internet Header
154+
#: Length (words)
152155
_version_ihl = UBInt8()
153-
# Differentiated Services Code Point (ToS - Type of Service) +
154-
# Explicit Congestion Notification
156+
#: _dscp_ecn (:class:`UBInt8`): Differentiated Services Code Point
157+
#: (ToS - Type of Service) + Explicit Congestion Notification
155158
_dscp_ecn = UBInt8()
156-
# IP packet length (bytes)
159+
#: length (:class:`UBInt16`): IP packet length (bytes)
157160
length = UBInt16()
158-
# Packet ID - common to all fragments
161+
#: identification (:class:`UBInt16`): Packet ID - common to all fragments
159162
identification = UBInt16()
160-
# Fragmentation flags + fragmentation offset
163+
#: _flags_offset (:class:`UBInt16`): Fragmentation flags + fragmentation
164+
#: offset
161165
_flags_offset = UBInt16()
162-
# Packet time-to-live
166+
#: ttl (:class:`UBInt8`): Packet time-to-live
163167
ttl = UBInt8()
164-
# Upper layer protocol number
168+
#: protocol (:class:`UBInt8`): Upper layer protocol number
165169
protocol = UBInt8()
166-
# Header checksum
170+
#: checksum (:class:`UBInt16`): Header checksum
167171
checksum = UBInt16()
168-
# Source address
172+
#: source (:class:`IPAddress`): Source IPv4 address
169173
source = IPAddress()
170-
# Destination address
174+
#: destination (:class:`IPAddress`): Destination IPv4 address
171175
destination = IPAddress()
172-
# IP Options - up to 320 bits, always padded to 32 bits
176+
#: options (:class:`BinaryData`): IP Options - up to 320 bits, always
177+
#: padded to 32 bits
173178
options = BinaryData()
174-
# Packet data
179+
#: data (:class:`BinaryData`): Packet data
175180
data = BinaryData()
176181

177182
def __init__(self, version=4, ihl=5, dscp=0, ecn=0, length=0, # noqa

tests/test_foundation/test_network_types.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,44 @@ def test_GenTLV_value_unpack(self):
1616
tlv_unpacked.unpack(tlv.pack())
1717
self.assertEqual(tlv.value.value, tlv_unpacked.value.value)
1818

19-
def test_IPv4_pack_unpack(self):
19+
20+
class TestIPv4(unittest.TestCase):
21+
"""Test IPv4 packets."""
22+
23+
def test_IPv4_pack(self):
2024
"""Test pack/unpack of IPv4 class."""
21-
packet = IPv4(ttl=64, protocol=17, source="192.168.0.1",
22-
destination="172.16.200.132", data=b'testdata')
25+
packet = IPv4(dscp=10, ttl=64, protocol=17, source="192.168.0.10",
26+
destination="172.16.10.30", options=b'1000',
27+
data=b'testdata')
2328
packed = packet.pack()
29+
expected = b'F(\x00 \x00\x00\x00\x00@\x11\x02'
30+
expected += b'\xc5\xc0\xa8\x00\n\xac\x10\n\x1e1000testdata'
31+
self.assertEqual(packed, expected)
32+
33+
def test_IPv4_unpack(self):
34+
"""Test unpack of IPv4 binary packet."""
35+
raw = b'FP\x00$\x00\x00\x00\x00\x80\x06W'
36+
raw += b'\xf4\n\x9aN\x81\xc0\xa8\xc7\xcc1000somemoredata'
37+
expected = IPv4(dscp=20, ttl=128, protocol=6, source="10.154.78.129",
38+
destination="192.168.199.204", options=b'1000',
39+
data=b'somemoredata')
40+
expected.pack()
2441
unpacked = IPv4()
25-
unpacked.unpack(packed)
26-
self.assertEqual(packed, unpacked.pack())
42+
unpacked.unpack(raw)
43+
self.assertEqual(unpacked, expected)
44+
45+
def test_IPv4_size(self):
46+
"""Test Header size for IPv4 packet."""
47+
packet = IPv4()
48+
packet.pack()
49+
self.assertEqual(20, packet.get_size())
50+
self.assertEqual(20, packet.length)
51+
self.assertEqual(20, packet.ihl * 4)
52+
53+
def test_IPv4_checksum(self):
54+
"""Test if the IPv4 checksum is being calculated correclty."""
55+
packet = IPv4(dscp=10, ttl=64, protocol=17, source="192.168.0.10",
56+
destination="172.16.10.30", options=b'1000',
57+
data=b'testdata')
58+
packet.pack()
59+
self.assertEqual(packet.checksum, 709)

0 commit comments

Comments
 (0)