1 # Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
3 # SPDX-License-Identifier: GPL-2.0
5 # Test operation of the "if" shell command.
11 # The list of "if test" conditions to test.
13 # Base if functionality.
20 ('test aaa = aaa', True),
21 ('test aaa = bbb', False),
23 ('test aaa != bbb', True),
24 ('test aaa != aaa', False),
26 ('test aaa < bbb', True),
27 ('test bbb < aaa', False),
29 ('test bbb > aaa', True),
30 ('test aaa > bbb', False),
32 ('test 123 -eq 123', True),
33 ('test 123 -eq 456', False),
35 ('test 123 -ne 456', True),
36 ('test 123 -ne 123', False),
38 ('test 123 -lt 456', True),
39 ('test 123 -lt 123', False),
40 ('test 456 -lt 123', False),
42 ('test 123 -le 456', True),
43 ('test 123 -le 123', True),
44 ('test 456 -le 123', False),
46 ('test 456 -gt 123', True),
47 ('test 123 -gt 123', False),
48 ('test 123 -gt 456', False),
50 ('test 456 -ge 123', True),
51 ('test 123 -ge 123', True),
52 ('test 123 -ge 456', False),
55 ('test -z "aaa"', False),
57 ('test -n "aaa"', True),
58 ('test -n ""', False),
60 # Inversion of simple tests.
62 ('test ! aaa = aaa', False),
63 ('test ! aaa = bbb', True),
64 ('test ! ! aaa = aaa', True),
65 ('test ! ! aaa = bbb', False),
69 ('test aaa != aaa -o bbb != bbb', False),
70 ('test aaa != aaa -o bbb = bbb', True),
71 ('test aaa = aaa -o bbb != bbb', True),
72 ('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),
79 # Inversion within binary operators.
81 ('test ! aaa != aaa -o ! bbb != bbb', True),
82 ('test ! aaa != aaa -o ! bbb = bbb', True),
83 ('test ! aaa = aaa -o ! bbb != bbb', True),
84 ('test ! aaa = aaa -o ! bbb = bbb', False),
86 ('test ! ! aaa != aaa -o ! ! bbb != bbb', False),
87 ('test ! ! aaa != aaa -o ! ! bbb = bbb', True),
88 ('test ! ! aaa = aaa -o ! ! bbb != bbb', True),
89 ('test ! ! aaa = aaa -o ! ! bbb = bbb', True),
93 ('test -z "$ut_var_nonexistent"', True),
94 ('test -z "$ut_var_exists"', False),
97 def exec_hush_if(u_boot_console, expr, result):
98 """Execute a shell "if" command, and validate its result."""
100 config = u_boot_console.config.buildconfig
101 maxargs = int(config.get('config_sys_maxargs', '0'))
102 args = len(expr.split(' ')) - 1
104 u_boot_console.log.warning('CONFIG_SYS_MAXARGS too low; need ' +
108 cmd = 'if ' + expr + '; then echo true; else echo false; fi'
109 response = u_boot_console.run_command(cmd)
110 assert response.strip() == str(result).lower()
112 @pytest.mark.buildconfigspec('hush_parser')
113 def test_hush_if_test_setup(u_boot_console):
114 """Set up environment variables used during the "if" tests."""
116 u_boot_console.run_command('setenv ut_var_nonexistent')
117 u_boot_console.run_command('setenv ut_var_exists 1')
119 @pytest.mark.buildconfigspec('hush_parser')
120 @pytest.mark.parametrize('expr,result', subtests)
121 def test_hush_if_test(u_boot_console, expr, result):
122 """Test a single "if test" condition."""
124 exec_hush_if(u_boot_console, expr, result)
126 @pytest.mark.buildconfigspec('hush_parser')
127 def test_hush_if_test_teardown(u_boot_console):
128 """Clean up environment variables used during the "if" tests."""
130 u_boot_console.run_command('setenv ut_var_exists')
132 @pytest.mark.buildconfigspec('hush_parser')
133 # We might test this on real filesystems via UMS, DFU, 'save', etc.
134 # Of those, only UMS currently allows file removal though.
135 @pytest.mark.boardspec('sandbox')
136 def test_hush_if_test_host_file_exists(u_boot_console):
137 """Test the "if test -e" shell command."""
139 test_file = u_boot_console.config.result_dir + \
140 '/creating_this_file_breaks_u_boot_tests'
146 assert not os.path.exists(test_file)
148 expr = 'test -e hostfs - ' + test_file
149 exec_hush_if(u_boot_console, expr, False)
152 with file(test_file, 'wb'):
154 assert os.path.exists(test_file)
156 expr = 'test -e hostfs - ' + test_file
157 exec_hush_if(u_boot_console, expr, True)
161 expr = 'test -e hostfs - ' + test_file
162 exec_hush_if(u_boot_console, expr, False)