xref: /rk3399_rockchip-uboot/test/py/tests/test_net.py (revision 6a2981a7132c206e6099938fa0476301f4dfe3fd)
1e5bb279fSStephen Warren# Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
2e5bb279fSStephen Warren#
3e5bb279fSStephen Warren# SPDX-License-Identifier: GPL-2.0
4e5bb279fSStephen Warren
5e5bb279fSStephen Warren# Test various network-related functionality, such as the dhcp, ping, and
6e5bb279fSStephen Warren# tftpboot commands.
7e5bb279fSStephen Warren
8e5bb279fSStephen Warrenimport pytest
9e5bb279fSStephen Warrenimport u_boot_utils
10e5bb279fSStephen Warren
11e8debf39SStephen Warren"""
12e5bb279fSStephen WarrenNote: This test relies on boardenv_* containing configuration values to define
13e5bb279fSStephen Warrenwhich the network environment available for testing. Without this, this test
14e5bb279fSStephen Warrenwill be automatically skipped.
15e5bb279fSStephen Warren
16e5bb279fSStephen WarrenFor example:
17e5bb279fSStephen Warren
1856382a81SStephen Warren# Boolean indicating whether the Ethernet device is attached to USB, and hence
1956382a81SStephen Warren# USB enumeration needs to be performed prior to network tests.
2056382a81SStephen Warren# This variable may be omitted if its value is False.
2156382a81SStephen Warrenenv__net_uses_usb = False
2256382a81SStephen Warren
2356382a81SStephen Warren# Boolean indicating whether the Ethernet device is attached to PCI, and hence
2456382a81SStephen Warren# PCI enumeration needs to be performed prior to network tests.
2556382a81SStephen Warren# This variable may be omitted if its value is False.
2656382a81SStephen Warrenenv__net_uses_pci = True
27e5bb279fSStephen Warren
28e5bb279fSStephen Warren# True if a DHCP server is attached to the network, and should be tested.
29e5bb279fSStephen Warren# If DHCP testing is not possible or desired, this variable may be omitted or
30e5bb279fSStephen Warren# set to False.
31e5bb279fSStephen Warrenenv__net_dhcp_server = True
32e5bb279fSStephen Warren
33e5bb279fSStephen Warren# A list of environment variables that should be set in order to configure a
34e5bb279fSStephen Warren# static IP. If solely relying on DHCP, this variable may be omitted or set to
35e5bb279fSStephen Warren# an empty list.
36e5bb279fSStephen Warrenenv__net_static_env_vars = [
37e5bb279fSStephen Warren    ("ipaddr", "10.0.0.100"),
38e5bb279fSStephen Warren    ("netmask", "255.255.255.0"),
39e5bb279fSStephen Warren    ("serverip", "10.0.0.1"),
40e5bb279fSStephen Warren]
41e5bb279fSStephen Warren
42e5bb279fSStephen Warren# Details regarding a file that may be read from a TFTP server. This variable
43e5bb279fSStephen Warren# may be omitted or set to None if TFTP testing is not possible or desired.
44e5bb279fSStephen Warrenenv__net_tftp_readable_file = {
45e5bb279fSStephen Warren    "fn": "ubtest-readable.bin",
463ba1352bSMichal Simek    "addr": 0x10000000,
47e5bb279fSStephen Warren    "size": 5058624,
48e5bb279fSStephen Warren    "crc32": "c2244b26",
49e5bb279fSStephen Warren}
50*6a2981a7SGuillaume GARDET
51*6a2981a7SGuillaume GARDET# Details regarding a file that may be read from a NFS server. This variable
52*6a2981a7SGuillaume GARDET# may be omitted or set to None if NFS testing is not possible or desired.
53*6a2981a7SGuillaume GARDETenv__net_nfs_readable_file = {
54*6a2981a7SGuillaume GARDET    "fn": "ubtest-readable.bin",
55*6a2981a7SGuillaume GARDET    "addr": 0x10000000,
56*6a2981a7SGuillaume GARDET    "size": 5058624,
57*6a2981a7SGuillaume GARDET    "crc32": "c2244b26",
58*6a2981a7SGuillaume GARDET}
59e8debf39SStephen Warren"""
60e5bb279fSStephen Warren
61e5bb279fSStephen Warrennet_set_up = False
62e5bb279fSStephen Warren
63e5bb279fSStephen Warrendef test_net_pre_commands(u_boot_console):
64e8debf39SStephen Warren    """Execute any commands required to enable network hardware.
65e5bb279fSStephen Warren
66e5bb279fSStephen Warren    These commands are provided by the boardenv_* file; see the comment at the
67e5bb279fSStephen Warren    beginning of this file.
68e8debf39SStephen Warren    """
69e5bb279fSStephen Warren
7056382a81SStephen Warren    init_usb = u_boot_console.config.env.get('env__net_uses_usb', False)
7156382a81SStephen Warren    if init_usb:
7256382a81SStephen Warren        u_boot_console.run_command('usb start')
73e5bb279fSStephen Warren
7456382a81SStephen Warren    init_pci = u_boot_console.config.env.get('env__net_uses_pci', False)
7556382a81SStephen Warren    if init_pci:
7656382a81SStephen Warren        u_boot_console.run_command('pci enum')
77e5bb279fSStephen Warren
78e5bb279fSStephen Warren@pytest.mark.buildconfigspec('cmd_dhcp')
79e5bb279fSStephen Warrendef test_net_dhcp(u_boot_console):
80e8debf39SStephen Warren    """Test the dhcp command.
81e5bb279fSStephen Warren
82e5bb279fSStephen Warren    The boardenv_* file may be used to enable/disable this test; see the
83e5bb279fSStephen Warren    comment at the beginning of this file.
84e8debf39SStephen Warren    """
85e5bb279fSStephen Warren
86e5bb279fSStephen Warren    test_dhcp = u_boot_console.config.env.get('env__net_dhcp_server', False)
87e5bb279fSStephen Warren    if not test_dhcp:
88e5bb279fSStephen Warren        pytest.skip('No DHCP server available')
89e5bb279fSStephen Warren
90e5bb279fSStephen Warren    u_boot_console.run_command('setenv autoload no')
91e5bb279fSStephen Warren    output = u_boot_console.run_command('dhcp')
92e5bb279fSStephen Warren    assert 'DHCP client bound to address ' in output
93e5bb279fSStephen Warren
94e5bb279fSStephen Warren    global net_set_up
95e5bb279fSStephen Warren    net_set_up = True
96e5bb279fSStephen Warren
97e5bb279fSStephen Warren@pytest.mark.buildconfigspec('net')
98e5bb279fSStephen Warrendef test_net_setup_static(u_boot_console):
99e8debf39SStephen Warren    """Set up a static IP configuration.
100e5bb279fSStephen Warren
101e5bb279fSStephen Warren    The configuration is provided by the boardenv_* file; see the comment at
102e5bb279fSStephen Warren    the beginning of this file.
103e8debf39SStephen Warren    """
104e5bb279fSStephen Warren
105e5bb279fSStephen Warren    env_vars = u_boot_console.config.env.get('env__net_static_env_vars', None)
106e5bb279fSStephen Warren    if not env_vars:
107e5bb279fSStephen Warren        pytest.skip('No static network configuration is defined')
108e5bb279fSStephen Warren
109e5bb279fSStephen Warren    for (var, val) in env_vars:
110e5bb279fSStephen Warren        u_boot_console.run_command('setenv %s %s' % (var, val))
111e5bb279fSStephen Warren
112e5bb279fSStephen Warren    global net_set_up
113e5bb279fSStephen Warren    net_set_up = True
114e5bb279fSStephen Warren
115e5bb279fSStephen Warren@pytest.mark.buildconfigspec('cmd_ping')
116e5bb279fSStephen Warrendef test_net_ping(u_boot_console):
117e8debf39SStephen Warren    """Test the ping command.
118e5bb279fSStephen Warren
119e5bb279fSStephen Warren    The $serverip (as set up by either test_net_dhcp or test_net_setup_static)
120e5bb279fSStephen Warren    is pinged. The test validates that the host is alive, as reported by the
121e5bb279fSStephen Warren    ping command's output.
122e8debf39SStephen Warren    """
123e5bb279fSStephen Warren
124e5bb279fSStephen Warren    if not net_set_up:
125a2ec5606SStephen Warren        pytest.skip('Network not initialized')
126e5bb279fSStephen Warren
127e5bb279fSStephen Warren    output = u_boot_console.run_command('ping $serverip')
128e5bb279fSStephen Warren    assert 'is alive' in output
129e5bb279fSStephen Warren
130e5bb279fSStephen Warren@pytest.mark.buildconfigspec('cmd_net')
131e5bb279fSStephen Warrendef test_net_tftpboot(u_boot_console):
132e8debf39SStephen Warren    """Test the tftpboot command.
133e5bb279fSStephen Warren
134e5bb279fSStephen Warren    A file is downloaded from the TFTP server, its size and optionally its
135e5bb279fSStephen Warren    CRC32 are validated.
136e5bb279fSStephen Warren
137e5bb279fSStephen Warren    The details of the file to download are provided by the boardenv_* file;
138e5bb279fSStephen Warren    see the comment at the beginning of this file.
139e8debf39SStephen Warren    """
140e5bb279fSStephen Warren
141e5bb279fSStephen Warren    if not net_set_up:
142a2ec5606SStephen Warren        pytest.skip('Network not initialized')
143e5bb279fSStephen Warren
144e5bb279fSStephen Warren    f = u_boot_console.config.env.get('env__net_tftp_readable_file', None)
145e5bb279fSStephen Warren    if not f:
146e5bb279fSStephen Warren        pytest.skip('No TFTP readable file to read')
147e5bb279fSStephen Warren
1483ba1352bSMichal Simek    addr = f.get('addr', None)
1493ba1352bSMichal Simek    if not addr:
150e5bb279fSStephen Warren        addr = u_boot_utils.find_ram_base(u_boot_console)
1513ba1352bSMichal Simek
152e5bb279fSStephen Warren    fn = f['fn']
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
167e5bb279fSStephen Warren    output = u_boot_console.run_command('crc32 %x $filesize' % addr)
168e5bb279fSStephen Warren    assert expected_crc in output
169*6a2981a7SGuillaume GARDET
170*6a2981a7SGuillaume GARDET@pytest.mark.buildconfigspec('cmd_nfs')
171*6a2981a7SGuillaume GARDETdef test_net_nfs(u_boot_console):
172*6a2981a7SGuillaume GARDET    """Test the nfs command.
173*6a2981a7SGuillaume GARDET
174*6a2981a7SGuillaume GARDET    A file is downloaded from the NFS server, its size and optionally its
175*6a2981a7SGuillaume GARDET    CRC32 are validated.
176*6a2981a7SGuillaume GARDET
177*6a2981a7SGuillaume GARDET    The details of the file to download are provided by the boardenv_* file;
178*6a2981a7SGuillaume GARDET    see the comment at the beginning of this file.
179*6a2981a7SGuillaume GARDET    """
180*6a2981a7SGuillaume GARDET
181*6a2981a7SGuillaume GARDET    if not net_set_up:
182*6a2981a7SGuillaume GARDET        pytest.skip('Network not initialized')
183*6a2981a7SGuillaume GARDET
184*6a2981a7SGuillaume GARDET    f = u_boot_console.config.env.get('env__net_nfs_readable_file', None)
185*6a2981a7SGuillaume GARDET    if not f:
186*6a2981a7SGuillaume GARDET        pytest.skip('No NFS readable file to read')
187*6a2981a7SGuillaume GARDET
188*6a2981a7SGuillaume GARDET    addr = f.get('addr', None)
189*6a2981a7SGuillaume GARDET    if not addr:
190*6a2981a7SGuillaume GARDET        addr = u_boot_utils.find_ram_base(u_boot_console)
191*6a2981a7SGuillaume GARDET
192*6a2981a7SGuillaume GARDET    fn = f['fn']
193*6a2981a7SGuillaume GARDET    output = u_boot_console.run_command('nfs %x %s' % (addr, fn))
194*6a2981a7SGuillaume GARDET    expected_text = 'Bytes transferred = '
195*6a2981a7SGuillaume GARDET    sz = f.get('size', None)
196*6a2981a7SGuillaume GARDET    if sz:
197*6a2981a7SGuillaume GARDET        expected_text += '%d' % sz
198*6a2981a7SGuillaume GARDET    assert expected_text in output
199*6a2981a7SGuillaume GARDET
200*6a2981a7SGuillaume GARDET    expected_crc = f.get('crc32', None)
201*6a2981a7SGuillaume GARDET    if not expected_crc:
202*6a2981a7SGuillaume GARDET        return
203*6a2981a7SGuillaume GARDET
204*6a2981a7SGuillaume GARDET    if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
205*6a2981a7SGuillaume GARDET        return
206*6a2981a7SGuillaume GARDET
207*6a2981a7SGuillaume GARDET    output = u_boot_console.run_command('crc32 %x $filesize' % addr)
208*6a2981a7SGuillaume GARDET    assert expected_crc in output
209