3 # By Gerald Combs <gerald@wireshark.org>
5 # Copyright (c) 2018 Dario Lombardo <lomato@gmail.com>
7 # SPDX-License-Identifier: GPL-2.0-or-later
9 '''outputformats tests'''
14 from matchers
import *
18 def check_outputformat(cmd_tshark
, request
, dirs
, capture_file
):
19 def check_outputformat_real(format_option
, pcap_file
='dhcp.pcap',
20 extra_args
=[], expected
=None, multiline
=False, env
=None):
21 ''' Check a capture file against a sample, in json format. '''
22 tshark_proc
= subprocess
.run([cmd_tshark
, '-r', capture_file(pcap_file
),
23 '-T', format_option
] + extra_args
,
24 check
=True, capture_output
=True, encoding
='utf-8', env
=env
)
26 # If a filename is given, load the expected values from those.
27 if isinstance(expected
, str):
28 testdata
= open(os
.path
.join(dirs
.baseline_dir
, expected
)).read()
30 expected
= [json
.loads(line
) for line
in testdata
.splitlines()]
32 expected
= json
.loads(testdata
)
33 actual
= tshark_proc
.stdout
35 actual
= actual
.splitlines()
36 assert len(expected
) == len(actual
)
37 for expectedObj
, actualStr
in zip(expected
, actual
):
38 assert expectedObj
== json
.loads(actualStr
)
40 actual
= json
.loads(actual
)
41 assert expected
== actual
43 return check_outputformat_real
46 class TestOutputFormats
:
49 def test_outputformat_json(self
, check_outputformat
, base_env
):
50 '''Decode some captures into json'''
51 check_outputformat("json", expected
="dhcp.json", env
=base_env
)
53 def test_outputformat_json_asctime(self
, check_outputformat
, base_env
):
54 '''Decode some captures into json, with absolute times like asctime, for backwards compatibility '''
55 check_outputformat("json", extra_args
=['-o', 'protocols.display_abs_time_ascii:ALWAYS'],
56 expected
="dhcp-asctime.json", env
=base_env
)
58 def test_outputformat_jsonraw(self
, check_outputformat
, base_env
):
59 '''Decode some captures into jsonraw'''
60 check_outputformat("jsonraw", expected
="dhcp.jsonraw", env
=base_env
)
62 def test_outputformat_ek(self
, check_outputformat
, base_env
):
63 '''Decode some captures into ek'''
64 check_outputformat("ek", expected
="dhcp.ek", multiline
=True, env
=base_env
)
66 def test_outputformat_ek_raw(self
, check_outputformat
, base_env
):
67 '''Decode some captures into ek, with raw data'''
68 check_outputformat("ek", expected
="dhcp-raw.ek", multiline
=True, extra_args
=['-x'], env
=base_env
)
70 def test_outputformat_json_select_field(self
, check_outputformat
, base_env
):
71 '''Checks that the -e option works with -Tjson.'''
72 check_outputformat("json", extra_args
=['-eframe.number', '-c1'], expected
=[
74 "_index": "packets-2004-12-05",
87 def test_outputformat_ek_select_field(self
, check_outputformat
, base_env
):
88 '''Checks that the -e option works with -Tek.'''
89 check_outputformat("ek", extra_args
=['-eframe.number', '-c1'], expected
=[
90 {"index": {"_index": "packets-2004-12-05", "_type": "doc"}},
91 {"timestamp": "1102274184317", "layers": {"frame_number": ["1"]}}
92 ], multiline
=True, env
=base_env
)
94 def test_outputformat_ek_filter_field(self
, check_outputformat
, base_env
):
95 ''' Check that the option -j works with -Tek.'''
96 check_outputformat("ek", extra_args
=['-j', 'dhcp'], expected
="dhcp-filter.ek",
97 multiline
=True, env
=base_env
)