test_hush_if_test.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # SPDX-License-Identifier: GPL-2.0
  2. # Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
  3. # Test operation of the "if" shell command.
  4. import os
  5. import os.path
  6. import pytest
  7. # TODO: These tests should be converted to a C test.
  8. # For more information please take a look at the thread
  9. # https://lists.denx.de/pipermail/u-boot/2019-October/388732.html
  10. pytestmark = pytest.mark.buildconfigspec('hush_parser')
  11. # The list of "if test" conditions to test.
  12. subtests = (
  13. # Base if functionality.
  14. ('true', True),
  15. ('false', False),
  16. # Basic operators.
  17. ('test aaa = aaa', True),
  18. ('test aaa = bbb', False),
  19. ('test aaa != bbb', True),
  20. ('test aaa != aaa', False),
  21. ('test aaa < bbb', True),
  22. ('test bbb < aaa', False),
  23. ('test bbb > aaa', True),
  24. ('test aaa > bbb', False),
  25. ('test 123 -eq 123', True),
  26. ('test 123 -eq 456', False),
  27. ('test 123 -ne 456', True),
  28. ('test 123 -ne 123', False),
  29. ('test 123 -lt 456', True),
  30. ('test 123 -lt 123', False),
  31. ('test 456 -lt 123', False),
  32. ('test 123 -le 456', True),
  33. ('test 123 -le 123', True),
  34. ('test 456 -le 123', False),
  35. ('test 456 -gt 123', True),
  36. ('test 123 -gt 123', False),
  37. ('test 123 -gt 456', False),
  38. ('test 456 -ge 123', True),
  39. ('test 123 -ge 123', True),
  40. ('test 123 -ge 456', False),
  41. # Octal tests
  42. ('test 010 -eq 010', True),
  43. ('test 010 -eq 011', False),
  44. ('test 010 -ne 011', True),
  45. ('test 010 -ne 010', False),
  46. # Hexadecimal tests
  47. ('test 0x2000000 -gt 0x2000001', False),
  48. ('test 0x2000000 -gt 0x2000000', False),
  49. ('test 0x2000000 -gt 0x1ffffff', True),
  50. # Mixed tests
  51. ('test 010 -eq 10', False),
  52. ('test 010 -ne 10', True),
  53. ('test 0xa -eq 10', True),
  54. ('test 0xa -eq 012', True),
  55. ('test 2000000 -gt 0x1ffffff', False),
  56. ('test 0x2000000 -gt 1ffffff', True),
  57. ('test 0x2000000 -lt 1ffffff', False),
  58. ('test 0x2000000 -eq 2000000', False),
  59. ('test 0x2000000 -ne 2000000', True),
  60. ('test -z ""', True),
  61. ('test -z "aaa"', False),
  62. ('test -n "aaa"', True),
  63. ('test -n ""', False),
  64. # Inversion of simple tests.
  65. ('test ! aaa = aaa', False),
  66. ('test ! aaa = bbb', True),
  67. ('test ! ! aaa = aaa', True),
  68. ('test ! ! aaa = bbb', False),
  69. # Binary operators.
  70. ('test aaa != aaa -o bbb != bbb', False),
  71. ('test aaa != aaa -o bbb = bbb', True),
  72. ('test aaa = aaa -o bbb != bbb', True),
  73. ('test aaa = aaa -o bbb = bbb', True),
  74. ('test aaa != aaa -a bbb != bbb', False),
  75. ('test aaa != aaa -a bbb = bbb', False),
  76. ('test aaa = aaa -a bbb != bbb', False),
  77. ('test aaa = aaa -a bbb = bbb', True),
  78. # Inversion within binary operators.
  79. ('test ! aaa != aaa -o ! bbb != bbb', True),
  80. ('test ! aaa != aaa -o ! bbb = bbb', True),
  81. ('test ! aaa = aaa -o ! bbb != bbb', True),
  82. ('test ! aaa = aaa -o ! bbb = bbb', False),
  83. ('test ! ! aaa != aaa -o ! ! bbb != bbb', False),
  84. ('test ! ! aaa != aaa -o ! ! bbb = bbb', True),
  85. ('test ! ! aaa = aaa -o ! ! bbb != bbb', True),
  86. ('test ! ! aaa = aaa -o ! ! bbb = bbb', True),
  87. # -z operator.
  88. ('test -z "$ut_var_nonexistent"', True),
  89. ('test -z "$ut_var_exists"', False),
  90. )
  91. def exec_hush_if(u_boot_console, expr, result):
  92. """Execute a shell "if" command, and validate its result."""
  93. config = u_boot_console.config.buildconfig
  94. maxargs = int(config.get('config_sys_maxargs', '0'))
  95. args = len(expr.split(' ')) - 1
  96. if args > maxargs:
  97. u_boot_console.log.warning('CONFIG_SYS_MAXARGS too low; need ' +
  98. str(args))
  99. pytest.skip()
  100. cmd = 'if ' + expr + '; then echo true; else echo false; fi'
  101. response = u_boot_console.run_command(cmd)
  102. assert response.strip() == str(result).lower()
  103. def test_hush_if_test_setup(u_boot_console):
  104. """Set up environment variables used during the "if" tests."""
  105. u_boot_console.run_command('setenv ut_var_nonexistent')
  106. u_boot_console.run_command('setenv ut_var_exists 1')
  107. @pytest.mark.buildconfigspec('cmd_echo')
  108. @pytest.mark.parametrize('expr,result', subtests)
  109. def test_hush_if_test(u_boot_console, expr, result):
  110. """Test a single "if test" condition."""
  111. exec_hush_if(u_boot_console, expr, result)
  112. def test_hush_if_test_teardown(u_boot_console):
  113. """Clean up environment variables used during the "if" tests."""
  114. u_boot_console.run_command('setenv ut_var_exists')
  115. # We might test this on real filesystems via UMS, DFU, 'save', etc.
  116. # Of those, only UMS currently allows file removal though.
  117. @pytest.mark.buildconfigspec('cmd_echo')
  118. @pytest.mark.boardspec('sandbox')
  119. def test_hush_if_test_host_file_exists(u_boot_console):
  120. """Test the "if test -e" shell command."""
  121. test_file = u_boot_console.config.result_dir + \
  122. '/creating_this_file_breaks_u_boot_tests'
  123. try:
  124. os.unlink(test_file)
  125. except:
  126. pass
  127. assert not os.path.exists(test_file)
  128. expr = 'test -e hostfs - ' + test_file
  129. exec_hush_if(u_boot_console, expr, False)
  130. try:
  131. with open(test_file, 'wb'):
  132. pass
  133. assert os.path.exists(test_file)
  134. expr = 'test -e hostfs - ' + test_file
  135. exec_hush_if(u_boot_console, expr, True)
  136. finally:
  137. os.unlink(test_file)
  138. expr = 'test -e hostfs - ' + test_file
  139. exec_hush_if(u_boot_console, expr, False)