Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0 |
Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 2 | # Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved. |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 3 | |
| 4 | # Test operation of the "if" shell command. |
| 5 | |
| 6 | import os |
| 7 | import os.path |
| 8 | import pytest |
| 9 | |
Michal Simek | a4eb5f1 | 2019-10-10 13:00:38 +0200 | [diff] [blame] | 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 |
| 13 | |
Michal Simek | 0976652 | 2017-05-18 09:23:14 +0200 | [diff] [blame] | 14 | pytestmark = pytest.mark.buildconfigspec('hush_parser') |
| 15 | |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 16 | # The list of "if test" conditions to test. |
| 17 | subtests = ( |
| 18 | # Base if functionality. |
| 19 | |
| 20 | ('true', True), |
| 21 | ('false', False), |
| 22 | |
| 23 | # Basic operators. |
| 24 | |
| 25 | ('test aaa = aaa', True), |
| 26 | ('test aaa = bbb', False), |
| 27 | |
| 28 | ('test aaa != bbb', True), |
| 29 | ('test aaa != aaa', False), |
| 30 | |
| 31 | ('test aaa < bbb', True), |
| 32 | ('test bbb < aaa', False), |
| 33 | |
| 34 | ('test bbb > aaa', True), |
| 35 | ('test aaa > bbb', False), |
| 36 | |
| 37 | ('test 123 -eq 123', True), |
| 38 | ('test 123 -eq 456', False), |
| 39 | |
| 40 | ('test 123 -ne 456', True), |
| 41 | ('test 123 -ne 123', False), |
| 42 | |
| 43 | ('test 123 -lt 456', True), |
| 44 | ('test 123 -lt 123', False), |
| 45 | ('test 456 -lt 123', False), |
| 46 | |
| 47 | ('test 123 -le 456', True), |
| 48 | ('test 123 -le 123', True), |
| 49 | ('test 456 -le 123', False), |
| 50 | |
| 51 | ('test 456 -gt 123', True), |
| 52 | ('test 123 -gt 123', False), |
| 53 | ('test 123 -gt 456', False), |
| 54 | |
| 55 | ('test 456 -ge 123', True), |
| 56 | ('test 123 -ge 123', True), |
| 57 | ('test 123 -ge 456', False), |
| 58 | |
Michal Simek | a4eb5f1 | 2019-10-10 13:00:38 +0200 | [diff] [blame] | 59 | # Octal tests |
| 60 | |
| 61 | ('test 010 -eq 010', True), |
| 62 | ('test 010 -eq 011', False), |
| 63 | |
| 64 | ('test 010 -ne 011', True), |
| 65 | ('test 010 -ne 010', False), |
| 66 | |
| 67 | # Hexadecimal tests |
| 68 | |
| 69 | ('test 0x2000000 -gt 0x2000001', False), |
| 70 | ('test 0x2000000 -gt 0x2000000', False), |
| 71 | ('test 0x2000000 -gt 0x1ffffff', True), |
| 72 | |
| 73 | # Mixed tests |
| 74 | |
| 75 | ('test 010 -eq 10', False), |
| 76 | ('test 010 -ne 10', True), |
| 77 | ('test 0xa -eq 10', True), |
| 78 | ('test 0xa -eq 012', True), |
| 79 | |
| 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), |
| 85 | |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 86 | ('test -z ""', True), |
| 87 | ('test -z "aaa"', False), |
| 88 | |
| 89 | ('test -n "aaa"', True), |
| 90 | ('test -n ""', False), |
| 91 | |
| 92 | # Inversion of simple tests. |
| 93 | |
| 94 | ('test ! aaa = aaa', False), |
| 95 | ('test ! aaa = bbb', True), |
| 96 | ('test ! ! aaa = aaa', True), |
| 97 | ('test ! ! aaa = bbb', False), |
| 98 | |
| 99 | # Binary operators. |
| 100 | |
| 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), |
| 105 | |
| 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), |
| 110 | |
| 111 | # Inversion within binary operators. |
| 112 | |
| 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), |
| 117 | |
| 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), |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 122 | ) |
| 123 | |
| 124 | def exec_hush_if(u_boot_console, expr, result): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 125 | """Execute a shell "if" command, and validate its result.""" |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 126 | |
Stephen Warren | 7646624 | 2016-02-15 17:40:34 -0700 | [diff] [blame] | 127 | config = u_boot_console.config.buildconfig |
| 128 | maxargs = int(config.get('config_sys_maxargs', '0')) |
| 129 | args = len(expr.split(' ')) - 1 |
| 130 | if args > maxargs: |
| 131 | u_boot_console.log.warning('CONFIG_SYS_MAXARGS too low; need ' + |
| 132 | str(args)) |
| 133 | pytest.skip() |
| 134 | |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 135 | cmd = 'if ' + expr + '; then echo true; else echo false; fi' |
| 136 | response = u_boot_console.run_command(cmd) |
| 137 | assert response.strip() == str(result).lower() |
| 138 | |
Michal Simek | 240a036 | 2017-05-18 09:23:15 +0200 | [diff] [blame] | 139 | @pytest.mark.buildconfigspec('cmd_echo') |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 140 | @pytest.mark.parametrize('expr,result', subtests) |
| 141 | def test_hush_if_test(u_boot_console, expr, result): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 142 | """Test a single "if test" condition.""" |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 143 | |
| 144 | exec_hush_if(u_boot_console, expr, result) |
| 145 | |
Simon Glass | 9ce82a5 | 2021-09-19 15:14:49 -0600 | [diff] [blame] | 146 | def test_hush_z(u_boot_console): |
| 147 | """Test the -z operator""" |
| 148 | u_boot_console.run_command('setenv ut_var_nonexistent') |
| 149 | u_boot_console.run_command('setenv ut_var_exists 1') |
| 150 | exec_hush_if(u_boot_console, 'test -z "$ut_var_nonexistent"', True) |
| 151 | exec_hush_if(u_boot_console, 'test -z "$ut_var_exists"', False) |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 152 | u_boot_console.run_command('setenv ut_var_exists') |
| 153 | |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 154 | # We might test this on real filesystems via UMS, DFU, 'save', etc. |
| 155 | # Of those, only UMS currently allows file removal though. |
Michal Simek | 240a036 | 2017-05-18 09:23:15 +0200 | [diff] [blame] | 156 | @pytest.mark.buildconfigspec('cmd_echo') |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 157 | @pytest.mark.boardspec('sandbox') |
| 158 | def test_hush_if_test_host_file_exists(u_boot_console): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 159 | """Test the "if test -e" shell command.""" |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 160 | |
| 161 | test_file = u_boot_console.config.result_dir + \ |
| 162 | '/creating_this_file_breaks_u_boot_tests' |
| 163 | |
| 164 | try: |
| 165 | os.unlink(test_file) |
| 166 | except: |
| 167 | pass |
| 168 | assert not os.path.exists(test_file) |
| 169 | |
| 170 | expr = 'test -e hostfs - ' + test_file |
| 171 | exec_hush_if(u_boot_console, expr, False) |
| 172 | |
| 173 | try: |
Paul Burton | 92f0ac4 | 2017-09-14 14:34:49 -0700 | [diff] [blame] | 174 | with open(test_file, 'wb'): |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 175 | pass |
| 176 | assert os.path.exists(test_file) |
| 177 | |
| 178 | expr = 'test -e hostfs - ' + test_file |
| 179 | exec_hush_if(u_boot_console, expr, True) |
| 180 | finally: |
| 181 | os.unlink(test_file) |
| 182 | |
| 183 | expr = 'test -e hostfs - ' + test_file |
| 184 | exec_hush_if(u_boot_console, expr, False) |