Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Enteee committed May 3, 2016
1 parent a4142a8 commit 73933e9
Show file tree
Hide file tree
Showing 14 changed files with 770 additions and 43 deletions.
16 changes: 4 additions & 12 deletions pdml2flow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,13 @@ def pdml2flow():
action='store_true',
help='Debug mode [default: {}]'.format(Conf.DEBUG)
)
conf = vars(parser.parse_args())
conf = vars(parser.parse_args(args=Conf.ARGS))
# split each flowdef to a path
try:
if(conf.FLOW_DEF_STR):
conf.FLOW_DEF = Conf.get_real_paths(conf.FLOW_DEF_STR, Conf.FLOW_DEF_NESTCHAR)
except AttributeError:
pass
# apply configuration
start_parser(conf)

def pdml2xml():
Expand Down Expand Up @@ -98,21 +97,14 @@ def pdml2frame(output_type):
action='store_true',
help='Debug mode [default: {}]'.format(Conf.DEBUG)
)
conf = vars(parser.parse_args())
conf = vars(parser.parse_args(args=Conf.ARGS))
start_parser(conf)

def start_parser(conf):
def start_parser(conf = {}):
# apply configuration
Conf.set(conf)
# create an XMLReader
parser = xml.sax.make_parser()
# turn off namepsaces
parser.setFeature(xml.sax.handler.feature_namespaces, 0)
# override the default ContextHandler
handler = PdmlHandler()
parser.setContentHandler(handler)
try:
parser.parse(sys.stdin)
xml.sax.parse(Conf.IN, PdmlHandler())
except xml.sax._exceptions.SAXParseException as e:
# this might happen when a pdml file is malformed
warning('Parser returned exception: {}'.format(e))
Expand Down
17 changes: 14 additions & 3 deletions pdml2flow/conf.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
#!/usr/bin/env python3
# vim: set fenc=utf8 ts=4 sw=4 et :
import sys

class Conf():
"""The global configuration class"""

@staticmethod
def get_real_paths(paths, nestchar):
return [ path.split(nestchar) + ['raw'] for path in paths ]

ARGS = sys.argv[1:]
IN = sys.stdin
OUT = sys.stdout
OUT_DEBUG = sys.stderr
OUT_WARNING = sys.stderr

