more packet-kerberos...
[metze/wireshark/wip.git] / test / suite_nameres.py
1 #
2 # -*- coding: utf-8 -*-
3 # Wireshark tests
4 # By Gerald Combs <gerald@wireshark.org>
5 #
6 # Ported from a set of Bash scripts which were copyright 2005 Ulf Lamping
7 #
8 # SPDX-License-Identifier: GPL-2.0-or-later
9 #
10 '''Name resolution tests'''
11
12 import os.path
13 import shutil
14 import subprocesstest
15 import fixtures
16
17 tf_str = { True: 'TRUE', False: 'FALSE' }
18
19 custom_profile_name = 'Custom Profile'
20
21 @fixtures.fixture
22 def nameres_env(test_env, program_path, conf_path):
23     bundle_path = os.path.join(program_path, 'Wireshark.app', 'Contents', 'MacOS')
24     if os.path.isdir(bundle_path):
25         global_path = bundle_path
26     else:
27         global_path = program_path
28     custom_profile_path = os.path.join(conf_path, 'profiles', custom_profile_name)
29     os.makedirs(custom_profile_path)
30     this_dir = os.path.dirname(__file__)
31     hosts_path_pfx = os.path.join(this_dir, 'hosts.')
32     shutil.copyfile(hosts_path_pfx + 'global', os.path.join(global_path, 'hosts'))
33     shutil.copyfile(hosts_path_pfx + 'personal', os.path.join(conf_path, 'hosts'))
34     shutil.copyfile(hosts_path_pfx + 'custom', os.path.join(custom_profile_path, 'hosts'))
35     return test_env
36
37
38 @fixtures.fixture
39 def check_name_resolution(cmd_tshark, capture_file, nameres_env):
40     def check_name_resolution_real(self, o_net_name, o_external_name_res, o_hosts_file, custom_profile, grep_str, fail_on_match=False):
41         tshark_cmd = (cmd_tshark,
42             '-r', capture_file('dns+icmp.pcapng.gz'),
43             '-o', 'nameres.network_name: ' + tf_str[o_net_name],
44             '-o', 'nameres.use_external_name_resolver: ' + tf_str[o_external_name_res],
45             '-o', 'nameres.hosts_file_handling: ' + tf_str[o_hosts_file],
46             )
47         if custom_profile:
48             tshark_cmd += ('-C', custom_profile_name)
49         self.assertRun(tshark_cmd, env=nameres_env)
50         if fail_on_match:
51             self.assertFalse(self.grepOutput(grep_str))
52         else:
53             self.assertTrue(self.grepOutput(grep_str))
54     return check_name_resolution_real
55
56
57 @fixtures.mark_usefixtures('test_env')
58 @fixtures.uses_fixtures
59 class case_name_resolution(subprocesstest.SubprocessTestCase):
60
61     def test_name_resolution_net_t_ext_f_hosts_f_global(self, check_name_resolution):
62         '''Name resolution, no external, no profile hosts, global profile.'''
63         # nameres.network_name: True
64         # nameres.use_external_name_resolver: False
65         # nameres.hosts_file_handling: False
66         # Profile: Default
67         check_name_resolution(self, True, False, False, False, 'global-8-8-8-8')
68
69     def test_name_resolution_net_t_ext_f_hosts_f_personal(self, check_name_resolution):
70         '''Name resolution, no external, no profile hosts, personal profile.'''
71         # nameres.network_name: True
72         # nameres.use_external_name_resolver: False
73         # nameres.hosts_file_handling: False
74         # Profile: Default
75         check_name_resolution(self, True, False, False, False, 'personal-8-8-4-4')
76
77     def test_name_resolution_net_t_ext_f_hosts_f_custom(self, check_name_resolution):
78         '''Name resolution, no external, no profile hosts, custom profile.'''
79         # nameres.network_name: True
80         # nameres_use_external_name_resolver: False
81         # nameres.hosts_file_handling: False
82         # Profile: Custom
83         check_name_resolution(self, True, False, False, True, 'custom-4-2-2-2')
84
85     def test_name_resolution_net_t_ext_f_hosts_t_global(self, check_name_resolution):
86         '''Name resolution, no external, profile hosts, global profile.'''
87         # nameres.network_name: True
88         # nameres.use_external_name_resolver: False
89         # nameres.hosts_file_handling: True
90         # Profile: Default
91         check_name_resolution(self, True, False, True, False, 'global-8-8-8-8', True)
92
93     def test_name_resolution_net_t_ext_f_hosts_t_personal(self, check_name_resolution):
94         '''Name resolution, no external, profile hosts, personal profile.'''
95         # nameres.network_name: True
96         # nameres.use_external_name_resolver: False
97         # nameres.hosts_file_handling: True
98         # Profile: Default
99         check_name_resolution(self, True, False, True, False, 'personal-8-8-4-4')
100
101     def test_name_resolution_net_t_ext_f_hosts_t_custom(self, check_name_resolution):
102         '''Name resolution, no external, profile hosts, custom profile.'''
103         # nameres.network_name: True
104         # nameres_use_external_name_resolver: False
105         # nameres.hosts_file_handling: True
106         # Profile: Custom
107         check_name_resolution(self, True, False, True, True, 'custom-4-2-2-2')
108
109     def test_hosts_any(self, cmd_tshark, capture_file):
110         self.assertRun((cmd_tshark,
111                 '-r', capture_file('dns+icmp.pcapng.gz'),
112                 '-qz', 'hosts',
113                 ))
114         self.assertTrue(self.grepOutput('174.137.42.65\twww.wireshark.org'))
115         self.assertTrue(self.grepOutput('fe80::6233:4bff:fe13:c558\tCrunch.local'))
116
117     def test_hosts_ipv4(self, cmd_tshark, capture_file):
118         self.assertRun((cmd_tshark,
119                 '-r', capture_file('dns+icmp.pcapng.gz'),
120                 '-qz', 'hosts,ipv4',
121                 ))
122         self.assertTrue(self.grepOutput('174.137.42.65\twww.wireshark.org'))
123         self.assertFalse(self.grepOutput('fe80::6233:4bff:fe13:c558\tCrunch.local'))
124
125     def test_hosts_ipv6(self, cmd_tshark, capture_file):
126         self.assertRun((cmd_tshark,
127                 '-r', capture_file('dns+icmp.pcapng.gz'),
128                 '-qz', 'hosts,ipv6',
129                 ))
130         self.assertTrue(self.grepOutput('fe80::6233:4bff:fe13:c558\tCrunch.local'))
131         self.assertFalse(self.grepOutput('174.137.42.65\twww.wireshark.org'))