Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 1 | # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. |
| 2 | # |
| 3 | # SPDX-License-Identifier: GPL-2.0 |
| 4 | |
| 5 | # Test various network-related functionality, such as the dhcp, ping, and |
| 6 | # tftpboot commands. |
| 7 | |
| 8 | import pytest |
| 9 | import u_boot_utils |
| 10 | |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 11 | """ |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 12 | Note: This test relies on boardenv_* containing configuration values to define |
| 13 | which the network environment available for testing. Without this, this test |
| 14 | will be automatically skipped. |
| 15 | |
| 16 | For example: |
| 17 | |
Stephen Warren | 8d57b92 | 2016-01-26 11:10:14 -0700 | [diff] [blame] | 18 | # Boolean indicating whether the Ethernet device is attached to USB, and hence |
| 19 | # USB enumeration needs to be performed prior to network tests. |
| 20 | # This variable may be omitted if its value is False. |
| 21 | env__net_uses_usb = False |
| 22 | |
| 23 | # Boolean indicating whether the Ethernet device is attached to PCI, and hence |
| 24 | # PCI enumeration needs to be performed prior to network tests. |
| 25 | # This variable may be omitted if its value is False. |
| 26 | env__net_uses_pci = True |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 27 | |
| 28 | # True if a DHCP server is attached to the network, and should be tested. |
| 29 | # If DHCP testing is not possible or desired, this variable may be omitted or |
| 30 | # set to False. |
| 31 | env__net_dhcp_server = True |
| 32 | |
| 33 | # A list of environment variables that should be set in order to configure a |
| 34 | # static IP. If solely relying on DHCP, this variable may be omitted or set to |
| 35 | # an empty list. |
| 36 | env__net_static_env_vars = [ |
| 37 | ("ipaddr", "10.0.0.100"), |
| 38 | ("netmask", "255.255.255.0"), |
| 39 | ("serverip", "10.0.0.1"), |
| 40 | ] |
| 41 | |
| 42 | # Details regarding a file that may be read from a TFTP server. This variable |
| 43 | # may be omitted or set to None if TFTP testing is not possible or desired. |
| 44 | env__net_tftp_readable_file = { |
| 45 | "fn": "ubtest-readable.bin", |
Michal Simek | 6596e03 | 2016-04-04 20:06:14 +0200 | [diff] [blame] | 46 | "addr": 0x10000000, |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 47 | "size": 5058624, |
| 48 | "crc32": "c2244b26", |
| 49 | } |
Guillaume GARDET | 1b0102d | 2016-09-14 10:29:12 +0200 | [diff] [blame] | 50 | |
| 51 | # Details regarding a file that may be read from a NFS server. This variable |
| 52 | # may be omitted or set to None if NFS testing is not possible or desired. |
| 53 | env__net_nfs_readable_file = { |
| 54 | "fn": "ubtest-readable.bin", |
| 55 | "addr": 0x10000000, |
| 56 | "size": 5058624, |
| 57 | "crc32": "c2244b26", |
| 58 | } |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 59 | """ |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 60 | |
| 61 | net_set_up = False |
| 62 | |
| 63 | def test_net_pre_commands(u_boot_console): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 64 | """Execute any commands required to enable network hardware. |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 65 | |
| 66 | These commands are provided by the boardenv_* file; see the comment at the |
| 67 | beginning of this file. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 68 | """ |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 69 | |
Stephen Warren | 8d57b92 | 2016-01-26 11:10:14 -0700 | [diff] [blame] | 70 | init_usb = u_boot_console.config.env.get('env__net_uses_usb', False) |
| 71 | if init_usb: |
| 72 | u_boot_console.run_command('usb start') |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 73 | |
Stephen Warren | 8d57b92 | 2016-01-26 11:10:14 -0700 | [diff] [blame] | 74 | init_pci = u_boot_console.config.env.get('env__net_uses_pci', False) |
| 75 | if init_pci: |
| 76 | u_boot_console.run_command('pci enum') |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 77 | |
| 78 | @pytest.mark.buildconfigspec('cmd_dhcp') |
| 79 | def test_net_dhcp(u_boot_console): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 80 | """Test the dhcp command. |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 81 | |
| 82 | The boardenv_* file may be used to enable/disable this test; see the |
| 83 | comment at the beginning of this file. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 84 | """ |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 85 | |
| 86 | test_dhcp = u_boot_console.config.env.get('env__net_dhcp_server', False) |
| 87 | if not test_dhcp: |
| 88 | pytest.skip('No DHCP server available') |
| 89 | |
| 90 | u_boot_console.run_command('setenv autoload no') |
| 91 | output = u_boot_console.run_command('dhcp') |
| 92 | assert 'DHCP client bound to address ' in output |
| 93 | |
| 94 | global net_set_up |
| 95 | net_set_up = True |
| 96 | |
| 97 | @pytest.mark.buildconfigspec('net') |
| 98 | def test_net_setup_static(u_boot_console): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 99 | """Set up a static IP configuration. |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 100 | |
| 101 | The configuration is provided by the boardenv_* file; see the comment at |
| 102 | the beginning of this file. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 103 | """ |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 104 | |
| 105 | env_vars = u_boot_console.config.env.get('env__net_static_env_vars', None) |
| 106 | if not env_vars: |
| 107 | pytest.skip('No static network configuration is defined') |
| 108 | |
| 109 | for (var, val) in env_vars: |
| 110 | u_boot_console.run_command('setenv %s %s' % (var, val)) |
| 111 | |
| 112 | global net_set_up |
| 113 | net_set_up = True |
| 114 | |
| 115 | @pytest.mark.buildconfigspec('cmd_ping') |
| 116 | def test_net_ping(u_boot_console): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 117 | """Test the ping command. |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 118 | |
| 119 | The $serverip (as set up by either test_net_dhcp or test_net_setup_static) |
| 120 | is pinged. The test validates that the host is alive, as reported by the |
| 121 | ping command's output. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 122 | """ |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 123 | |
| 124 | if not net_set_up: |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 125 | pytest.skip('Network not initialized') |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 126 | |
| 127 | output = u_boot_console.run_command('ping $serverip') |
| 128 | assert 'is alive' in output |
| 129 | |
| 130 | @pytest.mark.buildconfigspec('cmd_net') |
| 131 | def test_net_tftpboot(u_boot_console): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 132 | """Test the tftpboot command. |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 133 | |
| 134 | A file is downloaded from the TFTP server, its size and optionally its |
| 135 | CRC32 are validated. |
| 136 | |
| 137 | The details of the file to download are provided by the boardenv_* file; |
| 138 | see the comment at the beginning of this file. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 139 | """ |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 140 | |
| 141 | if not net_set_up: |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 142 | pytest.skip('Network not initialized') |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 143 | |
| 144 | f = u_boot_console.config.env.get('env__net_tftp_readable_file', None) |
| 145 | if not f: |
| 146 | pytest.skip('No TFTP readable file to read') |
| 147 | |
Michal Simek | 6596e03 | 2016-04-04 20:06:14 +0200 | [diff] [blame] | 148 | addr = f.get('addr', None) |
| 149 | if not addr: |
Alexander Graf | 3a2e262 | 2016-11-17 18:31:02 +0100 | [diff] [blame] | 150 | addr = u_boot_utils.find_ram_base(u_boot_console) + (1024 * 1024 * 4) |
Michal Simek | 6596e03 | 2016-04-04 20:06:14 +0200 | [diff] [blame] | 151 | |
Stephen Warren | 1a21855 | 2016-01-21 16:05:31 -0700 | [diff] [blame] | 152 | fn = f['fn'] |
| 153 | output = u_boot_console.run_command('tftpboot %x %s' % (addr, fn)) |
| 154 | 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 | |
| 167 | output = u_boot_console.run_command('crc32 %x $filesize' % addr) |
| 168 | assert expected_crc in output |
Guillaume GARDET | 1b0102d | 2016-09-14 10:29:12 +0200 | [diff] [blame] | 169 | |
| 170 | @pytest.mark.buildconfigspec('cmd_nfs') |
| 171 | def 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: |
Alexander Graf | 3a2e262 | 2016-11-17 18:31:02 +0100 | [diff] [blame] | 190 | addr = u_boot_utils.find_ram_base(u_boot_console) + (1024 * 1024 * 4) |
Guillaume GARDET | 1b0102d | 2016-09-14 10:29:12 +0200 | [diff] [blame] | 191 | |
| 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 |