1 # SPDX-License-Identifier: GPL-2.0
2 # Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
4 # Test operation of the "if" shell command.
10 # TODO: These tests should be converted to a C test.
11 # For more information please take a look at the thread
12 # https://lists.denx.de/pipermail/u-boot/2019-October/388732.html
14 pytestmark = pytest.mark.buildconfigspec('hush_parser')
16 # The list of "if test" conditions to test.
18 # Base if functionality.
25 ('test aaa = aaa', True),
26 ('test aaa = bbb', False),
28 ('test aaa != bbb', True),
29 ('test aaa != aaa', False),
31 ('test aaa < bbb', True),
32 ('test bbb < aaa', False),
34 ('test bbb > aaa', True),
35 ('test aaa > bbb', False),
37 ('test 123 -eq 123', True),
38 ('test 123 -eq 456', False),
40 ('test 123 -ne 456', True),
41 ('test 123 -ne 123', False),
43 ('test 123 -lt 456', True),
44 ('test 123 -lt 123', False),
45 ('test 456 -lt 123', False),
47 ('test 123 -le 456', True),
48 ('test 123 -le 123', True),
49 ('test 456 -le 123', False),
51 ('test 456 -gt 123', True),
52 ('test 123 -gt 123', False),
53 ('test 123 -gt 456', False),
55 ('test 456 -ge 123', True),
56 ('test 123 -ge 123', True),
57 ('test 123 -ge 456', False),
61 ('test 010 -eq 010', True),
62 ('test 010 -eq 011', False),
64 ('test 010 -ne 011', True),
65 ('test 010 -ne 010', False),
69 ('test 0x2000000 -gt 0x2000001', False),
70 ('test 0x2000000 -gt 0x2000000', False),
71 ('test 0x2000000 -gt 0x1ffffff', True),
75 ('test 010 -eq 10', False),
76 ('test 010 -ne 10', True),
77 ('test 0xa -eq 10', True),
78 ('test 0xa -eq 012', True),
80 ('test 2000000 -gt 0x1ffffff', False),
81 ('test 0x2000000 -gt 1ffffff', True),
82 ('test 0x2000000 -lt 1ffffff', False),
83 ('test 0x2000000 -eq 2000000', False),
84 ('test 0x2000000 -ne 2000000', True),
87 ('test -z "aaa"', False),
89 ('test -n "aaa"', True),
90 ('test -n ""', False),
92 # Inversion of simple tests.
94 ('test ! aaa = aaa', False),
95 ('test ! aaa = bbb', True),
96 ('test ! ! aaa = aaa', True),
97 ('test ! ! aaa = bbb', False),
101 ('test aaa != aaa -o bbb != bbb', False),
102 ('test aaa != aaa -o bbb = bbb', True),
103 ('test aaa = aaa -o bbb != bbb', True),
104 ('test aaa = aaa -o bbb = bbb', True),
106 ('test aaa != aaa -a bbb != bbb', False),
107 ('test aaa != aaa -a bbb = bbb', False),
108 ('test aaa = aaa -a bbb != bbb', False),
109 ('test aaa = aaa -a bbb = bbb', True),
111 # Inversion within binary operators.
113 ('test ! aaa != aaa -o ! bbb != bbb', True),
114 ('test ! aaa != aaa -o ! bbb = bbb', True),
115 ('test ! aaa = aaa -o ! bbb != bbb', True),
116 ('test ! aaa = aaa -o ! bbb = bbb', False),
118 ('test ! ! aaa != aaa -o ! ! bbb != bbb', False),
119 ('test ! ! aaa != aaa -o ! ! bbb = bbb', True),
120 ('test ! ! aaa = aaa -o ! ! bbb != bbb', True),
121 ('test ! ! aaa = aaa -o ! ! bbb = bbb', True),
125 ('test -z "$ut_var_nonexistent"', True),
126 ('test -z "$ut_var_exists"', False),
129 def exec_hush_if(u_boot_console, expr, result):
130 """Execute a shell "if" command, and validate its result."""
132 config = u_boot_console.config.buildconfig
133 maxargs = int(config.get('config_sys_maxargs', '0'))
134 args = len(expr.split(' ')) - 1
136 u_boot_console.log.warning('CONFIG_SYS_MAXARGS too low; need ' +
140 cmd = 'if ' + expr + '; then echo true; else echo false; fi'
141 response = u_boot_console.run_command(cmd)
142 assert response.strip() == str(result).lower()
144 def test_hush_if_test_setup(u_boot_console):
145 """Set up environment variables used during the "if" tests."""
147 u_boot_console.run_command('setenv ut_var_nonexistent')
148 u_boot_console.run_command('setenv ut_var_exists 1')
150 @pytest.mark.buildconfigspec('cmd_echo')
151 @pytest.mark.parametrize('expr,result', subtests)
152 def test_hush_if_test(u_boot_console, expr, result):
153 """Test a single "if test" condition."""
155 exec_hush_if(u_boot_console, expr, result)
157 def test_hush_if_test_teardown(u_boot_console):
158 """Clean up environment variables used during the "if" tests."""
160 u_boot_console.run_command('setenv ut_var_exists')
162 # We might test this on real filesystems via UMS, DFU, 'save', etc.
163 # Of those, only UMS currently allows file removal though.
164 @pytest.mark.buildconfigspec('cmd_echo')
165 @pytest.mark.boardspec('sandbox')
166 def test_hush_if_test_host_file_exists(u_boot_console):
167 """Test the "if test -e" shell command."""
169 test_file = u_boot_console.config.result_dir + \
170 '/creating_this_file_breaks_u_boot_tests'
176 assert not os.path.exists(test_file)
178 expr = 'test -e hostfs - ' + test_file
179 exec_hush_if(u_boot_console, expr, False)
182 with open(test_file, 'wb'):
184 assert os.path.exists(test_file)
186 expr = 'test -e hostfs - ' + test_file
187 exec_hush_if(u_boot_console, expr, True)
191 expr = 'test -e hostfs - ' + test_file
192 exec_hush_if(u_boot_console, expr, False)