more
[metze/wireshark/wip.git] / test / suite_unittests.py
1 #
2 # -*- coding: utf-8 -*-
3 # Wireshark tests
4 # By
5 # Gerald Combs <gerald@wireshark.org>
6 # Gilbert Ramirez <gram [AT] alumni.rice.edu>
7 #
8 # Ported from a set of Bash scripts which were copyright 2005 Ulf Lamping
9 #
10 # SPDX-License-Identifier: GPL-2.0-or-later
11 #
12 '''EPAN unit tests'''
13
14 import config
15 import difflib
16 import os.path
17 import pprint
18 import re
19 import subprocesstest
20 import unittest
21
22 class case_unittests(subprocesstest.SubprocessTestCase):
23     def test_unit_exntest(self):
24         '''exntest'''
25         self.assertRun(os.path.join(config.program_path, 'exntest'))
26
27     def test_unit_oids_test(self):
28         '''oids_test'''
29         self.assertRun(os.path.join(config.program_path, 'oids_test'))
30
31     def test_unit_reassemble_test(self):
32         '''reassemble_test'''
33         self.assertRun(os.path.join(config.program_path, 'reassemble_test'))
34
35     def test_unit_tvbtest(self):
36         '''tvbtest'''
37         self.assertRun(os.path.join(config.program_path, 'tvbtest'))
38
39     def test_unit_wmem_test(self):
40         '''wmem_test'''
41         self.assertRun((os.path.join(config.program_path, 'wmem_test'),
42             '--verbose'
43         ))
44
45     def test_unit_wmem_test(self):
46         '''wmem_test'''
47         self.assertRun((os.path.join(config.program_path, 'wmem_test'),
48             '--verbose'
49         ))
50
51     def test_unit_fieldcount(self):
52         '''fieldcount'''
53         self.assertRun((config.cmd_tshark, '-G', 'fieldcount'))
54
55     def test_unit_ctest_coverage(self):
56         '''Make sure CTest runs all of our tests.'''
57         with open(os.path.join(config.this_dir, '..', 'CMakeLists.txt')) as cml_fd:
58             group_re = re.compile('set *\( *_test_group_list')
59             in_list = False
60             cml_groups = []
61             for cml_line in cml_fd:
62                 if group_re.search(cml_line):
63                     in_list = True
64                     continue
65                 if in_list:
66                     if ')' in cml_line:
67                         break
68                     cml_groups.append(cml_line.strip())
69         cml_groups.sort()
70         if not config.all_groups == cml_groups:
71             diff = '\n'.join(list(difflib.unified_diff(config.all_groups, cml_groups, 'all test groups', 'CMakeLists.txt test groups')))
72             self.fail("CMakeLists.txt doesn't test all available groups:\n" + diff)
73
74
75 class Proto:
76     """Data for a protocol."""
77     def __init__(self, line):
78         data = line.split("\t")
79         assert len(data) == 3, "expected 3 columns in %s" % data
80         assert data[0] == "P"
81         self.name = data[1]
82         self.abbrev = data[2]
83
84 class Field:
85     """Data for a field."""
86     def __init__(self, line):
87         data = line.split("\t")
88         assert len(data) == 8, "expected 8 columns in %s" % data
89         assert data[0] == "F"
90         self.name = data[1]
91         self.abbrev = data[2]
92         self.ftype = data[3]
93         self.parent = data[4]
94         self.base = data[5]
95         self.bitmask = int(data[6],0)
96         self.blurb = data[7]
97
98 class case_unit_ftsanity(subprocesstest.SubprocessTestCase):
99     def test_unit_ftsanity(self):
100         """Looks for problems in field type definitions."""
101         tshark_proc = self.assertRun((config.cmd_tshark, "-G", "fields"))
102
103         lines = tshark_proc.stdout_str.splitlines()
104         # XXX We don't currently check protos.
105         protos = [Proto(x) for x in lines if x[0] == "P"]
106         fields = [Field(x) for x in lines if x[0] == "F"]
107
108         err_list = []
109         for field in fields:
110             if field.bitmask != 0:
111                 if field.ftype.find("FT_UINT") != 0 and \
112                         field.ftype.find("FT_INT") != 0 and \
113                         field.ftype != "FT_BOOLEAN" and \
114                         field.ftype != "FT_CHAR":
115                     err_list.append("%s has a bitmask 0x%x but is type %s" % \
116                             (field.abbrev, field.bitmask, field.ftype))
117
118         self.assertEqual(len(err_list), 0, 'Found field type errors: \n' + '\n'.join(err_list))