blob: 383e26c03b09c42b26d4ec67ac5a4e3b5e5af981 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0
Stephen Warrend6310622016-01-15 11:15:26 -07002# Copyright (c) 2015 Stephen Warren
3# Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
Stephen Warrend6310622016-01-15 11:15:26 -07004
Heinrich Schuchardtd1896032021-11-26 23:30:27 +01005"""
6Test operation of shell commands relating to environment variables.
7"""
Stephen Warrend6310622016-01-15 11:15:26 -07008
Patrick Delaunay57951c52020-07-28 11:51:24 +02009import os
10import os.path
Max Krummenacher7f76cde2022-12-09 13:09:56 +010011import re
Heinrich Schuchardtd1896032021-11-26 23:30:27 +010012from subprocess import call, CalledProcessError
Simon Glass7327fe72021-10-21 21:08:46 -060013import tempfile
Patrick Delaunay57951c52020-07-28 11:51:24 +020014
Stephen Warrend6310622016-01-15 11:15:26 -070015import pytest
Simon Glassfb916372025-02-09 09:07:15 -070016import utils
Stephen Warrend6310622016-01-15 11:15:26 -070017
18# FIXME: This might be useful for other tests;
19# perhaps refactor it into ConsoleBase or some other state object?
20class StateTestEnv(object):
Stephen Warren75e731e2016-01-26 13:41:30 -070021 """Container that represents the state of all U-Boot environment variables.
Stephen Warrend6310622016-01-15 11:15:26 -070022 This enables quick determination of existant/non-existant variable
23 names.
Stephen Warren75e731e2016-01-26 13:41:30 -070024 """
Stephen Warrend6310622016-01-15 11:15:26 -070025
Simon Glassddba5202025-02-09 09:07:14 -070026 def __init__(self, ubman):
Stephen Warren75e731e2016-01-26 13:41:30 -070027 """Initialize a new StateTestEnv object.
Stephen Warrend6310622016-01-15 11:15:26 -070028
29 Args:
Simon Glassddba5202025-02-09 09:07:14 -070030 ubman: A U-Boot console.
Stephen Warrend6310622016-01-15 11:15:26 -070031
32 Returns:
33 Nothing.
Stephen Warren75e731e2016-01-26 13:41:30 -070034 """
Stephen Warrend6310622016-01-15 11:15:26 -070035
Simon Glassddba5202025-02-09 09:07:14 -070036 self.ubman = ubman
Stephen Warrend6310622016-01-15 11:15:26 -070037 self.get_env()
38 self.set_var = self.get_non_existent_var()
39
40 def get_env(self):
Stephen Warren75e731e2016-01-26 13:41:30 -070041 """Read all current environment variables from U-Boot.
Stephen Warrend6310622016-01-15 11:15:26 -070042
43 Args:
44 None.
45
46 Returns:
47 Nothing.
Stephen Warren75e731e2016-01-26 13:41:30 -070048 """
Stephen Warrend6310622016-01-15 11:15:26 -070049
Simon Glassddba5202025-02-09 09:07:14 -070050 if self.ubman.config.buildconfig.get(
Stephen Warren9c401e42016-06-16 12:59:34 -060051 'config_version_variable', 'n') == 'y':
Simon Glassddba5202025-02-09 09:07:14 -070052 with self.ubman.disable_check('main_signon'):
53 response = self.ubman.run_command('printenv')
Heiko Schocher52e05e92016-06-07 08:31:15 +020054 else:
Simon Glassddba5202025-02-09 09:07:14 -070055 response = self.ubman.run_command('printenv')
Stephen Warrend6310622016-01-15 11:15:26 -070056 self.env = {}
57 for l in response.splitlines():
58 if not '=' in l:
59 continue
Stephen Warren66b5b9e2019-12-18 11:37:21 -070060 (var, value) = l.split('=', 1)
Stephen Warrend6310622016-01-15 11:15:26 -070061 self.env[var] = value
62
63 def get_existent_var(self):
Stephen Warren75e731e2016-01-26 13:41:30 -070064 """Return the name of an environment variable that exists.
Stephen Warrend6310622016-01-15 11:15:26 -070065
66 Args:
67 None.
68
69 Returns:
70 The name of an environment variable.
Stephen Warren75e731e2016-01-26 13:41:30 -070071 """
Stephen Warrend6310622016-01-15 11:15:26 -070072
73 for var in self.env:
74 return var
75
76 def get_non_existent_var(self):
Stephen Warren75e731e2016-01-26 13:41:30 -070077 """Return the name of an environment variable that does not exist.
Stephen Warrend6310622016-01-15 11:15:26 -070078
79 Args:
80 None.
81
82 Returns:
83 The name of an environment variable.
Stephen Warren75e731e2016-01-26 13:41:30 -070084 """
Stephen Warrend6310622016-01-15 11:15:26 -070085
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 Warrene1d24d02016-01-22 12:30:08 -070093ste = None
94@pytest.fixture(scope='function')
Simon Glassddba5202025-02-09 09:07:14 -070095def state_test_env(ubman):
Stephen Warren75e731e2016-01-26 13:41:30 -070096 """pytest fixture to provide a StateTestEnv object to tests."""
Stephen Warrend6310622016-01-15 11:15:26 -070097
Stephen Warrene1d24d02016-01-22 12:30:08 -070098 global ste
99 if not ste:
Simon Glassddba5202025-02-09 09:07:14 -0700100 ste = StateTestEnv(ubman)
Stephen Warrene1d24d02016-01-22 12:30:08 -0700101 return ste
Stephen Warrend6310622016-01-15 11:15:26 -0700102
103def unset_var(state_test_env, var):
Stephen Warren75e731e2016-01-26 13:41:30 -0700104 """Unset an environment variable.
Stephen Warrend6310622016-01-15 11:15:26 -0700105
106 This both executes a U-Boot shell command and updates a StateTestEnv
107 object.
108
109 Args:
Stephen Warren2b467242016-01-28 10:18:03 -0700110 state_test_env: The StateTestEnv object to update.
Stephen Warrend6310622016-01-15 11:15:26 -0700111 var: The variable name to unset.
112
113 Returns:
114 Nothing.
Stephen Warren75e731e2016-01-26 13:41:30 -0700115 """
Stephen Warrend6310622016-01-15 11:15:26 -0700116
Simon Glassddba5202025-02-09 09:07:14 -0700117 state_test_env.ubman.run_command('setenv %s' % var)
Stephen Warrend6310622016-01-15 11:15:26 -0700118 if var in state_test_env.env:
119 del state_test_env.env[var]
120
121def set_var(state_test_env, var, value):
Stephen Warren75e731e2016-01-26 13:41:30 -0700122 """Set an environment variable.
Stephen Warrend6310622016-01-15 11:15:26 -0700123
124 This both executes a U-Boot shell command and updates a StateTestEnv
125 object.
126
127 Args:
Stephen Warren2b467242016-01-28 10:18:03 -0700128 state_test_env: The StateTestEnv object to update.
Stephen Warrend6310622016-01-15 11:15:26 -0700129 var: The variable name to set.
130 value: The value to set the variable to.
131
132 Returns:
133 Nothing.
Stephen Warren75e731e2016-01-26 13:41:30 -0700134 """
Stephen Warrend6310622016-01-15 11:15:26 -0700135
Simon Glassddba5202025-02-09 09:07:14 -0700136 bc = state_test_env.ubman.config.buildconfig
Stephen Warren16abcd92017-11-10 11:59:15 +0100137 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
Simon Glassddba5202025-02-09 09:07:14 -0700144 state_test_env.ubman.run_command(
Stephen Warren16abcd92017-11-10 11:59:15 +0100145 'setenv %s %s%s%s' % (var, quote, value, quote))
Stephen Warrend6310622016-01-15 11:15:26 -0700146 state_test_env.env[var] = value
147
148def validate_empty(state_test_env, var):
Stephen Warren75e731e2016-01-26 13:41:30 -0700149 """Validate that a variable is not set, using U-Boot shell commands.
Stephen Warrend6310622016-01-15 11:15:26 -0700150
151 Args:
152 var: The variable name to test.
153
154 Returns:
155 Nothing.
Stephen Warren75e731e2016-01-26 13:41:30 -0700156 """
Stephen Warrend6310622016-01-15 11:15:26 -0700157
Simon Glassddba5202025-02-09 09:07:14 -0700158 response = state_test_env.ubman.run_command('echo ${%s}' % var)
Stephen Warrend6310622016-01-15 11:15:26 -0700159 assert response == ''
160
161def validate_set(state_test_env, var, value):
Stephen Warren75e731e2016-01-26 13:41:30 -0700162 """Validate that a variable is set, using U-Boot shell commands.
Stephen Warrend6310622016-01-15 11:15:26 -0700163
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 Warren75e731e2016-01-26 13:41:30 -0700170 """
Stephen Warrend6310622016-01-15 11:15:26 -0700171
172 # echo does not preserve leading, internal, or trailing whitespace in the
173 # value. printenv does, and hence allows more complete testing.
Simon Glassddba5202025-02-09 09:07:14 -0700174 response = state_test_env.ubman.run_command('printenv %s' % var)
Stephen Warrend6310622016-01-15 11:15:26 -0700175 assert response == ('%s=%s' % (var, value))
176
Max Krummenacher7f76cde2022-12-09 13:09:56 +0100177@pytest.mark.boardspec('sandbox')
Simon Glassddba5202025-02-09 09:07:14 -0700178def test_env_initial_env_file(ubman):
Max Krummenacher7f76cde2022-12-09 13:09:56 +0100179 """Test that the u-boot-initial-env make target works"""
Simon Glass32701112025-02-09 09:07:17 -0700180 builddir = 'O=' + ubman.config.build_dir
181 envfile = ubman.config.build_dir + '/u-boot-initial-env'
Max Krummenacher7f76cde2022-12-09 13:09:56 +0100182
183 # remove if already exists from an older run
184 try:
185 os.remove(envfile)
186 except:
187 pass
188
Simon Glass32701112025-02-09 09:07:17 -0700189 utils.run_and_log(ubman, ['make', builddir, 'u-boot-initial-env'])
Max Krummenacher7f76cde2022-12-09 13:09:56 +0100190
191 assert os.path.exists(envfile)
192
193 # assume that every environment has a board variable, e.g. board=sandbox
194 with open(envfile, 'r') as file:
195 env = file.read()
196 regex = re.compile('board=.+\\n')
197 assert re.search(regex, env)
198
Stephen Warrend6310622016-01-15 11:15:26 -0700199def test_env_echo_exists(state_test_env):
Stephen Warren75e731e2016-01-26 13:41:30 -0700200 """Test echoing a variable that exists."""
Stephen Warrend6310622016-01-15 11:15:26 -0700201
202 var = state_test_env.get_existent_var()
203 value = state_test_env.env[var]
204 validate_set(state_test_env, var, value)
205
Michal Simeke710ab72017-05-15 14:29:02 +0200206@pytest.mark.buildconfigspec('cmd_echo')
Stephen Warrend6310622016-01-15 11:15:26 -0700207def test_env_echo_non_existent(state_test_env):
Stephen Warren75e731e2016-01-26 13:41:30 -0700208 """Test echoing a variable that doesn't exist."""
Stephen Warrend6310622016-01-15 11:15:26 -0700209
210 var = state_test_env.set_var
211 validate_empty(state_test_env, var)
212
213def test_env_printenv_non_existent(state_test_env):
Stephen Warren75e731e2016-01-26 13:41:30 -0700214 """Test printenv error message for non-existant variables."""
Stephen Warrend6310622016-01-15 11:15:26 -0700215
216 var = state_test_env.set_var
Simon Glassddba5202025-02-09 09:07:14 -0700217 c = state_test_env.ubman
Stephen Warrend6310622016-01-15 11:15:26 -0700218 with c.disable_check('error_notification'):
219 response = c.run_command('printenv %s' % var)
Heinrich Schuchardtd1896032021-11-26 23:30:27 +0100220 assert response == '## Error: "%s" not defined' % var
Stephen Warrend6310622016-01-15 11:15:26 -0700221
Michal Simeke710ab72017-05-15 14:29:02 +0200222@pytest.mark.buildconfigspec('cmd_echo')
Stephen Warrend6310622016-01-15 11:15:26 -0700223def test_env_unset_non_existent(state_test_env):
Stephen Warren75e731e2016-01-26 13:41:30 -0700224 """Test unsetting a nonexistent variable."""
Stephen Warrend6310622016-01-15 11:15:26 -0700225
226 var = state_test_env.get_non_existent_var()
227 unset_var(state_test_env, var)
228 validate_empty(state_test_env, var)
229
230def test_env_set_non_existent(state_test_env):
Stephen Warren75e731e2016-01-26 13:41:30 -0700231 """Test set a non-existant variable."""
Stephen Warrend6310622016-01-15 11:15:26 -0700232
233 var = state_test_env.set_var
234 value = 'foo'
235 set_var(state_test_env, var, value)
236 validate_set(state_test_env, var, value)
237
238def test_env_set_existing(state_test_env):
Stephen Warren75e731e2016-01-26 13:41:30 -0700239 """Test setting an existant variable."""
Stephen Warrend6310622016-01-15 11:15:26 -0700240
241 var = state_test_env.set_var
242 value = 'bar'
243 set_var(state_test_env, var, value)
244 validate_set(state_test_env, var, value)
245
Michal Simeke710ab72017-05-15 14:29:02 +0200246@pytest.mark.buildconfigspec('cmd_echo')
Stephen Warrend6310622016-01-15 11:15:26 -0700247def test_env_unset_existing(state_test_env):
Stephen Warren75e731e2016-01-26 13:41:30 -0700248 """Test unsetting a variable."""
Stephen Warrend6310622016-01-15 11:15:26 -0700249
250 var = state_test_env.set_var
251 unset_var(state_test_env, var)
252 validate_empty(state_test_env, var)
253
254def test_env_expansion_spaces(state_test_env):
Stephen Warren75e731e2016-01-26 13:41:30 -0700255 """Test expanding a variable that contains a space in its value."""
Stephen Warrend6310622016-01-15 11:15:26 -0700256
257 var_space = None
258 var_test = None
259 try:
260 var_space = state_test_env.get_non_existent_var()
261 set_var(state_test_env, var_space, ' ')
262
263 var_test = state_test_env.get_non_existent_var()
264 value = ' 1${%(var_space)s}${%(var_space)s} 2 ' % locals()
265 set_var(state_test_env, var_test, value)
266 value = ' 1 2 '
267 validate_set(state_test_env, var_test, value)
268 finally:
269 if var_space:
270 unset_var(state_test_env, var_space)
271 if var_test:
272 unset_var(state_test_env, var_test)
Quentin Schulze74def52018-07-09 19:16:30 +0200273
274@pytest.mark.buildconfigspec('cmd_importenv')
275def test_env_import_checksum_no_size(state_test_env):
276 """Test that omitted ('-') size parameter with checksum validation fails the
277 env import function.
278 """
Simon Glassddba5202025-02-09 09:07:14 -0700279 c = state_test_env.ubman
Simon Glassfb916372025-02-09 09:07:15 -0700280 ram_base = utils.find_ram_base(state_test_env.ubman)
Quentin Schulze74def52018-07-09 19:16:30 +0200281 addr = '%08x' % ram_base
282
283 with c.disable_check('error_notification'):
284 response = c.run_command('env import -c %s -' % addr)
Heinrich Schuchardtd1896032021-11-26 23:30:27 +0100285 assert response == '## Error: external checksum format must pass size'
Quentin Schulze74def52018-07-09 19:16:30 +0200286
287@pytest.mark.buildconfigspec('cmd_importenv')
288def test_env_import_whitelist_checksum_no_size(state_test_env):
289 """Test that omitted ('-') size parameter with checksum validation fails the
290 env import function when variables are passed as parameters.
291 """
Simon Glassddba5202025-02-09 09:07:14 -0700292 c = state_test_env.ubman
Simon Glassfb916372025-02-09 09:07:15 -0700293 ram_base = utils.find_ram_base(state_test_env.ubman)
Quentin Schulze74def52018-07-09 19:16:30 +0200294 addr = '%08x' % ram_base
295
296 with c.disable_check('error_notification'):
297 response = c.run_command('env import -c %s - foo1 foo2 foo4' % addr)
Heinrich Schuchardtd1896032021-11-26 23:30:27 +0100298 assert response == '## Error: external checksum format must pass size'
Quentin Schulze74def52018-07-09 19:16:30 +0200299
300@pytest.mark.buildconfigspec('cmd_exportenv')
301@pytest.mark.buildconfigspec('cmd_importenv')
302def test_env_import_whitelist(state_test_env):
303 """Test importing only a handful of env variables from an environment."""
Simon Glassddba5202025-02-09 09:07:14 -0700304 c = state_test_env.ubman
Simon Glassfb916372025-02-09 09:07:15 -0700305 ram_base = utils.find_ram_base(state_test_env.ubman)
Quentin Schulze74def52018-07-09 19:16:30 +0200306 addr = '%08x' % ram_base
307
308 set_var(state_test_env, 'foo1', 'bar1')
309 set_var(state_test_env, 'foo2', 'bar2')
310 set_var(state_test_env, 'foo3', 'bar3')
311
312 c.run_command('env export %s' % addr)
313
314 unset_var(state_test_env, 'foo1')
315 set_var(state_test_env, 'foo2', 'test2')
316 set_var(state_test_env, 'foo4', 'bar4')
317
318 # no foo1 in current env, foo2 overridden, foo3 should be of the value
319 # before exporting and foo4 should be of the value before importing.
320 c.run_command('env import %s - foo1 foo2 foo4' % addr)
321
322 validate_set(state_test_env, 'foo1', 'bar1')
323 validate_set(state_test_env, 'foo2', 'bar2')
324 validate_set(state_test_env, 'foo3', 'bar3')
325 validate_set(state_test_env, 'foo4', 'bar4')
326
327 # Cleanup test environment
328 unset_var(state_test_env, 'foo1')
329 unset_var(state_test_env, 'foo2')
330 unset_var(state_test_env, 'foo3')
331 unset_var(state_test_env, 'foo4')
332
333@pytest.mark.buildconfigspec('cmd_exportenv')
334@pytest.mark.buildconfigspec('cmd_importenv')
335def test_env_import_whitelist_delete(state_test_env):
336
337 """Test importing only a handful of env variables from an environment, with.
338 deletion if a var A that is passed to env import is not in the
339 environment to be imported.
340 """
Simon Glassddba5202025-02-09 09:07:14 -0700341 c = state_test_env.ubman
Simon Glassfb916372025-02-09 09:07:15 -0700342 ram_base = utils.find_ram_base(state_test_env.ubman)
Quentin Schulze74def52018-07-09 19:16:30 +0200343 addr = '%08x' % ram_base
344
345 set_var(state_test_env, 'foo1', 'bar1')
346 set_var(state_test_env, 'foo2', 'bar2')
347 set_var(state_test_env, 'foo3', 'bar3')
348
349 c.run_command('env export %s' % addr)
350
351 unset_var(state_test_env, 'foo1')
352 set_var(state_test_env, 'foo2', 'test2')
353 set_var(state_test_env, 'foo4', 'bar4')
354
355 # no foo1 in current env, foo2 overridden, foo3 should be of the value
356 # before exporting and foo4 should be empty.
357 c.run_command('env import -d %s - foo1 foo2 foo4' % addr)
358
359 validate_set(state_test_env, 'foo1', 'bar1')
360 validate_set(state_test_env, 'foo2', 'bar2')
361 validate_set(state_test_env, 'foo3', 'bar3')
362 validate_empty(state_test_env, 'foo4')
363
364 # Cleanup test environment
365 unset_var(state_test_env, 'foo1')
366 unset_var(state_test_env, 'foo2')
367 unset_var(state_test_env, 'foo3')
368 unset_var(state_test_env, 'foo4')
Patrick Delaunaydc4d1c52020-06-19 14:03:37 +0200369
370@pytest.mark.buildconfigspec('cmd_nvedit_info')
371def test_env_info(state_test_env):
372
373 """Test 'env info' command with all possible options.
374 """
Simon Glassddba5202025-02-09 09:07:14 -0700375 c = state_test_env.ubman
Patrick Delaunaydc4d1c52020-06-19 14:03:37 +0200376
377 response = c.run_command('env info')
378 nb_line = 0
379 for l in response.split('\n'):
380 if 'env_valid = ' in l:
381 assert '= invalid' in l or '= valid' in l or '= redundant' in l
382 nb_line += 1
383 elif 'env_ready =' in l or 'env_use_default =' in l:
384 assert '= true' in l or '= false' in l
385 nb_line += 1
386 else:
Heinrich Schuchardtd1896032021-11-26 23:30:27 +0100387 assert True
Patrick Delaunaydc4d1c52020-06-19 14:03:37 +0200388 assert nb_line == 3
389
390 response = c.run_command('env info -p -d')
Heinrich Schuchardtd1896032021-11-26 23:30:27 +0100391 assert 'Default environment is used' in response or \
392 "Environment was loaded from persistent storage" in response
393 assert 'Environment can be persisted' in response or \
394 "Environment cannot be persisted" in response
Patrick Delaunaydc4d1c52020-06-19 14:03:37 +0200395
396 response = c.run_command('env info -p -d -q')
397 assert response == ""
398
399 response = c.run_command('env info -p -q')
400 assert response == ""
401
402 response = c.run_command('env info -d -q')
403 assert response == ""
404
405@pytest.mark.boardspec('sandbox')
406@pytest.mark.buildconfigspec('cmd_nvedit_info')
407@pytest.mark.buildconfigspec('cmd_echo')
408def test_env_info_sandbox(state_test_env):
Patrick Delaunaydc4d1c52020-06-19 14:03:37 +0200409 """Test 'env info' command result with several options on sandbox
410 with a known ENV configuration: ready & default & persistent
411 """
Simon Glassddba5202025-02-09 09:07:14 -0700412 c = state_test_env.ubman
Patrick Delaunaydc4d1c52020-06-19 14:03:37 +0200413
414 response = c.run_command('env info')
415 assert 'env_ready = true' in response
416 assert 'env_use_default = true' in response
417
418 response = c.run_command('env info -p -d')
419 assert 'Default environment is used' in response
420 assert 'Environment cannot be persisted' in response
421
422 response = c.run_command('env info -d -q')
423 response = c.run_command('echo $?')
424 assert response == "0"
425
426 response = c.run_command('env info -p -q')
427 response = c.run_command('echo $?')
428 assert response == "1"
429
430 response = c.run_command('env info -d -p -q')
431 response = c.run_command('echo $?')
432 assert response == "1"
Patrick Delaunay57951c52020-07-28 11:51:24 +0200433
434def mk_env_ext4(state_test_env):
435
436 """Create a empty ext4 file system volume."""
Simon Glassddba5202025-02-09 09:07:14 -0700437 c = state_test_env.ubman
Patrick Delaunay57951c52020-07-28 11:51:24 +0200438 filename = 'env.ext4.img'
439 persistent = c.config.persistent_data_dir + '/' + filename
440 fs_img = c.config.result_dir + '/' + filename
441
442 if os.path.exists(persistent):
443 c.log.action('Disk image file ' + persistent + ' already exists')
444 else:
Andy Shevchenko9cdba9b2021-02-11 16:40:09 +0200445 # Some distributions do not add /sbin to the default PATH, where mkfs.ext4 lives
446 os.environ["PATH"] += os.pathsep + '/sbin'
Patrick Delaunay57951c52020-07-28 11:51:24 +0200447 try:
Simon Glassfb916372025-02-09 09:07:15 -0700448 utils.run_and_log(c, 'dd if=/dev/zero of=%s bs=1M count=16' % persistent)
449 utils.run_and_log(c, 'mkfs.ext4 %s' % persistent)
450 sb_content = utils.run_and_log(c, 'tune2fs -l %s' % persistent)
Stephen Warrenf7d10932020-08-04 11:28:33 -0600451 if 'metadata_csum' in sb_content:
Simon Glassfb916372025-02-09 09:07:15 -0700452 utils.run_and_log(c, 'tune2fs -O ^metadata_csum %s' % persistent)
Patrick Delaunay57951c52020-07-28 11:51:24 +0200453 except CalledProcessError:
454 call('rm -f %s' % persistent, shell=True)
455 raise
456
Simon Glassfb916372025-02-09 09:07:15 -0700457 utils.run_and_log(c, ['cp', '-f', persistent, fs_img])
Patrick Delaunay57951c52020-07-28 11:51:24 +0200458 return fs_img
459
460@pytest.mark.boardspec('sandbox')
461@pytest.mark.buildconfigspec('cmd_echo')
462@pytest.mark.buildconfigspec('cmd_nvedit_info')
463@pytest.mark.buildconfigspec('cmd_nvedit_load')
464@pytest.mark.buildconfigspec('cmd_nvedit_select')
465@pytest.mark.buildconfigspec('env_is_in_ext4')
466def test_env_ext4(state_test_env):
467
468 """Test ENV in EXT4 on sandbox."""
Simon Glassddba5202025-02-09 09:07:14 -0700469 c = state_test_env.ubman
Patrick Delaunay57951c52020-07-28 11:51:24 +0200470 fs_img = ''
471 try:
472 fs_img = mk_env_ext4(state_test_env)
473
474 c.run_command('host bind 0 %s' % fs_img)
475
476 response = c.run_command('ext4ls host 0:0')
477 assert 'uboot.env' not in response
478
479 # force env location: EXT4 (prio 1 in sandbox)
480 response = c.run_command('env select EXT4')
481 assert 'Select Environment on EXT4: OK' in response
482
483 response = c.run_command('env save')
484 assert 'Saving Environment to EXT4' in response
485
486 response = c.run_command('env load')
487 assert 'Loading Environment from EXT4... OK' in response
488
489 response = c.run_command('ext4ls host 0:0')
Heinrich Schuchardt19c781d2024-10-26 08:40:48 +0200490 assert '8192 uboot.env' in response
Patrick Delaunay57951c52020-07-28 11:51:24 +0200491
492 response = c.run_command('env info')
493 assert 'env_valid = valid' in response
494 assert 'env_ready = true' in response
495 assert 'env_use_default = false' in response
496
497 response = c.run_command('env info -p -d')
498 assert 'Environment was loaded from persistent storage' in response
499 assert 'Environment can be persisted' in response
500
501 response = c.run_command('env info -d -q')
502 assert response == ""
503 response = c.run_command('echo $?')
504 assert response == "1"
505
506 response = c.run_command('env info -p -q')
507 assert response == ""
508 response = c.run_command('echo $?')
509 assert response == "0"
510
Patrick Delaunayfe6e1f12020-07-28 11:51:27 +0200511 response = c.run_command('env erase')
512 assert 'OK' in response
513
514 response = c.run_command('env load')
515 assert 'Loading Environment from EXT4... ' in response
516 assert 'bad CRC, using default environment' in response
517
518 response = c.run_command('env info')
519 assert 'env_valid = invalid' in response
520 assert 'env_ready = true' in response
521 assert 'env_use_default = true' in response
522
523 response = c.run_command('env info -p -d')
524 assert 'Default environment is used' in response
525 assert 'Environment can be persisted' in response
526
Patrick Delaunay57951c52020-07-28 11:51:24 +0200527 # restore env location: NOWHERE (prio 0 in sandbox)
528 response = c.run_command('env select nowhere')
529 assert 'Select Environment on nowhere: OK' in response
530
531 response = c.run_command('env load')
532 assert 'Loading Environment from nowhere... OK' in response
533
534 response = c.run_command('env info')
535 assert 'env_valid = invalid' in response
536 assert 'env_ready = true' in response
537 assert 'env_use_default = true' in response
538
539 response = c.run_command('env info -p -d')
540 assert 'Default environment is used' in response
541 assert 'Environment cannot be persisted' in response
542
543 finally:
544 if fs_img:
545 call('rm -f %s' % fs_img, shell=True)
Simon Glass7327fe72021-10-21 21:08:46 -0600546
Simon Glassddba5202025-02-09 09:07:14 -0700547def test_env_text(ubman):
Simon Glass7327fe72021-10-21 21:08:46 -0600548 """Test the script that converts the environment to a text file"""
549
550 def check_script(intext, expect_val):
551 """Check a test case
552
553 Args:
554 intext: Text to pass to the script
555 expect_val: Expected value of the CONFIG_EXTRA_ENV_TEXT string, or
556 None if we expect it not to be defined
557 """
558 with tempfile.TemporaryDirectory() as path:
559 fname = os.path.join(path, 'infile')
560 with open(fname, 'w') as inf:
561 print(intext, file=inf)
Simon Glass32701112025-02-09 09:07:17 -0700562 result = utils.run_and_log(ubman, ['awk', '-f', script, fname])
Simon Glass7327fe72021-10-21 21:08:46 -0600563 if expect_val is not None:
564 expect = '#define CONFIG_EXTRA_ENV_TEXT "%s"\n' % expect_val
565 assert result == expect
566 else:
567 assert result == ''
568
Simon Glass32701112025-02-09 09:07:17 -0700569 script = os.path.join(ubman.config.source_dir, 'scripts', 'env2string.awk')
Simon Glass7327fe72021-10-21 21:08:46 -0600570
571 # simple script with a single var
572 check_script('fred=123', 'fred=123\\0')
573
574 # no vars
575 check_script('', None)
576
577 # two vars
578 check_script('''fred=123
Simon Glass6e638af2022-03-12 22:47:49 -0700579mary=456''', 'fred=123\\0mary=456\\0')
Simon Glass7327fe72021-10-21 21:08:46 -0600580
581 # blank lines
582 check_script('''fred=123
583
584
Simon Glass6e638af2022-03-12 22:47:49 -0700585mary=456
Simon Glass7327fe72021-10-21 21:08:46 -0600586
Simon Glass6e638af2022-03-12 22:47:49 -0700587''', 'fred=123\\0mary=456\\0')
Simon Glass7327fe72021-10-21 21:08:46 -0600588
589 # append
590 check_script('''fred=123
Simon Glass6e638af2022-03-12 22:47:49 -0700591mary=456
592fred+= 456''', 'fred=123 456\\0mary=456\\0')
Simon Glass7327fe72021-10-21 21:08:46 -0600593
594 # append from empty
595 check_script('''fred=
Simon Glass6e638af2022-03-12 22:47:49 -0700596mary=456
597fred+= 456''', 'fred= 456\\0mary=456\\0')
Simon Glass7327fe72021-10-21 21:08:46 -0600598
599 # variable with + in it
Simon Glass6e638af2022-03-12 22:47:49 -0700600 check_script('fred+mary=123', 'fred+mary=123\\0')
Simon Glass7327fe72021-10-21 21:08:46 -0600601
602 # ignores variables that are empty
603 check_script('''fred=
604fred+=
Simon Glass6e638af2022-03-12 22:47:49 -0700605mary=456''', 'mary=456\\0')
Simon Glass7327fe72021-10-21 21:08:46 -0600606
607 # single-character env name
Simon Glass6e638af2022-03-12 22:47:49 -0700608 check_script('''m=123
Simon Glass7327fe72021-10-21 21:08:46 -0600609e=456
Simon Glass6e638af2022-03-12 22:47:49 -0700610m+= 456''', 'e=456\\0m=123 456\\0')
Simon Glass7327fe72021-10-21 21:08:46 -0600611
612 # contains quotes
613 check_script('''fred="my var"
Simon Glass6e638af2022-03-12 22:47:49 -0700614mary=another"''', 'fred=\\"my var\\"\\0mary=another\\"\\0')
Simon Glass7327fe72021-10-21 21:08:46 -0600615
616 # variable name ending in +
617 check_script('''fred\\+=my var
618fred++= again''', 'fred+=my var again\\0')
619
620 # variable name containing +
621 check_script('''fred+jane=both
622fred+jane+=again
Simon Glass6e638af2022-03-12 22:47:49 -0700623mary=456''', 'fred+jane=bothagain\\0mary=456\\0')
Simon Glass7327fe72021-10-21 21:08:46 -0600624
625 # multi-line vars - new vars always start at column 1
626 check_script('''fred=first
627 second
628\tthird with tab
629
630 after blank
631 confusing=oops
Simon Glass6e638af2022-03-12 22:47:49 -0700632mary=another"''', 'fred=first second third with tab after blank confusing=oops\\0mary=another\\"\\0')
Simon Glass7327fe72021-10-21 21:08:46 -0600633
634 # real-world example
635 check_script('''ubifs_boot=
636 env exists bootubipart ||
637 env set bootubipart UBI;
638 env exists bootubivol ||
639 env set bootubivol boot;
640 if ubi part ${bootubipart} &&
641 ubifsmount ubi${devnum}:${bootubivol};
642 then
643 devtype=ubi;
644 run scan_dev_for_boot;
645 fi
646''',
647 'ubifs_boot=env exists bootubipart || env set bootubipart UBI; '
648 'env exists bootubivol || env set bootubivol boot; '
649 'if ubi part ${bootubipart} && ubifsmount ubi${devnum}:${bootubivol}; '
650 'then devtype=ubi; run scan_dev_for_boot; fi\\0')