blob: 2495608786dea33b6cae583f67045687dbe04f86 [file] [log] [blame]
Stephen Warren1a218552016-01-21 16:05:31 -07001# SPDX-License-Identifier: GPL-2.0
Tom Rini10e47792018-05-06 17:58:06 -04002# Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
Stephen Warren1a218552016-01-21 16:05:31 -07003
4# Test various network-related functionality, such as the dhcp, ping, and
5# tftpboot commands.
6
7import pytest
8import u_boot_utils
Love Kumar55ef4532023-10-03 18:42:46 +05309import uuid
Love Kumar49f87492023-11-08 12:40:31 +053010import datetime
Stephen Warren1a218552016-01-21 16:05:31 -070011
Stephen Warren75e731e2016-01-26 13:41:30 -070012"""
Stephen Warren1a218552016-01-21 16:05:31 -070013Note: This test relies on boardenv_* containing configuration values to define
Ehsan Mohandesi3eb50532023-04-21 17:08:22 -070014which network environment is available for testing. Without this, this test
Stephen Warren1a218552016-01-21 16:05:31 -070015will be automatically skipped.
16
17For example:
18
Stephen Warren8d57b922016-01-26 11:10:14 -070019# 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.
22env__net_uses_usb = False
23
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.
27env__net_uses_pci = True
Stephen Warren1a218552016-01-21 16:05:31 -070028
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
31# set to False.
32env__net_dhcp_server = True
33
Sean Edmondd407acf2023-04-11 10:48:48 -070034# 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
36# set to False.
37env__net_dhcp6_server = True
38
Stephen Warren1a218552016-01-21 16:05:31 -070039# 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
41# an empty list.
42env__net_static_env_vars = [
Simon Glasse9f4d872018-12-27 08:11:13 -070043 ('ipaddr', '10.0.0.100'),
44 ('netmask', '255.255.255.0'),
45 ('serverip', '10.0.0.1'),
Stephen Warren1a218552016-01-21 16:05:31 -070046]
47
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.
50env__net_tftp_readable_file = {
Simon Glasse9f4d872018-12-27 08:11:13 -070051 'fn': 'ubtest-readable.bin',
52 'addr': 0x10000000,
53 'size': 5058624,
54 'crc32': 'c2244b26',
Love Kumar49f87492023-11-08 12:40:31 +053055 'timeout': 50000,
56 'fnu': 'ubtest-upload.bin',
Stephen Warren1a218552016-01-21 16:05:31 -070057}
Guillaume GARDET1b0102d2016-09-14 10:29:12 +020058
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.
61env__net_nfs_readable_file = {
Simon Glasse9f4d872018-12-27 08:11:13 -070062 'fn': 'ubtest-readable.bin',
63 'addr': 0x10000000,
64 'size': 5058624,
65 'crc32': 'c2244b26',
Guillaume GARDET1b0102d2016-09-14 10:29:12 +020066}
Ehsan Mohandesi3eb50532023-04-21 17:08:22 -070067
Love Kumar55ef4532023-10-03 18:42:46 +053068# 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.
70env__net_pxe_readable_file = {
71 'fn': 'default',
72 'addr': 0x2000000,
73 'size': 74,
74 'timeout': 50000,
75 'pattern': 'Linux',
76}
77
Ehsan Mohandesi3eb50532023-04-21 17:08:22 -070078# 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
80variable may be omitted or set to False.
81env__router_on_net = True
Stephen Warren75e731e2016-01-26 13:41:30 -070082"""
Stephen Warren1a218552016-01-21 16:05:31 -070083
84net_set_up = False
Sean Edmondd407acf2023-04-11 10:48:48 -070085net6_set_up = False
Stephen Warren1a218552016-01-21 16:05:31 -070086
87def test_net_pre_commands(u_boot_console):
Stephen Warren75e731e2016-01-26 13:41:30 -070088 """Execute any commands required to enable network hardware.
Stephen Warren1a218552016-01-21 16:05:31 -070089
90 These commands are provided by the boardenv_* file; see the comment at the
91 beginning of this file.
Stephen Warren75e731e2016-01-26 13:41:30 -070092 """
Stephen Warren1a218552016-01-21 16:05:31 -070093
Stephen Warren8d57b922016-01-26 11:10:14 -070094 init_usb = u_boot_console.config.env.get('env__net_uses_usb', False)
95 if init_usb:
96 u_boot_console.run_command('usb start')
Stephen Warren1a218552016-01-21 16:05:31 -070097
Stephen Warren8d57b922016-01-26 11:10:14 -070098 init_pci = u_boot_console.config.env.get('env__net_uses_pci', False)
99 if init_pci:
100 u_boot_console.run_command('pci enum')
Stephen Warren1a218552016-01-21 16:05:31 -0700101
102@pytest.mark.buildconfigspec('cmd_dhcp')
103def test_net_dhcp(u_boot_console):
Stephen Warren75e731e2016-01-26 13:41:30 -0700104 """Test the dhcp command.
Stephen Warren1a218552016-01-21 16:05:31 -0700105
106 The boardenv_* file may be used to enable/disable this test; see the
107 comment at the beginning of this file.
Stephen Warren75e731e2016-01-26 13:41:30 -0700108 """
Stephen Warren1a218552016-01-21 16:05:31 -0700109
110 test_dhcp = u_boot_console.config.env.get('env__net_dhcp_server', False)
111 if not test_dhcp:
112 pytest.skip('No DHCP server available')
113
114 u_boot_console.run_command('setenv autoload no')
115 output = u_boot_console.run_command('dhcp')
116 assert 'DHCP client bound to address ' in output
117
118 global net_set_up
119 net_set_up = True
120
Sean Edmondd407acf2023-04-11 10:48:48 -0700121@pytest.mark.buildconfigspec('cmd_dhcp6')
122def test_net_dhcp6(u_boot_console):
123 """Test the dhcp6 command.
124
125 The boardenv_* file may be used to enable/disable this test; see the
126 comment at the beginning of this file.
127 """
128
129 test_dhcp6 = u_boot_console.config.env.get('env__net_dhcp6_server', False)
130 if not test_dhcp6:
131 pytest.skip('No DHCP6 server available')
132
133 u_boot_console.run_command('setenv autoload no')
134 output = u_boot_console.run_command('dhcp6')
135 assert 'DHCP6 client bound to ' in output
136
137 global net6_set_up
138 net6_set_up = True
139
Stephen Warren1a218552016-01-21 16:05:31 -0700140@pytest.mark.buildconfigspec('net')
141def test_net_setup_static(u_boot_console):
Stephen Warren75e731e2016-01-26 13:41:30 -0700142 """Set up a static IP configuration.
Stephen Warren1a218552016-01-21 16:05:31 -0700143
144 The configuration is provided by the boardenv_* file; see the comment at
145 the beginning of this file.
Stephen Warren75e731e2016-01-26 13:41:30 -0700146 """
Stephen Warren1a218552016-01-21 16:05:31 -0700147
148 env_vars = u_boot_console.config.env.get('env__net_static_env_vars', None)
149 if not env_vars:
150 pytest.skip('No static network configuration is defined')
151
152 for (var, val) in env_vars:
153 u_boot_console.run_command('setenv %s %s' % (var, val))
154
155 global net_set_up
156 net_set_up = True
157
158@pytest.mark.buildconfigspec('cmd_ping')
159def test_net_ping(u_boot_console):
Stephen Warren75e731e2016-01-26 13:41:30 -0700160 """Test the ping command.
Stephen Warren1a218552016-01-21 16:05:31 -0700161
162 The $serverip (as set up by either test_net_dhcp or test_net_setup_static)
163 is pinged. The test validates that the host is alive, as reported by the
164 ping command's output.
Stephen Warren75e731e2016-01-26 13:41:30 -0700165 """
Stephen Warren1a218552016-01-21 16:05:31 -0700166
167 if not net_set_up:
Stephen Warren3deb8962016-01-26 13:41:31 -0700168 pytest.skip('Network not initialized')
Stephen Warren1a218552016-01-21 16:05:31 -0700169
170 output = u_boot_console.run_command('ping $serverip')
171 assert 'is alive' in output
172
Ehsan Mohandesi3eb50532023-04-21 17:08:22 -0700173@pytest.mark.buildconfigspec('IPV6_ROUTER_DISCOVERY')
174def test_net_network_discovery(u_boot_console):
175 """Test the network discovery feature of IPv6.
176
177 An IPv6 network command (ping6 in this case) is run to make U-Boot send a
178 router solicitation packet, receive a router advertisement message, and
179 parse it.
180 A router advertisement service needs to be running for this test to succeed.
181 U-Boot receives the RA, processes it, and if successful, assigns the gateway
182 IP and prefix length.
183 The configuration is provided by the boardenv_* file; see the comment at
184 the beginning of this file.
185 """
186
187 router_on_net = u_boot_console.config.env.get('env__router_on_net', False)
188 if not router_on_net:
189 pytest.skip('No router on network')
190
191 fake_host_ip = 'fe80::215:5dff:fef6:2ec6'
192 output = u_boot_console.run_command('ping6 ' + fake_host_ip)
193 assert 'ROUTER SOLICITATION 1' in output
194 assert 'Set gatewayip6:' in output
195 assert '0000:0000:0000:0000:0000:0000:0000:0000' not in output
196
Stephen Warren1a218552016-01-21 16:05:31 -0700197@pytest.mark.buildconfigspec('cmd_net')
198def test_net_tftpboot(u_boot_console):
Stephen Warren75e731e2016-01-26 13:41:30 -0700199 """Test the tftpboot command.
Stephen Warren1a218552016-01-21 16:05:31 -0700200
201 A file is downloaded from the TFTP server, its size and optionally its
202 CRC32 are validated.
203
204 The details of the file to download are provided by the boardenv_* file;
205 see the comment at the beginning of this file.
Stephen Warren75e731e2016-01-26 13:41:30 -0700206 """
Stephen Warren1a218552016-01-21 16:05:31 -0700207
208 if not net_set_up:
Stephen Warren3deb8962016-01-26 13:41:31 -0700209 pytest.skip('Network not initialized')
Stephen Warren1a218552016-01-21 16:05:31 -0700210
211 f = u_boot_console.config.env.get('env__net_tftp_readable_file', None)
212 if not f:
213 pytest.skip('No TFTP readable file to read')
214
Michal Simek6596e032016-04-04 20:06:14 +0200215 addr = f.get('addr', None)
Michal Simek6596e032016-04-04 20:06:14 +0200216
Stephen Warren1a218552016-01-21 16:05:31 -0700217 fn = f['fn']
Heinrich Schuchardt23287c82019-01-26 15:25:12 +0100218 if not addr:
219 output = u_boot_console.run_command('tftpboot %s' % (fn))
220 else:
221 output = u_boot_console.run_command('tftpboot %x %s' % (addr, fn))
Stephen Warren1a218552016-01-21 16:05:31 -0700222 expected_text = 'Bytes transferred = '
223 sz = f.get('size', None)
224 if sz:
225 expected_text += '%d' % sz
226 assert expected_text in output
227
228 expected_crc = f.get('crc32', None)
229 if not expected_crc:
230 return
231
232 if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
233 return
234
Heinrich Schuchardt23287c82019-01-26 15:25:12 +0100235 output = u_boot_console.run_command('crc32 $fileaddr $filesize')
Stephen Warren1a218552016-01-21 16:05:31 -0700236 assert expected_crc in output
Guillaume GARDET1b0102d2016-09-14 10:29:12 +0200237
238@pytest.mark.buildconfigspec('cmd_nfs')
239def test_net_nfs(u_boot_console):
240 """Test the nfs command.
241
242 A file is downloaded from the NFS server, its size and optionally its
243 CRC32 are validated.
244
245 The details of the file to download are provided by the boardenv_* file;
246 see the comment at the beginning of this file.
247 """
248
249 if not net_set_up:
250 pytest.skip('Network not initialized')
251
252 f = u_boot_console.config.env.get('env__net_nfs_readable_file', None)
253 if not f:
254 pytest.skip('No NFS readable file to read')
255
256 addr = f.get('addr', None)
257 if not addr:
Quentin Schulzdae1aa82018-07-09 19:16:27 +0200258 addr = u_boot_utils.find_ram_base(u_boot_console)
Guillaume GARDET1b0102d2016-09-14 10:29:12 +0200259
260 fn = f['fn']
261 output = u_boot_console.run_command('nfs %x %s' % (addr, fn))
262 expected_text = 'Bytes transferred = '
263 sz = f.get('size', None)
264 if sz:
265 expected_text += '%d' % sz
266 assert expected_text in output
267
268 expected_crc = f.get('crc32', None)
269 if not expected_crc:
270 return
271
272 if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
273 return
274
275 output = u_boot_console.run_command('crc32 %x $filesize' % addr)
276 assert expected_crc in output
Love Kumar55ef4532023-10-03 18:42:46 +0530277
278@pytest.mark.buildconfigspec("cmd_net")
279@pytest.mark.buildconfigspec("cmd_pxe")
280def test_net_pxe_get(u_boot_console):
281 """Test the pxe get command.
282
283 A pxe configuration file is downloaded from the TFTP server and interpreted
284 to boot the images mentioned in pxe configuration file.
285
286 The details of the file to download are provided by the boardenv_* file;
287 see the comment at the beginning of this file.
288 """
289
290 if not net_set_up:
291 pytest.skip("Network not initialized")
292
293 test_net_setup_static(u_boot_console)
294
295 f = u_boot_console.config.env.get("env__net_pxe_readable_file", None)
296 if not f:
297 pytest.skip("No PXE readable file to read")
298
299 addr = f.get("addr", None)
300 timeout = f.get("timeout", u_boot_console.p.timeout)
301
302 pxeuuid = uuid.uuid1()
303 u_boot_console.run_command(f"setenv pxeuuid {pxeuuid}")
304 expected_text_uuid = f"Retrieving file: pxelinux.cfg/{pxeuuid}"
305
306 ethaddr = u_boot_console.run_command("echo $ethaddr")
307 ethaddr = ethaddr.replace(':', '-')
308 expected_text_ethaddr = f"Retrieving file: pxelinux.cfg/01-{ethaddr}"
309
310 ip = u_boot_console.run_command("echo $ipaddr")
311 ip = ip.split('.')
312 ipaddr_file = "".join(['%02x' % int(x) for x in ip]).upper()
313 expected_text_ipaddr = f"Retrieving file: pxelinux.cfg/{ipaddr_file}"
314 expected_text_default = f"Retrieving file: pxelinux.cfg/default"
315
316 with u_boot_console.temporary_timeout(timeout):
317 output = u_boot_console.run_command("pxe get")
318
319 assert "TIMEOUT" not in output
320 assert expected_text_uuid in output
321 assert expected_text_ethaddr in output
322 assert expected_text_ipaddr in output
323
324 i = 1
325 for i in range(0, len(ipaddr_file) - 1):
326 expected_text_ip = f"Retrieving file: pxelinux.cfg/{ipaddr_file[:-i]}"
327 assert expected_text_ip in output
328 i += 1
329
330 assert expected_text_default in output
331 assert "Config file 'default.boot' found" in output
Love Kumar49f87492023-11-08 12:40:31 +0530332
333@pytest.mark.buildconfigspec("cmd_crc32")
334@pytest.mark.buildconfigspec("cmd_net")
335@pytest.mark.buildconfigspec("cmd_tftpput")
336def test_net_tftpput(u_boot_console):
337 """Test the tftpput command.
338
339 A file is downloaded from the TFTP server and then uploaded to the TFTP
340 server, its size and its CRC32 are validated.
341
342 The details of the file to download are provided by the boardenv_* file;
343 see the comment at the beginning of this file.
344 """
345
346 if not net_set_up:
347 pytest.skip("Network not initialized")
348
349 f = u_boot_console.config.env.get("env__net_tftp_readable_file", None)
350 if not f:
351 pytest.skip("No TFTP readable file to read")
352
353 addr = f.get("addr", None)
354 if not addr:
355 addr = u_boot_utils.find_ram_base(u_boot_console)
356
357 sz = f.get("size", None)
358 timeout = f.get("timeout", u_boot_console.p.timeout)
359 fn = f["fn"]
360 fnu = f.get("fnu", "_".join([datetime.datetime.now().strftime("%y%m%d%H%M%S"), fn]))
361 expected_text = "Bytes transferred = "
362 if sz:
363 expected_text += "%d" % sz
364
365 with u_boot_console.temporary_timeout(timeout):
366 output = u_boot_console.run_command("tftpboot %x %s" % (addr, fn))
367
368 assert "TIMEOUT" not in output
369 assert expected_text in output
370
371 expected_tftpb_crc = f.get("crc32", None)
372
373 output = u_boot_console.run_command("crc32 $fileaddr $filesize")
374 assert expected_tftpb_crc in output
375
376 with u_boot_console.temporary_timeout(timeout):
377 output = u_boot_console.run_command(
378 "tftpput $fileaddr $filesize $serverip:%s" % (fnu)
379 )
380
381 expected_text = "Bytes transferred = "
382 if sz:
383 expected_text += "%d" % sz
384 addr = addr + sz
385 assert "TIMEOUT" not in output
386 assert "Access violation" not in output
387 assert expected_text in output
388
389 with u_boot_console.temporary_timeout(timeout):
390 output = u_boot_console.run_command("tftpboot %x %s" % (addr, fnu))
391
392 expected_text = "Bytes transferred = "
393 if sz:
394 expected_text += "%d" % sz
395 assert "TIMEOUT" not in output
396 assert expected_text in output
397
398 output = u_boot_console.run_command("crc32 $fileaddr $filesize")
399 assert expected_tftpb_crc in output