File: check_dissector.py

package info (click to toggle)
wireshark 4.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 351,244 kB
  • sloc: ansic: 3,101,885; cpp: 129,710; xml: 100,972; python: 56,512; perl: 24,575; sh: 5,874; lex: 4,383; pascal: 4,304; makefile: 165; ruby: 113; objc: 91; tcl: 35
file content (176 lines) | stat: -rwxr-xr-x 5,874 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python3
# Wireshark - Network traffic analyzer
# By Gerald Combs <gerald@wireshark.org>
# Copyright 1998 Gerald Combs
#
# SPDX-License-Identifier: GPL-2.0-or-later

import sys
import os
import signal
import argparse
import subprocess
import re

# Run battery of tests on one or more dissectors.

# For text colouring/highlighting.
class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    ADDED = '\033[45m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'


# Try to exit soon after Ctrl-C is pressed.
should_exit = False

def signal_handler(sig, frame):
    global should_exit
    should_exit = True
    print('You pressed Ctrl+C - exiting')

signal.signal(signal.SIGINT, signal_handler)

# Command-line args
parser = argparse.ArgumentParser(description="Run gamut of tests on dissector(s)")
parser.add_argument('--file', action='append',
                    help='specify individual dissector file to test')
parser.add_argument('--file-list', action='store',
                    help='file with list of dissectors')
parser.add_argument('--open', action='store_true',
                    help='look for dissectors among upon files')
parser.add_argument('--commits', action='store',
                    help='last N commits to check')
parser.add_argument('--build-folder', action='store',
                    help='build folder')

args = parser.parse_args()

if not args.file and not args.file_list and not args.open and not args.commits:
    print('Need to specify --file, --file-list or --open or --commits')
    exit(1)

# TODO: verify build-folder if set.

# Get list of files to check.
dissectors = []

# Individually-selected files
if args.file:
    for f in args.file:
            if not os.path.isfile(f):
                print('Chosen file', f, 'does not exist.')
                exit(1)
            else:
                dissectors.append(f)

# List of dissectors stored in a file
if args.file_list:
    if not os.path.isfile(args.file_list):
        print('Dissector-list file', args.file_list, 'does not exist.')
        exit(1)
    else:
        with open(args.file_list, 'r') as f:
            contents = f.read().splitlines()
            for f in contents:
                if not os.path.isfile(f):
                    print('Chosen file', f, 'does not exist.')
                    exit(1)
                else:
                    dissectors.append(f)

def is_dissector_file(filename):
    p = re.compile(r'.*(packet|file)-.*\.c')
    return p.match(filename)

if args.open:
    # Unstaged changes.
    command = ['git', 'diff', '--name-only']
    files = [f.decode('utf-8')
             for f in subprocess.check_output(command).splitlines()]
    # Filter files.
    # TODO: should filter here (and below) with a better check for dissectors
    dissectors = list(filter(lambda f : is_dissector_file, files))

    # Staged changes.
    command = ['git', 'diff', '--staged', '--name-only']
    files_staged = [f.decode('utf-8')
                    for f in subprocess.check_output(command).splitlines()]
    # Filter files.  TODO: also check directory?
    files_staged = list(filter(lambda f : f.endswith('.c'), files_staged))
    for f in files_staged:
        if f not in files:
            dissectors.append(f)

if args.commits:
    # Get files affected by specified number of commits.
    command = ['git', 'diff', '--name-only', 'HEAD~' + args.commits]
    files = {f.decode('utf-8')
             for f in subprocess.check_output(command).splitlines()}
    # Will examine dissector files only
    files = set(filter(is_dissector_file, files))
    dissectors.extend(files)


# Tools that should be run on selected files.
# Boolean arg is for whether build-dir is needed in order to run it.
# 3rd is Windows support.
tools = [
    ('tools/check_spelling.py --comments --no-wikipedia', False,  True),
    ('tools/check_tfs.py --check-value-strings',          False,  True),
    ('tools/check_typed_item_calls.py --all-checks ' +
      '--extra-value-string-checks --check-expert-items', False,  True),
    ('tools/check_static.py',                             True,   False),
    ('tools/check_dissector_urls.py',                     False,  True),
    ('tools/check_val_to_str.py',                         False,  True),
    ('tools/check_col_apis.py',                           False,  True),
    ('tools/cppcheck/cppcheck.sh',                        False,  True),
    ('tools/checkhf.pl',                                  False,  True),
    ('tools/checkAPIs.pl',                                False,  True),
    ('tools/fix-encoding-args.pl',                        False,  True),
    ('tools/checkfiltername.pl',                          False,  True)
]


def run_check(tool, dissectors, python):
    # Create command-line with all dissectors included
    command = ''

    # Don't trust shebang on windows.
    if sys.platform.startswith('win'):
        if python:
            command += 'python.exe '
        else:
            command += 'perl.exe '

    command += tool[0]
    if tool[1]:
        command += ' --build-folder ' + args.build_folder

    for d in dissectors:
        # Add this dissector file to command-line args
        command += ((' --file' if python else '') + ' ' + d)

    # Run it
    print(bcolors.BOLD + command + bcolors.ENDC)
    os.system(command)


# Run all checks on all of my dissectors.
if len(dissectors):
    for tool in tools:
        if should_exit:
            exit(1)
        if ((not sys.platform.startswith('win') or tool[2]) and # Supported on this platform?
            (not tool[1] or (tool[1] and args.build_folder))):   # Have --build-folder if needed?

            # Run it.
            run_check(tool, dissectors, tool[0].find('.py') != -1)
else:
    print('No dissectors selected')