Skip to content

Commit

Permalink
Correct tests & rearrange code (#25)
Browse files Browse the repository at this point in the history
* Correct tests & rearrange code
  • Loading branch information
NexSabre authored May 17, 2021
1 parent 1c66002 commit 32ee11d
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 38 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
v0.11.1
* Fix test to run correctly w/GitHub Actions

v0.11
* Add `to_dict` and `to_list`. Allow to dump packet layers into dict Dict[AnyStr, AnyStr]
and list List[Dict[AnyStr, AnyStr]]
Expand Down
3 changes: 2 additions & 1 deletion scapy_helper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
from scapy_helper.main import get_hex, show_diff, show_hex, table, hex_equal, diff
from scapy_helper.test_case_extensions.packet_assert import PacketAssert

from scapy_helper.helpers.to_dict import to_dict, to_list
from scapy_helper.helpers.to_dict import to_dict
from scapy_helper.helpers.to_list import to_list
23 changes: 1 addition & 22 deletions scapy_helper/helpers/to_dict.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
__values = (int, float, str, bytes, bool,
list, tuple, set,
dict)


def _layer2dict(frame):
temp_dict = {}

if not getattr(frame, 'fields_desc', None):
return
for _field in frame.fields_desc:
value = getattr(frame, _field.name)
if isinstance(value, type(None)):
value = None
elif not isinstance(value, __values):
value = _layer2dict(value)
temp_dict[_field.name] = value
return {frame.name: temp_dict}
from scapy_helper.helpers.utils import _layer2dict


def to_dict(packet, layer=0, extend=False):
return _layer2dict(packet.getlayer(layer))


def to_list(packet, extend=False):
return [_layer2dict(packet.getlayer(x)) for x in range(len(packet.layers()))]
5 changes: 5 additions & 0 deletions scapy_helper/helpers/to_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from scapy_helper.helpers.utils import _layer2dict


def to_list(packet, extend=False):
return [_layer2dict(packet.getlayer(x)) for x in range(len(packet.layers()))]
18 changes: 18 additions & 0 deletions scapy_helper/helpers/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
__values = (int, float, str, bytes, bool,
list, tuple, set,
dict)


def _layer2dict(frame):
temp_dict = {}

if not getattr(frame, 'fields_desc', None):
return
for _field in frame.fields_desc:
value = getattr(frame, _field.name)
if isinstance(value, type(None)):
value = None
elif not isinstance(value, __values):
value = _layer2dict(value)
temp_dict[_field.name] = value
return {frame.name: temp_dict}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
long_description_content_type="text/markdown",
author="Nex Sabre",
author_email="nexsabre@protonmail.com",
version="0.11",
version="0.11.1",
url="https://github.com/NexSabre/scapy_helper",
packages=find_packages(),
classifiers=[
Expand Down
15 changes: 1 addition & 14 deletions test/test_to_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from scapy.layers.inet import TCP, IP
from scapy.layers.l2 import Ether

from scapy_helper.helpers.to_dict import to_dict, to_list
from scapy_helper.helpers.to_dict import to_dict


class TestToDict(TestCase):
Expand All @@ -28,16 +28,3 @@ def test_simple_dict_get_second_element(self):
self.assertEqual(to_dict_result, packet_result)


class TestToList(TestCase):
def test_simple_list(self):
packet = Ether(dst="ff:ff:ff:ff:ff:ff", src="00:00:00:00:00:00") / IP(src="0.0.0.0", dst="127.0.0.1") / TCP()
packet_result = [{'Ethernet': {'src': '00:00:00:00:00:00', 'dst': 'ff:ff:ff:ff:ff:ff', 'type': 2048}}, {
'IP': {'frag': 0, 'src': '0.0.0.0', 'proto': 6, 'tos': 0, 'dst': '127.0.0.1', 'chksum': None, 'len': None,
'options': [], 'version': 4, 'flags': None, 'ihl': None, 'ttl': 64, 'id': 1}}, {
'TCP': {'reserved': 0, 'seq': 0, 'ack': 0, 'dataofs': None, 'urgptr': 0, 'window': 8192,
'flags': None, 'chksum': None, 'dport': 80, 'sport': 20, 'options': []}}]

to_list_result = to_list(packet)

self.assertTrue(isinstance(to_list_result, list))
self.assertEqual(to_list_result, packet_result)
21 changes: 21 additions & 0 deletions test/test_to_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from unittest import TestCase

from scapy.layers.inet import IP, TCP
from scapy.layers.l2 import Ether

from scapy_helper import to_list


class TestToList(TestCase):
def test_simple_list(self):
packet = Ether(dst="ff:ff:ff:ff:ff:ff", src="00:00:00:00:00:00") / IP(src="0.0.0.0", dst="127.0.0.1") / TCP()
packet_result = [{'Ethernet': {'src': '00:00:00:00:00:00', 'dst': 'ff:ff:ff:ff:ff:ff', 'type': 2048}}, {
'IP': {'frag': 0, 'src': '0.0.0.0', 'proto': 6, 'tos': 0, 'dst': '127.0.0.1', 'chksum': None, 'len': None,
'options': [], 'version': 4, 'flags': None, 'ihl': None, 'ttl': 64, 'id': 1}}, {
'TCP': {'reserved': 0, 'seq': 0, 'ack': 0, 'dataofs': None, 'urgptr': 0, 'window': 8192,
'flags': None, 'chksum': None, 'dport': 80, 'sport': 20, 'options': []}}]

to_list_result = to_list(packet)

self.assertTrue(isinstance(to_list_result, list))
self.assertEqual(to_list_result, packet_result)

0 comments on commit 32ee11d

Please sign in to comment.