test_net.py 4.8 KB

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