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