1 # SPDX-License-Identifier: GPL-2.0
2 # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
4 # Test various network-related functionality, such as the dhcp, ping, and
13 Note: This test relies on boardenv_* containing configuration values to define
14 which network environment is available for testing. Without this, this test
15 will be automatically skipped.
19 # Boolean indicating whether the Ethernet device is attached to USB, and hence
20 # USB enumeration needs to be performed prior to network tests.
21 # This variable may be omitted if its value is False.
22 env__net_uses_usb = False
24 # Boolean indicating whether the Ethernet device is attached to PCI, and hence
25 # PCI enumeration needs to be performed prior to network tests.
26 # This variable may be omitted if its value is False.
27 env__net_uses_pci = True
29 # True if a DHCP server is attached to the network, and should be tested.
30 # If DHCP testing is not possible or desired, this variable may be omitted or
32 env__net_dhcp_server = True
34 # True if a DHCPv6 server is attached to the network, and should be tested.
35 # If DHCPv6 testing is not possible or desired, this variable may be omitted or
37 env__net_dhcp6_server = True
39 # A list of environment variables that should be set in order to configure a
40 # static IP. If solely relying on DHCP, this variable may be omitted or set to
42 env__net_static_env_vars = [
43 ('ipaddr', '10.0.0.100'),
44 ('netmask', '255.255.255.0'),
45 ('serverip', '10.0.0.1'),
48 # Details regarding a file that may be read from a TFTP server. This variable
49 # may be omitted or set to None if TFTP testing is not possible or desired.
50 env__net_tftp_readable_file = {
51 'fn': 'ubtest-readable.bin',
56 'fnu': 'ubtest-upload.bin',
59 # Details regarding a file that may be read from a NFS server. This variable
60 # may be omitted or set to None if NFS testing is not possible or desired.
61 env__net_nfs_readable_file = {
62 'fn': 'ubtest-readable.bin',
68 # Details regarding a file that may be read from a TFTP server. This variable
69 # may be omitted or set to None if PXE testing is not possible or desired.
70 env__net_pxe_readable_file = {
78 # True if a router advertisement service is connected to the network, and should
79 # be tested. If router advertisement testing is not possible or desired, this
80 variable may be omitted or set to False.
81 env__router_on_net = True
87 def test_net_pre_commands(u_boot_console):
88 """Execute any commands required to enable network hardware.
90 These commands are provided by the boardenv_* file; see the comment at the
91 beginning of this file.
94 init_usb = u_boot_console.config.env.get('env__net_uses_usb', False)
96 u_boot_console.run_command('usb start')
98 init_pci = u_boot_console.config.env.get('env__net_uses_pci', False)
100 u_boot_console.run_command('pci enum')
102 u_boot_console.run_command('net list')
104 @pytest.mark.buildconfigspec('cmd_dhcp')
105 def test_net_dhcp(u_boot_console):
106 """Test the dhcp command.
108 The boardenv_* file may be used to enable/disable this test; see the
109 comment at the beginning of this file.
112 test_dhcp = u_boot_console.config.env.get('env__net_dhcp_server', False)
114 pytest.skip('No DHCP server available')
116 u_boot_console.run_command('setenv autoload no')
117 output = u_boot_console.run_command('dhcp')
118 assert 'DHCP client bound to address ' in output
123 @pytest.mark.buildconfigspec('cmd_dhcp6')
124 def test_net_dhcp6(u_boot_console):
125 """Test the dhcp6 command.
127 The boardenv_* file may be used to enable/disable this test; see the
128 comment at the beginning of this file.
131 test_dhcp6 = u_boot_console.config.env.get('env__net_dhcp6_server', False)
133 pytest.skip('No DHCP6 server available')
135 u_boot_console.run_command('setenv autoload no')
136 output = u_boot_console.run_command('dhcp6')
137 assert 'DHCP6 client bound to ' in output
142 @pytest.mark.buildconfigspec('net')
143 def test_net_setup_static(u_boot_console):
144 """Set up a static IP configuration.
146 The configuration is provided by the boardenv_* file; see the comment at
147 the beginning of this file.
150 env_vars = u_boot_console.config.env.get('env__net_static_env_vars', None)
152 pytest.skip('No static network configuration is defined')
154 for (var, val) in env_vars:
155 u_boot_console.run_command('setenv %s %s' % (var, val))
160 @pytest.mark.buildconfigspec('cmd_ping')
161 def test_net_ping(u_boot_console):
162 """Test the ping command.
164 The $serverip (as set up by either test_net_dhcp or test_net_setup_static)
165 is pinged. The test validates that the host is alive, as reported by the
166 ping command's output.
170 pytest.skip('Network not initialized')
172 output = u_boot_console.run_command('ping $serverip')
173 assert 'is alive' in output
175 @pytest.mark.buildconfigspec('IPV6_ROUTER_DISCOVERY')
176 def test_net_network_discovery(u_boot_console):
177 """Test the network discovery feature of IPv6.
179 An IPv6 network command (ping6 in this case) is run to make U-Boot send a
180 router solicitation packet, receive a router advertisement message, and
182 A router advertisement service needs to be running for this test to succeed.
183 U-Boot receives the RA, processes it, and if successful, assigns the gateway
184 IP and prefix length.
185 The configuration is provided by the boardenv_* file; see the comment at
186 the beginning of this file.
189 router_on_net = u_boot_console.config.env.get('env__router_on_net', False)
190 if not router_on_net:
191 pytest.skip('No router on network')
193 fake_host_ip = 'fe80::215:5dff:fef6:2ec6'
194 output = u_boot_console.run_command('ping6 ' + fake_host_ip)
195 assert 'ROUTER SOLICITATION 1' in output
196 assert 'Set gatewayip6:' in output
197 assert '0000:0000:0000:0000:0000:0000:0000:0000' not in output
199 @pytest.mark.buildconfigspec('cmd_net')
200 def test_net_tftpboot(u_boot_console):
201 """Test the tftpboot command.
203 A file is downloaded from the TFTP server, its size and optionally its
206 The details of the file to download are provided by the boardenv_* file;
207 see the comment at the beginning of this file.
211 pytest.skip('Network not initialized')
213 f = u_boot_console.config.env.get('env__net_tftp_readable_file', None)
215 pytest.skip('No TFTP readable file to read')
217 addr = f.get('addr', None)
221 output = u_boot_console.run_command('tftpboot %s' % (fn))
223 output = u_boot_console.run_command('tftpboot %x %s' % (addr, fn))
224 expected_text = 'Bytes transferred = '
225 sz = f.get('size', None)
227 expected_text += '%d' % sz
228 assert expected_text in output
230 expected_crc = f.get('crc32', None)
234 if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
237 output = u_boot_console.run_command('crc32 $fileaddr $filesize')
238 assert expected_crc in output
240 @pytest.mark.buildconfigspec('cmd_nfs')
241 def test_net_nfs(u_boot_console):
242 """Test the nfs command.
244 A file is downloaded from the NFS server, its size and optionally its
247 The details of the file to download are provided by the boardenv_* file;
248 see the comment at the beginning of this file.
252 pytest.skip('Network not initialized')
254 f = u_boot_console.config.env.get('env__net_nfs_readable_file', None)
256 pytest.skip('No NFS readable file to read')
258 addr = f.get('addr', None)
260 addr = u_boot_utils.find_ram_base(u_boot_console)
263 output = u_boot_console.run_command('nfs %x %s' % (addr, fn))
264 expected_text = 'Bytes transferred = '
265 sz = f.get('size', None)
267 expected_text += '%d' % sz
268 assert expected_text in output
270 expected_crc = f.get('crc32', None)
274 if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
277 output = u_boot_console.run_command('crc32 %x $filesize' % addr)
278 assert expected_crc in output
280 @pytest.mark.buildconfigspec("cmd_net")
281 @pytest.mark.buildconfigspec("cmd_pxe")
282 def test_net_pxe_get(u_boot_console):
283 """Test the pxe get command.
285 A pxe configuration file is downloaded from the TFTP server and interpreted
286 to boot the images mentioned in pxe configuration file.
288 The details of the file to download are provided by the boardenv_* file;
289 see the comment at the beginning of this file.
293 pytest.skip("Network not initialized")
295 test_net_setup_static(u_boot_console)
297 f = u_boot_console.config.env.get("env__net_pxe_readable_file", None)
299 pytest.skip("No PXE readable file to read")
301 addr = f.get("addr", None)
302 timeout = f.get("timeout", u_boot_console.p.timeout)
304 pxeuuid = uuid.uuid1()
305 u_boot_console.run_command(f"setenv pxeuuid {pxeuuid}")
306 expected_text_uuid = f"Retrieving file: pxelinux.cfg/{pxeuuid}"
308 ethaddr = u_boot_console.run_command("echo $ethaddr")
309 ethaddr = ethaddr.replace(':', '-')
310 expected_text_ethaddr = f"Retrieving file: pxelinux.cfg/01-{ethaddr}"
312 ip = u_boot_console.run_command("echo $ipaddr")
314 ipaddr_file = "".join(['%02x' % int(x) for x in ip]).upper()
315 expected_text_ipaddr = f"Retrieving file: pxelinux.cfg/{ipaddr_file}"
316 expected_text_default = f"Retrieving file: pxelinux.cfg/default"
318 with u_boot_console.temporary_timeout(timeout):
319 output = u_boot_console.run_command("pxe get")
321 assert "TIMEOUT" not in output
322 assert expected_text_uuid in output
323 assert expected_text_ethaddr in output
324 assert expected_text_ipaddr in output
327 for i in range(0, len(ipaddr_file) - 1):
328 expected_text_ip = f"Retrieving file: pxelinux.cfg/{ipaddr_file[:-i]}"
329 assert expected_text_ip in output
332 assert expected_text_default in output
333 assert "Config file 'default.boot' found" in output
335 @pytest.mark.buildconfigspec("cmd_crc32")
336 @pytest.mark.buildconfigspec("cmd_net")
337 @pytest.mark.buildconfigspec("cmd_tftpput")
338 def test_net_tftpput(u_boot_console):
339 """Test the tftpput command.
341 A file is downloaded from the TFTP server and then uploaded to the TFTP
342 server, its size and its CRC32 are validated.
344 The details of the file to download are provided by the boardenv_* file;
345 see the comment at the beginning of this file.
349 pytest.skip("Network not initialized")
351 f = u_boot_console.config.env.get("env__net_tftp_readable_file", None)
353 pytest.skip("No TFTP readable file to read")
355 addr = f.get("addr", None)
357 addr = u_boot_utils.find_ram_base(u_boot_console)
359 sz = f.get("size", None)
360 timeout = f.get("timeout", u_boot_console.p.timeout)
362 fnu = f.get("fnu", "_".join([datetime.datetime.now().strftime("%y%m%d%H%M%S"), fn]))
363 expected_text = "Bytes transferred = "
365 expected_text += "%d" % sz
367 with u_boot_console.temporary_timeout(timeout):
368 output = u_boot_console.run_command("tftpboot %x %s" % (addr, fn))
370 assert "TIMEOUT" not in output
371 assert expected_text in output
373 expected_tftpb_crc = f.get("crc32", None)
375 output = u_boot_console.run_command("crc32 $fileaddr $filesize")
376 assert expected_tftpb_crc in output
378 with u_boot_console.temporary_timeout(timeout):
379 output = u_boot_console.run_command(
380 "tftpput $fileaddr $filesize $serverip:%s" % (fnu)
383 expected_text = "Bytes transferred = "
385 expected_text += "%d" % sz
387 assert "TIMEOUT" not in output
388 assert "Access violation" not in output
389 assert expected_text in output
391 with u_boot_console.temporary_timeout(timeout):
392 output = u_boot_console.run_command("tftpboot %x %s" % (addr, fnu))
394 expected_text = "Bytes transferred = "
396 expected_text += "%d" % sz
397 assert "TIMEOUT" not in output
398 assert expected_text in output
400 output = u_boot_console.run_command("crc32 $fileaddr $filesize")
401 assert expected_tftpb_crc in output