blob: cd5b791a6aac25524fcb43ffb584c481f32a623f [file] [log] [blame]
Stephen Warrene5bb2792016-01-21 16:05:31 -07001# SPDX-License-Identifier: GPL-2.0
Tom Rini83d290c2018-05-06 17:58:06 -04002# Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
Stephen Warrene5bb2792016-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 Kumar7a82bff2023-10-03 18:42:46 +05309import uuid
Stephen Warrene5bb2792016-01-21 16:05:31 -070010
Stephen Warrene8debf32016-01-26 13:41:30 -070011"""
Stephen Warrene5bb2792016-01-21 16:05:31 -070012Note: This test relies on boardenv_* containing configuration values to define
Ehsan Mohandesieafbe162023-04-21 17:08:22 -070013which network environment is available for testing. Without this, this test
Stephen Warrene5bb2792016-01-21 16:05:31 -070014will be automatically skipped.
15
16For example:
17
Stephen Warren56382a82016-01-26 11:10:14 -070018# Boolean indicating whether the Ethernet device is attached to USB, and hence
19# USB enumeration needs to be performed prior to network tests.
20# This variable may be omitted if its value is False.
21env__net_uses_usb = False
22
23# Boolean indicating whether the Ethernet device is attached to PCI, and hence
24# PCI enumeration needs to be performed prior to network tests.
25# This variable may be omitted if its value is False.
26env__net_uses_pci = True
Stephen Warrene5bb2792016-01-21 16:05:31 -070027
28# True if a DHCP server is attached to the network, and should be tested.
29# If DHCP testing is not possible or desired, this variable may be omitted or
30# set to False.
31env__net_dhcp_server = True
32
Sean Edmond29fb68c2023-04-11 10:48:48 -070033# True if a DHCPv6 server is attached to the network, and should be tested.
34# If DHCPv6 testing is not possible or desired, this variable may be omitted or
35# set to False.
36env__net_dhcp6_server = True
37
Stephen Warrene5bb2792016-01-21 16:05:31 -070038# A list of environment variables that should be set in order to configure a
39# static IP. If solely relying on DHCP, this variable may be omitted or set to
40# an empty list.
41env__net_static_env_vars = [
Simon Glass871bf7d2018-12-27 08:11:13 -070042 ('ipaddr', '10.0.0.100'),
43 ('netmask', '255.255.255.0'),
44 ('serverip', '10.0.0.1'),
Stephen Warrene5bb2792016-01-21 16:05:31 -070045]
46
47# Details regarding a file that may be read from a TFTP server. This variable
48# may be omitted or set to None if TFTP testing is not possible or desired.
49env__net_tftp_readable_file = {
Simon Glass871bf7d2018-12-27 08:11:13 -070050 'fn': 'ubtest-readable.bin',
51 'addr': 0x10000000,
52 'size': 5058624,
53 'crc32': 'c2244b26',
Stephen Warrene5bb2792016-01-21 16:05:31 -070054}
Guillaume GARDET6a2981a2016-09-14 10:29:12 +020055
56# Details regarding a file that may be read from a NFS server. This variable
57# may be omitted or set to None if NFS testing is not possible or desired.
58env__net_nfs_readable_file = {
Simon Glass871bf7d2018-12-27 08:11:13 -070059 'fn': 'ubtest-readable.bin',
60 'addr': 0x10000000,
61 'size': 5058624,
62 'crc32': 'c2244b26',
Guillaume GARDET6a2981a2016-09-14 10:29:12 +020063}
Ehsan Mohandesieafbe162023-04-21 17:08:22 -070064
Love Kumar7a82bff2023-10-03 18:42:46 +053065# Details regarding a file that may be read from a TFTP server. This variable
66# may be omitted or set to None if PXE testing is not possible or desired.
67env__net_pxe_readable_file = {
68 'fn': 'default',
69 'addr': 0x2000000,
70 'size': 74,
71 'timeout': 50000,
72 'pattern': 'Linux',
73}
74
Ehsan Mohandesieafbe162023-04-21 17:08:22 -070075# True if a router advertisement service is connected to the network, and should
76# be tested. If router advertisement testing is not possible or desired, this
77variable may be omitted or set to False.
78env__router_on_net = True
Stephen Warrene8debf32016-01-26 13:41:30 -070079"""
Stephen Warrene5bb2792016-01-21 16:05:31 -070080
81net_set_up = False
Sean Edmond29fb68c2023-04-11 10:48:48 -070082net6_set_up = False
Stephen Warrene5bb2792016-01-21 16:05:31 -070083
84def test_net_pre_commands(u_boot_console):
Stephen Warrene8debf32016-01-26 13:41:30 -070085 """Execute any commands required to enable network hardware.
Stephen Warrene5bb2792016-01-21 16:05:31 -070086
87 These commands are provided by the boardenv_* file; see the comment at the
88 beginning of this file.
Stephen Warrene8debf32016-01-26 13:41:30 -070089 """
Stephen Warrene5bb2792016-01-21 16:05:31 -070090
Stephen Warren56382a82016-01-26 11:10:14 -070091 init_usb = u_boot_console.config.env.get('env__net_uses_usb', False)
92 if init_usb:
93 u_boot_console.run_command('usb start')
Stephen Warrene5bb2792016-01-21 16:05:31 -070094
Stephen Warren56382a82016-01-26 11:10:14 -070095 init_pci = u_boot_console.config.env.get('env__net_uses_pci', False)
96 if init_pci:
97 u_boot_console.run_command('pci enum')
Stephen Warrene5bb2792016-01-21 16:05:31 -070098
Maxim Uvarove6163462023-12-26 21:46:12 +060099 u_boot_console.run_command('net list')
100
Stephen Warrene5bb2792016-01-21 16:05:31 -0700101@pytest.mark.buildconfigspec('cmd_dhcp')
102def test_net_dhcp(u_boot_console):
Stephen Warrene8debf32016-01-26 13:41:30 -0700103 """Test the dhcp command.
Stephen Warrene5bb2792016-01-21 16:05:31 -0700104
105 The boardenv_* file may be used to enable/disable this test; see the
106 comment at the beginning of this file.
Stephen Warrene8debf32016-01-26 13:41:30 -0700107 """
Stephen Warrene5bb2792016-01-21 16:05:31 -0700108
109 test_dhcp = u_boot_console.config.env.get('env__net_dhcp_server', False)
110 if not test_dhcp:
111 pytest.skip('No DHCP server available')
112
113 u_boot_console.run_command('setenv autoload no')
114 output = u_boot_console.run_command('dhcp')
115 assert 'DHCP client bound to address ' in output
116
117 global net_set_up
118 net_set_up = True
119
Sean Edmond29fb68c2023-04-11 10:48:48 -0700120@pytest.mark.buildconfigspec('cmd_dhcp6')
121def test_net_dhcp6(u_boot_console):
122 """Test the dhcp6 command.
123
124 The boardenv_* file may be used to enable/disable this test; see the
125 comment at the beginning of this file.
126 """
127
128 test_dhcp6 = u_boot_console.config.env.get('env__net_dhcp6_server', False)
129 if not test_dhcp6:
130 pytest.skip('No DHCP6 server available')
131
132 u_boot_console.run_command('setenv autoload no')
133 output = u_boot_console.run_command('dhcp6')
134 assert 'DHCP6 client bound to ' in output
135
136 global net6_set_up
137 net6_set_up = True
138
Stephen Warrene5bb2792016-01-21 16:05:31 -0700139@pytest.mark.buildconfigspec('net')
140def test_net_setup_static(u_boot_console):
Stephen Warrene8debf32016-01-26 13:41:30 -0700141 """Set up a static IP configuration.
Stephen Warrene5bb2792016-01-21 16:05:31 -0700142
143 The configuration is provided by the boardenv_* file; see the comment at
144 the beginning of this file.
Stephen Warrene8debf32016-01-26 13:41:30 -0700145 """
Stephen Warrene5bb2792016-01-21 16:05:31 -0700146
147 env_vars = u_boot_console.config.env.get('env__net_static_env_vars', None)
148 if not env_vars:
149 pytest.skip('No static network configuration is defined')
150
151 for (var, val) in env_vars:
152 u_boot_console.run_command('setenv %s %s' % (var, val))
153
154 global net_set_up
155 net_set_up = True
156
157@pytest.mark.buildconfigspec('cmd_ping')
158def test_net_ping(u_boot_console):
Stephen Warrene8debf32016-01-26 13:41:30 -0700159 """Test the ping command.
Stephen Warrene5bb2792016-01-21 16:05:31 -0700160
161 The $serverip (as set up by either test_net_dhcp or test_net_setup_static)
162 is pinged. The test validates that the host is alive, as reported by the
163 ping command's output.
Stephen Warrene8debf32016-01-26 13:41:30 -0700164 """
Stephen Warrene5bb2792016-01-21 16:05:31 -0700165
166 if not net_set_up:
Stephen Warrena2ec5602016-01-26 13:41:31 -0700167 pytest.skip('Network not initialized')
Stephen Warrene5bb2792016-01-21 16:05:31 -0700168
169 output = u_boot_console.run_command('ping $serverip')
170 assert 'is alive' in output
171
Ehsan Mohandesieafbe162023-04-21 17:08:22 -0700172@pytest.mark.buildconfigspec('IPV6_ROUTER_DISCOVERY')
173def test_net_network_discovery(u_boot_console):
174 """Test the network discovery feature of IPv6.
175
176 An IPv6 network command (ping6 in this case) is run to make U-Boot send a
177 router solicitation packet, receive a router advertisement message, and
178 parse it.
179 A router advertisement service needs to be running for this test to succeed.
180 U-Boot receives the RA, processes it, and if successful, assigns the gateway
181 IP and prefix length.
182 The configuration is provided by the boardenv_* file; see the comment at
183 the beginning of this file.
184 """
185
186 router_on_net = u_boot_console.config.env.get('env__router_on_net', False)
187 if not router_on_net:
188 pytest.skip('No router on network')
189
190 fake_host_ip = 'fe80::215:5dff:fef6:2ec6'
191 output = u_boot_console.run_command('ping6 ' + fake_host_ip)
192 assert 'ROUTER SOLICITATION 1' in output
193 assert 'Set gatewayip6:' in output
194 assert '0000:0000:0000:0000:0000:0000:0000:0000' not in output
195
Stephen Warrene5bb2792016-01-21 16:05:31 -0700196@pytest.mark.buildconfigspec('cmd_net')
197def test_net_tftpboot(u_boot_console):
Stephen Warrene8debf32016-01-26 13:41:30 -0700198 """Test the tftpboot command.
Stephen Warrene5bb2792016-01-21 16:05:31 -0700199
200 A file is downloaded from the TFTP server, its size and optionally its
201 CRC32 are validated.
202
203 The details of the file to download are provided by the boardenv_* file;
204 see the comment at the beginning of this file.
Stephen Warrene8debf32016-01-26 13:41:30 -0700205 """
Stephen Warrene5bb2792016-01-21 16:05:31 -0700206
207 if not net_set_up:
Stephen Warrena2ec5602016-01-26 13:41:31 -0700208 pytest.skip('Network not initialized')
Stephen Warrene5bb2792016-01-21 16:05:31 -0700209
210 f = u_boot_console.config.env.get('env__net_tftp_readable_file', None)
211 if not f:
212 pytest.skip('No TFTP readable file to read')
213
Michal Simek3ba13522016-04-04 20:06:14 +0200214 addr = f.get('addr', None)
Michal Simek3ba13522016-04-04 20:06:14 +0200215
Stephen Warrene5bb2792016-01-21 16:05:31 -0700216 fn = f['fn']
Heinrich Schuchardtb1b1bab2019-01-26 15:25:12 +0100217 if not addr:
218 output = u_boot_console.run_command('tftpboot %s' % (fn))
219 else:
220 output = u_boot_console.run_command('tftpboot %x %s' % (addr, fn))
Stephen Warrene5bb2792016-01-21 16:05:31 -0700221 expected_text = 'Bytes transferred = '
222 sz = f.get('size', None)
223 if sz:
224 expected_text += '%d' % sz
225 assert expected_text in output
226
227 expected_crc = f.get('crc32', None)
228 if not expected_crc:
229 return
230
231 if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
232 return
233
Heinrich Schuchardtb1b1bab2019-01-26 15:25:12 +0100234 output = u_boot_console.run_command('crc32 $fileaddr $filesize')
Stephen Warrene5bb2792016-01-21 16:05:31 -0700235 assert expected_crc in output
Guillaume GARDET6a2981a2016-09-14 10:29:12 +0200236
237@pytest.mark.buildconfigspec('cmd_nfs')
238def test_net_nfs(u_boot_console):
239 """Test the nfs command.
240
241 A file is downloaded from the NFS server, its size and optionally its
242 CRC32 are validated.
243
244 The details of the file to download are provided by the boardenv_* file;
245 see the comment at the beginning of this file.
246 """
247
248 if not net_set_up:
249 pytest.skip('Network not initialized')
250
251 f = u_boot_console.config.env.get('env__net_nfs_readable_file', None)
252 if not f:
253 pytest.skip('No NFS readable file to read')
254
255 addr = f.get('addr', None)
256 if not addr:
Quentin Schulzf4eef402018-07-09 19:16:27 +0200257 addr = u_boot_utils.find_ram_base(u_boot_console)
Guillaume GARDET6a2981a2016-09-14 10:29:12 +0200258
259 fn = f['fn']
260 output = u_boot_console.run_command('nfs %x %s' % (addr, fn))
261 expected_text = 'Bytes transferred = '
262 sz = f.get('size', None)
263 if sz:
264 expected_text += '%d' % sz
265 assert expected_text in output
266
267 expected_crc = f.get('crc32', None)
268 if not expected_crc:
269 return
270
271 if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
272 return
273
274 output = u_boot_console.run_command('crc32 %x $filesize' % addr)
275 assert expected_crc in output
Love Kumar7a82bff2023-10-03 18:42:46 +0530276
277@pytest.mark.buildconfigspec("cmd_net")
278@pytest.mark.buildconfigspec("cmd_pxe")
279def test_net_pxe_get(u_boot_console):
280 """Test the pxe get command.
281
282 A pxe configuration file is downloaded from the TFTP server and interpreted
283 to boot the images mentioned in pxe configuration file.
284
285 The details of the file to download are provided by the boardenv_* file;
286 see the comment at the beginning of this file.
287 """
288
289 if not net_set_up:
290 pytest.skip("Network not initialized")
291
292 test_net_setup_static(u_boot_console)
293
294 f = u_boot_console.config.env.get("env__net_pxe_readable_file", None)
295 if not f:
296 pytest.skip("No PXE readable file to read")
297
298 addr = f.get("addr", None)
299 timeout = f.get("timeout", u_boot_console.p.timeout)
300
301 pxeuuid = uuid.uuid1()
302 u_boot_console.run_command(f"setenv pxeuuid {pxeuuid}")
303 expected_text_uuid = f"Retrieving file: pxelinux.cfg/{pxeuuid}"
304
305 ethaddr = u_boot_console.run_command("echo $ethaddr")
306 ethaddr = ethaddr.replace(':', '-')
307 expected_text_ethaddr = f"Retrieving file: pxelinux.cfg/01-{ethaddr}"
308
309 ip = u_boot_console.run_command("echo $ipaddr")
310 ip = ip.split('.')
311 ipaddr_file = "".join(['%02x' % int(x) for x in ip]).upper()
312 expected_text_ipaddr = f"Retrieving file: pxelinux.cfg/{ipaddr_file}"
313 expected_text_default = f"Retrieving file: pxelinux.cfg/default"
314
315 with u_boot_console.temporary_timeout(timeout):
316 output = u_boot_console.run_command("pxe get")
317
318 assert "TIMEOUT" not in output
319 assert expected_text_uuid in output
320 assert expected_text_ethaddr in output
321 assert expected_text_ipaddr in output
322
323 i = 1
324 for i in range(0, len(ipaddr_file) - 1):
325 expected_text_ip = f"Retrieving file: pxelinux.cfg/{ipaddr_file[:-i]}"
326 assert expected_text_ip in output
327 i += 1
328
329 assert expected_text_default in output
330 assert "Config file 'default.boot' found" in output