test_net.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. # Details regarding a file that may be read from a NFS server. This variable
  42. # may be omitted or set to None if NFS testing is not possible or desired.
  43. env__net_nfs_readable_file = {
  44. "fn": "ubtest-readable.bin",
  45. "addr": 0x10000000,
  46. "size": 5058624,
  47. "crc32": "c2244b26",
  48. }
  49. """
  50. net_set_up = False
  51. def test_net_pre_commands(u_boot_console):
  52. """Execute any commands required to enable network hardware.
  53. These commands are provided by the boardenv_* file; see the comment at the
  54. beginning of this file.
  55. """
  56. init_usb = u_boot_console.config.env.get('env__net_uses_usb', False)
  57. if init_usb:
  58. u_boot_console.run_command('usb start')
  59. init_pci = u_boot_console.config.env.get('env__net_uses_pci', False)
  60. if init_pci:
  61. u_boot_console.run_command('pci enum')
  62. @pytest.mark.buildconfigspec('cmd_dhcp')
  63. def test_net_dhcp(u_boot_console):
  64. """Test the dhcp command.
  65. The boardenv_* file may be used to enable/disable this test; see the
  66. comment at the beginning of this file.
  67. """
  68. test_dhcp = u_boot_console.config.env.get('env__net_dhcp_server', False)
  69. if not test_dhcp:
  70. pytest.skip('No DHCP server available')
  71. u_boot_console.run_command('setenv autoload no')
  72. output = u_boot_console.run_command('dhcp')
  73. assert 'DHCP client bound to address ' in output
  74. global net_set_up
  75. net_set_up = True
  76. @pytest.mark.buildconfigspec('net')
  77. def test_net_setup_static(u_boot_console):
  78. """Set up a static IP configuration.
  79. The configuration is provided by the boardenv_* file; see the comment at
  80. the beginning of this file.
  81. """
  82. env_vars = u_boot_console.config.env.get('env__net_static_env_vars', None)
  83. if not env_vars:
  84. pytest.skip('No static network configuration is defined')
  85. for (var, val) in env_vars:
  86. u_boot_console.run_command('setenv %s %s' % (var, val))
  87. global net_set_up
  88. net_set_up = True
  89. @pytest.mark.buildconfigspec('cmd_ping')
  90. def test_net_ping(u_boot_console):
  91. """Test the ping command.
  92. The $serverip (as set up by either test_net_dhcp or test_net_setup_static)
  93. is pinged. The test validates that the host is alive, as reported by the
  94. ping command's output.
  95. """
  96. if not net_set_up:
  97. pytest.skip('Network not initialized')
  98. output = u_boot_console.run_command('ping $serverip')
  99. assert 'is alive' in output
  100. @pytest.mark.buildconfigspec('cmd_net')
  101. def test_net_tftpboot(u_boot_console):
  102. """Test the tftpboot command.
  103. A file is downloaded from the TFTP server, its size and optionally its
  104. CRC32 are validated.
  105. The details of the file to download are provided by the boardenv_* file;
  106. see the comment at the beginning of this file.
  107. """
  108. if not net_set_up:
  109. pytest.skip('Network not initialized')
  110. f = u_boot_console.config.env.get('env__net_tftp_readable_file', None)
  111. if not f:
  112. pytest.skip('No TFTP readable file to read')
  113. addr = f.get('addr', None)
  114. if not addr:
  115. addr = u_boot_utils.find_ram_base(u_boot_console) + (1024 * 1024 * 4)
  116. fn = f['fn']
  117. output = u_boot_console.run_command('tftpboot %x %s' % (addr, fn))
  118. expected_text = 'Bytes transferred = '
  119. sz = f.get('size', None)
  120. if sz:
  121. expected_text += '%d' % sz
  122. assert expected_text in output
  123. expected_crc = f.get('crc32', None)
  124. if not expected_crc:
  125. return
  126. if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
  127. return
  128. output = u_boot_console.run_command('crc32 %x $filesize' % addr)
  129. assert expected_crc in output
  130. @pytest.mark.buildconfigspec('cmd_nfs')
  131. def test_net_nfs(u_boot_console):
  132. """Test the nfs command.
  133. A file is downloaded from the NFS server, its size and optionally its
  134. CRC32 are validated.
  135. The details of the file to download are provided by the boardenv_* file;
  136. see the comment at the beginning of this file.
  137. """
  138. if not net_set_up:
  139. pytest.skip('Network not initialized')
  140. f = u_boot_console.config.env.get('env__net_nfs_readable_file', None)
  141. if not f:
  142. pytest.skip('No NFS readable file to read')
  143. addr = f.get('addr', None)
  144. if not addr:
  145. addr = u_boot_utils.find_ram_base(u_boot_console) + (1024 * 1024 * 4)
  146. fn = f['fn']
  147. output = u_boot_console.run_command('nfs %x %s' % (addr, fn))
  148. expected_text = 'Bytes transferred = '
  149. sz = f.get('size', None)
  150. if sz:
  151. expected_text += '%d' % sz
  152. assert expected_text in output
  153. expected_crc = f.get('crc32', None)
  154. if not expected_crc:
  155. return
  156. if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
  157. return
  158. output = u_boot_console.run_command('crc32 %x $filesize' % addr)
  159. assert expected_crc in output