-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correct tests & rearrange code (#25)
* Correct tests & rearrange code
- Loading branch information
Showing
8 changed files
with
52 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()))] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()))] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |