test_net.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. # SPDX-License-Identifier: GPL-2.0
  2. # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
  3. # Test various network-related functionality, such as the dhcp, ping, and
  4. # tftpboot commands.
  5. import pytest
  6. import u_boot_utils
  7. """
  8. Note: This test relies on boardenv_* containing configuration values to define
  9. which network environment is available for testing. Without this, this test
  10. will be automatically skipped.
  11. For example:
  12. # Boolean indicating whether the Ethernet device is attached to USB, and hence
  13. # USB enumeration needs to be performed prior to network tests.
  14. # This variable may be omitted if its value is False.
  15. env__net_uses_usb = False
  16. # Boolean indicating whether the Ethernet device is attached to PCI, and hence
  17. # PCI enumeration needs to be performed prior to network tests.
  18. # This variable may be omitted if its value is False.
  19. env__net_uses_pci = True
  20. # True if a DHCP server is attached to the network, and should be tested.
  21. # If DHCP testing is not possible or desired, this variable may be omitted or
  22. # set to False.
  23. env__net_dhcp_server = True
  24. # True if a DHCPv6 server is attached to the network, and should be tested.
  25. # If DHCPv6 testing is not possible or desired, this variable may be omitted or
  26. # set to False.
  27. env__net_dhcp6_server = True
  28. # A list of environment variables that should be set in order to configure a
  29. # static IP. If solely relying on DHCP, this variable may be omitted or set to
  30. # an empty list.
  31. env__net_static_env_vars = [
  32. ('ipaddr', '10.0.0.100'),
  33. ('netmask', '255.255.255.0'),
  34. ('serverip', '10.0.0.1'),
  35. ]
  36. # Details regarding a file that may be read from a TFTP server. This variable
  37. # may be omitted or set to None if TFTP testing is not possible or desired.
  38. env__net_tftp_readable_file = {
  39. 'fn': 'ubtest-readable.bin',
  40. 'addr': 0x10000000,
  41. 'size': 5058624,
  42. 'crc32': 'c2244b26',
  43. }
  44. # Details regarding a file that may be read from a NFS server. This variable
  45. # may be omitted or set to None if NFS testing is not possible or desired.
  46. env__net_nfs_readable_file = {
  47. 'fn': 'ubtest-readable.bin',
  48. 'addr': 0x10000000,
  49. 'size': 5058624,
  50. 'crc32': 'c2244b26',
  51. }
  52. # True if a router advertisement service is connected to the network, and should
  53. # be tested. If router advertisement testing is not possible or desired, this
  54. variable may be omitted or set to False.
  55. env__router_on_net = True
  56. """
  57. net_set_up = False
  58. net6_set_up = False
  59. def test_net_pre_commands(u_boot_console):
  60. """Execute any commands required to enable network hardware.
  61. These commands are provided by the boardenv_* file; see the comment at the
  62. beginning of this file.
  63. """
  64. init_usb = u_boot_console.config.env.get('env__net_uses_usb', False)
  65. if init_usb:
  66. u_boot_console.run_command('usb start')
  67. init_pci = u_boot_console.config.env.get('env__net_uses_pci', False)
  68. if init_pci:
  69. u_boot_console.run_command('pci enum')
  70. @pytest.mark.buildconfigspec('cmd_dhcp')
  71. def test_net_dhcp(u_boot_console):
  72. """Test the dhcp command.
  73. The boardenv_* file may be used to enable/disable this test; see the
  74. comment at the beginning of this file.
  75. """
  76. test_dhcp = u_boot_console.config.env.get('env__net_dhcp_server', False)
  77. if not test_dhcp:
  78. pytest.skip('No DHCP server available')
  79. u_boot_console.run_command('setenv autoload no')
  80. output = u_boot_console.run_command('dhcp')
  81. assert 'DHCP client bound to address ' in output
  82. global net_set_up
  83. net_set_up = True
  84. @pytest.mark.buildconfigspec('cmd_dhcp6')
  85. def test_net_dhcp6(u_boot_console):
  86. """Test the dhcp6 command.
  87. The boardenv_* file may be used to enable/disable this test; see the
  88. comment at the beginning of this file.
  89. """
  90. test_dhcp6 = u_boot_console.config.env.get('env__net_dhcp6_server', False)
  91. if not test_dhcp6:
  92. pytest.skip('No DHCP6 server available')
  93. u_boot_console.run_command('setenv autoload no')
  94. output = u_boot_console.run_command('dhcp6')
  95. assert 'DHCP6 client bound to ' in output
  96. global net6_set_up
  97. net6_set_up = True
  98. @pytest.mark.buildconfigspec('net')
  99. def test_net_setup_static(u_boot_console):
  100. """Set up a static IP configuration.
  101. The configuration is provided by the boardenv_* file; see the comment at
  102. the beginning of this file.
  103. """
  104. env_vars = u_boot_console.config.env.get('env__net_static_env_vars', None)
  105. if not env_vars:
  106. pytest.skip('No static network configuration is defined')
  107. for (var, val) in env_vars:
  108. u_boot_console.run_command('setenv %s %s' % (var, val))
  109. global net_set_up
  110. net_set_up = True
  111. @pytest.mark.buildconfigspec('cmd_ping')
  112. def test_net_ping(u_boot_console):
  113. """Test the ping command.
  114. The $serverip (as set up by either test_net_dhcp or test_net_setup_static)
  115. is pinged. The test validates that the host is alive, as reported by the
  116. ping command's output.
  117. """
  118. if not net_set_up:
  119. pytest.skip('Network not initialized')
  120. output = u_boot_console.run_command('ping $serverip')
  121. assert 'is alive' in output
  122. @pytest.mark.buildconfigspec('IPV6_ROUTER_DISCOVERY')
  123. def test_net_network_discovery(u_boot_console):
  124. """Test the network discovery feature of IPv6.
  125. An IPv6 network command (ping6 in this case) is run to make U-Boot send a
  126. router solicitation packet, receive a router advertisement message, and
  127. parse it.
  128. A router advertisement service needs to be running for this test to succeed.
  129. U-Boot receives the RA, processes it, and if successful, assigns the gateway
  130. IP and prefix length.
  131. The configuration is provided by the boardenv_* file; see the comment at
  132. the beginning of this file.
  133. """
  134. router_on_net = u_boot_console.config.env.get('env__router_on_net', False)
  135. if not router_on_net:
  136. pytest.skip('No router on network')
  137. fake_host_ip = 'fe80::215:5dff:fef6:2ec6'
  138. output = u_boot_console.run_command('ping6 ' + fake_host_ip)
  139. assert 'ROUTER SOLICITATION 1' in output
  140. assert 'Set gatewayip6:' in output
  141. assert '0000:0000:0000:0000:0000:0000:0000:0000' not in output
  142. @pytest.mark.buildconfigspec('cmd_net')
  143. def test_net_tftpboot(u_boot_console):
  144. """Test the tftpboot command.
  145. A file is downloaded from the TFTP server, its size and optionally its
  146. CRC32 are validated.
  147. The details of the file to download are provided by the boardenv_* file;
  148. see the comment at the beginning of this file.
  149. """
  150. if not net_set_up:
  151. pytest.skip('Network not initialized')
  152. f = u_boot_console.config.env.get('env__net_tftp_readable_file', None)
  153. if not f:
  154. pytest.skip('No TFTP readable file to read')
  155. addr = f.get('addr', None)
  156. fn = f['fn']
  157. if not addr:
  158. output = u_boot_console.run_command('tftpboot %s' % (fn))
  159. else:
  160. output = u_boot_console.run_command('tftpboot %x %s' % (addr, fn))
  161. expected_text = 'Bytes transferred = '
  162. sz = f.get('size', None)
  163. if sz:
  164. expected_text += '%d' % sz
  165. assert expected_text in output
  166. expected_crc = f.get('crc32', None)
  167. if not expected_crc:
  168. return
  169. if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
  170. return
  171. output = u_boot_console.run_command('crc32 $fileaddr $filesize')
  172. assert expected_crc in output
  173. @pytest.mark.buildconfigspec('cmd_nfs')
  174. def test_net_nfs(u_boot_console):
  175. """Test the nfs command.
  176. A file is downloaded from the NFS server, its size and optionally its
  177. CRC32 are validated.
  178. The details of the file to download are provided by the boardenv_* file;
  179. see the comment at the beginning of this file.
  180. """
  181. if not net_set_up:
  182. pytest.skip('Network not initialized')
  183. f = u_boot_console.config.env.get('env__net_nfs_readable_file', None)
  184. if not f:
  185. pytest.skip('No NFS readable file to read')
  186. addr = f.get('addr', None)
  187. if not addr:
  188. addr = u_boot_utils.find_ram_base(u_boot_console)
  189. fn = f['fn']
  190. output = u_boot_console.run_command('nfs %x %s' % (addr, fn))
  191. expected_text = 'Bytes transferred = '
  192. sz = f.get('size', None)
  193. if sz:
  194. expected_text += '%d' % sz
  195. assert expected_text in output
  196. expected_crc = f.get('crc32', None)
  197. if not expected_crc:
  198. return
  199. if u_boot_console.config.buildconfig.get('config_cmd_crc32', 'n') != 'y':
  200. return
  201. output = u_boot_console.run_command('crc32 %x $filesize' % addr)
  202. assert expected_crc in output