blob: 4ab58b44248a80c6144b3bd5b45374bbb765de4d [file] [log] [blame]
Stephen Warrene5bb2792016-01-21 16:05:31 -07001# 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
8import pytest
9import u_boot_utils
10
Stephen Warrene8debf32016-01-26 13:41:30 -070011"""
Stephen Warrene5bb2792016-01-21 16:05:31 -070012Note: This test relies on boardenv_* containing configuration values to define
13which the network environment available for testing. Without this, this test
14will be automatically skipped.
15
16For example:
17
Stephen Warren56382a82016-01-26 11:10:14 -070018# 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.
21env__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.
26env__net_uses_pci = True
Stephen Warrene5bb2792016-01-21 16:05:31 -070027
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.
31env__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.
36env__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.
44env__net_tftp_readable_file = {
45 "fn": "ubtest-readable.bin",
Michal Simek3ba13522016-04-04 20:06:14 +020046 "addr": 0x10000000,
Stephen Warrene5bb2792016-01-21 16:05:31 -070047 "size": 5058624,
48 "crc32": "c2244b26",
49}
Stephen Warrene8debf32016-01-26 13:41:30 -070050"""
Stephen Warrene5bb2792016-01-21 16:05:31 -070051
52net_set_up = False
53
54def test_net_pre_commands(u_boot_console):
Stephen Warrene8debf32016-01-26 13:41:30 -070055 """Execute any commands required to enable network hardware.
Stephen Warrene5bb2792016-01-21 16:05:31 -070056
57 These commands are provided by the boardenv_* file; see the comment at the
58 beginning of this file.
Stephen Warrene8debf32016-01-26 13:41:30 -070059 """
Stephen Warrene5bb2792016-01-21 16:05:31 -070060
Stephen Warren56382a82016-01-26 11:10:14 -070061 init_usb = u_boot_console.config.env.get('env__net_uses_usb', False)
62 if init_usb:
63 u_boot_console.run_command('usb start')
Stephen Warrene5bb2792016-01-21 16:05:31 -070064
Stephen Warren56382a82016-01-26 11:10:14 -070065 init_pci = u_boot_console.config.env.get('env__net_uses_pci', False)
66 if init_pci:
67 u_boot_console.run_command('pci enum')
Stephen Warrene5bb2792016-01-21 16:05:31 -070068
69@pytest.mark.buildconfigspec('cmd_dhcp')
70def test_net_dhcp(u_boot_console):
Stephen Warrene8debf32016-01-26 13:41:30 -070071 """Test the dhcp command.
Stephen Warrene5bb2792016-01-21 16:05:31 -070072
73 The boardenv_* file may be used to enable/disable this test; see the
74 comment at the beginning of this file.
Stephen Warrene8debf32016-01-26 13:41:30 -070075 """
Stephen Warrene5bb2792016-01-21 16:05:31 -070076
77 test_dhcp = u_boot_console.config.env.get('env__net_dhcp_server', False)
78 if not test_dhcp:
79 pytest.skip('No DHCP server available')
80
81 u_boot_console.run_command('setenv autoload no')
82 output = u_boot_console.run_command('dhcp')
83 assert 'DHCP client bound to address ' in output
84
85 global net_set_up
86 net_set_up = True
87
88@pytest.mark.buildconfigspec('net')
89def test_net_setup_static(u_boot_console):
Stephen Warrene8debf32016-01-26 13:41:30 -070090 """Set up a static IP configuration.
Stephen Warrene5bb2792016-01-21 16:05:31 -070091
92 The configuration is provided by the boardenv_* file; see the comment at
93 the beginning of this file.
Stephen Warrene8debf32016-01-26 13:41:30 -070094 """
Stephen Warrene5bb2792016-01-21 16:05:31 -070095
96 env_vars = u_boot_console.config.env.get('env__net_static_env_vars', None)
97 if not env_vars:
98 pytest.skip('No static network configuration is defined')
99
100 for (var, val) in env_vars:
101 u_boot_console.run_command('setenv %s %s' % (var, val))
102
103 global net_set_up
104 net_set_up = True
105
106@pytest.mark.buildconfigspec('cmd_ping')
107def test_net_ping(u_boot_console):
Stephen Warrene8debf32016-01-26 13:41:30 -0700108 """Test the ping command.
Stephen Warrene5bb2792016-01-21 16:05:31 -0700109
110 The $serverip (as set up by either test_net_dhcp or test_net_setup_static)
111 is pinged. The test validates that the host is alive, as reported by the
112 ping command's output.
Stephen Warrene8debf32016-01-26 13:41:30 -0700113 """
Stephen Warrene5bb2792016-01-21 16:05:31 -0700114
115 if not net_set_up:
Stephen Warrena2ec5602016-01-26 13:41:31 -0700116 pytest.skip('Network not initialized')
Stephen Warrene5bb2792016-01-21 16:05:31 -0700117
118 output = u_boot_console.run_command('ping $serverip')
119 assert 'is alive' in output
120
121@pytest.mark.buildconfigspec('cmd_net')
122def test_net_tftpboot(u_boot_console):
Stephen Warrene8debf32016-01-26 13:41:30 -0700123 """Test the tftpboot command.
Stephen Warrene5bb2792016-01-21 16:05:31 -0700124
125 A file is downloaded from the TFTP server, its size and optionally its
126 CRC32 are validated.
127
128 The details of the file to download are provided by the boardenv_* file;
129 see the comment at the beginning of this file.
Stephen Warrene8debf32016-01-26 13:41:30 -0700130 """
Stephen Warrene5bb2792016-01-21 16:05:31 -0700131
132 if not net_set_up:
Stephen Warrena2ec5602016-01-26 13:41:31 -0700133 pytest.skip('Network not initialized')
Stephen Warrene5bb2792016-01-21 16:05:31 -0700134
135 f = u_boot_console.config.env.get('env__net_tftp_readable_file', None)
136 if not f:
137 pytest.skip('No TFTP readable file to read')
138
Michal Simek3ba13522016-04-04 20:06:14 +0200139 addr = f.get('addr', None)
140 if not addr:
141 addr = u_boot_utils.find_ram_base(u_boot_console)
142
Stephen Warrene5bb2792016-01-21 16:05:31 -0700143 fn = f['fn']
144 output = u_boot_console.run_command('tftpboot %x %s' % (addr, fn))
145 expected_text = 'Bytes transferred = '
146 sz = f.get('size', None)
147 if sz:
148 expected_text += '%d' % sz
149 assert expected_text in output
150
151 expected_crc = f.get('crc32', None)
152 if not expected_crc:
153 return
154
155 if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
156 return
157
158 output = u_boot_console.run_command('crc32 %x $filesize' % addr)
159 assert expected_crc in output