Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0 |
Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 2 | # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. |
Stephen Warren | 1a21855 | 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 | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 10 | """ |
Stephen Warren | 1a21855 | 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 | 8d57b92 | 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 | 1a21855 | 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 | |
| 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. |
| 35 | env__net_static_env_vars = [ |
| 36 | ("ipaddr", "10.0.0.100"), |
| 37 | ("netmask", "255.255.255.0"), |
| 38 | ("serverip", "10.0.0.1"), |
| 39 | ] |
| 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. |
| 43 | env__net_tftp_readable_file = { |
| 44 | "fn": "ubtest-readable.bin", |
Michal Simek | 6596e03 | 2016-04-04 20:06:14 +0200 | [diff] [blame] | 45 | "addr": 0x10000000, |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 46 | "size": 5058624, |
| 47 | "crc32": "c2244b26", |
| 48 | } |
Guillaume GARDET | 1b0102d | 2016-09-14 10:29:12 +0200 | [diff] [blame] | 49 | |
| 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. |
| 52 | env__net_nfs_readable_file = { |
| 53 | "fn": "ubtest-readable.bin", |
| 54 | "addr": 0x10000000, |
| 55 | "size": 5058624, |
| 56 | "crc32": "c2244b26", |
| 57 | } |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 58 | """ |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 59 | |
| 60 | net_set_up = False |
| 61 | |
| 62 | def test_net_pre_commands(u_boot_console): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 63 | """Execute any commands required to enable network hardware. |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 64 | |
| 65 | These commands are provided by the boardenv_* file; see the comment at the |
| 66 | beginning of this file. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 67 | """ |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 68 | |
Stephen Warren | 8d57b92 | 2016-01-26 11:10:14 -0700 | [diff] [blame] | 69 | 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 Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 72 | |
Stephen Warren | 8d57b92 | 2016-01-26 11:10:14 -0700 | [diff] [blame] | 73 | 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 Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 76 | |
| 77 | @pytest.mark.buildconfigspec('cmd_dhcp') |
| 78 | def test_net_dhcp(u_boot_console): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 79 | """Test the dhcp command. |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 80 | |
| 81 | The boardenv_* file may be used to enable/disable this test; see the |
| 82 | comment at the beginning of this file. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 83 | """ |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 84 | |
| 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') |
| 97 | def test_net_setup_static(u_boot_console): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 98 | """Set up a static IP configuration. |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 99 | |
| 100 | The configuration is provided by the boardenv_* file; see the comment at |
| 101 | the beginning of this file. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 102 | """ |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 103 | |
| 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') |
| 115 | def test_net_ping(u_boot_console): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 116 | """Test the ping command. |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 117 | |
| 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 Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 121 | """ |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 122 | |
| 123 | if not net_set_up: |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 124 | pytest.skip('Network not initialized') |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 125 | |
| 126 | output = u_boot_console.run_command('ping $serverip') |
| 127 | assert 'is alive' in output |
| 128 | |
| 129 | @pytest.mark.buildconfigspec('cmd_net') |
| 130 | def test_net_tftpboot(u_boot_console): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 131 | """Test the tftpboot command. |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 132 | |
| 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 Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 138 | """ |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 139 | |
| 140 | if not net_set_up: |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 141 | pytest.skip('Network not initialized') |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 142 | |
| 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 Simek | 6596e03 | 2016-04-04 20:06:14 +0200 | [diff] [blame] | 147 | addr = f.get('addr', None) |
| 148 | if not addr: |
Quentin Schulz | dae1aa8 | 2018-07-09 19:16:27 +0200 | [diff] [blame] | 149 | addr = u_boot_utils.find_ram_base(u_boot_console) |
Michal Simek | 6596e03 | 2016-04-04 20:06:14 +0200 | [diff] [blame] | 150 | |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 151 | fn = f['fn'] |
| 152 | output = u_boot_console.run_command('tftpboot %x %s' % (addr, fn)) |
| 153 | expected_text = 'Bytes transferred = ' |
| 154 | sz = f.get('size', None) |
| 155 | if sz: |
| 156 | expected_text += '%d' % sz |
| 157 | assert expected_text in output |
| 158 | |
| 159 | expected_crc = f.get('crc32', None) |
| 160 | if not expected_crc: |
| 161 | return |
| 162 | |
| 163 | if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y': |
| 164 | return |
| 165 | |
| 166 | output = u_boot_console.run_command('crc32 %x $filesize' % addr) |
| 167 | assert expected_crc in output |
Guillaume GARDET | 1b0102d | 2016-09-14 10:29:12 +0200 | [diff] [blame] | 168 | |
| 169 | @pytest.mark.buildconfigspec('cmd_nfs') |
| 170 | def test_net_nfs(u_boot_console): |
| 171 | """Test the nfs command. |
| 172 | |
| 173 | A file is downloaded from the NFS server, its size and optionally its |
| 174 | CRC32 are validated. |
| 175 | |
| 176 | The details of the file to download are provided by the boardenv_* file; |
| 177 | see the comment at the beginning of this file. |
| 178 | """ |
| 179 | |
| 180 | if not net_set_up: |
| 181 | pytest.skip('Network not initialized') |
| 182 | |
| 183 | f = u_boot_console.config.env.get('env__net_nfs_readable_file', None) |
| 184 | if not f: |
| 185 | pytest.skip('No NFS readable file to read') |
| 186 | |
| 187 | addr = f.get('addr', None) |
| 188 | if not addr: |
Quentin Schulz | dae1aa8 | 2018-07-09 19:16:27 +0200 | [diff] [blame] | 189 | addr = u_boot_utils.find_ram_base(u_boot_console) |
Guillaume GARDET | 1b0102d | 2016-09-14 10:29:12 +0200 | [diff] [blame] | 190 | |
| 191 | fn = f['fn'] |
| 192 | output = u_boot_console.run_command('nfs %x %s' % (addr, fn)) |
| 193 | expected_text = 'Bytes transferred = ' |
| 194 | sz = f.get('size', None) |
| 195 | if sz: |
| 196 | expected_text += '%d' % sz |
| 197 | assert expected_text in output |
| 198 | |
| 199 | expected_crc = f.get('crc32', None) |
| 200 | if not expected_crc: |
| 201 | return |
| 202 | |
| 203 | if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y': |
| 204 | return |
| 205 | |
| 206 | output = u_boot_console.run_command('crc32 %x $filesize' % addr) |
| 207 | assert expected_crc in output |