tshark: fix crash with -Tjson and -e field

Fix the assertion to check for the actual requirements. Add tests for -T
combined with -e.

Bug: 15444
Change-Id: I83e7663572db0c60194f6d6128b9e1ae7396b3f6
Fixes: v2.9.1rc0-226-g30c90fa745 ("epan: use json_dumper for json outputs.")
Reviewed-on: https://code.wireshark.org/review/31724
Petri-Dish: Peter Wu <peter@lekensteyn.nl>
Tested-by: Petri Dish Buildbot
Reviewed-by: Anders Broman <a.broman58@gmail.com>
This commit is contained in:
Peter Wu 2019-01-24 19:25:29 +01:00 committed by Anders Broman
parent d3f1e2fa19
commit e85c8bed87
2 changed files with 55 additions and 18 deletions

View File

@ -390,7 +390,7 @@ write_ek_proto_tree(output_fields_t* fields,
proto_tree_write_node_ek(edt->tree, &data);
} else {
/* Write out specified fields */
write_specified_fields(FORMAT_EK, fields, edt, cinfo, fh, data.dumper);
write_specified_fields(FORMAT_EK, fields, edt, cinfo, NULL, data.dumper);
}
json_dumper_end_object(&dumper);
@ -2339,7 +2339,12 @@ static void write_specified_fields(fields_format format, output_fields_t *fields
g_assert(fields);
g_assert(fields->fields);
g_assert(edt);
g_assert(fh);
/* JSON formats must go through json_dumper */
if (format == FORMAT_JSON || format == FORMAT_EK) {
g_assert(!fh && dumper);
} else {
g_assert(fh && !dumper);
}
data.fields = fields;
data.edt = edt;

View File

@ -14,41 +14,73 @@ import subprocesstest
import fixtures
from matchers import *
@fixtures.fixture
def check_outputformat(cmd_tshark, dirs, capture_file):
''' Check a capture file against a sample, in json format. '''
def check_outputformat_real(self, pcap_file, format_option, format_file, multiline=False):
self.maxDiff = 1000000
tshark_proc = self.assertRun((cmd_tshark, '-r', capture_file(pcap_file), '-T', format_option,))
expected = open(os.path.join(dirs.baseline_dir, format_file)).read()
@fixtures.fixture
def check_outputformat(cmd_tshark, request, dirs, capture_file):
''' Check a capture file against a sample, in json format. '''
def check_outputformat_real(format_option, pcap_file='dhcp.pcap',
extra_args=[], expected=None, multiline=False):
self = request.instance
tshark_proc = self.assertRun([cmd_tshark, '-r', capture_file(pcap_file),
'-T', format_option] + extra_args)
# If a filename is given, load the expected values from those.
if isinstance(expected, str):
testdata = open(os.path.join(dirs.baseline_dir, expected)).read()
if multiline:
expected = [json.loads(line) for line in testdata.splitlines()]
else:
expected = json.loads(testdata)
actual = tshark_proc.stdout_str
if multiline:
expected = expected.splitlines()
actual = actual.splitlines()
self.assertEqual(len(expected), len(actual))
for line1, line2 in zip(expected, actual):
json.loads(line1)
json.loads(line2)
self.assertEqual(json.loads(line1), json.loads(line2))
for expectedObj, actualStr in zip(expected, actual):
self.assertEqual(expectedObj, json.loads(actualStr))
else:
expected = json.loads(expected)
actual = json.loads(actual)
self.assertEqual(expected, actual)
return check_outputformat_real
@fixtures.mark_usefixtures('base_env')
@fixtures.uses_fixtures
class case_outputformats(subprocesstest.SubprocessTestCase):
maxDiff = 1000000
def test_outputformat_json(self, check_outputformat):
'''Decode some captures into json'''
check_outputformat(self, "dhcp.pcap", "json", "dhcp.json")
check_outputformat("json", expected="dhcp.json")
def test_outputformat_jsonraw(self, check_outputformat):
'''Decode some captures into jsonraw'''
check_outputformat(self, "dhcp.pcap", "jsonraw", "dhcp.jsonraw")
check_outputformat("jsonraw", expected="dhcp.jsonraw")
def test_outputformat_ek(self, check_outputformat):
'''Decode some captures into ek'''
check_outputformat(self, "dhcp.pcap", "ek", "dhcp.ek", True)
check_outputformat("ek", expected="dhcp.ek", multiline=True)
def test_outputformat_json_select_field(self, check_outputformat):
'''Checks that the -e option works with -Tjson.'''
check_outputformat("json", extra_args=['-eframe.number', '-c1'], expected=[
{
"_index": "packets-2004-12-05",
"_type": "pcap_file",
"_score": None,
"_source": {
"layers": {
"frame.number": [
"1"
]
}
}
}
])
def test_outputformat_ek_select_field(self, check_outputformat):
'''Checks that the -e option works with -Tek.'''
check_outputformat("ek", extra_args=['-eframe.number', '-c1'], expected=[
{"index": {"_index": "packets-2004-12-05", "_type": "pcap_file"}},
{"timestamp": "1102274184317", "layers": {"frame_number": ["1"]}}
], multiline=True)