test_hush_if_test.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. pytestmark = pytest.mark.buildconfigspec('hush_parser')
  8. # The list of "if test" conditions to test.
  9. subtests = (
  10. # Base if functionality.
  11. ('true', True),
  12. ('false', False),
  13. # Basic operators.
  14. ('test aaa = aaa', True),
  15. ('test aaa = bbb', False),
  16. ('test aaa != bbb', True),
  17. ('test aaa != aaa', False),
  18. ('test aaa < bbb', True),
  19. ('test bbb < aaa', False),
  20. ('test bbb > aaa', True),
  21. ('test aaa > bbb', False),
  22. ('test 123 -eq 123', True),
  23. ('test 123 -eq 456', False),
  24. ('test 123 -ne 456', True),
  25. ('test 123 -ne 123', False),
  26. ('test 123 -lt 456', True),
  27. ('test 123 -lt 123', False),
  28. ('test 456 -lt 123', False),
  29. ('test 123 -le 456', True),
  30. ('test 123 -le 123', True),
  31. ('test 456 -le 123', False),
  32. ('test 456 -gt 123', True),
  33. ('test 123 -gt 123', False),
  34. ('test 123 -gt 456', False),
  35. ('test 456 -ge 123', True),
  36. ('test 123 -ge 123', True),
  37. ('test 123 -ge 456', False),
  38. ('test -z ""', True),
  39. ('test -z "aaa"', False),
  40. ('test -n "aaa"', True),
  41. ('test -n ""', False),
  42. # Inversion of simple tests.
  43. ('test ! aaa = aaa', False),
  44. ('test ! aaa = bbb', True),
  45. ('test ! ! aaa = aaa', True),
  46. ('test ! ! aaa = bbb', False),
  47. # Binary operators.
  48. ('test aaa != aaa -o bbb != bbb', False),
  49. ('test aaa != aaa -o bbb = bbb', True),
  50. ('test aaa = aaa -o bbb != bbb', True),
  51. ('test aaa = aaa -o bbb = bbb', True),
  52. ('test aaa != aaa -a bbb != bbb', False),
  53. ('test aaa != aaa -a bbb = bbb', False),
  54. ('test aaa = aaa -a bbb != bbb', False),
  55. ('test aaa = aaa -a bbb = bbb', True),
  56. # Inversion within binary operators.
  57. ('test ! aaa != aaa -o ! bbb != bbb', True),
  58. ('test ! aaa != aaa -o ! bbb = bbb', True),
  59. ('test ! aaa = aaa -o ! bbb != bbb', True),
  60. ('test ! aaa = aaa -o ! bbb = bbb', False),
  61. ('test ! ! aaa != aaa -o ! ! bbb != bbb', False),
  62. ('test ! ! aaa != aaa -o ! ! bbb = bbb', True),
  63. ('test ! ! aaa = aaa -o ! ! bbb != bbb', True),
  64. ('test ! ! aaa = aaa -o ! ! bbb = bbb', True),
  65. # -z operator.
  66. ('test -z "$ut_var_nonexistent"', True),
  67. ('test -z "$ut_var_exists"', False),
  68. )
  69. def exec_hush_if(u_boot_console, expr, result):
  70. """Execute a shell "if" command, and validate its result."""
  71. config = u_boot_console.config.buildconfig
  72. maxargs = int(config.get('config_sys_maxargs', '0'))
  73. args = len(expr.split(' ')) - 1
  74. if args > maxargs:
  75. u_boot_console.log.warning('CONFIG_SYS_MAXARGS too low; need ' +
  76. str(args))
  77. pytest.skip()
  78. cmd = 'if ' + expr + '; then echo true; else echo false; fi'
  79. response = u_boot_console.run_command(cmd)
  80. assert response.strip() == str(result).lower()
  81. def test_hush_if_test_setup(u_boot_console):
  82. """Set up environment variables used during the "if" tests."""
  83. u_boot_console.run_command('setenv ut_var_nonexistent')
  84. u_boot_console.run_command('setenv ut_var_exists 1')
  85. @pytest.mark.buildconfigspec('cmd_echo')
  86. @pytest.mark.parametrize('expr,result', subtests)
  87. def test_hush_if_test(u_boot_console, expr, result):
  88. """Test a single "if test" condition."""
  89. exec_hush_if(u_boot_console, expr, result)
  90. def test_hush_if_test_teardown(u_boot_console):
  91. """Clean up environment variables used during the "if" tests."""
  92. u_boot_console.run_command('setenv ut_var_exists')
  93. # We might test this on real filesystems via UMS, DFU, 'save', etc.
  94. # Of those, only UMS currently allows file removal though.
  95. @pytest.mark.buildconfigspec('cmd_echo')
  96. @pytest.mark.boardspec('sandbox')
  97. def test_hush_if_test_host_file_exists(u_boot_console):
  98. """Test the "if test -e" shell command."""
  99. test_file = u_boot_console.config.result_dir + \
  100. '/creating_this_file_breaks_u_boot_tests'
  101. try:
  102. os.unlink(test_file)
  103. except:
  104. pass
  105. assert not os.path.exists(test_file)
  106. expr = 'test -e hostfs - ' + test_file
  107. exec_hush_if(u_boot_console, expr, False)
  108. try:
  109. with open(test_file, 'wb'):
  110. pass
  111. assert os.path.exists(test_file)
  112. expr = 'test -e hostfs - ' + test_file
  113. exec_hush_if(u_boot_console, expr, True)
  114. finally:
  115. os.unlink(test_file)
  116. expr = 'test -e hostfs - ' + test_file
  117. exec_hush_if(u_boot_console, expr, False)