Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0 |
Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 2 | # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 3 | |
| 4 | # Test various network-related functionality, such as the dhcp, ping, and |
| 5 | # tftpboot commands. |
| 6 | |
| 7 | import pytest |
| 8 | import u_boot_utils |
| 9 | |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 10 | """ |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 11 | Note: This test relies on boardenv_* containing configuration values to define |
Ehsan Mohandesi | eafbe16 | 2023-04-21 17:08:22 -0700 | [diff] [blame] | 12 | which network environment is available for testing. Without this, this test |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 13 | will be automatically skipped. |
| 14 | |
| 15 | For example: |
| 16 | |
Stephen Warren | 56382a8 | 2016-01-26 11:10:14 -0700 | [diff] [blame] | 17 | # 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. |
| 20 | env__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. |
| 25 | env__net_uses_pci = True |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 26 | |
| 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. |
| 30 | env__net_dhcp_server = True |
| 31 | |
Sean Edmond | 29fb68c | 2023-04-11 10:48:48 -0700 | [diff] [blame] | 32 | # 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. |
| 35 | env__net_dhcp6_server = True |
| 36 | |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 37 | # 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. |
| 40 | env__net_static_env_vars = [ |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 41 | ('ipaddr', '10.0.0.100'), |
| 42 | ('netmask', '255.255.255.0'), |
| 43 | ('serverip', '10.0.0.1'), |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 44 | ] |
| 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. |
| 48 | env__net_tftp_readable_file = { |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 49 | 'fn': 'ubtest-readable.bin', |
| 50 | 'addr': 0x10000000, |
| 51 | 'size': 5058624, |
| 52 | 'crc32': 'c2244b26', |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 53 | } |
Guillaume GARDET | 6a2981a | 2016-09-14 10:29:12 +0200 | [diff] [blame] | 54 | |
| 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. |
| 57 | env__net_nfs_readable_file = { |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 58 | 'fn': 'ubtest-readable.bin', |
| 59 | 'addr': 0x10000000, |
| 60 | 'size': 5058624, |
| 61 | 'crc32': 'c2244b26', |
Guillaume GARDET | 6a2981a | 2016-09-14 10:29:12 +0200 | [diff] [blame] | 62 | } |
Ehsan Mohandesi | eafbe16 | 2023-04-21 17:08:22 -0700 | [diff] [blame] | 63 | |
| 64 | # True if a router advertisement service is connected to the network, and should |
| 65 | # be tested. If router advertisement testing is not possible or desired, this |
| 66 | variable may be omitted or set to False. |
| 67 | env__router_on_net = True |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 68 | """ |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 69 | |
| 70 | net_set_up = False |
Sean Edmond | 29fb68c | 2023-04-11 10:48:48 -0700 | [diff] [blame] | 71 | net6_set_up = False |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 72 | |
| 73 | def test_net_pre_commands(u_boot_console): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 74 | """Execute any commands required to enable network hardware. |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 75 | |
| 76 | These commands are provided by the boardenv_* file; see the comment at the |
| 77 | beginning of this file. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 78 | """ |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 79 | |
Stephen Warren | 56382a8 | 2016-01-26 11:10:14 -0700 | [diff] [blame] | 80 | init_usb = u_boot_console.config.env.get('env__net_uses_usb', False) |
| 81 | if init_usb: |
| 82 | u_boot_console.run_command('usb start') |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 83 | |
Stephen Warren | 56382a8 | 2016-01-26 11:10:14 -0700 | [diff] [blame] | 84 | init_pci = u_boot_console.config.env.get('env__net_uses_pci', False) |
| 85 | if init_pci: |
| 86 | u_boot_console.run_command('pci enum') |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 87 | |
| 88 | @pytest.mark.buildconfigspec('cmd_dhcp') |
| 89 | def test_net_dhcp(u_boot_console): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 90 | """Test the dhcp command. |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 91 | |
| 92 | The boardenv_* file may be used to enable/disable this test; see the |
| 93 | comment at the beginning of this file. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 94 | """ |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 95 | |
| 96 | test_dhcp = u_boot_console.config.env.get('env__net_dhcp_server', False) |
| 97 | if not test_dhcp: |
| 98 | pytest.skip('No DHCP server available') |
| 99 | |
| 100 | u_boot_console.run_command('setenv autoload no') |
| 101 | output = u_boot_console.run_command('dhcp') |
| 102 | assert 'DHCP client bound to address ' in output |
| 103 | |
| 104 | global net_set_up |
| 105 | net_set_up = True |
| 106 | |
Sean Edmond | 29fb68c | 2023-04-11 10:48:48 -0700 | [diff] [blame] | 107 | @pytest.mark.buildconfigspec('cmd_dhcp6') |
| 108 | def test_net_dhcp6(u_boot_console): |
| 109 | """Test the dhcp6 command. |
| 110 | |
| 111 | The boardenv_* file may be used to enable/disable this test; see the |
| 112 | comment at the beginning of this file. |
| 113 | """ |
| 114 | |
| 115 | test_dhcp6 = u_boot_console.config.env.get('env__net_dhcp6_server', False) |
| 116 | if not test_dhcp6: |
| 117 | pytest.skip('No DHCP6 server available') |
| 118 | |
| 119 | u_boot_console.run_command('setenv autoload no') |
| 120 | output = u_boot_console.run_command('dhcp6') |
| 121 | assert 'DHCP6 client bound to ' in output |
| 122 | |
| 123 | global net6_set_up |
| 124 | net6_set_up = True |
| 125 | |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 126 | @pytest.mark.buildconfigspec('net') |
| 127 | def test_net_setup_static(u_boot_console): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 128 | """Set up a static IP configuration. |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 129 | |
| 130 | The configuration is provided by the boardenv_* file; see the comment at |
| 131 | the beginning of this file. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 132 | """ |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 133 | |
| 134 | env_vars = u_boot_console.config.env.get('env__net_static_env_vars', None) |
| 135 | if not env_vars: |
| 136 | pytest.skip('No static network configuration is defined') |
| 137 | |
| 138 | for (var, val) in env_vars: |
| 139 | u_boot_console.run_command('setenv %s %s' % (var, val)) |
| 140 | |
| 141 | global net_set_up |
| 142 | net_set_up = True |
| 143 | |
| 144 | @pytest.mark.buildconfigspec('cmd_ping') |
| 145 | def test_net_ping(u_boot_console): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 146 | """Test the ping command. |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 147 | |
| 148 | The $serverip (as set up by either test_net_dhcp or test_net_setup_static) |
| 149 | is pinged. The test validates that the host is alive, as reported by the |
| 150 | ping command's output. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 151 | """ |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 152 | |
| 153 | if not net_set_up: |
Stephen Warren | a2ec560 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 154 | pytest.skip('Network not initialized') |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 155 | |
| 156 | output = u_boot_console.run_command('ping $serverip') |
| 157 | assert 'is alive' in output |
| 158 | |
Ehsan Mohandesi | eafbe16 | 2023-04-21 17:08:22 -0700 | [diff] [blame] | 159 | @pytest.mark.buildconfigspec('IPV6_ROUTER_DISCOVERY') |
| 160 | def test_net_network_discovery(u_boot_console): |
| 161 | """Test the network discovery feature of IPv6. |
| 162 | |
| 163 | An IPv6 network command (ping6 in this case) is run to make U-Boot send a |
| 164 | router solicitation packet, receive a router advertisement message, and |
| 165 | parse it. |
| 166 | A router advertisement service needs to be running for this test to succeed. |
| 167 | U-Boot receives the RA, processes it, and if successful, assigns the gateway |
| 168 | IP and prefix length. |
| 169 | The configuration is provided by the boardenv_* file; see the comment at |
| 170 | the beginning of this file. |
| 171 | """ |
| 172 | |
| 173 | router_on_net = u_boot_console.config.env.get('env__router_on_net', False) |
| 174 | if not router_on_net: |
| 175 | pytest.skip('No router on network') |
| 176 | |
| 177 | fake_host_ip = 'fe80::215:5dff:fef6:2ec6' |
| 178 | output = u_boot_console.run_command('ping6 ' + fake_host_ip) |
| 179 | assert 'ROUTER SOLICITATION 1' in output |
| 180 | assert 'Set gatewayip6:' in output |
| 181 | assert '0000:0000:0000:0000:0000:0000:0000:0000' not in output |
| 182 | |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 183 | @pytest.mark.buildconfigspec('cmd_net') |
| 184 | def test_net_tftpboot(u_boot_console): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 185 | """Test the tftpboot command. |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 186 | |
| 187 | A file is downloaded from the TFTP server, its size and optionally its |
| 188 | CRC32 are validated. |
| 189 | |
| 190 | The details of the file to download are provided by the boardenv_* file; |
| 191 | see the comment at the beginning of this file. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 192 | """ |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 193 | |
| 194 | if not net_set_up: |
Stephen Warren | a2ec560 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 195 | pytest.skip('Network not initialized') |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 196 | |
| 197 | f = u_boot_console.config.env.get('env__net_tftp_readable_file', None) |
| 198 | if not f: |
| 199 | pytest.skip('No TFTP readable file to read') |
| 200 | |
Michal Simek | 3ba1352 | 2016-04-04 20:06:14 +0200 | [diff] [blame] | 201 | addr = f.get('addr', None) |
Michal Simek | 3ba1352 | 2016-04-04 20:06:14 +0200 | [diff] [blame] | 202 | |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 203 | fn = f['fn'] |
Heinrich Schuchardt | b1b1bab | 2019-01-26 15:25:12 +0100 | [diff] [blame] | 204 | if not addr: |
| 205 | output = u_boot_console.run_command('tftpboot %s' % (fn)) |
| 206 | else: |
| 207 | output = u_boot_console.run_command('tftpboot %x %s' % (addr, fn)) |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 208 | expected_text = 'Bytes transferred = ' |
| 209 | sz = f.get('size', None) |
| 210 | if sz: |
| 211 | expected_text += '%d' % sz |
| 212 | assert expected_text in output |
| 213 | |
| 214 | expected_crc = f.get('crc32', None) |
| 215 | if not expected_crc: |
| 216 | return |
| 217 | |
| 218 | if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y': |
| 219 | return |
| 220 | |
Heinrich Schuchardt | b1b1bab | 2019-01-26 15:25:12 +0100 | [diff] [blame] | 221 | output = u_boot_console.run_command('crc32 $fileaddr $filesize') |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 222 | assert expected_crc in output |
Guillaume GARDET | 6a2981a | 2016-09-14 10:29:12 +0200 | [diff] [blame] | 223 | |
| 224 | @pytest.mark.buildconfigspec('cmd_nfs') |
| 225 | def test_net_nfs(u_boot_console): |
| 226 | """Test the nfs command. |
| 227 | |
| 228 | A file is downloaded from the NFS server, its size and optionally its |
| 229 | CRC32 are validated. |
| 230 | |
| 231 | The details of the file to download are provided by the boardenv_* file; |
| 232 | see the comment at the beginning of this file. |
| 233 | """ |
| 234 | |
| 235 | if not net_set_up: |
| 236 | pytest.skip('Network not initialized') |
| 237 | |
| 238 | f = u_boot_console.config.env.get('env__net_nfs_readable_file', None) |
| 239 | if not f: |
| 240 | pytest.skip('No NFS readable file to read') |
| 241 | |
| 242 | addr = f.get('addr', None) |
| 243 | if not addr: |
Quentin Schulz | f4eef40 | 2018-07-09 19:16:27 +0200 | [diff] [blame] | 244 | addr = u_boot_utils.find_ram_base(u_boot_console) |
Guillaume GARDET | 6a2981a | 2016-09-14 10:29:12 +0200 | [diff] [blame] | 245 | |
| 246 | fn = f['fn'] |
| 247 | output = u_boot_console.run_command('nfs %x %s' % (addr, fn)) |
| 248 | expected_text = 'Bytes transferred = ' |
| 249 | sz = f.get('size', None) |
| 250 | if sz: |
| 251 | expected_text += '%d' % sz |
| 252 | assert expected_text in output |
| 253 | |
| 254 | expected_crc = f.get('crc32', None) |
| 255 | if not expected_crc: |
| 256 | return |
| 257 | |
| 258 | if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y': |
| 259 | return |
| 260 | |
| 261 | output = u_boot_console.run_command('crc32 %x $filesize' % addr) |
| 262 | assert expected_crc in output |