blob: 8b88425a6598ea40b16842f33691ee6cf82a8319 [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
11# The list of "if test" conditions to test.
12subtests = (
13 # Base if functionality.
14
15 ('true', True),
16 ('false', False),
17
18 # Basic operators.
19
20 ('test aaa = aaa', True),
21 ('test aaa = bbb', False),
22
23 ('test aaa != bbb', True),
24 ('test aaa != aaa', False),
25
26 ('test aaa < bbb', True),
27 ('test bbb < aaa', False),
28
29 ('test bbb > aaa', True),
30 ('test aaa > bbb', False),
31
32 ('test 123 -eq 123', True),
33 ('test 123 -eq 456', False),
34
35 ('test 123 -ne 456', True),
36 ('test 123 -ne 123', False),
37
38 ('test 123 -lt 456', True),
39 ('test 123 -lt 123', False),
40 ('test 456 -lt 123', False),
41
42 ('test 123 -le 456', True),
43 ('test 123 -le 123', True),
44 ('test 456 -le 123', False),
45
46 ('test 456 -gt 123', True),
47 ('test 123 -gt 123', False),
48 ('test 123 -gt 456', False),
49
50 ('test 456 -ge 123', True),
51 ('test 123 -ge 123', True),
52 ('test 123 -ge 456', False),
53
54 ('test -z ""', True),
55 ('test -z "aaa"', False),
56
57 ('test -n "aaa"', True),
58 ('test -n ""', False),
59
60 # Inversion of simple tests.
61
62 ('test ! aaa = aaa', False),
63 ('test ! aaa = bbb', True),
64 ('test ! ! aaa = aaa', True),
65 ('test ! ! aaa = bbb', False),
66
67 # Binary operators.
68
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),
73
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),
78
79 # Inversion within binary operators.
80
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),
85
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),
90
91 # -z operator.
92
93 ('test -z "$ut_var_nonexistent"', True),
94 ('test -z "$ut_var_exists"', False),
95)
96
97def exec_hush_if(u_boot_console, expr, result):
Stephen Warren75e731e2016-01-26 13:41:30 -070098 """Execute a shell "if" command, and validate its result."""
Stephen Warren47a2ca72016-01-15 11:15:29 -070099
100 cmd = 'if ' + expr + '; then echo true; else echo false; fi'
101 response = u_boot_console.run_command(cmd)
102 assert response.strip() == str(result).lower()
103
104@pytest.mark.buildconfigspec('sys_hush_parser')
105def test_hush_if_test_setup(u_boot_console):
Stephen Warren75e731e2016-01-26 13:41:30 -0700106 """Set up environment variables used during the "if" tests."""
Stephen Warren47a2ca72016-01-15 11:15:29 -0700107
108 u_boot_console.run_command('setenv ut_var_nonexistent')
109 u_boot_console.run_command('setenv ut_var_exists 1')
110
111@pytest.mark.buildconfigspec('sys_hush_parser')
112@pytest.mark.parametrize('expr,result', subtests)
113def test_hush_if_test(u_boot_console, expr, result):
Stephen Warren75e731e2016-01-26 13:41:30 -0700114 """Test a single "if test" condition."""
Stephen Warren47a2ca72016-01-15 11:15:29 -0700115
116 exec_hush_if(u_boot_console, expr, result)
117
118@pytest.mark.buildconfigspec('sys_hush_parser')
119def test_hush_if_test_teardown(u_boot_console):
Stephen Warren75e731e2016-01-26 13:41:30 -0700120 """Clean up environment variables used during the "if" tests."""
Stephen Warren47a2ca72016-01-15 11:15:29 -0700121
122 u_boot_console.run_command('setenv ut_var_exists')
123
124@pytest.mark.buildconfigspec('sys_hush_parser')
125# We might test this on real filesystems via UMS, DFU, 'save', etc.
126# Of those, only UMS currently allows file removal though.
127@pytest.mark.boardspec('sandbox')
128def test_hush_if_test_host_file_exists(u_boot_console):
Stephen Warren75e731e2016-01-26 13:41:30 -0700129 """Test the "if test -e" shell command."""
Stephen Warren47a2ca72016-01-15 11:15:29 -0700130
131 test_file = u_boot_console.config.result_dir + \
132 '/creating_this_file_breaks_u_boot_tests'
133
134 try:
135 os.unlink(test_file)
136 except:
137 pass
138 assert not os.path.exists(test_file)
139
140 expr = 'test -e hostfs - ' + test_file
141 exec_hush_if(u_boot_console, expr, False)
142
143 try:
144 with file(test_file, 'wb'):
145 pass
146 assert os.path.exists(test_file)
147
148 expr = 'test -e hostfs - ' + test_file
149 exec_hush_if(u_boot_console, expr, True)
150 finally:
151 os.unlink(test_file)
152
153 expr = 'test -e hostfs - ' + test_file
154 exec_hush_if(u_boot_console, expr, False)