From 29e452e84fa9358754f33af1f0902a231305e052 Mon Sep 17 00:00:00 2001 From: Gruzjan Date: Mon, 5 Jul 2021 20:42:36 +0200 Subject: [PATCH 1/6] Add tests for packets in TestPacketData --- makefile | 9 +++++++++ requirements-dev.txt | 1 + tests/test_packet_data.py | 21 +++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/makefile b/makefile index 44a32e2..39826f0 100644 --- a/makefile +++ b/makefile @@ -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/p4lang/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" \ No newline at end of file diff --git a/requirements-dev.txt b/requirements-dev.txt index 03f940c..6fea93d 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -3,3 +3,4 @@ black flake8 pytest~=6.2.4 +git+https://github.com/p4lang/ptf.git \ No newline at end of file diff --git a/tests/test_packet_data.py b/tests/test_packet_data.py index c463d9a..401c200 100644 --- a/tests/test_packet_data.py +++ b/tests/test_packet_data.py @@ -6,6 +6,7 @@ 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 * class TestPacketData: @@ -28,3 +29,23 @@ def test_custom_packet_data(self): raise Exception( f"Missing layer ${expected_packet} in packet. PyShark decode correctly?" ) + def test_tcp_packet(self): + size = 300 + pkt = simple_tcp_packet(pktlen=size) + assert len(bytes(pkt)) == size, "Packet length should be " + str(size) + + def test_ip_packet(self): + size = 300 + pkt = simple_ip_packet(pktlen=size) + assert len(bytes(pkt)) == size, "Packet length should be " + str(size) + + def test_udp_packet(self): + size = 300 + pkt = simple_udp_packet(pktlen=size) + assert len(bytes(pkt)) == size, "Packet length should be " + str(size) + + def test_eth_packet(self): + size = 300 + pkt = simple_eth_packet(pktlen=size) + assert len(bytes(pkt)) == size, "Packet length should be " + str(size) + \ No newline at end of file From 1292631ace9dd00c7ffa60acf7b4022c4378ab16 Mon Sep 17 00:00:00 2001 From: Gruzjan Date: Mon, 5 Jul 2021 20:48:00 +0200 Subject: [PATCH 2/6] Reformat with black --- tests/test_packet_data.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_packet_data.py b/tests/test_packet_data.py index 401c200..b9b5b79 100644 --- a/tests/test_packet_data.py +++ b/tests/test_packet_data.py @@ -29,11 +29,12 @@ def test_custom_packet_data(self): raise Exception( f"Missing layer ${expected_packet} in packet. PyShark decode correctly?" ) + def test_tcp_packet(self): size = 300 pkt = simple_tcp_packet(pktlen=size) assert len(bytes(pkt)) == size, "Packet length should be " + str(size) - + def test_ip_packet(self): size = 300 pkt = simple_ip_packet(pktlen=size) @@ -42,10 +43,9 @@ def test_ip_packet(self): def test_udp_packet(self): size = 300 pkt = simple_udp_packet(pktlen=size) - assert len(bytes(pkt)) == size, "Packet length should be " + str(size) - + assert len(bytes(pkt)) == size, "Packet length should be " + str(size) + def test_eth_packet(self): size = 300 pkt = simple_eth_packet(pktlen=size) assert len(bytes(pkt)) == size, "Packet length should be " + str(size) - \ No newline at end of file From a2b8213e69c1b4f5770f4a1c975e117bb1fde165 Mon Sep 17 00:00:00 2001 From: Gruzjan Date: Mon, 12 Jul 2021 18:51:24 +0200 Subject: [PATCH 3/6] Change tests to base on checking layer existance and if specific attrubutes stay the same after packet decode --- tests/test_packet_data.py | 73 ++++++++++++++++++++++++++++++++------- 1 file changed, 60 insertions(+), 13 deletions(-) diff --git a/tests/test_packet_data.py b/tests/test_packet_data.py index b9b5b79..4b39efc 100644 --- a/tests/test_packet_data.py +++ b/tests/test_packet_data.py @@ -6,7 +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 * +from ptf.testutils import ( + simple_tcp_packet, + simple_ip_packet, + simple_udp_packet, + simple_eth_packet, +) class TestPacketData: @@ -31,21 +36,63 @@ def test_custom_packet_data(self): ) def test_tcp_packet(self): - size = 300 - pkt = simple_tcp_packet(pktlen=size) - assert len(bytes(pkt)) == size, "Packet length should be " + str(size) + 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): - size = 300 - pkt = simple_ip_packet(pktlen=size) - assert len(bytes(pkt)) == size, "Packet length should be " + str(size) + 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): - size = 300 - pkt = simple_udp_packet(pktlen=size) - assert len(bytes(pkt)) == size, "Packet length should be " + str(size) + 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): - size = 300 - pkt = simple_eth_packet(pktlen=size) - assert len(bytes(pkt)) == size, "Packet length should be " + str(size) + dst = "01:02:03:04:05:06" + pkt = simple_eth_packet(eth_dst="01:02:03:04:05:06") + 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}." + ) From bd802bfceafd352f0d9d3d19840f6b615a497e7f Mon Sep 17 00:00:00 2001 From: Gruzjan Date: Tue, 20 Jul 2021 12:01:53 +0200 Subject: [PATCH 4/6] Packet testing based on expecting layers --- tests/test_packet_data.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_packet_data.py b/tests/test_packet_data.py index 4b39efc..2aa10d4 100644 --- a/tests/test_packet_data.py +++ b/tests/test_packet_data.py @@ -83,7 +83,7 @@ def test_udp_packet(self): def test_eth_packet(self): dst = "01:02:03:04:05:06" - pkt = simple_eth_packet(eth_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] From eef0c5889ba0bd7ec52278a7e040871aaa1853eb Mon Sep 17 00:00:00 2001 From: Gruzjan Date: Tue, 20 Jul 2021 12:10:52 +0200 Subject: [PATCH 5/6] Change ptf's source --- makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/makefile b/makefile index 39826f0..f708daa 100644 --- a/makefile +++ b/makefile @@ -17,7 +17,7 @@ build: install-ptf: clean @echo "Getting ptf..." - git clone https://github.com/p4lang/ptf.git + 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 From 556631d1b157dc4716a5a43767031952b03a6bd0 Mon Sep 17 00:00:00 2001 From: Gruzjan Date: Tue, 20 Jul 2021 12:13:46 +0200 Subject: [PATCH 6/6] Change ptf's source --- requirements-dev.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index 6fea93d..c25c707 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -3,4 +3,4 @@ black flake8 pytest~=6.2.4 -git+https://github.com/p4lang/ptf.git \ No newline at end of file +git+https://github.com/PacketHelper/ptf.git \ No newline at end of file