test_hush_if_test.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. # 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. @pytest.mark.buildconfigspec('hush_parser')
  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.buildconfigspec('hush_parser')
  87. @pytest.mark.parametrize('expr,result', subtests)
  88. def test_hush_if_test(u_boot_console, expr, result):
  89. """Test a single "if test" condition."""
  90. exec_hush_if(u_boot_console, expr, result)
  91. @pytest.mark.buildconfigspec('hush_parser')
  92. def test_hush_if_test_teardown(u_boot_console):
  93. """Clean up environment variables used during the "if" tests."""
  94. u_boot_console.run_command('setenv ut_var_exists')
  95. @pytest.mark.buildconfigspec('hush_parser')
  96. # We might test this on real filesystems via UMS, DFU, 'save', etc.
  97. # Of those, only UMS currently allows file removal though.
  98. @pytest.mark.boardspec('sandbox')
  99. def test_hush_if_test_host_file_exists(u_boot_console):
  100. """Test the "if test -e" shell command."""
  101. test_file = u_boot_console.config.result_dir + \
  102. '/creating_this_file_breaks_u_boot_tests'
  103. try:
  104. os.unlink(test_file)
  105. except:
  106. pass
  107. assert not os.path.exists(test_file)
  108. expr = 'test -e hostfs - ' + test_file
  109. exec_hush_if(u_boot_console, expr, False)
  110. try:
  111. with file(test_file, 'wb'):
  112. pass
  113. assert os.path.exists(test_file)
  114. expr = 'test -e hostfs - ' + test_file
  115. exec_hush_if(u_boot_console, expr, True)
  116. finally:
  117. os.unlink(test_file)
  118. expr = 'test -e hostfs - ' + test_file
  119. exec_hush_if(u_boot_console, expr, False)