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 | 0976652 | 2017-05-18 09:23:14 +0200 | [diff] [blame] | 10 | pytestmark = pytest.mark.buildconfigspec('hush_parser') |
| 11 | |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 12 | # The list of "if test" conditions to test. |
| 13 | subtests = ( |
| 14 | # Base if functionality. |
| 15 | |
| 16 | ('true', True), |
| 17 | ('false', False), |
| 18 | |
| 19 | # Basic operators. |
| 20 | |
| 21 | ('test aaa = aaa', True), |
| 22 | ('test aaa = bbb', False), |
| 23 | |
| 24 | ('test aaa != bbb', True), |
| 25 | ('test aaa != aaa', False), |
| 26 | |
| 27 | ('test aaa < bbb', True), |
| 28 | ('test bbb < aaa', False), |
| 29 | |
| 30 | ('test bbb > aaa', True), |
| 31 | ('test aaa > bbb', False), |
| 32 | |
| 33 | ('test 123 -eq 123', True), |
| 34 | ('test 123 -eq 456', False), |
| 35 | |
| 36 | ('test 123 -ne 456', True), |
| 37 | ('test 123 -ne 123', False), |
| 38 | |
| 39 | ('test 123 -lt 456', True), |
| 40 | ('test 123 -lt 123', False), |
| 41 | ('test 456 -lt 123', False), |
| 42 | |
| 43 | ('test 123 -le 456', True), |
| 44 | ('test 123 -le 123', True), |
| 45 | ('test 456 -le 123', False), |
| 46 | |
| 47 | ('test 456 -gt 123', True), |
| 48 | ('test 123 -gt 123', False), |
| 49 | ('test 123 -gt 456', False), |
| 50 | |
| 51 | ('test 456 -ge 123', True), |
| 52 | ('test 123 -ge 123', True), |
| 53 | ('test 123 -ge 456', False), |
| 54 | |
| 55 | ('test -z ""', True), |
| 56 | ('test -z "aaa"', False), |
| 57 | |
| 58 | ('test -n "aaa"', True), |
| 59 | ('test -n ""', False), |
| 60 | |
| 61 | # Inversion of simple tests. |
| 62 | |
| 63 | ('test ! aaa = aaa', False), |
| 64 | ('test ! aaa = bbb', True), |
| 65 | ('test ! ! aaa = aaa', True), |
| 66 | ('test ! ! aaa = bbb', False), |
| 67 | |
| 68 | # Binary operators. |
| 69 | |
| 70 | ('test aaa != aaa -o bbb != bbb', False), |
| 71 | ('test aaa != aaa -o bbb = bbb', True), |
| 72 | ('test aaa = aaa -o bbb != bbb', True), |
| 73 | ('test aaa = aaa -o bbb = bbb', True), |
| 74 | |
| 75 | ('test aaa != aaa -a bbb != bbb', False), |
| 76 | ('test aaa != aaa -a bbb = bbb', False), |
| 77 | ('test aaa = aaa -a bbb != bbb', False), |
| 78 | ('test aaa = aaa -a bbb = bbb', True), |
| 79 | |
| 80 | # Inversion within binary operators. |
| 81 | |
| 82 | ('test ! aaa != aaa -o ! bbb != bbb', True), |
| 83 | ('test ! aaa != aaa -o ! bbb = bbb', True), |
| 84 | ('test ! aaa = aaa -o ! bbb != bbb', True), |
| 85 | ('test ! aaa = aaa -o ! bbb = bbb', False), |
| 86 | |
| 87 | ('test ! ! aaa != aaa -o ! ! bbb != bbb', False), |
| 88 | ('test ! ! aaa != aaa -o ! ! bbb = bbb', True), |
| 89 | ('test ! ! aaa = aaa -o ! ! bbb != bbb', True), |
| 90 | ('test ! ! aaa = aaa -o ! ! bbb = bbb', True), |
| 91 | |
| 92 | # -z operator. |
| 93 | |
| 94 | ('test -z "$ut_var_nonexistent"', True), |
| 95 | ('test -z "$ut_var_exists"', False), |
| 96 | ) |
| 97 | |
| 98 | def exec_hush_if(u_boot_console, expr, result): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 99 | """Execute a shell "if" command, and validate its result.""" |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 100 | |
Stephen Warren | 7646624 | 2016-02-15 17:40:34 -0700 | [diff] [blame] | 101 | config = u_boot_console.config.buildconfig |
| 102 | maxargs = int(config.get('config_sys_maxargs', '0')) |
| 103 | args = len(expr.split(' ')) - 1 |
| 104 | if args > maxargs: |
| 105 | u_boot_console.log.warning('CONFIG_SYS_MAXARGS too low; need ' + |
| 106 | str(args)) |
| 107 | pytest.skip() |
| 108 | |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 109 | cmd = 'if ' + expr + '; then echo true; else echo false; fi' |
| 110 | response = u_boot_console.run_command(cmd) |
| 111 | assert response.strip() == str(result).lower() |
| 112 | |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 113 | def test_hush_if_test_setup(u_boot_console): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 114 | """Set up environment variables used during the "if" tests.""" |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 115 | |
| 116 | u_boot_console.run_command('setenv ut_var_nonexistent') |
| 117 | u_boot_console.run_command('setenv ut_var_exists 1') |
| 118 | |
Michal Simek | 240a036 | 2017-05-18 09:23:15 +0200 | [diff] [blame] | 119 | @pytest.mark.buildconfigspec('cmd_echo') |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 120 | @pytest.mark.parametrize('expr,result', subtests) |
| 121 | def test_hush_if_test(u_boot_console, expr, result): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 122 | """Test a single "if test" condition.""" |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 123 | |
| 124 | exec_hush_if(u_boot_console, expr, result) |
| 125 | |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 126 | def test_hush_if_test_teardown(u_boot_console): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 127 | """Clean up environment variables used during the "if" tests.""" |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 128 | |
| 129 | u_boot_console.run_command('setenv ut_var_exists') |
| 130 | |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 131 | # We might test this on real filesystems via UMS, DFU, 'save', etc. |
| 132 | # Of those, only UMS currently allows file removal though. |
Michal Simek | 240a036 | 2017-05-18 09:23:15 +0200 | [diff] [blame] | 133 | @pytest.mark.buildconfigspec('cmd_echo') |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 134 | @pytest.mark.boardspec('sandbox') |
| 135 | def test_hush_if_test_host_file_exists(u_boot_console): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 136 | """Test the "if test -e" shell command.""" |
Stephen Warren | 47a2ca7 | 2016-01-15 11:15:29 -0700 | [diff] [blame] | 137 | |
| 138 | test_file = u_boot_console.config.result_dir + \ |
| 139 | '/creating_this_file_breaks_u_boot_tests' |
| 140 | |
| 141 | try: |
| 142 | os.unlink(test_file) |
| 143 | except: |
| 144 | pass |
| 145 | assert not os.path.exists(test_file) |
| 146 | |
| 147 | expr = 'test -e hostfs - ' + test_file |
| 148 | exec_hush_if(u_boot_console, expr, False) |
| 149 | |
| 150 | try: |
| 151 | with file(test_file, 'wb'): |
| 152 | pass |
| 153 | assert os.path.exists(test_file) |
| 154 | |
| 155 | expr = 'test -e hostfs - ' + test_file |
| 156 | exec_hush_if(u_boot_console, expr, True) |
| 157 | finally: |
| 158 | os.unlink(test_file) |
| 159 | |
| 160 | expr = 'test -e hostfs - ' + test_file |
| 161 | exec_hush_if(u_boot_console, expr, False) |