test_net.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 the network environment 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. # A list of environment variables that should be set in order to configure a
  25. # static IP. If solely relying on DHCP, this variable may be omitted or set to
  26. # an empty list.
  27. env__net_static_env_vars = [
  28. ('ipaddr', '10.0.0.100'),
  29. ('netmask', '255.255.255.0'),
  30. ('serverip', '10.0.0.1'),
  31. ]
  32. # Details regarding a file that may be read from a TFTP server. This variable
  33. # may be omitted or set to None if TFTP testing is not possible or desired.
  34. env__net_tftp_readable_file = {
  35. 'fn': 'ubtest-readable.bin',
  36. 'addr': 0x10000000,
  37. 'size': 5058624,
  38. 'crc32': 'c2244b26',
  39. }
  40. # Details regarding a file that may be read from a NFS server. This variable
  41. # may be omitted or set to None if NFS testing is not possible or desired.
  42. env__net_nfs_readable_file = {
  43. 'fn': 'ubtest-readable.bin',
  44. 'addr': 0x10000000,
  45. 'size': 5058624,
  46. 'crc32': 'c2244b26',
  47. }
  48. """
  49. net_set_up = False
  50. def test_net_pre_commands(u_boot_console):
  51. """Execute any commands required to enable network hardware.
  52. These commands are provided by the boardenv_* file; see the comment at the
  53. beginning of this file.
  54. """
  55. init_usb = u_boot_console.config.env.get('env__net_uses_usb', False)
  56. if init_usb:
  57. u_boot_console.run_command('usb start')
  58. init_pci = u_boot_console.config.env.get('env__net_uses_pci', False)
  59. if init_pci:
  60. u_boot_console.run_command('pci enum')
  61. @pytest.mark.buildconfigspec('cmd_dhcp')
  62. def test_net_dhcp(u_boot_console):
  63. """Test the dhcp command.
  64. The boardenv_* file may be used to enable/disable this test; see the
  65. comment at the beginning of this file.
  66. """
  67. test_dhcp = u_boot_console.config.env.get('env__net_dhcp_server', False)
  68. if not test_dhcp:
  69. pytest.skip('No DHCP server available')
  70. u_boot_console.run_command('setenv autoload no')
  71. output = u_boot_console.run_command('dhcp')
  72. assert 'DHCP client bound to address ' in output
  73. global net_set_up
  74. net_set_up = True
  75. @pytest.mark.buildconfigspec('net')
  76. def test_net_setup_static(u_boot_console):
  77. """Set up a static IP configuration.
  78. The configuration is provided by the boardenv_* file; see the comment at
  79. the beginning of this file.
  80. """
  81. env_vars = u_boot_console.config.env.get('env__net_static_env_vars', None)
  82. if not env_vars:
  83. pytest.skip('No static network configuration is defined')
  84. for (var, val) in env_vars:
  85. u_boot_console.run_command('setenv %s %s' % (var, val))
  86. global net_set_up
  87. net_set_up = True
  88. @pytest.mark.buildconfigspec('cmd_ping')
  89. def test_net_ping(u_boot_console):
  90. """Test the ping command.
  91. The $serverip (as set up by either test_net_dhcp or test_net_setup_static)
  92. is pinged. The test validates that the host is alive, as reported by the
  93. ping command's output.
  94. """
  95. if not net_set_up:
  96. pytest.skip('Network not initialized')
  97. output = u_boot_console.run_command('ping $serverip')
  98. assert 'is alive' in output
  99. @pytest.mark.buildconfigspec('cmd_net')
  100. def test_net_tftpboot(u_boot_console):
  101. """Test the tftpboot command.
  102. A file is downloaded from the TFTP server, its size and optionally its
  103. CRC32 are validated.
  104. The details of the file to download are provided by the boardenv_* file;
  105. see the comment at the beginning of this file.
  106. """
  107. if not net_set_up:
  108. pytest.skip('Network not initialized')
  109. f = u_boot_console.config.env.get('env__net_tftp_readable_file', None)
  110. if not f:
  111. pytest.skip('No TFTP readable file to read')
  112. addr = f.get('addr', None)
  113. fn = f['fn']
  114. if not addr:
  115. output = u_boot_console.run_command('tftpboot %s' % (fn))
  116. else:
  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 $fileaddr $filesize')
  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)
  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