blob: 9ca6743afd9c8fb46b113416f90423d6696f3c88 [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
9
Stephen Warrene8debf32016-01-26 13:41:30 -070010"""
Stephen Warrene5bb2792016-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 Warren56382a82016-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 Warrene5bb2792016-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
32# A list of environment variables that should be set in order to configure a
33# static IP. If solely relying on DHCP, this variable may be omitted or set to
34# an empty list.
35env__net_static_env_vars = [
Simon Glass871bf7d2018-12-27 08:11:13 -070036 ('ipaddr', '10.0.0.100'),
37 ('netmask', '255.255.255.0'),
38 ('serverip', '10.0.0.1'),
Stephen Warrene5bb2792016-01-21 16:05:31 -070039]
40
41# Details regarding a file that may be read from a TFTP server. This variable
42# may be omitted or set to None if TFTP testing is not possible or desired.
43env__net_tftp_readable_file = {
Simon Glass871bf7d2018-12-27 08:11:13 -070044 'fn': 'ubtest-readable.bin',
45 'addr': 0x10000000,
46 'size': 5058624,
47 'crc32': 'c2244b26',
Stephen Warrene5bb2792016-01-21 16:05:31 -070048}
Guillaume GARDET6a2981a2016-09-14 10:29:12 +020049
50# Details regarding a file that may be read from a NFS server. This variable
51# may be omitted or set to None if NFS testing is not possible or desired.
52env__net_nfs_readable_file = {
Simon Glass871bf7d2018-12-27 08:11:13 -070053 'fn': 'ubtest-readable.bin',
54 'addr': 0x10000000,
55 'size': 5058624,
56 'crc32': 'c2244b26',
Guillaume GARDET6a2981a2016-09-14 10:29:12 +020057}
Stephen Warrene8debf32016-01-26 13:41:30 -070058"""
Stephen Warrene5bb2792016-01-21 16:05:31 -070059
60net_set_up = False
61
62def test_net_pre_commands(u_boot_console):
Stephen Warrene8debf32016-01-26 13:41:30 -070063 """Execute any commands required to enable network hardware.
Stephen Warrene5bb2792016-01-21 16:05:31 -070064
65 These commands are provided by the boardenv_* file; see the comment at the
66 beginning of this file.
Stephen Warrene8debf32016-01-26 13:41:30 -070067 """
Stephen Warrene5bb2792016-01-21 16:05:31 -070068
Stephen Warren56382a82016-01-26 11:10:14 -070069 init_usb = u_boot_console.config.env.get('env__net_uses_usb', False)
70 if init_usb:
71 u_boot_console.run_command('usb start')
Stephen Warrene5bb2792016-01-21 16:05:31 -070072
Stephen Warren56382a82016-01-26 11:10:14 -070073 init_pci = u_boot_console.config.env.get('env__net_uses_pci', False)
74 if init_pci:
75 u_boot_console.run_command('pci enum')
Stephen Warrene5bb2792016-01-21 16:05:31 -070076
77@pytest.mark.buildconfigspec('cmd_dhcp')
78def test_net_dhcp(u_boot_console):
Stephen Warrene8debf32016-01-26 13:41:30 -070079 """Test the dhcp command.
Stephen Warrene5bb2792016-01-21 16:05:31 -070080
81 The boardenv_* file may be used to enable/disable this test; see the
82 comment at the beginning of this file.
Stephen Warrene8debf32016-01-26 13:41:30 -070083 """
Stephen Warrene5bb2792016-01-21 16:05:31 -070084
85 test_dhcp = u_boot_console.config.env.get('env__net_dhcp_server', False)
86 if not test_dhcp:
87 pytest.skip('No DHCP server available')
88
89 u_boot_console.run_command('setenv autoload no')
90 output = u_boot_console.run_command('dhcp')
91 assert 'DHCP client bound to address ' in output
92
93 global net_set_up
94 net_set_up = True
95
96@pytest.mark.buildconfigspec('net')
97def test_net_setup_static(u_boot_console):
Stephen Warrene8debf32016-01-26 13:41:30 -070098 """Set up a static IP configuration.
Stephen Warrene5bb2792016-01-21 16:05:31 -070099
100 The configuration is provided by the boardenv_* file; see the comment at
101 the beginning of this file.
Stephen Warrene8debf32016-01-26 13:41:30 -0700102 """
Stephen Warrene5bb2792016-01-21 16:05:31 -0700103
104 env_vars = u_boot_console.config.env.get('env__net_static_env_vars', None)
105 if not env_vars:
106 pytest.skip('No static network configuration is defined')
107
108 for (var, val) in env_vars:
109 u_boot_console.run_command('setenv %s %s' % (var, val))
110
111 global net_set_up
112 net_set_up = True
113
114@pytest.mark.buildconfigspec('cmd_ping')
115def test_net_ping(u_boot_console):
Stephen Warrene8debf32016-01-26 13:41:30 -0700116 """Test the ping command.
Stephen Warrene5bb2792016-01-21 16:05:31 -0700117
118 The $serverip (as set up by either test_net_dhcp or test_net_setup_static)
119 is pinged. The test validates that the host is alive, as reported by the
120 ping command's output.
Stephen Warrene8debf32016-01-26 13:41:30 -0700121 """
Stephen Warrene5bb2792016-01-21 16:05:31 -0700122
123 if not net_set_up:
Stephen Warrena2ec5602016-01-26 13:41:31 -0700124 pytest.skip('Network not initialized')
Stephen Warrene5bb2792016-01-21 16:05:31 -0700125
126 output = u_boot_console.run_command('ping $serverip')
127 assert 'is alive' in output
128
129@pytest.mark.buildconfigspec('cmd_net')
130def test_net_tftpboot(u_boot_console):
Stephen Warrene8debf32016-01-26 13:41:30 -0700131 """Test the tftpboot command.
Stephen Warrene5bb2792016-01-21 16:05:31 -0700132
133 A file is downloaded from the TFTP server, its size and optionally its
134 CRC32 are validated.
135
136 The details of the file to download are provided by the boardenv_* file;
137 see the comment at the beginning of this file.
Stephen Warrene8debf32016-01-26 13:41:30 -0700138 """
Stephen Warrene5bb2792016-01-21 16:05:31 -0700139
140 if not net_set_up:
Stephen Warrena2ec5602016-01-26 13:41:31 -0700141 pytest.skip('Network not initialized')
Stephen Warrene5bb2792016-01-21 16:05:31 -0700142
143 f = u_boot_console.config.env.get('env__net_tftp_readable_file', None)
144 if not f:
145 pytest.skip('No TFTP readable file to read')
146
Michal Simek3ba13522016-04-04 20:06:14 +0200147 addr = f.get('addr', None)
Michal Simek3ba13522016-04-04 20:06:14 +0200148
Stephen Warrene5bb2792016-01-21 16:05:31 -0700149 fn = f['fn']
Heinrich Schuchardtb1b1bab2019-01-26 15:25:12 +0100150 if not addr:
151 output = u_boot_console.run_command('tftpboot %s' % (fn))
152 else:
153 output = u_boot_console.run_command('tftpboot %x %s' % (addr, fn))
Stephen Warrene5bb2792016-01-21 16:05:31 -0700154 expected_text = 'Bytes transferred = '
155 sz = f.get('size', None)
156 if sz:
157 expected_text += '%d' % sz
158 assert expected_text in output
159
160 expected_crc = f.get('crc32', None)
161 if not expected_crc:
162 return
163
164 if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
165 return
166
Heinrich Schuchardtb1b1bab2019-01-26 15:25:12 +0100167 output = u_boot_console.run_command('crc32 $fileaddr $filesize')
Stephen Warrene5bb2792016-01-21 16:05:31 -0700168 assert expected_crc in output
Guillaume GARDET6a2981a2016-09-14 10:29:12 +0200169
170@pytest.mark.buildconfigspec('cmd_nfs')
171def test_net_nfs(u_boot_console):
172 """Test the nfs command.
173
174 A file is downloaded from the NFS server, its size and optionally its
175 CRC32 are validated.
176
177 The details of the file to download are provided by the boardenv_* file;
178 see the comment at the beginning of this file.
179 """
180
181 if not net_set_up:
182 pytest.skip('Network not initialized')
183
184 f = u_boot_console.config.env.get('env__net_nfs_readable_file', None)
185 if not f:
186 pytest.skip('No NFS readable file to read')
187
188 addr = f.get('addr', None)
189 if not addr:
Quentin Schulzf4eef402018-07-09 19:16:27 +0200190 addr = u_boot_utils.find_ram_base(u_boot_console)
Guillaume GARDET6a2981a2016-09-14 10:29:12 +0200191
192 fn = f['fn']
193 output = u_boot_console.run_command('nfs %x %s' % (addr, fn))
194 expected_text = 'Bytes transferred = '
195 sz = f.get('size', None)
196 if sz:
197 expected_text += '%d' % sz
198 assert expected_text in output
199
200 expected_crc = f.get('crc32', None)
201 if not expected_crc:
202 return
203
204 if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
205 return
206
207 output = u_boot_console.run_command('crc32 %x $filesize' % addr)
208 assert expected_crc in output