3 # By Gerald Combs <gerald@wireshark.org>
5 # Ported from a set of Bash scripts which were copyright 2005 Ulf Lamping
7 # SPDX-License-Identifier: GPL-2.0-or-later
9 '''Name resolution tests'''
14 from subprocesstest
import grep_output
17 tf_str
= { True: 'TRUE', False: 'FALSE' }
19 custom_profile_name
= 'Custom Profile'
22 def nameres_setup(program_path
, conf_path
):
23 bundle_path
= os
.path
.join(program_path
, 'Wireshark.app', 'Contents', 'MacOS')
24 if os
.path
.isdir(bundle_path
):
25 # Don't modify our application bundle.
28 global_path
= program_path
29 custom_profile_path
= os
.path
.join(conf_path
, 'profiles', custom_profile_name
)
30 os
.makedirs(custom_profile_path
)
31 this_dir
= os
.path
.dirname(__file__
)
32 hosts_path_pfx
= os
.path
.join(this_dir
, 'hosts.')
34 if global_path
is not None:
35 shutil
.copyfile(hosts_path_pfx
+ 'global', os
.path
.join(global_path
, 'hosts'))
36 shutil
.copyfile(hosts_path_pfx
+ 'personal', os
.path
.join(conf_path
, 'hosts'))
37 shutil
.copyfile(hosts_path_pfx
+ 'custom', os
.path
.join(custom_profile_path
, 'hosts'))
38 return global_path
is not None
42 def check_name_resolution(cmd_tshark
, capture_file
, nameres_setup
, test_env
):
43 def check_name_resolution_real(o_net_name
, o_external_name_res
, custom_profile
, grep_str
, fail_on_match
=False):
44 if grep_str
.startswith('global') and not nameres_setup
:
45 pytest
.skip('Global name resolution tests would require modifying the application bundle')
46 tshark_cmd
= (cmd_tshark
,
47 '-r', capture_file('dns+icmp.pcapng.gz'),
48 '-o', 'nameres.network_name: ' + tf_str
[o_net_name
],
49 '-o', 'nameres.use_external_name_resolver: ' + tf_str
[o_external_name_res
],
52 tshark_cmd
+= ('-C', custom_profile_name
)
53 proc
= subprocess
.run(tshark_cmd
, check
=True, capture_output
=True, encoding
='utf-8', env
=test_env
)
55 assert not grep_output(proc
.stdout
, grep_str
)
57 assert grep_output(proc
.stdout
, grep_str
)
58 return check_name_resolution_real
61 class TestNameResolution
:
63 def test_name_resolution_net_t_ext_f_hosts_f_global(self
, check_name_resolution
):
64 '''Name resolution, no external, global profile.'''
65 # nameres.network_name: True
66 # nameres.use_external_name_resolver: False
68 check_name_resolution(True, False, False, 'global-8-8-8-8')
70 def test_name_resolution_net_t_ext_f_hosts_f_personal(self
, check_name_resolution
):
71 '''Name resolution, no external, personal profile.'''
72 # nameres.network_name: True
73 # nameres.use_external_name_resolver: False
75 check_name_resolution(True, 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
82 check_name_resolution(True, False, True, 'custom-4-2-2-2')
84 def test_hosts_any(self
, cmd_tshark
, capture_file
, base_env
):
85 stdout
= subprocess
.check_output((cmd_tshark
,
86 '-r', capture_file('dns+icmp.pcapng.gz'),
88 ), encoding
='utf-8', env
=base_env
)
89 assert '174.137.42.65\twww.wireshark.org' in stdout
90 assert 'fe80::6233:4bff:fe13:c558\tCrunch.local' in stdout
92 def test_hosts_ipv4(self
, cmd_tshark
, capture_file
, base_env
):
93 stdout
= subprocess
.check_output((cmd_tshark
,
94 '-r', capture_file('dns+icmp.pcapng.gz'),
96 ), encoding
='utf-8', env
=base_env
)
97 assert '174.137.42.65\twww.wireshark.org' in stdout
98 assert 'fe80::6233:4bff:fe13:c558\tCrunch.local' not in stdout
100 def test_hosts_ipv6(self
, cmd_tshark
, capture_file
, base_env
):
101 stdout
= subprocess
.check_output((cmd_tshark
,
102 '-r', capture_file('dns+icmp.pcapng.gz'),
104 ), encoding
='utf-8', env
=base_env
)
105 assert '174.137.42.65\twww.wireshark.org' not in stdout
106 assert 'fe80::6233:4bff:fe13:c558\tCrunch.local' in stdout