test_net.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. # Boolean indicating whether the Ethernet device is attached to USB, and hence
  14. # USB enumeration needs to be performed prior to network tests.
  15. # This variable may be omitted if its value is False.
  16. env__net_uses_usb = False
  17. # Boolean indicating whether the Ethernet device is attached to PCI, and hence
  18. # PCI enumeration needs to be performed prior to network tests.
  19. # This variable may be omitted if its value is False.
  20. env__net_uses_pci = True
  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. "addr": 0x10000000,
  38. "size": 5058624,
  39. "crc32": "c2244b26",
  40. }
  41. """
  42. net_set_up = False
  43. def test_net_pre_commands(u_boot_console):
  44. """Execute any commands required to enable network hardware.
  45. These commands are provided by the boardenv_* file; see the comment at the
  46. beginning of this file.
  47. """
  48. init_usb = u_boot_console.config.env.get('env__net_uses_usb', False)
  49. if init_usb:
  50. u_boot_console.run_command('usb start')
  51. init_pci = u_boot_console.config.env.get('env__net_uses_pci', False)
  52. if init_pci:
  53. u_boot_console.run_command('pci enum')
  54. @pytest.mark.buildconfigspec('cmd_dhcp')
  55. def test_net_dhcp(u_boot_console):
  56. """Test the dhcp command.
  57. The boardenv_* file may be used to enable/disable this test; see the
  58. comment at the beginning of this file.
  59. """
  60. test_dhcp = u_boot_console.config.env.get('env__net_dhcp_server', False)
  61. if not test_dhcp:
  62. pytest.skip('No DHCP server available')
  63. u_boot_console.run_command('setenv autoload no')
  64. output = u_boot_console.run_command('dhcp')
  65. assert 'DHCP client bound to address ' in output
  66. global net_set_up
  67. net_set_up = True
  68. @pytest.mark.buildconfigspec('net')
  69. def test_net_setup_static(u_boot_console):
  70. """Set up a static IP configuration.
  71. The configuration is provided by the boardenv_* file; see the comment at
  72. the beginning of this file.
  73. """
  74. env_vars = u_boot_console.config.env.get('env__net_static_env_vars', None)
  75. if not env_vars:
  76. pytest.skip('No static network configuration is defined')
  77. for (var, val) in env_vars:
  78. u_boot_console.run_command('setenv %s %s' % (var, val))
  79. global net_set_up
  80. net_set_up = True
  81. @pytest.mark.buildconfigspec('cmd_ping')
  82. def test_net_ping(u_boot_console):
  83. """Test the ping command.
  84. The $serverip (as set up by either test_net_dhcp or test_net_setup_static)
  85. is pinged. The test validates that the host is alive, as reported by the
  86. ping command's output.
  87. """
  88. if not net_set_up:
  89. pytest.skip('Network not initialized')
  90. output = u_boot_console.run_command('ping $serverip')
  91. assert 'is alive' in output
  92. @pytest.mark.buildconfigspec('cmd_net')
  93. def test_net_tftpboot(u_boot_console):
  94. """Test the tftpboot command.
  95. A file is downloaded from the TFTP server, its size and optionally its
  96. CRC32 are validated.
  97. The details of the file to download are provided by the boardenv_* file;
  98. see the comment at the beginning of this file.
  99. """
  100. if not net_set_up:
  101. pytest.skip('Network not initialized')
  102. f = u_boot_console.config.env.get('env__net_tftp_readable_file', None)
  103. if not f:
  104. pytest.skip('No TFTP readable file to read')
  105. addr = f.get('addr', None)
  106. if not addr:
  107. addr = u_boot_utils.find_ram_base(u_boot_console)
  108. fn = f['fn']
  109. output = u_boot_console.run_command('tftpboot %x %s' % (addr, fn))
  110. expected_text = 'Bytes transferred = '
  111. sz = f.get('size', None)
  112. if sz:
  113. expected_text += '%d' % sz
  114. assert expected_text in output
  115. expected_crc = f.get('crc32', None)
  116. if not expected_crc:
  117. return
  118. if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
  119. return
  120. output = u_boot_console.run_command('crc32 %x $filesize' % addr)
  121. assert expected_crc in output