FLOW_DEF_NESTCHAR = '.'
FLOW_DEF_STR = [
'vlan{}id'.format(FLOW_DEF_NESTCHAR),
Expand All @@ -30,11 +38,14 @@ def get_real_paths(paths, nestchar):
DEBUG = False
METADATA = False

"""
Applies a configuration to the global config object
"""
@staticmethod
def set(conf):
"""Applies a configuration to the global config object"""
for name, value in conf.items():
setattr(Conf, name.upper(), value)

@staticmethod
def get():
"""Gets the configuration as a dict"""
return {attr: getattr(Conf, attr) for attr in dir(Conf()) if not callable(attr) and not attr.startswith("__")}

2 changes: 1 addition & 1 deletion pdml2flow/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class Flow():

""" The overall packet time """
# The overall packet time
newest_overall_frame_time = 0

def __init__(self, first_frame):
Expand Down
8 changes: 4 additions & 4 deletions pdml2flow/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

from .conf import Conf

def debug(*objs, file=sys.stderr):
def debug(*objs):
# import here because of circular dependencies
from .flow import Flow
if Conf.DEBUG:
print("[Debug: {}] ".format(Flow.newest_overall_frame_time), *objs, file=file)
print("[Debug: {}] ".format(Flow.newest_overall_frame_time), *objs, file=Conf.OUT_DEBUG)

def warning(*objs, file=sys.stderr):
def warning(*objs):
# import here because of circular dependencies
from .flow import Flow
print("[Warning: {}] ".format(Flow.newest_overall_frame_time), *objs, file=file)
print("[Warning: {}] ".format(Flow.newest_overall_frame_time), *objs, file=Conf.OUT_WARNING)

5 changes: 3 additions & 2 deletions pdml2flow/pdmlhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .logging import *

class PdmlHandler(xml.sax.ContentHandler):

def __init__(self):
self.__frame = {}
self.__flows = {}
Expand Down Expand Up @@ -58,7 +59,7 @@ def endElement(self, tag):
if flow.not_expired():
new_flows[flowid] = flow
else:
print(flow)
print(flow, file=Conf.OUT)
self.__flows = new_flows
# the flow definition
flowid = Flow.get_flow_id(self.__frame)
Expand All @@ -77,4 +78,4 @@ def endDocument(self):
# print all flows @ end
for (flowid, flow) in self.__flows.items():
# before printing clean all empty laves
print(flow)
print(flow, file=Conf.OUT)
4 changes: 2 additions & 2 deletions test/autovivification_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env python3
# vim: set fenc=utf8 ts=4 sw=4 et :
import unittest
from .testcase import TestCase

from pdml2flow.autovivification import AutoVivification

class TestAutoVivification(unittest.TestCase):
class TestAutoVivification(TestCase):

def test_clean_empty(self):
# sutff not to clean
Expand Down
17 changes: 13 additions & 4 deletions test/conf_test.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
#!/usr/bin/env python3
# vim: set fenc=utf8 ts=4 sw=4 et :
import unittest
import io

from .testcase import TestCase
from pdml2flow.conf import Conf

class TestConf(unittest.TestCase):
class TestConf(TestCase):

def test_get_real_paths(self):
p = Conf.get_real_paths([
'this{}is{}a{}path'.format(*[Conf.FLOW_DEF_NESTCHAR]*3)
], Conf.FLOW_DEF_NESTCHAR)
self.assertEqual(p , [['this', 'is', 'a', 'path', 'raw']])

def test_set(self):
pass
d = Conf.DEBUG
m = Conf.DATA_MAXLEN
e = Conf.EXTRACT_SHOW
newConf = dict()
newConf['debug'] = not d
newConf['DATA_MAXLEN'] = m + 1
Conf.set(newConf)
self.assertEqual(Conf.DEBUG, not d)
self.assertEqual(Conf.DATA_MAXLEN, m + 1)
self.assertEqual(Conf.EXTRACT_SHOW, e)

if __name__ == '__main__':
unittest.main()
132 changes: 132 additions & 0 deletions test/data/first/stdin
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="pdml2html.xsl"?>
<!-- You can find pdml2html.xsl in /usr/share/wireshark or at https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob_plain;f=pdml2html.xsl. -->
<pdml version="0" creator="wireshark/2.0.3" time="Tue May 3 12:08:03 2016" capture_file="">
<packet>
<proto name="geninfo" pos="0" showname="General information" size="66">
<field name="num" pos="0" show="1" showname="Number" value="1" size="66"/>
<field name="len" pos="0" show="66" showname="Frame Length" value="42" size="66"/>
<field name="caplen" pos="0" show="66" showname="Captured Length" value="42" size="66"/>
<field name="timestamp" pos="0" show="May 3, 2016 12:08:03.644608216 CEST" showname="Captured Time" value="1462270083.644608216" size="66"/>
</proto>
<proto name="frame" showname="Frame 1: 66 bytes on wire (528 bits), 66 bytes captured (528 bits) on interface 0" size="66" pos="0">
<field name="frame.interface_id" showname="Interface id: 0 (eth0)" size="0" pos="0" show="0"/>
<field name="frame.encap_type" showname="Encapsulation type: Ethernet (1)" size="0" pos="0" show="1"/>
<field name="frame.time" showname="Arrival Time: May 3, 2016 12:08:03.644608216 CEST" size="0" pos="0" show="May 3, 2016 12:08:03.644608216 CEST"/>
<field name="frame.offset_shift" showname="Time shift for this packet: 0.000000000 seconds" size="0" pos="0" show="0.000000000"/>
<field name="frame.time_epoch" showname="Epoch Time: 1462270083.644608216 seconds" size="0" pos="0" show="1462270083.644608216"/>
<field name="frame.time_delta" showname="Time delta from previous captured frame: 0.000000000 seconds" size="0" pos="0" show="0.000000000"/>
<field name="frame.time_delta_displayed" showname="Time delta from previous displayed frame: 0.000000000 seconds" size="0" pos="0" show="0.000000000"/>
<field name="frame.time_relative" showname="Time since reference or first frame: 0.000000000 seconds" size="0" pos="0" show="0.000000000"/>
<field name="frame.number" showname="Frame Number: 1" size="0" pos="0" show="1"/>
<field name="frame.len" showname="Frame Length: 66 bytes (528 bits)" size="0" pos="0" show="66"/>
<field name="frame.cap_len" showname="Capture Length: 66 bytes (528 bits)" size="0" pos="0" show="66"/>
<field name="frame.marked" showname="Frame is marked: False" size="0" pos="0" show="0"/>
<field name="frame.ignored" showname="Frame is ignored: False" size="0" pos="0" show="0"/>
<field name="frame.protocols" showname="Protocols in frame: eth:ethertype:ip:tcp" size="0" pos="0" show="eth:ethertype:ip:tcp"/>
</proto>
<proto name="eth" showname="Ethernet II, Src: AsustekC_ba:a3:9f (10:bf:48:ba:a3:9f), Dst: IETF-VRRP-VRID_11 (00:00:5e:00:01:11)" size="14" pos="0">
<field name="eth.dst" showname="Destination: IETF-VRRP-VRID_11 (00:00:5e:00:01:11)" size="6" pos="0" show="00:00:5e:00:01:11" value="00005e000111">
<field name="eth.dst_resolved" showname="Destination (resolved): IETF-VRRP-VRID_11" hide="yes" size="6" pos="0" show="IETF-VRRP-VRID_11" value="00005e000111"/>
<field name="eth.addr" showname="Address: IETF-VRRP-VRID_11 (00:00:5e:00:01:11)" size="6" pos="0" show="00:00:5e:00:01:11" value="00005e000111"/>
<field name="eth.addr_resolved" showname="Address (resolved): IETF-VRRP-VRID_11" hide="yes" size="6" pos="0" show="IETF-VRRP-VRID_11" value="00005e000111"/>
<field name="eth.lg" showname=".... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)" size="3" pos="0" show="0" value="0" unmaskedvalue="00005e"/>
<field name="eth.ig" showname=".... ...0 .... .... .... .... = IG bit: Individual address (unicast)" size="3" pos="0" show="0" value="0" unmaskedvalue="00005e"/>
</field>
<field name="eth.src" showname="Source: AsustekC_ba:a3:9f (10:bf:48:ba:a3:9f)" size="6" pos="6" show="10:bf:48:ba:a3:9f" value="10bf48baa39f">
<field name="eth.src_resolved" showname="Source (resolved): AsustekC_ba:a3:9f" hide="yes" size="6" pos="6" show="AsustekC_ba:a3:9f" value="10bf48baa39f"/>
<field name="eth.addr" showname="Address: AsustekC_ba:a3:9f (10:bf:48:ba:a3:9f)" size="6" pos="6" show="10:bf:48:ba:a3:9f" value="10bf48baa39f"/>
<field name="eth.addr_resolved" showname="Address (resolved): AsustekC_ba:a3:9f" hide="yes" size="6" pos="6" show="AsustekC_ba:a3:9f" value="10bf48baa39f"/>
<field name="eth.lg" showname=".... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)" size="3" pos="6" show="0" value="0" unmaskedvalue="10bf48"/>
<field name="eth.ig" showname=".... ...0 .... .... .... .... = IG bit: Individual address (unicast)" size="3" pos="6" show="0" value="0" unmaskedvalue="10bf48"/>
</field>
<field name="eth.type" showname="Type: IPv4 (0x0800)" size="2" pos="12" show="0x00000800" value="0800"/>
</proto>
<proto name="ip" showname="Internet Protocol Version 4, Src: 10.20.5.161, Dst: 71.19.149.209" size="20" pos="14">
<field name="ip.version" showname="0100 .... = Version: 4" size="1" pos="14" show="4" value="4" unmaskedvalue="45"/>
<field name="ip.hdr_len" showname=".... 0101 = Header Length: 20 bytes" size="1" pos="14" show="5" value="5" unmaskedvalue="45"/>
<field name="ip.dsfield" showname="Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)" size="1" pos="15" show="0x00000000" value="00">
<field name="ip.dsfield.dscp" showname="0000 00.. = Differentiated Services Codepoint: Default (0)" size="1" pos="15" show="0" value="0" unmaskedvalue="00"/>
<field name="ip.dsfield.ecn" showname=".... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0)" size="1" pos="15" show="0" value="0" unmaskedvalue="00"/>
</field>
<field name="ip.len" showname="Total Length: 52" size="2" pos="16" show="52" value="0034"/>
<field name="ip.id" showname="Identification: 0x96ab (38571)" size="2" pos="18" show="0x000096ab" value="96ab"/>
<field name="ip.flags" showname="Flags: 0x02 (Don&#x27;t Fragment)" size="1" pos="20" show="0x00000002" value="40">
<field name="ip.flags.rb" showname="0... .... = Reserved bit: Not set" size="1" pos="20" show="0" value="40"/>
<field name="ip.flags.df" showname=".1.. .... = Don&#x27;t fragment: Set" size="1" pos="20" show="1" value="40"/>
<field name="ip.flags.mf" showname="..0. .... = More fragments: Not set" size="1" pos="20" show="0" value="40"/>
</field>
<field name="ip.frag_offset" showname="Fragment offset: 0" size="2" pos="20" show="0" value="4000"/>
<field name="ip.ttl" showname="Time to live: 64" size="1" pos="22" show="64" value="40"/>
<field name="ip.proto" showname="Protocol: TCP (6)" size="1" pos="23" show="6" value="06"/>
<field name="ip.checksum" showname="Header checksum: 0xb77f [validation disabled]" size="2" pos="24" show="0x0000b77f" value="b77f">
<field name="ip.checksum_good" showname="Good: False" size="2" pos="24" show="0" value="b77f"/>
<field name="ip.checksum_bad" showname="Bad: False" size="2" pos="24" show="0" value="b77f"/>
</field>
<field name="ip.src" showname="Source: 10.20.5.161" size="4" pos="26" show="10.20.5.161" value="0a1405a1"/>
<field name="ip.addr" showname="Source or Destination Address: 10.20.5.161" hide="yes" size="4" pos="26" show="10.20.5.161" value="0a1405a1"/>
<field name="ip.src_host" showname="Source Host: 10.20.5.161" hide="yes" size="4" pos="26" show="10.20.5.161" value="0a1405a1"/>
<field name="ip.host" showname="Source or Destination Host: 10.20.5.161" hide="yes" size="4" pos="26" show="10.20.5.161" value="0a1405a1"/>
<field name="ip.dst" showname="Destination: 71.19.149.209" size="4" pos="30" show="71.19.149.209" value="471395d1"/>
<field name="ip.addr" showname="Source or Destination Address: 71.19.149.209" hide="yes" size="4" pos="30" show="71.19.149.209" value="471395d1"/>
<field name="ip.dst_host" showname="Destination Host: 71.19.149.209" hide="yes" size="4" pos="30" show="71.19.149.209" value="471395d1"/>
<field name="ip.host" showname="Source or Destination Host: 71.19.149.209" hide="yes" size="4" pos="30" show="71.19.149.209" value="471395d1"/>
<field name="" show="Source GeoIP: Unknown" size="4" pos="26" value="0a1405a1"/>
<field name="" show="Destination GeoIP: Unknown" size="4" pos="30" value="471395d1"/>
</proto>
<proto name="tcp" showname="Transmission Control Protocol, Src Port: 42852 (42852), Dst Port: 9630 (9630), Seq: 1, Ack: 1, Len: 0" size="32" pos="34">
<field name="tcp.srcport" showname="Source Port: 42852" size="2" pos="34" show="42852" value="a764"/>
<field name="tcp.dstport" showname="Destination Port: 9630" size="2" pos="36" show="9630" value="259e"/>
<field name="tcp.port" showname="Source or Destination Port: 42852" hide="yes" size="2" pos="34" show="42852" value="a764"/>
<field name="tcp.port" showname="Source or Destination Port: 9630" hide="yes" size="2" pos="36" show="9630" value="259e"/>
<field name="tcp.stream" showname="Stream index: 0" size="0" pos="34" show="0"/>
<field name="tcp.len" showname="TCP Segment Len: 0" size="1" pos="46" show="0" value="80"/>
<field name="tcp.seq" showname="Sequence number: 1 (relative sequence number)" size="4" pos="38" show="1" value="c3f5a291"/>
<field name="tcp.ack" showname="Acknowledgment number: 1 (relative ack number)" size="4" pos="42" show="1" value="c9a7a7f5"/>
<field name="tcp.hdr_len" showname="Header Length: 32 bytes" size="1" pos="46" show="32" value="80"/>
<field name="tcp.flags" showname="Flags: 0x010 (ACK)" size="2" pos="46" show="0x00000010" value="10" unmaskedvalue="8010">
<field name="tcp.flags.res" showname="000. .... .... = Reserved: Not set" size="1" pos="46" show="0" value="0" unmaskedvalue="80"/>
<field name="tcp.flags.ns" showname="...0 .... .... = Nonce: Not set" size="1" pos="46" show="0" value="0" unmaskedvalue="80"/>
<field name="tcp.flags.cwr" showname=".... 0... .... = Congestion Window Reduced (CWR): Not set" size="1" pos="47" show="0" value="0" unmaskedvalue="10"/>
<field name="tcp.flags.ecn" showname=".... .0.. .... = ECN-Echo: Not set" size="1" pos="47" show="0" value="0" unmaskedvalue="10"/>
<field name="tcp.flags.urg" showname=".... ..0. .... = Urgent: Not set" size="1" pos="47" show="0" value="0" unmaskedvalue="10"/>
<field name="tcp.flags.ack" showname=".... ...1 .... = Acknowledgment: Set" size="1" pos="47" show="1" value="FFFFFFFFFFFFFFFF" unmaskedvalue="10"/>
<field name="tcp.flags.push" showname=".... .... 0... = Push: Not set" size="1" pos="47" show="0" value="0" unmaskedvalue="10"/>
<field name="tcp.flags.reset" showname=".... .... .0.. = Reset: Not set" size="1" pos="47" show="0" value="0" unmaskedvalue="10"/>
<field name="tcp.flags.syn" showname=".... .... ..0. = Syn: Not set" size="1" pos="47" show="0" value="0" unmaskedvalue="10"/>
<field name="tcp.flags.fin" showname=".... .... ...0 = Fin: Not set" size="1" pos="47" show="0" value="0" unmaskedvalue="10"/>
<field name="tcp.flags.str" showname="TCP Flags: *******A****" size="2" pos="46" show="*******A****" value="8010"/>
</field>
<field name="tcp.window_size_value" showname="Window size value: 360" size="2" pos="48" show="360" value="0168"/>
<field name="tcp.window_size" showname="Calculated window size: 360" size="2" pos="48" show="360" value="0168"/>
<field name="tcp.window_size_scalefactor" showname="Window size scaling factor: -1 (unknown)" size="2" pos="48" show="-1" value="0168"/>
<field name="tcp.checksum" showname="Checksum: 0x7c69 [validation disabled]" size="2" pos="50" show="0x00007c69" value="7c69">
<field name="tcp.checksum_good" showname="Good Checksum: False" size="2" pos="50" show="0" value="7c69"/>
<field name="tcp.checksum_bad" showname="Bad Checksum: False" size="2" pos="50" show="0" value="7c69"/>
</field>
<field name="tcp.urgent_pointer" showname="Urgent pointer: 0" size="2" pos="52" show="0" value="0000"/>
<field name="tcp.options" showname="Options: (12 bytes), No-Operation (NOP), No-Operation (NOP), Timestamps" size="12" pos="54" show="01:01:08:0a:01:7a:e6:87:d9:e9:a5:3f" value="0101080a017ae687d9e9a53f">
<field name="" show="No-Operation (NOP)" size="1" pos="54" value="01">
<field name="tcp.options.type" showname="Type: 1" size="1" pos="54" show="1" value="01">
<field name="tcp.options.type.copy" showname="0... .... = Copy on fragmentation: No" size="1" pos="54" show="0" value="0" unmaskedvalue="01"/>
<field name="tcp.options.type.class" showname=".00. .... = Class: Control (0)" size="1" pos="54" show="0" value="0" unmaskedvalue="01"/>
<field name="tcp.options.type.number" showname="...0 0001 = Number: No-Operation (NOP) (1)" size="1" pos="54" show="1" value="1" unmaskedvalue="01"/>
</field>
</field>
<field name="" show="No-Operation (NOP)" size="1" pos="55" value="01">
<field name="tcp.options.type" showname="Type: 1" size="1" pos="55" show="1" value="01">
<field name="tcp.options.type.copy" showname="0... .... = Copy on fragmentation: No" size="1" pos="55" show="0" value="0" unmaskedvalue="01"/>
<field name="tcp.options.type.class" showname=".00. .... = Class: Control (0)" size="1" pos="55" show="0" value="0" unmaskedvalue="01"/>
<field name="tcp.options.type.number" showname="...0 0001 = Number: No-Operation (NOP) (1)" size="1" pos="55" show="1" value="1" unmaskedvalue="01"/>
</field>
</field>
<field name="" show="Timestamps: TSval 24831623, TSecr 3655968063" size="10" pos="56" value="080a017ae687d9e9a53f">
<field name="tcp.option_kind" showname="Kind: Time Stamp Option (8)" size="1" pos="56" show="8" value="08"/>
<field name="tcp.option_len" showname="Length: 10" size="1" pos="57" show="10" value="0a"/>
<field name="tcp.options.timestamp.tsval" showname="Timestamp value: 24831623" size="4" pos="58" show="24831623" value="017ae687"/>
<field name="tcp.options.timestamp.tsecr" showname="Timestamp echo reply: 3655968063" size="4" pos="62" show="3655968063" value="d9e9a53f"/>
</field>
</field>
</proto>
</packet>
</pdml>
Loading

0 comments on commit 73933e9

Please sign in to comment.