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