test_net.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
  2. #
  3. # SPDX-License-Identifier: GPL-2.0
  4. # Test various network-related functionality, such as the dhcp, ping, and
  5. # tftpboot commands.
  6. import pytest
  7. import u_boot_utils
  8. '''
  9. Note: This test relies on boardenv_* containing configuration values to define
  10. which the network environment available for testing. Without this, this test
  11. will be automatically skipped.
  12. For example:
  13. # Any commands that need to be executed prior to testing,
  14. # to get the network hardware into an operational state.
  15. #
  16. # If no commands are required, this variable may be omitted, or set to an
  17. # empty list.
  18. env__net_pre_commands = [
  19. "usb start",
  20. ]
  21. # True if a DHCP server is attached to the network, and should be tested.
  22. # If DHCP testing is not possible or desired, this variable may be omitted or
  23. # set to False.
  24. env__net_dhcp_server = True
  25. # A list of environment variables that should be set in order to configure a
  26. # static IP. If solely relying on DHCP, this variable may be omitted or set to
  27. # an empty list.
  28. env__net_static_env_vars = [
  29. ("ipaddr", "10.0.0.100"),
  30. ("netmask", "255.255.255.0"),
  31. ("serverip", "10.0.0.1"),
  32. ]
  33. # Details regarding a file that may be read from a TFTP server. This variable
  34. # may be omitted or set to None if TFTP testing is not possible or desired.
  35. env__net_tftp_readable_file = {
  36. "fn": "ubtest-readable.bin",
  37. "size": 5058624,
  38. "crc32": "c2244b26",
  39. }
  40. '''
  41. net_set_up = False
  42. def test_net_pre_commands(u_boot_console):
  43. '''Execute any commands required to enable network hardware.
  44. These commands are provided by the boardenv_* file; see the comment at the
  45. beginning of this file.
  46. '''
  47. cmds = u_boot_console.config.env.get('env__net_pre_commands', None)
  48. if not cmds:
  49. pytest.skip('No network pre-commands defined')
  50. for cmd in cmds:
  51. u_boot_console.run_command(cmd)
  52. @pytest.mark.buildconfigspec('cmd_dhcp')
  53. def test_net_dhcp(u_boot_console):
  54. '''Test the dhcp command.
  55. The boardenv_* file may be used to enable/disable this test; see the
  56. comment at the beginning of this file.
  57. '''
  58. test_dhcp = u_boot_console.config.env.get('env__net_dhcp_server', False)
  59. if not test_dhcp:
  60. pytest.skip('No DHCP server available')
  61. u_boot_console.run_command('setenv autoload no')
  62. output = u_boot_console.run_command('dhcp')
  63. assert 'DHCP client bound to address ' in output
  64. global net_set_up
  65. net_set_up = True
  66. @pytest.mark.buildconfigspec('net')
  67. def test_net_setup_static(u_boot_console):
  68. '''Set up a static IP configuration.
  69. The configuration is provided by the boardenv_* file; see the comment at
  70. the beginning of this file.
  71. '''
  72. env_vars = u_boot_console.config.env.get('env__net_static_env_vars', None)
  73. if not env_vars:
  74. pytest.skip('No static network configuration is defined')
  75. for (var, val) in env_vars:
  76. u_boot_console.run_command('setenv %s %s' % (var, val))
  77. global net_set_up
  78. net_set_up = True
  79. @pytest.mark.buildconfigspec('cmd_ping')
  80. def test_net_ping(u_boot_console):
  81. '''Test the ping command.
  82. The $serverip (as set up by either test_net_dhcp or test_net_setup_static)
  83. is pinged. The test validates that the host is alive, as reported by the
  84. ping command's output.
  85. '''
  86. if not net_set_up:
  87. pytest.skip("Network not initialized")
  88. output = u_boot_console.run_command('ping $serverip')
  89. assert 'is alive' in output
  90. @pytest.mark.buildconfigspec('cmd_net')
  91. def test_net_tftpboot(u_boot_console):
  92. '''Test the tftpboot command.
  93. A file is downloaded from the TFTP server, its size and optionally its
  94. CRC32 are validated.
  95. The details of the file to download are provided by the boardenv_* file;
  96. see the comment at the beginning of this file.
  97. '''
  98. if not net_set_up:
  99. pytest.skip("Network not initialized")
  100. f = u_boot_console.config.env.get('env__net_tftp_readable_file', None)
  101. if not f:
  102. pytest.skip('No TFTP readable file to read')
  103. addr = u_boot_utils.find_ram_base(u_boot_console)
  104. fn = f['fn']
  105. output = u_boot_console.run_command('tftpboot %x %s' % (addr, fn))
  106. expected_text = 'Bytes transferred = '
  107. sz = f.get('size', None)
  108. if sz:
  109. expected_text += '%d' % sz
  110. assert expected_text in output
  111. expected_crc = f.get('crc32', None)
  112. if not expected_crc:
  113. return
  114. if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
  115. return
  116. output = u_boot_console.run_command('crc32 %x $filesize' % addr)
  117. assert expected_crc in output