blob: c8f4208d31054f1f20432d61bc674b5e9e4da800 [file] [log] [blame]
Stephen Warren47a2ca72016-01-15 11:15:29 -07001# Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
2#
3# SPDX-License-Identifier: GPL-2.0
4
5# Test operation of the "if" shell command.
6
7import os
8import os.path
9import pytest
10
Michal Simek09766522017-05-18 09:23:14 +020011pytestmark = pytest.mark.buildconfigspec('hush_parser')
12
Stephen Warren47a2ca72016-01-15 11:15:29 -070013# The list of "if test" conditions to test.
14subtests = (
15 # Base if functionality.
16
17 ('true', True),
18 ('false', False),
19
20 # Basic operators.
21
22 ('test aaa = aaa', True),
23 ('test aaa = bbb', False),
24
25 ('test aaa != bbb', True),
26 ('test aaa != aaa', False),
27
28 ('test aaa < bbb', True),
29 ('test bbb < aaa', False),
30
31 ('test bbb > aaa', True),
32 ('test aaa > bbb', False),
33
34 ('test 123 -eq 123', True),
35 ('test 123 -eq 456', False),
36
37 ('test 123 -ne 456', True),
38 ('test 123 -ne 123', False),
39
40 ('test 123 -lt 456', True),
41 ('test 123 -lt 123', False),
42 ('test 456 -lt 123', False),
43
44 ('test 123 -le 456', True),
45 ('test 123 -le 123', True),
46 ('test 456 -le 123', False),
47
48 ('test 456 -gt 123', True),
49 ('test 123 -gt 123', False),
50 ('test 123 -gt 456', False),
51
52 ('test 456 -ge 123', True),
53 ('test 123 -ge 123', True),
54 ('test 123 -ge 456', False),
55
56 ('test -z ""', True),
57 ('test -z "aaa"', False),
58
59 ('test -n "aaa"', True),
60 ('test -n ""', False),
61
62 # Inversion of simple tests.
63
64 ('test ! aaa = aaa', False),
65 ('test ! aaa = bbb', True),
66 ('test ! ! aaa = aaa', True),
67 ('test ! ! aaa = bbb', False),
68
69 # Binary operators.
70
71 ('test aaa != aaa -o bbb != bbb', False),
72 ('test aaa != aaa -o bbb = bbb', True),
73 ('test aaa = aaa -o bbb != bbb', True),
74 ('test aaa = aaa -o bbb = bbb', True),
75
76 ('test aaa != aaa -a bbb != bbb', False),
77 ('test aaa != aaa -a bbb = bbb', False),
78 ('test aaa = aaa -a bbb != bbb', False),
79 ('test aaa = aaa -a bbb = bbb', True),
80
81 # Inversion within binary operators.
82
83 ('test ! aaa != aaa -o ! bbb != bbb', True),
84 ('test ! aaa != aaa -o ! bbb = bbb', True),
85 ('test ! aaa = aaa -o ! bbb != bbb', True),
86 ('test ! aaa = aaa -o ! bbb = bbb', False),
87
88 ('test ! ! aaa != aaa -o ! ! bbb != bbb', False),
89 ('test ! ! aaa != aaa -o ! ! bbb = bbb', True),
90 ('test ! ! aaa = aaa -o ! ! bbb != bbb', True),
91 ('test ! ! aaa = aaa -o ! ! bbb = bbb', True),
92
93 # -z operator.
94
95 ('test -z "$ut_var_nonexistent"', True),
96 ('test -z "$ut_var_exists"', False),
97)
98
99def exec_hush_if(u_boot_console, expr, result):
Stephen Warren75e731e2016-01-26 13:41:30 -0700100 """Execute a shell "if" command, and validate its result."""
Stephen Warren47a2ca72016-01-15 11:15:29 -0700101
Stephen Warren76466242016-02-15 17:40:34 -0700102 config = u_boot_console.config.buildconfig
103 maxargs = int(config.get('config_sys_maxargs', '0'))
104 args = len(expr.split(' ')) - 1
105 if args > maxargs:
106 u_boot_console.log.warning('CONFIG_SYS_MAXARGS too low; need ' +
107 str(args))
108 pytest.skip()
109
Stephen Warren47a2ca72016-01-15 11:15:29 -0700110 cmd = 'if ' + expr + '; then echo true; else echo false; fi'
111 response = u_boot_console.run_command(cmd)
112 assert response.strip() == str(result).lower()
113
Stephen Warren47a2ca72016-01-15 11:15:29 -0700114def test_hush_if_test_setup(u_boot_console):
Stephen Warren75e731e2016-01-26 13:41:30 -0700115 """Set up environment variables used during the "if" tests."""
Stephen Warren47a2ca72016-01-15 11:15:29 -0700116
117 u_boot_console.run_command('setenv ut_var_nonexistent')
118 u_boot_console.run_command('setenv ut_var_exists 1')
119
Michal Simek240a0362017-05-18 09:23:15 +0200120@pytest.mark.buildconfigspec('cmd_echo')
Stephen Warren47a2ca72016-01-15 11:15:29 -0700121@pytest.mark.parametrize('expr,result', subtests)
122def test_hush_if_test(u_boot_console, expr, result):
Stephen Warren75e731e2016-01-26 13:41:30 -0700123 """Test a single "if test" condition."""
Stephen Warren47a2ca72016-01-15 11:15:29 -0700124
125 exec_hush_if(u_boot_console, expr, result)
126
Stephen Warren47a2ca72016-01-15 11:15:29 -0700127def test_hush_if_test_teardown(u_boot_console):
Stephen Warren75e731e2016-01-26 13:41:30 -0700128 """Clean up environment variables used during the "if" tests."""
Stephen Warren47a2ca72016-01-15 11:15:29 -0700129
130 u_boot_console.run_command('setenv ut_var_exists')
131
Stephen Warren47a2ca72016-01-15 11:15:29 -0700132# We might test this on real filesystems via UMS, DFU, 'save', etc.
133# Of those, only UMS currently allows file removal though.
Michal Simek240a0362017-05-18 09:23:15 +0200134@pytest.mark.buildconfigspec('cmd_echo')
Stephen Warren47a2ca72016-01-15 11:15:29 -0700135@pytest.mark.boardspec('sandbox')
136def test_hush_if_test_host_file_exists(u_boot_console):
Stephen Warren75e731e2016-01-26 13:41:30 -0700137 """Test the "if test -e" shell command."""
Stephen Warren47a2ca72016-01-15 11:15:29 -0700138
139 test_file = u_boot_console.config.result_dir + \
140 '/creating_this_file_breaks_u_boot_tests'
141
142 try:
143 os.unlink(test_file)
144 except:
145 pass
146 assert not os.path.exists(test_file)
147
148 expr = 'test -e hostfs - ' + test_file
149 exec_hush_if(u_boot_console, expr, False)
150
151 try:
152 with file(test_file, 'wb'):
153 pass
154 assert os.path.exists(test_file)
155
156 expr = 'test -e hostfs - ' + test_file
157 exec_hush_if(u_boot_console, expr, True)
158 finally:
159 os.unlink(test_file)
160
161 expr = 'test -e hostfs - ' + test_file
162 exec_hush_if(u_boot_console, expr, False)