blob: 0447c0b2e0e292c67f99fd7021032f4a544730e5 [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
9
Stephen Warren75e731e2016-01-26 13:41:30 -070010"""
Stephen Warren1a218552016-01-21 16:05:31 -070011Note: This test relies on boardenv_* containing configuration values to define
12which the network environment available for testing. Without this, this test
13will be automatically skipped.
14
15For example:
16
Stephen Warren8d57b922016-01-26 11:10:14 -070017# Boolean indicating whether the Ethernet device is attached to USB, and hence
18# USB enumeration needs to be performed prior to network tests.
19# This variable may be omitted if its value is False.
20env__net_uses_usb = False
21
22# Boolean indicating whether the Ethernet device is attached to PCI, and hence
23# PCI enumeration needs to be performed prior to network tests.
24# This variable may be omitted if its value is False.
25env__net_uses_pci = True
Stephen Warren1a218552016-01-21 16:05:31 -070026
27# True if a DHCP server is attached to the network, and should be tested.
28# If DHCP testing is not possible or desired, this variable may be omitted or
29# set to False.
30env__net_dhcp_server = True
31
Sean Edmondd407acf2023-04-11 10:48:48 -070032# True if a DHCPv6 server is attached to the network, and should be tested.
33# If DHCPv6 testing is not possible or desired, this variable may be omitted or
34# set to False.
35env__net_dhcp6_server = True
36
Stephen Warren1a218552016-01-21 16:05:31 -070037# A list of environment variables that should be set in order to configure a
38# static IP. If solely relying on DHCP, this variable may be omitted or set to
39# an empty list.
40env__net_static_env_vars = [
Simon Glasse9f4d872018-12-27 08:11:13 -070041 ('ipaddr', '10.0.0.100'),
42 ('netmask', '255.255.255.0'),
43 ('serverip', '10.0.0.1'),
Stephen Warren1a218552016-01-21 16:05:31 -070044]
45
46# Details regarding a file that may be read from a TFTP server. This variable
47# may be omitted or set to None if TFTP testing is not possible or desired.
48env__net_tftp_readable_file = {
Simon Glasse9f4d872018-12-27 08:11:13 -070049 'fn': 'ubtest-readable.bin',
50 'addr': 0x10000000,
51 'size': 5058624,
52 'crc32': 'c2244b26',
Stephen Warren1a218552016-01-21 16:05:31 -070053}
Guillaume GARDET1b0102d2016-09-14 10:29:12 +020054
55# Details regarding a file that may be read from a NFS server. This variable
56# may be omitted or set to None if NFS testing is not possible or desired.
57env__net_nfs_readable_file = {
Simon Glasse9f4d872018-12-27 08:11:13 -070058 'fn': 'ubtest-readable.bin',
59 'addr': 0x10000000,
60 'size': 5058624,
61 'crc32': 'c2244b26',
Guillaume GARDET1b0102d2016-09-14 10:29:12 +020062}
Stephen Warren75e731e2016-01-26 13:41:30 -070063"""
Stephen Warren1a218552016-01-21 16:05:31 -070064
65net_set_up = False
Sean Edmondd407acf2023-04-11 10:48:48 -070066net6_set_up = False
Stephen Warren1a218552016-01-21 16:05:31 -070067
68def test_net_pre_commands(u_boot_console):
Stephen Warren75e731e2016-01-26 13:41:30 -070069 """Execute any commands required to enable network hardware.
Stephen Warren1a218552016-01-21 16:05:31 -070070
71 These commands are provided by the boardenv_* file; see the comment at the
72 beginning of this file.
Stephen Warren75e731e2016-01-26 13:41:30 -070073 """
Stephen Warren1a218552016-01-21 16:05:31 -070074
Stephen Warren8d57b922016-01-26 11:10:14 -070075 init_usb = u_boot_console.config.env.get('env__net_uses_usb', False)
76 if init_usb:
77 u_boot_console.run_command('usb start')
Stephen Warren1a218552016-01-21 16:05:31 -070078
Stephen Warren8d57b922016-01-26 11:10:14 -070079 init_pci = u_boot_console.config.env.get('env__net_uses_pci', False)
80 if init_pci:
81 u_boot_console.run_command('pci enum')
Stephen Warren1a218552016-01-21 16:05:31 -070082
83@pytest.mark.buildconfigspec('cmd_dhcp')
84def test_net_dhcp(u_boot_console):
Stephen Warren75e731e2016-01-26 13:41:30 -070085 """Test the dhcp command.
Stephen Warren1a218552016-01-21 16:05:31 -070086
87 The boardenv_* file may be used to enable/disable this test; see the
88 comment at the beginning of this file.
Stephen Warren75e731e2016-01-26 13:41:30 -070089 """
Stephen Warren1a218552016-01-21 16:05:31 -070090
91 test_dhcp = u_boot_console.config.env.get('env__net_dhcp_server', False)
92 if not test_dhcp:
93 pytest.skip('No DHCP server available')
94
95 u_boot_console.run_command('setenv autoload no')
96 output = u_boot_console.run_command('dhcp')
97 assert 'DHCP client bound to address ' in output
98
99 global net_set_up
100 net_set_up = True
101
Sean Edmondd407acf2023-04-11 10:48:48 -0700102@pytest.mark.buildconfigspec('cmd_dhcp6')
103def test_net_dhcp6(u_boot_console):
104 """Test the dhcp6 command.
105
106 The boardenv_* file may be used to enable/disable this test; see the
107 comment at the beginning of this file.
108 """
109
110 test_dhcp6 = u_boot_console.config.env.get('env__net_dhcp6_server', False)
111 if not test_dhcp6:
112 pytest.skip('No DHCP6 server available')
113
114 u_boot_console.run_command('setenv autoload no')
115 output = u_boot_console.run_command('dhcp6')
116 assert 'DHCP6 client bound to ' in output
117
118 global net6_set_up
119 net6_set_up = True
120
Stephen Warren1a218552016-01-21 16:05:31 -0700121@pytest.mark.buildconfigspec('net')
122def test_net_setup_static(u_boot_console):
Stephen Warren75e731e2016-01-26 13:41:30 -0700123 """Set up a static IP configuration.
Stephen Warren1a218552016-01-21 16:05:31 -0700124
125 The configuration is provided by the boardenv_* file; see the comment at
126 the beginning of this file.
Stephen Warren75e731e2016-01-26 13:41:30 -0700127 """
Stephen Warren1a218552016-01-21 16:05:31 -0700128
129 env_vars = u_boot_console.config.env.get('env__net_static_env_vars', None)
130 if not env_vars:
131 pytest.skip('No static network configuration is defined')
132
133 for (var, val) in env_vars:
134 u_boot_console.run_command('setenv %s %s' % (var, val))
135
136 global net_set_up
137 net_set_up = True
138
139@pytest.mark.buildconfigspec('cmd_ping')
140def test_net_ping(u_boot_console):
Stephen Warren75e731e2016-01-26 13:41:30 -0700141 """Test the ping command.
Stephen Warren1a218552016-01-21 16:05:31 -0700142
143 The $serverip (as set up by either test_net_dhcp or test_net_setup_static)
144 is pinged. The test validates that the host is alive, as reported by the
145 ping command's output.
Stephen Warren75e731e2016-01-26 13:41:30 -0700146 """
Stephen Warren1a218552016-01-21 16:05:31 -0700147
148 if not net_set_up:
Stephen Warren3deb8962016-01-26 13:41:31 -0700149 pytest.skip('Network not initialized')
Stephen Warren1a218552016-01-21 16:05:31 -0700150
151 output = u_boot_console.run_command('ping $serverip')
152 assert 'is alive' in output
153
154@pytest.mark.buildconfigspec('cmd_net')
155def test_net_tftpboot(u_boot_console):
Stephen Warren75e731e2016-01-26 13:41:30 -0700156 """Test the tftpboot command.
Stephen Warren1a218552016-01-21 16:05:31 -0700157
158 A file is downloaded from the TFTP server, its size and optionally its
159 CRC32 are validated.
160
161 The details of the file to download are provided by the boardenv_* file;
162 see the comment at the beginning of this file.
Stephen Warren75e731e2016-01-26 13:41:30 -0700163 """
Stephen Warren1a218552016-01-21 16:05:31 -0700164
165 if not net_set_up:
Stephen Warren3deb8962016-01-26 13:41:31 -0700166 pytest.skip('Network not initialized')
Stephen Warren1a218552016-01-21 16:05:31 -0700167
168 f = u_boot_console.config.env.get('env__net_tftp_readable_file', None)
169 if not f:
170 pytest.skip('No TFTP readable file to read')
171
Michal Simek6596e032016-04-04 20:06:14 +0200172 addr = f.get('addr', None)
Michal Simek6596e032016-04-04 20:06:14 +0200173
Stephen Warren1a218552016-01-21 16:05:31 -0700174 fn = f['fn']
Heinrich Schuchardt23287c82019-01-26 15:25:12 +0100175 if not addr:
176 output = u_boot_console.run_command('tftpboot %s' % (fn))
177 else:
178 output = u_boot_console.run_command('tftpboot %x %s' % (addr, fn))
Stephen Warren1a218552016-01-21 16:05:31 -0700179 expected_text = 'Bytes transferred = '
180 sz = f.get('size', None)
181 if sz:
182 expected_text += '%d' % sz
183 assert expected_text in output
184
185 expected_crc = f.get('crc32', None)
186 if not expected_crc:
187 return
188
189 if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
190 return
191
Heinrich Schuchardt23287c82019-01-26 15:25:12 +0100192 output = u_boot_console.run_command('crc32 $fileaddr $filesize')
Stephen Warren1a218552016-01-21 16:05:31 -0700193 assert expected_crc in output
Guillaume GARDET1b0102d2016-09-14 10:29:12 +0200194
195@pytest.mark.buildconfigspec('cmd_nfs')
196def test_net_nfs(u_boot_console):
197 """Test the nfs command.
198
199 A file is downloaded from the NFS server, its size and optionally its
200 CRC32 are validated.
201
202 The details of the file to download are provided by the boardenv_* file;
203 see the comment at the beginning of this file.
204 """
205
206 if not net_set_up:
207 pytest.skip('Network not initialized')
208
209 f = u_boot_console.config.env.get('env__net_nfs_readable_file', None)
210 if not f:
211 pytest.skip('No NFS readable file to read')
212
213 addr = f.get('addr', None)
214 if not addr:
Quentin Schulzdae1aa82018-07-09 19:16:27 +0200215 addr = u_boot_utils.find_ram_base(u_boot_console)
Guillaume GARDET1b0102d2016-09-14 10:29:12 +0200216
217 fn = f['fn']
218 output = u_boot_console.run_command('nfs %x %s' % (addr, fn))
219 expected_text = 'Bytes transferred = '
220 sz = f.get('size', None)
221 if sz:
222 expected_text += '%d' % sz
223 assert expected_text in output
224
225 expected_crc = f.get('crc32', None)
226 if not expected_crc:
227 return
228
229 if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
230 return
231
232 output = u_boot_console.run_command('crc32 %x $filesize' % addr)
233 assert expected_crc in output