xref: /openbmc/u-boot/test/py/tests/test_net.py (revision dffceb4b)
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
8import pytest
9import u_boot_utils
10
11"""
12Note: 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
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.
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
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.
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",
46    "size": 5058624,
47    "crc32": "c2244b26",
48}
49"""
50
51net_set_up = False
52
53def test_net_pre_commands(u_boot_console):
54    """Execute any commands required to enable network hardware.
55
56    These commands are provided by the boardenv_* file; see the comment at the
57    beginning of this file.
58    """
59
60    init_usb = u_boot_console.config.env.get('env__net_uses_usb', False)
61    if init_usb:
62        u_boot_console.run_command('usb start')
63
64    init_pci = u_boot_console.config.env.get('env__net_uses_pci', False)
65    if init_pci:
66        u_boot_console.run_command('pci enum')
67
68@pytest.mark.buildconfigspec('cmd_dhcp')
69def test_net_dhcp(u_boot_console):
70    """Test the dhcp command.
71
72    The boardenv_* file may be used to enable/disable this test; see the
73    comment at the beginning of this file.
74    """
75
76    test_dhcp = u_boot_console.config.env.get('env__net_dhcp_server', False)
77    if not test_dhcp:
78        pytest.skip('No DHCP server available')
79
80    u_boot_console.run_command('setenv autoload no')
81    output = u_boot_console.run_command('dhcp')
82    assert 'DHCP client bound to address ' in output
83
84    global net_set_up
85    net_set_up = True
86
87@pytest.mark.buildconfigspec('net')
88def test_net_setup_static(u_boot_console):
89    """Set up a static IP configuration.
90
91    The configuration is provided by the boardenv_* file; see the comment at
92    the beginning of this file.
93    """
94
95    env_vars = u_boot_console.config.env.get('env__net_static_env_vars', None)
96    if not env_vars:
97        pytest.skip('No static network configuration is defined')
98
99    for (var, val) in env_vars:
100        u_boot_console.run_command('setenv %s %s' % (var, val))
101
102    global net_set_up
103    net_set_up = True
104
105@pytest.mark.buildconfigspec('cmd_ping')
106def test_net_ping(u_boot_console):
107    """Test the ping command.
108
109    The $serverip (as set up by either test_net_dhcp or test_net_setup_static)
110    is pinged. The test validates that the host is alive, as reported by the
111    ping command's output.
112    """
113
114    if not net_set_up:
115        pytest.skip('Network not initialized')
116
117    output = u_boot_console.run_command('ping $serverip')
118    assert 'is alive' in output
119
120@pytest.mark.buildconfigspec('cmd_net')
121def test_net_tftpboot(u_boot_console):
122    """Test the tftpboot command.
123
124    A file is downloaded from the TFTP server, its size and optionally its
125    CRC32 are validated.
126
127    The details of the file to download are provided by the boardenv_* file;
128    see the comment at the beginning of this file.
129    """
130
131    if not net_set_up:
132        pytest.skip('Network not initialized')
133
134    f = u_boot_console.config.env.get('env__net_tftp_readable_file', None)
135    if not f:
136        pytest.skip('No TFTP readable file to read')
137
138    addr = u_boot_utils.find_ram_base(u_boot_console)
139    fn = f['fn']
140    output = u_boot_console.run_command('tftpboot %x %s' % (addr, fn))
141    expected_text = 'Bytes transferred = '
142    sz = f.get('size', None)
143    if sz:
144        expected_text += '%d' % sz
145    assert expected_text in output
146
147    expected_crc = f.get('crc32', None)
148    if not expected_crc:
149        return
150
151    if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
152        return
153
154    output = u_boot_console.run_command('crc32 %x $filesize' % addr)
155    assert expected_crc in output
156