test_hush_if_test.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
  2. #
  3. # SPDX-License-Identifier: GPL-2.0
  4. # Test operation of the "if" shell command.
  5. import os
  6. import os.path
  7. import pytest
  8. pytestmark = pytest.mark.buildconfigspec('hush_parser')
  9. # The list of "if test" conditions to test.
  10. subtests = (
  11. # Base if functionality.
  12. ('true', True),
  13. ('false', False),
  14. # Basic operators.
  15. ('test aaa = aaa', True),
  16. ('test aaa = bbb', False),
  17. ('test aaa != bbb', True),
  18. ('test aaa != aaa', False),
  19. ('test aaa < bbb', True),
  20. ('test bbb < aaa', False),
  21. ('test bbb > aaa', True),
  22. ('test aaa > bbb', False),
  23. ('test 123 -eq 123', True),
  24. ('test 123 -eq 456', False),
  25. ('test 123 -ne 456', True),
  26. ('test 123 -ne 123', False),
  27. ('test 123 -lt 456', True),
  28. ('test 123 -lt 123', False),
  29. ('test 456 -lt 123', False),
  30. ('test 123 -le 456', True),
  31. ('test 123 -le 123', True),
  32. ('test 456 -le 123', False),
  33. ('test 456 -gt 123', True),
  34. ('test 123 -gt 123', False),
  35. ('test 123 -gt 456', False),
  36. ('test 456 -ge 123', True),
  37. ('test 123 -ge 123', True),
  38. ('test 123 -ge 456', False),
  39. ('test -z ""', True),
  40. ('test -z "aaa"', False),
  41. ('test -n "aaa"', True),
  42. ('test -n ""', False),
  43. # Inversion of simple tests.
  44. ('test ! aaa = aaa', False),
  45. ('test ! aaa = bbb', True),
  46. ('test ! ! aaa = aaa', True),
  47. ('test ! ! aaa = bbb', False),
  48. # Binary operators.
  49. ('test aaa != aaa -o bbb != bbb', False),
  50. ('test aaa != aaa -o bbb = bbb', True),
  51. ('test aaa = aaa -o bbb != bbb', True),
  52. ('test aaa = aaa -o bbb = bbb', True),
  53. ('test aaa != aaa -a bbb != bbb', False),
  54. ('test aaa != aaa -a bbb = bbb', False),
  55. ('test aaa = aaa -a bbb != bbb', False),
  56. ('test aaa = aaa -a bbb = bbb', True),
  57. # Inversion within binary operators.
  58. ('test ! aaa != aaa -o ! bbb != bbb', True),
  59. ('test ! aaa != aaa -o ! bbb = bbb', True),
  60. ('test ! aaa = aaa -o ! bbb != bbb', True),
  61. ('test ! aaa = aaa -o ! bbb = bbb', False),
  62. ('test ! ! aaa != aaa -o ! ! bbb != bbb', False),
  63. ('test ! ! aaa != aaa -o ! ! bbb = bbb', True),
  64. ('test ! ! aaa = aaa -o ! ! bbb != bbb', True),
  65. ('test ! ! aaa = aaa -o ! ! bbb = bbb', True),
  66. # -z operator.
  67. ('test -z "$ut_var_nonexistent"', True),
  68. ('test -z "$ut_var_exists"', False),
  69. )
  70. def exec_hush_if(u_boot_console, expr, result):
  71. """Execute a shell "if" command, and validate its result."""
  72. config = u_boot_console.config.buildconfig
  73. maxargs = int(config.get('config_sys_maxargs', '0'))
  74. args = len(expr.split(' ')) - 1
  75. if args > maxargs:
  76. u_boot_console.log.warning('CONFIG_SYS_MAXARGS too low; need ' +
  77. str(args))
  78. pytest.skip()
  79. cmd = 'if ' + expr + '; then echo true; else echo false; fi'
  80. response = u_boot_console.run_command(cmd)
  81. assert response.strip() == str(result).lower()
  82. def test_hush_if_test_setup(u_boot_console):
  83. """Set up environment variables used during the "if" tests."""
  84. u_boot_console.run_command('setenv ut_var_nonexistent')
  85. u_boot_console.run_command('setenv ut_var_exists 1')
  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.boardspec('sandbox')
  96. def test_hush_if_test_host_file_exists(u_boot_console):
  97. """Test the "if test -e" shell command."""
  98. test_file = u_boot_console.config.result_dir + \
  99. '/creating_this_file_breaks_u_boot_tests'
  100. try:
  101. os.unlink(test_file)
  102. except:
  103. pass
  104. assert not os.path.exists(test_file)
  105. expr = 'test -e hostfs - ' + test_file
  106. exec_hush_if(u_boot_console, expr, False)
  107. try:
  108. with file(test_file, 'wb'):
  109. pass
  110. assert os.path.exists(test_file)
  111. expr = 'test -e hostfs - ' + test_file
  112. exec_hush_if(u_boot_console, expr, True)
  113. finally:
  114. os.unlink(test_file)
  115. expr = 'test -e hostfs - ' + test_file
  116. exec_hush_if(u_boot_console, expr, False)