summaryrefslogtreecommitdiffstats
path: root/test/suite_mergecap.py
blob: 95e657b5dacdc34b8156a4a34473a83b7655fb5b (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#
# Wireshark tests
# By Gerald Combs <gerald@wireshark.org>
#
# Ported from a set of Bash scripts which were copyright 2005 Ulf Lamping
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
'''Mergecap tests'''

import re
import subprocess
from subprocesstest import grep_output

testout_pcap = 'testout.pcap'
testout_pcapng = 'testout.pcapng'

file_type_to_descr = {
    'pcap': 'Wireshark/tcpdump/... - pcap',
    'pcapng': 'Wireshark/... - pcapng',
}

# common checking code:
# arg 1 = return value from mergecap command
# arg 2 = file type string
# arg 3 = file encap
# arg 4 = number of IDBs generated
# arg 5 = number of file packets merged
# arg 6 = number of some IDB packets merged
def check_mergecap(mergecap_proc, file_type, encapsulation, tot_packets, generated_idbs, idb_packets, cmd_capinfos, testout_file, env=None):

    assert mergecap_proc.returncode == 0
    assert grep_output(mergecap_proc.stderr, 'merging complete')
    assert file_type in file_type_to_descr, 'Invalid file type'

    capinfos_stdout = subprocess.check_output([cmd_capinfos, '-t', '-E', '-I', '-c', testout_file], encoding='utf-8', env=env)

    file_descr = file_type_to_descr[file_type]
    type_pat = r'File type:\s+{}'.format(file_descr)
    assert re.search(type_pat, capinfos_stdout), \
        'Failed to generate a {} file'.format(file_type)

    encap_pat = r'File encapsulation:\s+{}'.format(encapsulation)
    assert re.search(encap_pat, capinfos_stdout), \
        'Failed to generate an {} encapsulation'.format(encapsulation)

    pkt_pat = r'Number of packets:\s+{}'.format(tot_packets)
    assert re.search(pkt_pat, capinfos_stdout), \
        'Failed to generate {} packets'.format(tot_packets)

    gidb_pat = r'Number of interfaces in file:\s+{}'.format(generated_idbs)
    assert re.search(gidb_pat, capinfos_stdout), \
        'Failed to generate {} IDBs'.format(generated_idbs)

    midb_pat = r'\s+Number of packets\s+=\s+{}'.format(idb_packets)
    assert re.search(midb_pat, capinfos_stdout), \
        'Failed to merge {} IDB packets'.format(idb_packets)


class TestMergecapPcap:
    def test_mergecap_basic_1_pcap_pcap(self, cmd_mergecap, capture_file, result_file, cmd_capinfos, test_env):
        '''Merge a single pcap file to pcap'''
        # $MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
        testout_file = result_file(testout_pcap)
        mergecap_proc = subprocess.run((cmd_mergecap,
            '-V',
            '-F', 'pcap',
            '-w', testout_file,
            capture_file('dhcp.pcap'),
        ), capture_output=True, encoding='utf-8', env=test_env)
        check_mergecap(mergecap_proc, 'pcap', 'Ethernet', 4, 1, 4, cmd_capinfos, testout_file, test_env)

    def test_mergecap_basic_2_pcap_pcap(self, cmd_mergecap, capture_file, result_file, cmd_capinfos, test_env):
        '''Merge two pcap files to pcap'''
        # $MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
        testout_file = result_file(testout_pcap)
        mergecap_proc = subprocess.run((cmd_mergecap,
            '-V',
            '-F', 'pcap',
            '-w', testout_file,
            capture_file('dhcp.pcap'), capture_file('dhcp.pcap'),
        ), capture_output=True, encoding='utf-8', env=test_env)
        check_mergecap(mergecap_proc, 'pcap', 'Ethernet', 8, 1, 8, cmd_capinfos, testout_file, test_env)

    def test_mergecap_basic_3_empty_pcap_pcap(self, cmd_mergecap, capture_file, result_file, cmd_capinfos, test_env):
        '''Merge three pcap files to pcap, two empty'''
        # $MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}empty.pcap" "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}empty.pcap" > testout.txt 2>&1
        testout_file = result_file(testout_pcap)
        mergecap_proc = subprocess.run((cmd_mergecap,
            '-V',
            '-F', 'pcap',
            '-w', testout_file,
            capture_file('empty.pcap'), capture_file('dhcp.pcap'), capture_file('empty.pcap'),
        ), capture_output=True, encoding='utf-8', env=test_env)
        check_mergecap(mergecap_proc, 'pcap', 'Ethernet', 4, 1, 4, cmd_capinfos, testout_file, test_env)

    def test_mergecap_basic_2_nano_pcap_pcap(self, cmd_mergecap, capture_file, result_file, cmd_capinfos, test_env):
        '''Merge two pcap files to pcap, one with nanosecond timestamps'''
        # $MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}dhcp-nanosecond.pcap" "${CAPTURE_DIR}rsasnakeoil2.pcap" > testout.txt 2>&1
        testout_file = result_file(testout_pcap)
        mergecap_proc = subprocess.run((cmd_mergecap,
            '-V',
            '-F', 'pcap',
            '-w', testout_file,
            capture_file('dhcp-nanosecond.pcap'), capture_file('rsasnakeoil2.pcap'),
        ), capture_output=True, encoding='utf-8', env=test_env)
        check_mergecap(mergecap_proc, 'pcap', 'Ethernet', 62, 1, 62, cmd_capinfos, testout_file, test_env)


class TestMergecapPcapng:
    def test_mergecap_basic_1_pcap_pcapng(self, cmd_mergecap, capture_file, result_file, cmd_capinfos, test_env):
        '''Merge a single pcap file to pcapng'''
        # $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
        testout_file = result_file(testout_pcapng)
        mergecap_proc = subprocess.run((cmd_mergecap,
            '-V',
            '-w', testout_file,
            capture_file('dhcp.pcap'),
        ), capture_output=True, encoding='utf-8', env=test_env)
        check_mergecap(mergecap_proc, 'pcapng', 'Ethernet', 4, 1, 4, cmd_capinfos, testout_file, test_env)

    def test_mergecap_basic_2_pcap_pcapng(self, cmd_mergecap, capture_file, result_file, cmd_capinfos, test_env):
        '''Merge two pcap files to pcapng'''
        # $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
        testout_file = result_file(testout_pcapng)
        mergecap_proc = subprocess.run((cmd_mergecap,
            '-V',
            '-w', testout_file,
            capture_file('dhcp.pcap'), capture_file('dhcp.pcap'),
        ), capture_output=True, encoding='utf-8', env=test_env)
        check_mergecap(mergecap_proc, 'pcapng', 'Ethernet', 8, 1, 8, cmd_capinfos, testout_file, test_env)

    def test_mergecap_basic_2_pcap_none_pcapng(self, cmd_mergecap, capture_file, result_file, cmd_capinfos, test_env):
        '''Merge two pcap files to pcapng, "none" merge mode'''
        # $MERGECAP -vI 'none' -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
        testout_file = result_file(testout_pcapng)
        mergecap_proc = subprocess.run((cmd_mergecap,
            '-V',
            '-I', 'none',
            '-w', testout_file,
            capture_file('dhcp.pcap'), capture_file('dhcp.pcap'),
        ), capture_output=True, encoding='utf-8', env=test_env)
        check_mergecap(mergecap_proc, 'pcapng', 'Ethernet', 8, 2, 4, cmd_capinfos, testout_file, test_env)

    def test_mergecap_basic_2_pcap_all_pcapng(self, cmd_mergecap, capture_file, result_file, cmd_capinfos, test_env):
        '''Merge two pcap files to pcapng, "all" merge mode'''
        # $MERGECAP -vI 'all' -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
        testout_file = result_file(testout_pcapng)
        mergecap_proc = subprocess.run((cmd_mergecap,
            '-V',
            '-I', 'all',
            '-w', testout_file,
            capture_file('dhcp.pcap'), capture_file('dhcp.pcap'),
        ), capture_output=True, encoding='utf-8', env=test_env)
        check_mergecap(mergecap_proc, 'pcapng', 'Ethernet', 8, 1, 8, cmd_capinfos, testout_file, test_env)

    def test_mergecap_basic_2_pcap_any_pcapng(self, cmd_mergecap, capture_file, result_file, cmd_capinfos, test_env):
        '''Merge two pcap files to pcapng, "any" merge mode'''
        # $MERGECAP -vI 'any' -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
        testout_file = result_file(testout_pcapng)
        mergecap_proc = subprocess.run((cmd_mergecap,
            '-V',
            '-I', 'any',
            '-w', testout_file,
            capture_file('dhcp.pcap'), capture_file('dhcp.pcap'),
        ), capture_output=True, encoding='utf-8', env=test_env)
        check_mergecap(mergecap_proc, 'pcapng', 'Ethernet', 8, 1, 8, cmd_capinfos, testout_file, test_env)

    def test_mergecap_basic_1_pcapng_pcapng(self, cmd_mergecap, capture_file, result_file, cmd_capinfos, test_env):
        '''Merge a single pcapng file to pcapng'''
        # $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}dhcp.pcapng" > testout.txt 2>&1
        testout_file = result_file(testout_pcapng)
        mergecap_proc = subprocess.run((cmd_mergecap,
            '-V',
            '-w', testout_file,
            capture_file('dhcp.pcapng'),
        ), capture_output=True, encoding='utf-8', env=test_env)
        check_mergecap(mergecap_proc, 'pcapng', 'Ethernet', 4, 1, 4, cmd_capinfos, testout_file, test_env)

    def test_mergecap_1_pcapng_many_pcapng(self, cmd_mergecap, capture_file, result_file, cmd_capinfos, test_env):
        '''Merge one pcapng file with many interfaces to pcapng'''
        # $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}many_interfaces.pcapng.1" > testout.txt 2>&1
        testout_file = result_file(testout_pcapng)
        mergecap_proc = subprocess.run((cmd_mergecap,
            '-V',
            '-w', testout_file,
            capture_file('many_interfaces.pcapng.1'),
        ), capture_output=True, encoding='utf-8', env=test_env)
        check_mergecap(mergecap_proc, 'pcapng', 'Per packet', 64, 11, 62, cmd_capinfos, testout_file, test_env)

    def test_mergecap_3_pcapng_pcapng(self, cmd_mergecap, capture_file, result_file, cmd_capinfos, test_env):
        '''Merge multiple pcapng files with many interfaces to pcapng'''
        # $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
        testout_file = result_file(testout_pcapng)
        mergecap_proc = subprocess.run((cmd_mergecap,
            '-V',
            '-w', testout_file,
            capture_file('many_interfaces.pcapng.1'),
            capture_file('many_interfaces.pcapng.2'),
            capture_file('many_interfaces.pcapng.3'),
        ), capture_output=True, encoding='utf-8', env=test_env)
        check_mergecap( mergecap_proc, 'pcapng', 'Per packet', 88, 11, 86, cmd_capinfos, testout_file, test_env)

    def test_mergecap_3_pcapng_none_pcapng(self, cmd_mergecap, capture_file, result_file, cmd_capinfos, test_env):
        '''Merge multiple pcapng files with many interfaces to pcapng, "none" merge mode'''
        # $MERGECAP -vI 'none' -w testout.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
        testout_file = result_file(testout_pcapng)
        mergecap_proc = subprocess.run((cmd_mergecap,
            '-V',
            '-I', 'none',
            '-w', testout_file,
            capture_file('many_interfaces.pcapng.1'),
            capture_file('many_interfaces.pcapng.2'),
            capture_file('many_interfaces.pcapng.3'),
        ), capture_output=True, encoding='utf-8', env=test_env)
        check_mergecap(mergecap_proc, 'pcapng', 'Per packet', 88, 33, 62, cmd_capinfos, testout_file, test_env)

    def test_mergecap_3_pcapng_all_pcapng(self, cmd_mergecap, capture_file, result_file, cmd_capinfos, test_env):
        '''Merge multiple pcapng files to pcapng in "none" mode, then merge that to "all" mode.'''
        # build a pcapng of all the interfaces repeated by using mode 'none'
        # $MERGECAP -vI 'none' -w testin.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
        testin_file = result_file('testin.pcapng')
        subprocess.check_call((cmd_mergecap,
            '-V',
            '-I', 'none',
            '-w', testin_file,
            capture_file('many_interfaces.pcapng.1'),
            capture_file('many_interfaces.pcapng.2'),
            capture_file('many_interfaces.pcapng.3'),
        ))
        # the above generated 33 IDBs, 88 total pkts, 62 in first IDB

        # and use that generated pcap for our test
        # $MERGECAP -vI 'all' -w testout.pcap ./testin.pcap ./testin.pcap ./testin.pcap > testout.txt 2>&1
        testout_file = result_file(testout_pcapng)
        mergecap_proc = subprocess.run((cmd_mergecap,
            '-V',
            '-I', 'all',
            '-w', testout_file,
            testin_file, testin_file, testin_file,
        ), capture_output=True, encoding='utf-8', env=test_env)
        # check for 33 IDBs, 88*3=264 total pkts, 62*3=186 in first IDB
        check_mergecap(mergecap_proc, 'pcapng', 'Per packet', 264, 33, 186, cmd_capinfos, testout_file, test_env)

    def test_mergecap_3_pcapng_any_pcapng(self, cmd_mergecap, capture_file, result_file, cmd_capinfos, test_env):
        '''Merge multiple pcapng files to pcapng in "none" mode, then merge that to "all" mode.'''
        # build a pcapng of all the interfaces repeated by using mode 'none'
        # $MERGECAP -vI 'none' -w testin.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
        testin_file = result_file('testin.pcapng')
        subprocess.check_call((cmd_mergecap,
            '-V',
            '-I', 'none',
            '-w', testin_file,
            capture_file('many_interfaces.pcapng.1'),
            capture_file('many_interfaces.pcapng.2'),
            capture_file('many_interfaces.pcapng.3'),
        ), env=test_env)
        # the above generated 33 IDBs, 88 total pkts, 62 in first IDB

        # and use that generated pcap for our test
        # $MERGECAP -vI 'any' -w testout.pcap ./testin.pcap ./testin.pcap ./testin.pcap > testout.txt 2>&1
        testout_file = result_file(testout_pcapng)
        mergecap_proc = subprocess.run((cmd_mergecap,
            '-V',
            '-I', 'any',
            '-w', testout_file,
            testin_file, testin_file, testin_file,
        ), capture_output=True, encoding='utf-8', env=test_env)
        # check for 11 IDBs, 88*3=264 total pkts, 86*3=258 in first IDB
        check_mergecap(mergecap_proc, 'pcapng', 'Per packet', 264, 11, 258, cmd_capinfos, testout_file, test_env)