2 # -*- coding: utf-8 -*-
4 # By Gerald Combs <gerald@wireshark.org>
6 # Ported from a set of Bash scripts which were copyright 2005 Ulf Lamping
8 # SPDX-License-Identifier: GPL-2.0-or-later
10 '''Name resolution tests'''
17 tf_str = { True: 'TRUE', False: 'FALSE' }
19 custom_profile_name = 'Custom Profile'
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
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'))
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],
48 tshark_cmd += ('-C', custom_profile_name)
49 self.assertRun(tshark_cmd, env=nameres_env)
51 self.assertFalse(self.grepOutput(grep_str))
53 self.assertTrue(self.grepOutput(grep_str))
54 return check_name_resolution_real
57 @fixtures.mark_usefixtures('test_env')
58 @fixtures.uses_fixtures
59 class case_name_resolution(subprocesstest.SubprocessTestCase):
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
67 check_name_resolution(self, True, False, False, False, 'global-8-8-8-8')
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
75 check_name_resolution(self, True, False, False, False, 'personal-8-8-4-4')
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
83 check_name_resolution(self, True, False, False, True, 'custom-4-2-2-2')
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
91 check_name_resolution(self, True, False, True, False, 'global-8-8-8-8', True)
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
99 check_name_resolution(self, True, False, True, False, 'personal-8-8-4-4')
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
107 check_name_resolution(self, True, False, True, True, 'custom-4-2-2-2')
109 def test_hosts_any(self, cmd_tshark, capture_file):
110 self.assertRun((cmd_tshark,
111 '-r', capture_file('dns+icmp.pcapng.gz'),
114 self.assertTrue(self.grepOutput('174.137.42.65\twww.wireshark.org'))
115 self.assertTrue(self.grepOutput('fe80::6233:4bff:fe13:c558\tCrunch.local'))
117 def test_hosts_ipv4(self, cmd_tshark, capture_file):
118 self.assertRun((cmd_tshark,
119 '-r', capture_file('dns+icmp.pcapng.gz'),
122 self.assertTrue(self.grepOutput('174.137.42.65\twww.wireshark.org'))
123 self.assertFalse(self.grepOutput('fe80::6233:4bff:fe13:c558\tCrunch.local'))
125 def test_hosts_ipv6(self, cmd_tshark, capture_file):
126 self.assertRun((cmd_tshark,
127 '-r', capture_file('dns+icmp.pcapng.gz'),
130 self.assertTrue(self.grepOutput('fe80::6233:4bff:fe13:c558\tCrunch.local'))
131 self.assertFalse(self.grepOutput('174.137.42.65\twww.wireshark.org'))