Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0 |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 2 | # Copyright (c) 2015 Stephen Warren |
| 3 | # Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved. |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 4 | |
Heinrich Schuchardt | d189603 | 2021-11-26 23:30:27 +0100 | [diff] [blame] | 5 | """ |
| 6 | Test operation of shell commands relating to environment variables. |
| 7 | """ |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 8 | |
Patrick Delaunay | 57951c5 | 2020-07-28 11:51:24 +0200 | [diff] [blame] | 9 | import os |
| 10 | import os.path |
Heinrich Schuchardt | d189603 | 2021-11-26 23:30:27 +0100 | [diff] [blame] | 11 | from subprocess import call, CalledProcessError |
Simon Glass | 7327fe7 | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 12 | import tempfile |
Patrick Delaunay | 57951c5 | 2020-07-28 11:51:24 +0200 | [diff] [blame] | 13 | |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 14 | import pytest |
Quentin Schulz | e74def5 | 2018-07-09 19:16:30 +0200 | [diff] [blame] | 15 | import u_boot_utils |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 16 | |
| 17 | # FIXME: This might be useful for other tests; |
| 18 | # perhaps refactor it into ConsoleBase or some other state object? |
| 19 | class StateTestEnv(object): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 20 | """Container that represents the state of all U-Boot environment variables. |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 21 | This enables quick determination of existant/non-existant variable |
| 22 | names. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 23 | """ |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 24 | |
| 25 | def __init__(self, u_boot_console): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 26 | """Initialize a new StateTestEnv object. |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 27 | |
| 28 | Args: |
| 29 | u_boot_console: A U-Boot console. |
| 30 | |
| 31 | Returns: |
| 32 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 33 | """ |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 34 | |
| 35 | self.u_boot_console = u_boot_console |
| 36 | self.get_env() |
| 37 | self.set_var = self.get_non_existent_var() |
| 38 | |
| 39 | def get_env(self): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 40 | """Read all current environment variables from U-Boot. |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 41 | |
| 42 | Args: |
| 43 | None. |
| 44 | |
| 45 | Returns: |
| 46 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 47 | """ |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 48 | |
Stephen Warren | 9c401e4 | 2016-06-16 12:59:34 -0600 | [diff] [blame] | 49 | if self.u_boot_console.config.buildconfig.get( |
| 50 | 'config_version_variable', 'n') == 'y': |
Heiko Schocher | 52e05e9 | 2016-06-07 08:31:15 +0200 | [diff] [blame] | 51 | with self.u_boot_console.disable_check('main_signon'): |
| 52 | response = self.u_boot_console.run_command('printenv') |
| 53 | else: |
| 54 | response = self.u_boot_console.run_command('printenv') |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 55 | self.env = {} |
| 56 | for l in response.splitlines(): |
| 57 | if not '=' in l: |
| 58 | continue |
Stephen Warren | 66b5b9e | 2019-12-18 11:37:21 -0700 | [diff] [blame] | 59 | (var, value) = l.split('=', 1) |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 60 | self.env[var] = value |
| 61 | |
| 62 | def get_existent_var(self): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 63 | """Return the name of an environment variable that exists. |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 64 | |
| 65 | Args: |
| 66 | None. |
| 67 | |
| 68 | Returns: |
| 69 | The name of an environment variable. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 70 | """ |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 71 | |
| 72 | for var in self.env: |
| 73 | return var |
| 74 | |
| 75 | def get_non_existent_var(self): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 76 | """Return the name of an environment variable that does not exist. |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 77 | |
| 78 | Args: |
| 79 | None. |
| 80 | |
| 81 | Returns: |
| 82 | The name of an environment variable. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 83 | """ |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 84 | |
| 85 | n = 0 |
| 86 | while True: |
| 87 | var = 'test_env_' + str(n) |
| 88 | if var not in self.env: |
| 89 | return var |
| 90 | n += 1 |
| 91 | |
Stephen Warren | e1d24d0 | 2016-01-22 12:30:08 -0700 | [diff] [blame] | 92 | ste = None |
| 93 | @pytest.fixture(scope='function') |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 94 | def state_test_env(u_boot_console): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 95 | """pytest fixture to provide a StateTestEnv object to tests.""" |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 96 | |
Stephen Warren | e1d24d0 | 2016-01-22 12:30:08 -0700 | [diff] [blame] | 97 | global ste |
| 98 | if not ste: |
| 99 | ste = StateTestEnv(u_boot_console) |
| 100 | return ste |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 101 | |
| 102 | def unset_var(state_test_env, var): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 103 | """Unset an environment variable. |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 104 | |
| 105 | This both executes a U-Boot shell command and updates a StateTestEnv |
| 106 | object. |
| 107 | |
| 108 | Args: |
Stephen Warren | 2b46724 | 2016-01-28 10:18:03 -0700 | [diff] [blame] | 109 | state_test_env: The StateTestEnv object to update. |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 110 | var: The variable name to unset. |
| 111 | |
| 112 | Returns: |
| 113 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 114 | """ |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 115 | |
| 116 | state_test_env.u_boot_console.run_command('setenv %s' % var) |
| 117 | if var in state_test_env.env: |
| 118 | del state_test_env.env[var] |
| 119 | |
| 120 | def set_var(state_test_env, var, value): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 121 | """Set an environment variable. |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 122 | |
| 123 | This both executes a U-Boot shell command and updates a StateTestEnv |
| 124 | object. |
| 125 | |
| 126 | Args: |
Stephen Warren | 2b46724 | 2016-01-28 10:18:03 -0700 | [diff] [blame] | 127 | state_test_env: The StateTestEnv object to update. |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 128 | var: The variable name to set. |
| 129 | value: The value to set the variable to. |
| 130 | |
| 131 | Returns: |
| 132 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 133 | """ |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 134 | |
Stephen Warren | 16abcd9 | 2017-11-10 11:59:15 +0100 | [diff] [blame] | 135 | bc = state_test_env.u_boot_console.config.buildconfig |
| 136 | if bc.get('config_hush_parser', None): |
| 137 | quote = '"' |
| 138 | else: |
| 139 | quote = '' |
| 140 | if ' ' in value: |
| 141 | pytest.skip('Space in variable value on non-Hush shell') |
| 142 | |
| 143 | state_test_env.u_boot_console.run_command( |
| 144 | 'setenv %s %s%s%s' % (var, quote, value, quote)) |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 145 | state_test_env.env[var] = value |
| 146 | |
| 147 | def validate_empty(state_test_env, var): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 148 | """Validate that a variable is not set, using U-Boot shell commands. |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 149 | |
| 150 | Args: |
| 151 | var: The variable name to test. |
| 152 | |
| 153 | Returns: |
| 154 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 155 | """ |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 156 | |
Heinrich Schuchardt | ff3fbc6 | 2020-09-10 12:09:03 +0200 | [diff] [blame] | 157 | response = state_test_env.u_boot_console.run_command('echo ${%s}' % var) |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 158 | assert response == '' |
| 159 | |
| 160 | def validate_set(state_test_env, var, value): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 161 | """Validate that a variable is set, using U-Boot shell commands. |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 162 | |
| 163 | Args: |
| 164 | var: The variable name to test. |
| 165 | value: The value the variable is expected to have. |
| 166 | |
| 167 | Returns: |
| 168 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 169 | """ |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 170 | |
| 171 | # echo does not preserve leading, internal, or trailing whitespace in the |
| 172 | # value. printenv does, and hence allows more complete testing. |
| 173 | response = state_test_env.u_boot_console.run_command('printenv %s' % var) |
| 174 | assert response == ('%s=%s' % (var, value)) |
| 175 | |
| 176 | def test_env_echo_exists(state_test_env): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 177 | """Test echoing a variable that exists.""" |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 178 | |
| 179 | var = state_test_env.get_existent_var() |
| 180 | value = state_test_env.env[var] |
| 181 | validate_set(state_test_env, var, value) |
| 182 | |
Michal Simek | e710ab7 | 2017-05-15 14:29:02 +0200 | [diff] [blame] | 183 | @pytest.mark.buildconfigspec('cmd_echo') |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 184 | def test_env_echo_non_existent(state_test_env): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 185 | """Test echoing a variable that doesn't exist.""" |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 186 | |
| 187 | var = state_test_env.set_var |
| 188 | validate_empty(state_test_env, var) |
| 189 | |
| 190 | def test_env_printenv_non_existent(state_test_env): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 191 | """Test printenv error message for non-existant variables.""" |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 192 | |
| 193 | var = state_test_env.set_var |
| 194 | c = state_test_env.u_boot_console |
| 195 | with c.disable_check('error_notification'): |
| 196 | response = c.run_command('printenv %s' % var) |
Heinrich Schuchardt | d189603 | 2021-11-26 23:30:27 +0100 | [diff] [blame] | 197 | assert response == '## Error: "%s" not defined' % var |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 198 | |
Michal Simek | e710ab7 | 2017-05-15 14:29:02 +0200 | [diff] [blame] | 199 | @pytest.mark.buildconfigspec('cmd_echo') |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 200 | def test_env_unset_non_existent(state_test_env): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 201 | """Test unsetting a nonexistent variable.""" |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 202 | |
| 203 | var = state_test_env.get_non_existent_var() |
| 204 | unset_var(state_test_env, var) |
| 205 | validate_empty(state_test_env, var) |
| 206 | |
| 207 | def test_env_set_non_existent(state_test_env): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 208 | """Test set a non-existant variable.""" |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 209 | |
| 210 | var = state_test_env.set_var |
| 211 | value = 'foo' |
| 212 | set_var(state_test_env, var, value) |
| 213 | validate_set(state_test_env, var, value) |
| 214 | |
| 215 | def test_env_set_existing(state_test_env): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 216 | """Test setting an existant variable.""" |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 217 | |
| 218 | var = state_test_env.set_var |
| 219 | value = 'bar' |
| 220 | set_var(state_test_env, var, value) |
| 221 | validate_set(state_test_env, var, value) |
| 222 | |
Michal Simek | e710ab7 | 2017-05-15 14:29:02 +0200 | [diff] [blame] | 223 | @pytest.mark.buildconfigspec('cmd_echo') |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 224 | def test_env_unset_existing(state_test_env): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 225 | """Test unsetting a variable.""" |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 226 | |
| 227 | var = state_test_env.set_var |
| 228 | unset_var(state_test_env, var) |
| 229 | validate_empty(state_test_env, var) |
| 230 | |
| 231 | def test_env_expansion_spaces(state_test_env): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 232 | """Test expanding a variable that contains a space in its value.""" |
Stephen Warren | d631062 | 2016-01-15 11:15:26 -0700 | [diff] [blame] | 233 | |
| 234 | var_space = None |
| 235 | var_test = None |
| 236 | try: |
| 237 | var_space = state_test_env.get_non_existent_var() |
| 238 | set_var(state_test_env, var_space, ' ') |
| 239 | |
| 240 | var_test = state_test_env.get_non_existent_var() |
| 241 | value = ' 1${%(var_space)s}${%(var_space)s} 2 ' % locals() |
| 242 | set_var(state_test_env, var_test, value) |
| 243 | value = ' 1 2 ' |
| 244 | validate_set(state_test_env, var_test, value) |
| 245 | finally: |
| 246 | if var_space: |
| 247 | unset_var(state_test_env, var_space) |
| 248 | if var_test: |
| 249 | unset_var(state_test_env, var_test) |
Quentin Schulz | e74def5 | 2018-07-09 19:16:30 +0200 | [diff] [blame] | 250 | |
| 251 | @pytest.mark.buildconfigspec('cmd_importenv') |
| 252 | def test_env_import_checksum_no_size(state_test_env): |
| 253 | """Test that omitted ('-') size parameter with checksum validation fails the |
| 254 | env import function. |
| 255 | """ |
| 256 | c = state_test_env.u_boot_console |
| 257 | ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console) |
| 258 | addr = '%08x' % ram_base |
| 259 | |
| 260 | with c.disable_check('error_notification'): |
| 261 | response = c.run_command('env import -c %s -' % addr) |
Heinrich Schuchardt | d189603 | 2021-11-26 23:30:27 +0100 | [diff] [blame] | 262 | assert response == '## Error: external checksum format must pass size' |
Quentin Schulz | e74def5 | 2018-07-09 19:16:30 +0200 | [diff] [blame] | 263 | |
| 264 | @pytest.mark.buildconfigspec('cmd_importenv') |
| 265 | def test_env_import_whitelist_checksum_no_size(state_test_env): |
| 266 | """Test that omitted ('-') size parameter with checksum validation fails the |
| 267 | env import function when variables are passed as parameters. |
| 268 | """ |
| 269 | c = state_test_env.u_boot_console |
| 270 | ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console) |
| 271 | addr = '%08x' % ram_base |
| 272 | |
| 273 | with c.disable_check('error_notification'): |
| 274 | response = c.run_command('env import -c %s - foo1 foo2 foo4' % addr) |
Heinrich Schuchardt | d189603 | 2021-11-26 23:30:27 +0100 | [diff] [blame] | 275 | assert response == '## Error: external checksum format must pass size' |
Quentin Schulz | e74def5 | 2018-07-09 19:16:30 +0200 | [diff] [blame] | 276 | |
| 277 | @pytest.mark.buildconfigspec('cmd_exportenv') |
| 278 | @pytest.mark.buildconfigspec('cmd_importenv') |
| 279 | def test_env_import_whitelist(state_test_env): |
| 280 | """Test importing only a handful of env variables from an environment.""" |
| 281 | c = state_test_env.u_boot_console |
| 282 | ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console) |
| 283 | addr = '%08x' % ram_base |
| 284 | |
| 285 | set_var(state_test_env, 'foo1', 'bar1') |
| 286 | set_var(state_test_env, 'foo2', 'bar2') |
| 287 | set_var(state_test_env, 'foo3', 'bar3') |
| 288 | |
| 289 | c.run_command('env export %s' % addr) |
| 290 | |
| 291 | unset_var(state_test_env, 'foo1') |
| 292 | set_var(state_test_env, 'foo2', 'test2') |
| 293 | set_var(state_test_env, 'foo4', 'bar4') |
| 294 | |
| 295 | # no foo1 in current env, foo2 overridden, foo3 should be of the value |
| 296 | # before exporting and foo4 should be of the value before importing. |
| 297 | c.run_command('env import %s - foo1 foo2 foo4' % addr) |
| 298 | |
| 299 | validate_set(state_test_env, 'foo1', 'bar1') |
| 300 | validate_set(state_test_env, 'foo2', 'bar2') |
| 301 | validate_set(state_test_env, 'foo3', 'bar3') |
| 302 | validate_set(state_test_env, 'foo4', 'bar4') |
| 303 | |
| 304 | # Cleanup test environment |
| 305 | unset_var(state_test_env, 'foo1') |
| 306 | unset_var(state_test_env, 'foo2') |
| 307 | unset_var(state_test_env, 'foo3') |
| 308 | unset_var(state_test_env, 'foo4') |
| 309 | |
| 310 | @pytest.mark.buildconfigspec('cmd_exportenv') |
| 311 | @pytest.mark.buildconfigspec('cmd_importenv') |
| 312 | def test_env_import_whitelist_delete(state_test_env): |
| 313 | |
| 314 | """Test importing only a handful of env variables from an environment, with. |
| 315 | deletion if a var A that is passed to env import is not in the |
| 316 | environment to be imported. |
| 317 | """ |
| 318 | c = state_test_env.u_boot_console |
| 319 | ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console) |
| 320 | addr = '%08x' % ram_base |
| 321 | |
| 322 | set_var(state_test_env, 'foo1', 'bar1') |
| 323 | set_var(state_test_env, 'foo2', 'bar2') |
| 324 | set_var(state_test_env, 'foo3', 'bar3') |
| 325 | |
| 326 | c.run_command('env export %s' % addr) |
| 327 | |
| 328 | unset_var(state_test_env, 'foo1') |
| 329 | set_var(state_test_env, 'foo2', 'test2') |
| 330 | set_var(state_test_env, 'foo4', 'bar4') |
| 331 | |
| 332 | # no foo1 in current env, foo2 overridden, foo3 should be of the value |
| 333 | # before exporting and foo4 should be empty. |
| 334 | c.run_command('env import -d %s - foo1 foo2 foo4' % addr) |
| 335 | |
| 336 | validate_set(state_test_env, 'foo1', 'bar1') |
| 337 | validate_set(state_test_env, 'foo2', 'bar2') |
| 338 | validate_set(state_test_env, 'foo3', 'bar3') |
| 339 | validate_empty(state_test_env, 'foo4') |
| 340 | |
| 341 | # Cleanup test environment |
| 342 | unset_var(state_test_env, 'foo1') |
| 343 | unset_var(state_test_env, 'foo2') |
| 344 | unset_var(state_test_env, 'foo3') |
| 345 | unset_var(state_test_env, 'foo4') |
Patrick Delaunay | dc4d1c5 | 2020-06-19 14:03:37 +0200 | [diff] [blame] | 346 | |
| 347 | @pytest.mark.buildconfigspec('cmd_nvedit_info') |
| 348 | def test_env_info(state_test_env): |
| 349 | |
| 350 | """Test 'env info' command with all possible options. |
| 351 | """ |
| 352 | c = state_test_env.u_boot_console |
| 353 | |
| 354 | response = c.run_command('env info') |
| 355 | nb_line = 0 |
| 356 | for l in response.split('\n'): |
| 357 | if 'env_valid = ' in l: |
| 358 | assert '= invalid' in l or '= valid' in l or '= redundant' in l |
| 359 | nb_line += 1 |
| 360 | elif 'env_ready =' in l or 'env_use_default =' in l: |
| 361 | assert '= true' in l or '= false' in l |
| 362 | nb_line += 1 |
| 363 | else: |
Heinrich Schuchardt | d189603 | 2021-11-26 23:30:27 +0100 | [diff] [blame] | 364 | assert True |
Patrick Delaunay | dc4d1c5 | 2020-06-19 14:03:37 +0200 | [diff] [blame] | 365 | assert nb_line == 3 |
| 366 | |
| 367 | response = c.run_command('env info -p -d') |
Heinrich Schuchardt | d189603 | 2021-11-26 23:30:27 +0100 | [diff] [blame] | 368 | assert 'Default environment is used' in response or \ |
| 369 | "Environment was loaded from persistent storage" in response |
| 370 | assert 'Environment can be persisted' in response or \ |
| 371 | "Environment cannot be persisted" in response |
Patrick Delaunay | dc4d1c5 | 2020-06-19 14:03:37 +0200 | [diff] [blame] | 372 | |
| 373 | response = c.run_command('env info -p -d -q') |
| 374 | assert response == "" |
| 375 | |
| 376 | response = c.run_command('env info -p -q') |
| 377 | assert response == "" |
| 378 | |
| 379 | response = c.run_command('env info -d -q') |
| 380 | assert response == "" |
| 381 | |
| 382 | @pytest.mark.boardspec('sandbox') |
| 383 | @pytest.mark.buildconfigspec('cmd_nvedit_info') |
| 384 | @pytest.mark.buildconfigspec('cmd_echo') |
| 385 | def test_env_info_sandbox(state_test_env): |
Patrick Delaunay | dc4d1c5 | 2020-06-19 14:03:37 +0200 | [diff] [blame] | 386 | """Test 'env info' command result with several options on sandbox |
| 387 | with a known ENV configuration: ready & default & persistent |
| 388 | """ |
| 389 | c = state_test_env.u_boot_console |
| 390 | |
| 391 | response = c.run_command('env info') |
| 392 | assert 'env_ready = true' in response |
| 393 | assert 'env_use_default = true' in response |
| 394 | |
| 395 | response = c.run_command('env info -p -d') |
| 396 | assert 'Default environment is used' in response |
| 397 | assert 'Environment cannot be persisted' in response |
| 398 | |
| 399 | response = c.run_command('env info -d -q') |
| 400 | response = c.run_command('echo $?') |
| 401 | assert response == "0" |
| 402 | |
| 403 | response = c.run_command('env info -p -q') |
| 404 | response = c.run_command('echo $?') |
| 405 | assert response == "1" |
| 406 | |
| 407 | response = c.run_command('env info -d -p -q') |
| 408 | response = c.run_command('echo $?') |
| 409 | assert response == "1" |
Patrick Delaunay | 57951c5 | 2020-07-28 11:51:24 +0200 | [diff] [blame] | 410 | |
| 411 | def mk_env_ext4(state_test_env): |
| 412 | |
| 413 | """Create a empty ext4 file system volume.""" |
| 414 | c = state_test_env.u_boot_console |
| 415 | filename = 'env.ext4.img' |
| 416 | persistent = c.config.persistent_data_dir + '/' + filename |
| 417 | fs_img = c.config.result_dir + '/' + filename |
| 418 | |
| 419 | if os.path.exists(persistent): |
| 420 | c.log.action('Disk image file ' + persistent + ' already exists') |
| 421 | else: |
Andy Shevchenko | 9cdba9b | 2021-02-11 16:40:09 +0200 | [diff] [blame] | 422 | # Some distributions do not add /sbin to the default PATH, where mkfs.ext4 lives |
| 423 | os.environ["PATH"] += os.pathsep + '/sbin' |
Patrick Delaunay | 57951c5 | 2020-07-28 11:51:24 +0200 | [diff] [blame] | 424 | try: |
| 425 | u_boot_utils.run_and_log(c, 'dd if=/dev/zero of=%s bs=1M count=16' % persistent) |
Stephen Warren | f7d1093 | 2020-08-04 11:28:33 -0600 | [diff] [blame] | 426 | u_boot_utils.run_and_log(c, 'mkfs.ext4 %s' % persistent) |
| 427 | sb_content = u_boot_utils.run_and_log(c, 'tune2fs -l %s' % persistent) |
| 428 | if 'metadata_csum' in sb_content: |
| 429 | u_boot_utils.run_and_log(c, 'tune2fs -O ^metadata_csum %s' % persistent) |
Patrick Delaunay | 57951c5 | 2020-07-28 11:51:24 +0200 | [diff] [blame] | 430 | except CalledProcessError: |
| 431 | call('rm -f %s' % persistent, shell=True) |
| 432 | raise |
| 433 | |
| 434 | u_boot_utils.run_and_log(c, ['cp', '-f', persistent, fs_img]) |
| 435 | return fs_img |
| 436 | |
| 437 | @pytest.mark.boardspec('sandbox') |
| 438 | @pytest.mark.buildconfigspec('cmd_echo') |
| 439 | @pytest.mark.buildconfigspec('cmd_nvedit_info') |
| 440 | @pytest.mark.buildconfigspec('cmd_nvedit_load') |
| 441 | @pytest.mark.buildconfigspec('cmd_nvedit_select') |
| 442 | @pytest.mark.buildconfigspec('env_is_in_ext4') |
| 443 | def test_env_ext4(state_test_env): |
| 444 | |
| 445 | """Test ENV in EXT4 on sandbox.""" |
| 446 | c = state_test_env.u_boot_console |
| 447 | fs_img = '' |
| 448 | try: |
| 449 | fs_img = mk_env_ext4(state_test_env) |
| 450 | |
| 451 | c.run_command('host bind 0 %s' % fs_img) |
| 452 | |
| 453 | response = c.run_command('ext4ls host 0:0') |
| 454 | assert 'uboot.env' not in response |
| 455 | |
| 456 | # force env location: EXT4 (prio 1 in sandbox) |
| 457 | response = c.run_command('env select EXT4') |
| 458 | assert 'Select Environment on EXT4: OK' in response |
| 459 | |
| 460 | response = c.run_command('env save') |
| 461 | assert 'Saving Environment to EXT4' in response |
| 462 | |
| 463 | response = c.run_command('env load') |
| 464 | assert 'Loading Environment from EXT4... OK' in response |
| 465 | |
| 466 | response = c.run_command('ext4ls host 0:0') |
| 467 | assert '8192 uboot.env' in response |
| 468 | |
| 469 | response = c.run_command('env info') |
| 470 | assert 'env_valid = valid' in response |
| 471 | assert 'env_ready = true' in response |
| 472 | assert 'env_use_default = false' in response |
| 473 | |
| 474 | response = c.run_command('env info -p -d') |
| 475 | assert 'Environment was loaded from persistent storage' in response |
| 476 | assert 'Environment can be persisted' in response |
| 477 | |
| 478 | response = c.run_command('env info -d -q') |
| 479 | assert response == "" |
| 480 | response = c.run_command('echo $?') |
| 481 | assert response == "1" |
| 482 | |
| 483 | response = c.run_command('env info -p -q') |
| 484 | assert response == "" |
| 485 | response = c.run_command('echo $?') |
| 486 | assert response == "0" |
| 487 | |
Patrick Delaunay | fe6e1f1 | 2020-07-28 11:51:27 +0200 | [diff] [blame] | 488 | response = c.run_command('env erase') |
| 489 | assert 'OK' in response |
| 490 | |
| 491 | response = c.run_command('env load') |
| 492 | assert 'Loading Environment from EXT4... ' in response |
| 493 | assert 'bad CRC, using default environment' in response |
| 494 | |
| 495 | response = c.run_command('env info') |
| 496 | assert 'env_valid = invalid' in response |
| 497 | assert 'env_ready = true' in response |
| 498 | assert 'env_use_default = true' in response |
| 499 | |
| 500 | response = c.run_command('env info -p -d') |
| 501 | assert 'Default environment is used' in response |
| 502 | assert 'Environment can be persisted' in response |
| 503 | |
Patrick Delaunay | 57951c5 | 2020-07-28 11:51:24 +0200 | [diff] [blame] | 504 | # restore env location: NOWHERE (prio 0 in sandbox) |
| 505 | response = c.run_command('env select nowhere') |
| 506 | assert 'Select Environment on nowhere: OK' in response |
| 507 | |
| 508 | response = c.run_command('env load') |
| 509 | assert 'Loading Environment from nowhere... OK' in response |
| 510 | |
| 511 | response = c.run_command('env info') |
| 512 | assert 'env_valid = invalid' in response |
| 513 | assert 'env_ready = true' in response |
| 514 | assert 'env_use_default = true' in response |
| 515 | |
| 516 | response = c.run_command('env info -p -d') |
| 517 | assert 'Default environment is used' in response |
| 518 | assert 'Environment cannot be persisted' in response |
| 519 | |
| 520 | finally: |
| 521 | if fs_img: |
| 522 | call('rm -f %s' % fs_img, shell=True) |
Simon Glass | 7327fe7 | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 523 | |
| 524 | def test_env_text(u_boot_console): |
| 525 | """Test the script that converts the environment to a text file""" |
| 526 | |
| 527 | def check_script(intext, expect_val): |
| 528 | """Check a test case |
| 529 | |
| 530 | Args: |
| 531 | intext: Text to pass to the script |
| 532 | expect_val: Expected value of the CONFIG_EXTRA_ENV_TEXT string, or |
| 533 | None if we expect it not to be defined |
| 534 | """ |
| 535 | with tempfile.TemporaryDirectory() as path: |
| 536 | fname = os.path.join(path, 'infile') |
| 537 | with open(fname, 'w') as inf: |
| 538 | print(intext, file=inf) |
| 539 | result = u_boot_utils.run_and_log(cons, ['awk', '-f', script, fname]) |
| 540 | if expect_val is not None: |
| 541 | expect = '#define CONFIG_EXTRA_ENV_TEXT "%s"\n' % expect_val |
| 542 | assert result == expect |
| 543 | else: |
| 544 | assert result == '' |
| 545 | |
| 546 | cons = u_boot_console |
| 547 | script = os.path.join(cons.config.source_dir, 'scripts', 'env2string.awk') |
| 548 | |
| 549 | # simple script with a single var |
| 550 | check_script('fred=123', 'fred=123\\0') |
| 551 | |
| 552 | # no vars |
| 553 | check_script('', None) |
| 554 | |
| 555 | # two vars |
| 556 | check_script('''fred=123 |
Simon Glass | 6e638af | 2022-03-12 22:47:49 -0700 | [diff] [blame] | 557 | mary=456''', 'fred=123\\0mary=456\\0') |
Simon Glass | 7327fe7 | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 558 | |
| 559 | # blank lines |
| 560 | check_script('''fred=123 |
| 561 | |
| 562 | |
Simon Glass | 6e638af | 2022-03-12 22:47:49 -0700 | [diff] [blame] | 563 | mary=456 |
Simon Glass | 7327fe7 | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 564 | |
Simon Glass | 6e638af | 2022-03-12 22:47:49 -0700 | [diff] [blame] | 565 | ''', 'fred=123\\0mary=456\\0') |
Simon Glass | 7327fe7 | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 566 | |
| 567 | # append |
| 568 | check_script('''fred=123 |
Simon Glass | 6e638af | 2022-03-12 22:47:49 -0700 | [diff] [blame] | 569 | mary=456 |
| 570 | fred+= 456''', 'fred=123 456\\0mary=456\\0') |
Simon Glass | 7327fe7 | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 571 | |
| 572 | # append from empty |
| 573 | check_script('''fred= |
Simon Glass | 6e638af | 2022-03-12 22:47:49 -0700 | [diff] [blame] | 574 | mary=456 |
| 575 | fred+= 456''', 'fred= 456\\0mary=456\\0') |
Simon Glass | 7327fe7 | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 576 | |
| 577 | # variable with + in it |
Simon Glass | 6e638af | 2022-03-12 22:47:49 -0700 | [diff] [blame] | 578 | check_script('fred+mary=123', 'fred+mary=123\\0') |
Simon Glass | 7327fe7 | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 579 | |
| 580 | # ignores variables that are empty |
| 581 | check_script('''fred= |
| 582 | fred+= |
Simon Glass | 6e638af | 2022-03-12 22:47:49 -0700 | [diff] [blame] | 583 | mary=456''', 'mary=456\\0') |
Simon Glass | 7327fe7 | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 584 | |
| 585 | # single-character env name |
Simon Glass | 6e638af | 2022-03-12 22:47:49 -0700 | [diff] [blame] | 586 | check_script('''m=123 |
Simon Glass | 7327fe7 | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 587 | e=456 |
Simon Glass | 6e638af | 2022-03-12 22:47:49 -0700 | [diff] [blame] | 588 | m+= 456''', 'e=456\\0m=123 456\\0') |
Simon Glass | 7327fe7 | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 589 | |
| 590 | # contains quotes |
| 591 | check_script('''fred="my var" |
Simon Glass | 6e638af | 2022-03-12 22:47:49 -0700 | [diff] [blame] | 592 | mary=another"''', 'fred=\\"my var\\"\\0mary=another\\"\\0') |
Simon Glass | 7327fe7 | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 593 | |
| 594 | # variable name ending in + |
| 595 | check_script('''fred\\+=my var |
| 596 | fred++= again''', 'fred+=my var again\\0') |
| 597 | |
| 598 | # variable name containing + |
| 599 | check_script('''fred+jane=both |
| 600 | fred+jane+=again |
Simon Glass | 6e638af | 2022-03-12 22:47:49 -0700 | [diff] [blame] | 601 | mary=456''', 'fred+jane=bothagain\\0mary=456\\0') |
Simon Glass | 7327fe7 | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 602 | |
| 603 | # multi-line vars - new vars always start at column 1 |
| 604 | check_script('''fred=first |
| 605 | second |
| 606 | \tthird with tab |
| 607 | |
| 608 | after blank |
| 609 | confusing=oops |
Simon Glass | 6e638af | 2022-03-12 22:47:49 -0700 | [diff] [blame] | 610 | mary=another"''', 'fred=first second third with tab after blank confusing=oops\\0mary=another\\"\\0') |
Simon Glass | 7327fe7 | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 611 | |
| 612 | # real-world example |
| 613 | check_script('''ubifs_boot= |
| 614 | env exists bootubipart || |
| 615 | env set bootubipart UBI; |
| 616 | env exists bootubivol || |
| 617 | env set bootubivol boot; |
| 618 | if ubi part ${bootubipart} && |
| 619 | ubifsmount ubi${devnum}:${bootubivol}; |
| 620 | then |
| 621 | devtype=ubi; |
| 622 | run scan_dev_for_boot; |
| 623 | fi |
| 624 | ''', |
| 625 | 'ubifs_boot=env exists bootubipart || env set bootubipart UBI; ' |
| 626 | 'env exists bootubivol || env set bootubivol boot; ' |
| 627 | 'if ubi part ${bootubipart} && ubifsmount ubi${devnum}:${bootubivol}; ' |
| 628 | 'then devtype=ubi; run scan_dev_for_boot; fi\\0') |