Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Packet tests #9

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ build:
@echo "Building..."
python setup.py sdist bdist_wheel --universal
@echo "Building... Done"

install-ptf: clean
@echo "Getting ptf..."
git clone https://github.com/PacketHelper/ptf.git
cd ptf && python setup.py install
python setup.py sdist bdist_wheel --universal
pip install dist/ptf-0.9.1-py2.py3-none-any.whl
rm -rf ptf
@echo "Getting ptf... Done"
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
black
flake8
pytest~=6.2.4
git+https://github.com/PacketHelper/ptf.git
68 changes: 68 additions & 0 deletions tests/test_packet_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
from packet_helper_core.packet_data import PacketData
from packet_helper_core.utils.utils import decode_hex
from tests.utils.example_packets import EXAMPLE_ETHER
from ptf.testutils import (
simple_tcp_packet,
simple_ip_packet,
simple_udp_packet,
simple_eth_packet,
)


class TestPacketData:
Expand All @@ -28,3 +34,65 @@ def test_custom_packet_data(self):
raise Exception(
f"Missing layer ${expected_packet} in packet. PyShark decode correctly?"
)

def test_tcp_packet(self):
dport = 81
pkt = simple_tcp_packet(tcp_dport=dport)
packet = decode_hex(get_hex(pkt))
list_of_expected_packets = ("ETH", "IP", "TCP")
list_of_layers_from_packet = [x.layer_name.upper() for x in packet.layers]
for expected_packet in list_of_expected_packets:
if expected_packet not in list_of_layers_from_packet:
raise Exception(
f"Missing layer ${expected_packet} in packet. PyShark decode correctly?"
)
if packet.tcp.dstport != str(dport):
raise Exception(
f"TCP destination port mismatch. Is {packet.tcp.dstport}, should be {dport}."
)

def test_ip_packet(self):
ttl = 32
pkt = simple_ip_packet(ip_ttl=ttl)
packet = decode_hex(get_hex(pkt))
list_of_expected_packets = ("ETH", "IP")
list_of_layers_from_packet = [x.layer_name.upper() for x in packet.layers]
for expected_packet in list_of_expected_packets:
if expected_packet not in list_of_layers_from_packet:
raise Exception(
f"Missing layer ${expected_packet} in packet. PyShark decode correctly?"
)
if packet.ip.ttl != str(ttl):
raise Exception(f"IP ttl mismatch. Is {packet.ip.ttl}, should be {ttl}.")

def test_udp_packet(self):
dport = 81
pkt = simple_udp_packet(udp_dport=dport)
packet = decode_hex(get_hex(pkt))
list_of_expected_packets = ("ETH", "IP", "UDP")
list_of_layers_from_packet = [x.layer_name.upper() for x in packet.layers]
for expected_packet in list_of_expected_packets:
if expected_packet not in list_of_layers_from_packet:
raise Exception(
f"Missing layer ${expected_packet} in packet. PyShark decode correctly?"
)
if packet.udp.dstport != str(dport):
raise Exception(
f"UDP destination port mismatch. Is {packet.udp.dstport}, should be {dport}."
)

def test_eth_packet(self):
dst = "01:02:03:04:05:06"
pkt = simple_eth_packet(eth_dst=dst)
packet = decode_hex(get_hex(pkt))
list_of_expected_packets = ("ETH", "LLDP")
list_of_layers_from_packet = [x.layer_name.upper() for x in packet.layers]
for expected_packet in list_of_expected_packets:
if expected_packet not in list_of_layers_from_packet:
raise Exception(
f"Missing layer ${expected_packet} in packet. PyShark decode correctly?"
)
if packet.eth.dst != dst:
raise Exception(
f"ETH destination mismatch. Is {packet.eth.dst}, should be {dst}."
)