xref: /openbmc/u-boot/test/py/tests/test_net.py (revision b1b1bab7f92b838a252ab977f56d9c3584c14fb7)
1e5bb279fSStephen Warren# SPDX-License-Identifier: GPL-2.0
283d290c5STom Rini# Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
3e5bb279fSStephen Warren
4e5bb279fSStephen Warren# Test various network-related functionality, such as the dhcp, ping, and
5e5bb279fSStephen Warren# tftpboot commands.
6e5bb279fSStephen Warren
7e5bb279fSStephen Warrenimport pytest
8e5bb279fSStephen Warrenimport u_boot_utils
9e5bb279fSStephen Warren
10e8debf39SStephen Warren"""
11e5bb279fSStephen WarrenNote: This test relies on boardenv_* containing configuration values to define
12e5bb279fSStephen Warrenwhich the network environment available for testing. Without this, this test
13e5bb279fSStephen Warrenwill be automatically skipped.
14e5bb279fSStephen Warren
15e5bb279fSStephen WarrenFor example:
16e5bb279fSStephen Warren
1756382a81SStephen Warren# Boolean indicating whether the Ethernet device is attached to USB, and hence
1856382a81SStephen Warren# USB enumeration needs to be performed prior to network tests.
1956382a81SStephen Warren# This variable may be omitted if its value is False.
2056382a81SStephen Warrenenv__net_uses_usb = False
2156382a81SStephen Warren
2256382a81SStephen Warren# Boolean indicating whether the Ethernet device is attached to PCI, and hence
2356382a81SStephen Warren# PCI enumeration needs to be performed prior to network tests.
2456382a81SStephen Warren# This variable may be omitted if its value is False.
2556382a81SStephen Warrenenv__net_uses_pci = True
26e5bb279fSStephen Warren
27e5bb279fSStephen Warren# True if a DHCP server is attached to the network, and should be tested.
28e5bb279fSStephen Warren# If DHCP testing is not possible or desired, this variable may be omitted or
29e5bb279fSStephen Warren# set to False.
30e5bb279fSStephen Warrenenv__net_dhcp_server = True
31e5bb279fSStephen Warren
32e5bb279fSStephen Warren# A list of environment variables that should be set in order to configure a
33e5bb279fSStephen Warren# static IP. If solely relying on DHCP, this variable may be omitted or set to
34e5bb279fSStephen Warren# an empty list.
35e5bb279fSStephen Warrenenv__net_static_env_vars = [
36871bf7d9SSimon Glass    ('ipaddr', '10.0.0.100'),
37871bf7d9SSimon Glass    ('netmask', '255.255.255.0'),
38871bf7d9SSimon Glass    ('serverip', '10.0.0.1'),
39e5bb279fSStephen Warren]
40e5bb279fSStephen Warren
41e5bb279fSStephen Warren# Details regarding a file that may be read from a TFTP server. This variable
42e5bb279fSStephen Warren# may be omitted or set to None if TFTP testing is not possible or desired.
43e5bb279fSStephen Warrenenv__net_tftp_readable_file = {
44871bf7d9SSimon Glass    'fn': 'ubtest-readable.bin',
45871bf7d9SSimon Glass    'addr': 0x10000000,
46871bf7d9SSimon Glass    'size': 5058624,
47871bf7d9SSimon Glass    'crc32': 'c2244b26',
48e5bb279fSStephen Warren}
496a2981a7SGuillaume GARDET
506a2981a7SGuillaume GARDET# Details regarding a file that may be read from a NFS server. This variable
516a2981a7SGuillaume GARDET# may be omitted or set to None if NFS testing is not possible or desired.
526a2981a7SGuillaume GARDETenv__net_nfs_readable_file = {
53871bf7d9SSimon Glass    'fn': 'ubtest-readable.bin',
54871bf7d9SSimon Glass    'addr': 0x10000000,
55871bf7d9SSimon Glass    'size': 5058624,
56871bf7d9SSimon Glass    'crc32': 'c2244b26',
576a2981a7SGuillaume GARDET}
58e8debf39SStephen Warren"""
59e5bb279fSStephen Warren
60e5bb279fSStephen Warrennet_set_up = False
61e5bb279fSStephen Warren
62e5bb279fSStephen Warrendef test_net_pre_commands(u_boot_console):
63e8debf39SStephen Warren    """Execute any commands required to enable network hardware.
64e5bb279fSStephen Warren
65e5bb279fSStephen Warren    These commands are provided by the boardenv_* file; see the comment at the
66e5bb279fSStephen Warren    beginning of this file.
67e8debf39SStephen Warren    """
68e5bb279fSStephen Warren
6956382a81SStephen Warren    init_usb = u_boot_console.config.env.get('env__net_uses_usb', False)
7056382a81SStephen Warren    if init_usb:
7156382a81SStephen Warren        u_boot_console.run_command('usb start')
72e5bb279fSStephen Warren
7356382a81SStephen Warren    init_pci = u_boot_console.config.env.get('env__net_uses_pci', False)
7456382a81SStephen Warren    if init_pci:
7556382a81SStephen Warren        u_boot_console.run_command('pci enum')
76e5bb279fSStephen Warren
77e5bb279fSStephen Warren@pytest.mark.buildconfigspec('cmd_dhcp')
78e5bb279fSStephen Warrendef test_net_dhcp(u_boot_console):
79e8debf39SStephen Warren    """Test the dhcp command.
80e5bb279fSStephen Warren
81e5bb279fSStephen Warren    The boardenv_* file may be used to enable/disable this test; see the
82e5bb279fSStephen Warren    comment at the beginning of this file.
83e8debf39SStephen Warren    """
84e5bb279fSStephen Warren
85e5bb279fSStephen Warren    test_dhcp = u_boot_console.config.env.get('env__net_dhcp_server', False)
86e5bb279fSStephen Warren    if not test_dhcp:
87e5bb279fSStephen Warren        pytest.skip('No DHCP server available')
88e5bb279fSStephen Warren
89e5bb279fSStephen Warren    u_boot_console.run_command('setenv autoload no')
90e5bb279fSStephen Warren    output = u_boot_console.run_command('dhcp')
91e5bb279fSStephen Warren    assert 'DHCP client bound to address ' in output
92e5bb279fSStephen Warren
93e5bb279fSStephen Warren    global net_set_up
94e5bb279fSStephen Warren    net_set_up = True
95e5bb279fSStephen Warren
96e5bb279fSStephen Warren@pytest.mark.buildconfigspec('net')
97e5bb279fSStephen Warrendef test_net_setup_static(u_boot_console):
98e8debf39SStephen Warren    """Set up a static IP configuration.
99e5bb279fSStephen Warren
100e5bb279fSStephen Warren    The configuration is provided by the boardenv_* file; see the comment at
101e5bb279fSStephen Warren    the beginning of this file.
102e8debf39SStephen Warren    """
103e5bb279fSStephen Warren
104e5bb279fSStephen Warren    env_vars = u_boot_console.config.env.get('env__net_static_env_vars', None)
105e5bb279fSStephen Warren    if not env_vars:
106e5bb279fSStephen Warren        pytest.skip('No static network configuration is defined')
107e5bb279fSStephen Warren
108e5bb279fSStephen Warren    for (var, val) in env_vars:
109e5bb279fSStephen Warren        u_boot_console.run_command('setenv %s %s' % (var, val))
110e5bb279fSStephen Warren
111e5bb279fSStephen Warren    global net_set_up
112e5bb279fSStephen Warren    net_set_up = True
113e5bb279fSStephen Warren
114e5bb279fSStephen Warren@pytest.mark.buildconfigspec('cmd_ping')
115e5bb279fSStephen Warrendef test_net_ping(u_boot_console):
116e8debf39SStephen Warren    """Test the ping command.
117e5bb279fSStephen Warren
118e5bb279fSStephen Warren    The $serverip (as set up by either test_net_dhcp or test_net_setup_static)
119e5bb279fSStephen Warren    is pinged. The test validates that the host is alive, as reported by the
120e5bb279fSStephen Warren    ping command's output.
121e8debf39SStephen Warren    """
122e5bb279fSStephen Warren
123e5bb279fSStephen Warren    if not net_set_up:
124a2ec5606SStephen Warren        pytest.skip('Network not initialized')
125e5bb279fSStephen Warren
126e5bb279fSStephen Warren    output = u_boot_console.run_command('ping $serverip')
127e5bb279fSStephen Warren    assert 'is alive' in output
128e5bb279fSStephen Warren
129e5bb279fSStephen Warren@pytest.mark.buildconfigspec('cmd_net')
130e5bb279fSStephen Warrendef test_net_tftpboot(u_boot_console):
131e8debf39SStephen Warren    """Test the tftpboot command.
132e5bb279fSStephen Warren
133e5bb279fSStephen Warren    A file is downloaded from the TFTP server, its size and optionally its
134e5bb279fSStephen Warren    CRC32 are validated.
135e5bb279fSStephen Warren
136e5bb279fSStephen Warren    The details of the file to download are provided by the boardenv_* file;
137e5bb279fSStephen Warren    see the comment at the beginning of this file.
138e8debf39SStephen Warren    """
139e5bb279fSStephen Warren
140e5bb279fSStephen Warren    if not net_set_up:
141a2ec5606SStephen Warren        pytest.skip('Network not initialized')
142e5bb279fSStephen Warren
143e5bb279fSStephen Warren    f = u_boot_console.config.env.get('env__net_tftp_readable_file', None)
144e5bb279fSStephen Warren    if not f:
145e5bb279fSStephen Warren        pytest.skip('No TFTP readable file to read')
146e5bb279fSStephen Warren
1473ba1352bSMichal Simek    addr = f.get('addr', None)
1483ba1352bSMichal Simek
149e5bb279fSStephen Warren    fn = f['fn']
150*b1b1bab7SHeinrich Schuchardt    if not addr:
151*b1b1bab7SHeinrich Schuchardt        output = u_boot_console.run_command('tftpboot %s' % (fn))
152*b1b1bab7SHeinrich Schuchardt    else:
153e5bb279fSStephen Warren        output = u_boot_console.run_command('tftpboot %x %s' % (addr, fn))
154e5bb279fSStephen Warren    expected_text = 'Bytes transferred = '
155e5bb279fSStephen Warren    sz = f.get('size', None)
156e5bb279fSStephen Warren    if sz:
157e5bb279fSStephen Warren        expected_text += '%d' % sz
158e5bb279fSStephen Warren    assert expected_text in output
159e5bb279fSStephen Warren
160e5bb279fSStephen Warren    expected_crc = f.get('crc32', None)
161e5bb279fSStephen Warren    if not expected_crc:
162e5bb279fSStephen Warren        return
163e5bb279fSStephen Warren
164e5bb279fSStephen Warren    if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
165e5bb279fSStephen Warren        return
166e5bb279fSStephen Warren
167*b1b1bab7SHeinrich Schuchardt    output = u_boot_console.run_command('crc32 $fileaddr $filesize')
168e5bb279fSStephen Warren    assert expected_crc in output
1696a2981a7SGuillaume GARDET
1706a2981a7SGuillaume GARDET@pytest.mark.buildconfigspec('cmd_nfs')
1716a2981a7SGuillaume GARDETdef test_net_nfs(u_boot_console):
1726a2981a7SGuillaume GARDET    """Test the nfs command.
1736a2981a7SGuillaume GARDET
1746a2981a7SGuillaume GARDET    A file is downloaded from the NFS server, its size and optionally its
1756a2981a7SGuillaume GARDET    CRC32 are validated.
1766a2981a7SGuillaume GARDET
1776a2981a7SGuillaume GARDET    The details of the file to download are provided by the boardenv_* file;
1786a2981a7SGuillaume GARDET    see the comment at the beginning of this file.
1796a2981a7SGuillaume GARDET    """
1806a2981a7SGuillaume GARDET
1816a2981a7SGuillaume GARDET    if not net_set_up:
1826a2981a7SGuillaume GARDET        pytest.skip('Network not initialized')
1836a2981a7SGuillaume GARDET
1846a2981a7SGuillaume GARDET    f = u_boot_console.config.env.get('env__net_nfs_readable_file', None)
1856a2981a7SGuillaume GARDET    if not f:
1866a2981a7SGuillaume GARDET        pytest.skip('No NFS readable file to read')
1876a2981a7SGuillaume GARDET
1886a2981a7SGuillaume GARDET    addr = f.get('addr', None)
1896a2981a7SGuillaume GARDET    if not addr:
190f4eef40bSQuentin Schulz        addr = u_boot_utils.find_ram_base(u_boot_console)
1916a2981a7SGuillaume GARDET
1926a2981a7SGuillaume GARDET    fn = f['fn']
1936a2981a7SGuillaume GARDET    output = u_boot_console.run_command('nfs %x %s' % (addr, fn))
1946a2981a7SGuillaume GARDET    expected_text = 'Bytes transferred = '
1956a2981a7SGuillaume GARDET    sz = f.get('size', None)
1966a2981a7SGuillaume GARDET    if sz:
1976a2981a7SGuillaume GARDET        expected_text += '%d' % sz
1986a2981a7SGuillaume GARDET    assert expected_text in output
1996a2981a7SGuillaume GARDET
2006a2981a7SGuillaume GARDET    expected_crc = f.get('crc32', None)
2016a2981a7SGuillaume GARDET    if not expected_crc:
2026a2981a7SGuillaume GARDET        return
2036a2981a7SGuillaume GARDET
2046a2981a7SGuillaume GARDET    if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
2056a2981a7SGuillaume GARDET        return
2066a2981a7SGuillaume GARDET
2076a2981a7SGuillaume GARDET    output = u_boot_console.run_command('crc32 %x $filesize' % addr)
2086a2981a7SGuillaume GARDET    assert expected_crc in output
209