FIXUP: give names to sec_vt_command's
[wireshark-wip.git] / tools / ftsanity.py
blob82e53c550e4ac1fa8146253f3b01a324b5bef593
1 #!/usr/bin/env python
2 """
3 Check the sanity of field definitions in Wireshark.
4 """
6 # Gilbert Ramirez <gram [AT] alumni.rice.edu>
8 # Wireshark - Network traffic analyzer
9 # By Gerald Combs <gerald@wireshark.org>
10 # Copyright 1998 Gerald Combs
12 # This program is free software; you can redistribute it and/or
13 # modify it under the terms of the GNU General Public License
14 # as published by the Free Software Foundation; either version 2
15 # of the License, or (at your option) any later version.
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 import sys
28 try:
29 from optparse import OptionParser
30 except ImportError:
31 sys.exit("Need python 2.3.")
33 try:
34 import commands
35 except ImportError:
36 sys.exit("Need to run on Unix.")
39 errors = 0
41 class Proto:
42 """Data for a protocol."""
43 def __init__(self, line):
44 data = line.split("\t")
45 assert len(data) == 3
46 assert data[0] == "P"
47 self.name = data[1]
48 self.abbrev = data[2]
50 class Field:
51 """Data for a field."""
52 def __init__(self, line):
53 data = line.split("\t")
54 assert len(data) == 8
55 assert data[0] == "F"
56 self.name = data[1]
57 self.abbrev = data[2]
58 self.ftype = data[3]
59 self.parent = data[4]
60 self.blurb = data[5]
61 self.base = data[6]
62 self.bitmask = int(data[7],0)
66 def gather_data(tshark):
67 """Calls tshark and gathers data."""
68 cmd = "%s -G fields3" % (tshark,)
69 (status, output) = commands.getstatusoutput(cmd)
71 if status != 0:
72 sys.exit("Failed: " + cmd)
74 lines = output.split("\n")
75 protos = [Proto(x) for x in lines if x[0] == "P"]
76 fields = [Field(x) for x in lines if x[0] == "F"]
78 return protos, fields
81 def check_fields(fields):
82 """Looks for problems in field definitions."""
83 global errors
84 for field in fields:
85 if field.bitmask != 0:
86 if field.ftype.find("FT_UINT") != 0 and \
87 field.ftype.find("FT_INT") != 0 and \
88 field.ftype != "FT_BOOLEAN":
89 print "%s has a bitmask 0x%x but is type %s" % \
90 (field.abbrev, field.bitmask, field.ftype)
91 errors += 1
93 def run(tshark):
94 """Run the tests."""
95 global errors
96 protos, fields = gather_data(tshark)
98 check_fields(fields)
100 if errors > 0:
101 sys.exit("%d errors found" % (errors,))
102 else:
103 print "Success."
105 def main():
106 """Parse the command-line."""
107 usage = "%prog tshark"
108 parser = OptionParser(usage=usage)
110 (options, args) = parser.parse_args()
112 if len(args) != 1:
113 parser.error("Need location of tshark.")
115 run(args[0])
117 if __name__ == "__main__":
118 main()