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 |
| 12 | which the network environment available for testing. Without this, this test |
| 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 | } |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 63 | """ |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 64 | |
| 65 | net_set_up = False |
Sean Edmond | 29fb68c | 2023-04-11 10:48:48 -0700 | [diff] [blame^] | 66 | net6_set_up = False |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 67 | |
| 68 | def test_net_pre_commands(u_boot_console): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 69 | """Execute any commands required to enable network hardware. |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 70 | |
| 71 | These commands are provided by the boardenv_* file; see the comment at the |
| 72 | beginning of this file. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 73 | """ |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 74 | |
Stephen Warren | 56382a8 | 2016-01-26 11:10:14 -0700 | [diff] [blame] | 75 | 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 Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 78 | |
Stephen Warren | 56382a8 | 2016-01-26 11:10:14 -0700 | [diff] [blame] | 79 | 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 Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 82 | |
| 83 | @pytest.mark.buildconfigspec('cmd_dhcp') |
| 84 | def test_net_dhcp(u_boot_console): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 85 | """Test the dhcp command. |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 86 | |
| 87 | The boardenv_* file may be used to enable/disable this test; see the |
| 88 | comment at the beginning of this file. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 89 | """ |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 90 | |
| 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 Edmond | 29fb68c | 2023-04-11 10:48:48 -0700 | [diff] [blame^] | 102 | @pytest.mark.buildconfigspec('cmd_dhcp6') |
| 103 | def 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 Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 121 | @pytest.mark.buildconfigspec('net') |
| 122 | def test_net_setup_static(u_boot_console): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 123 | """Set up a static IP configuration. |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 124 | |
| 125 | The configuration is provided by the boardenv_* file; see the comment at |
| 126 | the beginning of this file. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 127 | """ |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 128 | |
| 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') |
| 140 | def test_net_ping(u_boot_console): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 141 | """Test the ping command. |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 142 | |
| 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 Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 146 | """ |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 147 | |
| 148 | if not net_set_up: |
Stephen Warren | a2ec560 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 149 | pytest.skip('Network not initialized') |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 150 | |
| 151 | output = u_boot_console.run_command('ping $serverip') |
| 152 | assert 'is alive' in output |
| 153 | |
| 154 | @pytest.mark.buildconfigspec('cmd_net') |
| 155 | def test_net_tftpboot(u_boot_console): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 156 | """Test the tftpboot command. |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 157 | |
| 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 Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 163 | """ |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 164 | |
| 165 | if not net_set_up: |
Stephen Warren | a2ec560 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 166 | pytest.skip('Network not initialized') |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 167 | |
| 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 Simek | 3ba1352 | 2016-04-04 20:06:14 +0200 | [diff] [blame] | 172 | addr = f.get('addr', None) |
Michal Simek | 3ba1352 | 2016-04-04 20:06:14 +0200 | [diff] [blame] | 173 | |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 174 | fn = f['fn'] |
Heinrich Schuchardt | b1b1bab | 2019-01-26 15:25:12 +0100 | [diff] [blame] | 175 | 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 Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 179 | 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 Schuchardt | b1b1bab | 2019-01-26 15:25:12 +0100 | [diff] [blame] | 192 | output = u_boot_console.run_command('crc32 $fileaddr $filesize') |
Stephen Warren | e5bb279 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 193 | assert expected_crc in output |
Guillaume GARDET | 6a2981a | 2016-09-14 10:29:12 +0200 | [diff] [blame] | 194 | |
| 195 | @pytest.mark.buildconfigspec('cmd_nfs') |
| 196 | def 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 Schulz | f4eef40 | 2018-07-09 19:16:27 +0200 | [diff] [blame] | 215 | addr = u_boot_utils.find_ram_base(u_boot_console) |
Guillaume GARDET | 6a2981a | 2016-09-14 10:29:12 +0200 | [diff] [blame] | 216 | |
| 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 |