blob: 056a2880303eef904a9914ca324c8560cd95fae3 [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
Stephen Warren47a2ca72016-01-15 11:15:29 -0700120@pytest.mark.parametrize('expr,result', subtests)
121def test_hush_if_test(u_boot_console, expr, result):
Stephen Warren75e731e2016-01-26 13:41:30 -0700122 """Test a single "if test" condition."""
Stephen Warren47a2ca72016-01-15 11:15:29 -0700123
124 exec_hush_if(u_boot_console, expr, result)
125
Stephen Warren47a2ca72016-01-15 11:15:29 -0700126def test_hush_if_test_teardown(u_boot_console):
Stephen Warren75e731e2016-01-26 13:41:30 -0700127 """Clean up environment variables used during the "if" tests."""
Stephen Warren47a2ca72016-01-15 11:15:29 -0700128
129 u_boot_console.run_command('setenv ut_var_exists')
130
Stephen Warren47a2ca72016-01-15 11:15:29 -0700131# We might test this on real filesystems via UMS, DFU, 'save', etc.
132# Of those, only UMS currently allows file removal though.
133@pytest.mark.boardspec('sandbox')
134def test_hush_if_test_host_file_exists(u_boot_console):
Stephen Warren75e731e2016-01-26 13:41:30 -0700135 """Test the "if test -e" shell command."""
Stephen Warren47a2ca72016-01-15 11:15:29 -0700136
137 test_file = u_boot_console.config.result_dir + \
138 '/creating_this_file_breaks_u_boot_tests'
139
140 try:
141 os.unlink(test_file)
142 except:
143 pass
144 assert not os.path.exists(test_file)
145
146 expr = 'test -e hostfs - ' + test_file
147 exec_hush_if(u_boot_console, expr, False)
148
149 try:
150 with file(test_file, 'wb'):
151 pass
152 assert os.path.exists(test_file)
153
154 expr = 'test -e hostfs - ' + test_file
155 exec_hush_if(u_boot_console, expr, True)
156 finally:
157 os.unlink(test_file)
158
159 expr = 'test -e hostfs - ' + test_file
160 exec_hush_if(u_boot_console, expr, False)