summaryrefslogtreecommitdiffstats
path: root/test/suite_extcaps.py
blob: 2b38379866b5c9d2de0c233c68f46cd56578d7f4 (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
#
# Wireshark tests
# By Gerald Combs <gerald@wireshark.org>
#
# Copyright (c) 2019 Dario Lombardo <lomato@gmail.com>
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
'''extcap tests'''

import subprocess
import re
import os
import sys
import pytest


@pytest.fixture
def check_extcap_execution(cmd_extcap, program_path, base_env):
    def check_extcap_interface_execution(extcap_name, interface):
        ''' Check if an extcap runs flawlessly for interface configuration. '''

        subprocess.check_call([cmd_extcap(extcap_name), '--extcap-interface',
                        interface, '--extcap-dlts'], cwd=program_path, env=base_env)
        subprocess.check_call([cmd_extcap(extcap_name), '--extcap-interface',
                        interface, '--extcap-config'], cwd=program_path, env=base_env)

    def extcap_get_interfaces(extcap_output):
        ''' Extract the interface name from extcap. '''
        parser = re.compile("{value=(.*?)}")
        interfaces = []
        for line in extcap_output.splitlines():
            if line.startswith('interface '):
                interfaces.append(parser.findall(line)[0])
        return interfaces

    def check_extcap_execution_real(extcap_name, always_present=True):
        '''
        Check if an extcap runs flawlessly.
        always_present: at least one interface is always offered by the extcap.
        '''

        subprocess.check_call([cmd_extcap(extcap_name), '--help'], cwd=program_path, env=base_env)
        extcap_stdout = subprocess.check_output(
            [cmd_extcap(extcap_name), '--extcap-interfaces'], cwd=program_path, encoding='utf-8', env=base_env)
        interfaces = extcap_get_interfaces(extcap_stdout)
        if always_present:
            assert len(interfaces) > 0
        for interface in interfaces:
            check_extcap_interface_execution(extcap_name, interface)

    return check_extcap_execution_real


class TestExtcaps:
    def test_androiddump(self, check_extcap_execution):
        ''' extcap interface tests for androiddump '''
        check_extcap_execution("androiddump", always_present=False)

    def test_ciscodump(self, check_extcap_execution):
        ''' extcap interface tests for ciscodump '''
        check_extcap_execution("ciscodump")

    def test_dpauxmon(self, check_extcap_execution):
        ''' extcap interface tests for dpauxmon '''
        if not sys.platform.startswith('linux'):
            pytest.skip('dpauxmon available on Linux only')
        check_extcap_execution("dpauxmon")

    def test_randpktdump(self, check_extcap_execution):
        ''' extcap interface tests for randpktdump '''
        check_extcap_execution("randpktdump")

    def test_sdjournal(self, check_extcap_execution):
        ''' extcap interface tests for sdjournal '''
        if not sys.platform.startswith('linux'):
            pytest.skip('sdjournal is available on Linux only')
        check_extcap_execution("sdjournal")

    def test_sshdump(self, check_extcap_execution):
        ''' extcap interface tests for sshdump '''
        check_extcap_execution("sshdump")

    def test_wifidump(self, check_extcap_execution):
        ''' extcap interface tests for wifidump '''
        check_extcap_execution("wifidump")

    def test_udpdump(self, check_extcap_execution):
        ''' extcap interface tests for udpdump '''
        check_extcap_execution("udpdump")