2018-05-06 21:58:06 +00:00
|
|
|
# SPDX-License-Identifier: GPL-2.0
|
2016-01-15 18:15:26 +00:00
|
|
|
# Copyright (c) 2015 Stephen Warren
|
|
|
|
# Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
|
2021-11-26 22:30:27 +00:00
|
|
|
"""
|
|
|
|
Test operation of shell commands relating to environment variables.
|
|
|
|
"""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
2020-07-28 09:51:24 +00:00
|
|
|
import os
|
|
|
|
import os.path
|
2022-12-09 12:09:56 +00:00
|
|
|
import re
|
2021-11-26 22:30:27 +00:00
|
|
|
from subprocess import call, CalledProcessError
|
2021-10-22 03:08:46 +00:00
|
|
|
import tempfile
|
2020-07-28 09:51:24 +00:00
|
|
|
|
2016-01-15 18:15:26 +00:00
|
|
|
import pytest
|
test/py: add test for whitelist of variables while importing environment
This tests that the importing of an environment with a specified
whitelist works as intended.
If there are variables passed as parameter to the env import command,
those only should be imported in the current environment.
For each variable passed as parameter, if
- foo is bar in current env and bar2 in exported env, after importing
exported env, foo shall be bar2,
- foo does not exist in current env and foo is bar2 in exported env,
after importing exported env, foo shall be bar2,
- foo is bar in current env and does not exist in exported env (but is
passed as parameter), after importing exported env, foo shall be empty
ONLY if the -d option is passed to env import, otherwise foo shall be
bar,
Any variable not passed as parameter should be left untouched.
Two other tests are made to test that size cannot be '-' if the checksum
protection is enabled.
Signed-off-by: Quentin Schulz <quentin.schulz@bootlin.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Stephen Warren <swarren@nvidia.com>
Tested-by: Stephen Warren <swarren@nvidia.com>
2018-07-09 17:16:30 +00:00
|
|
|
import u_boot_utils
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
# FIXME: This might be useful for other tests;
|
|
|
|
# perhaps refactor it into ConsoleBase or some other state object?
|
|
|
|
class StateTestEnv(object):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Container that represents the state of all U-Boot environment variables.
|
2016-01-15 18:15:26 +00:00
|
|
|
This enables quick determination of existant/non-existant variable
|
|
|
|
names.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
def __init__(self, u_boot_console):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Initialize a new StateTestEnv object.
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
u_boot_console: A U-Boot console.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
self.u_boot_console = u_boot_console
|
|
|
|
self.get_env()
|
|
|
|
self.set_var = self.get_non_existent_var()
|
|
|
|
|
|
|
|
def get_env(self):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Read all current environment variables from U-Boot.
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
None.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
2016-06-16 18:59:34 +00:00
|
|
|
if self.u_boot_console.config.buildconfig.get(
|
|
|
|
'config_version_variable', 'n') == 'y':
|
2016-06-07 06:31:15 +00:00
|
|
|
with self.u_boot_console.disable_check('main_signon'):
|
|
|
|
response = self.u_boot_console.run_command('printenv')
|
|
|
|
else:
|
|
|
|
response = self.u_boot_console.run_command('printenv')
|
2016-01-15 18:15:26 +00:00
|
|
|
self.env = {}
|
|
|
|
for l in response.splitlines():
|
|
|
|
if not '=' in l:
|
|
|
|
continue
|
test_env: don't strip() printenv results
get_env() was originally written to strip() the output of printenv to
isolate the test from any whitespace changes in printenv's output.
However, this throws away any whitespace in the variable value, which can
cause issues when test code expects to see that whitespace. In fact,
printenv never adds any whitespace at all, so there's no need to strip.
The strip causes a practical problem for test_env_echo_exists() if
state_test_env.get_existent_var() happens to choose a U-Boot variable that
contains trailing whitespace. This is true for variable boot_targets.
With Python 2, get_existent_var() never returned boot_targets so this
issue never caused a practical problem.
With Python 3, get_existent_var does sometimes return boot_targets, no
doubt due to Python 3's different dict hash key order implementation,
about 0.5-2% of the time, so this test appears intermittent. With the
strip removed, this intermittency is solved, since the test passes for all
possible U-Boot variables.
Signed-off-by: Stephen Warren <swarren@nvidia.com>
2019-12-18 18:37:21 +00:00
|
|
|
(var, value) = l.split('=', 1)
|
2016-01-15 18:15:26 +00:00
|
|
|
self.env[var] = value
|
|
|
|
|
|
|
|
def get_existent_var(self):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Return the name of an environment variable that exists.
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
None.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The name of an environment variable.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
for var in self.env:
|
|
|
|
return var
|
|
|
|
|
|
|
|
def get_non_existent_var(self):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Return the name of an environment variable that does not exist.
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
None.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The name of an environment variable.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
n = 0
|
|
|
|
while True:
|
|
|
|
var = 'test_env_' + str(n)
|
|
|
|
if var not in self.env:
|
|
|
|
return var
|
|
|
|
n += 1
|
|
|
|
|
test/py: move U-Boot respawn trigger to the test core
Prior to this change, U-Boot was lazilly (re-)spawned if/when a test
attempted to interact with it, and no active connection existed. This
approach was simple, yet had the disadvantage that U-Boot might be
spawned in the middle of a test function, e.g. after the test had already
performed actions such as creating data files, etc. In that case, this
could cause the log to contain the sequence (1) some test logs, (2)
U-Boot's boot process, (3) the rest of that test's logs. This isn't
optimally readable. This issue will affect the upcoming DFU and enhanced
UMS tests.
This change converts u_boot_console to be a function-scoped fixture, so
that pytest attempts to re-create the object for each test invocation.
This allows the fixture factory function to ensure that U-Boot is spawned
prior to every test. In practice, the same object is returned each time
so there is essentially no additional overhead due to this change.
This allows us to remove:
- The explicit ensure_spawned() call from test_sleep, since the core now
ensures that the spawn happens before the test code is executed.
- The laxy calls to ensure_spawned() in the u_boot_console_*
implementations.
The one downside is that test_env's "state_ttest_env" fixture must be
converted to a function-scoped fixture too, since a module-scoped fixture
cannot use a function-scoped fixture. To avoid overhead, we use the same
trick of returning the same object each time.
Signed-off-by: Stephen Warren <swarren@nvidia.com>
Acked-by: Simon Glass <sjg@chromium.org>
2016-01-22 19:30:08 +00:00
|
|
|
ste = None
|
|
|
|
@pytest.fixture(scope='function')
|
2016-01-15 18:15:26 +00:00
|
|
|
def state_test_env(u_boot_console):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""pytest fixture to provide a StateTestEnv object to tests."""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
test/py: move U-Boot respawn trigger to the test core
Prior to this change, U-Boot was lazilly (re-)spawned if/when a test
attempted to interact with it, and no active connection existed. This
approach was simple, yet had the disadvantage that U-Boot might be
spawned in the middle of a test function, e.g. after the test had already
performed actions such as creating data files, etc. In that case, this
could cause the log to contain the sequence (1) some test logs, (2)
U-Boot's boot process, (3) the rest of that test's logs. This isn't
optimally readable. This issue will affect the upcoming DFU and enhanced
UMS tests.
This change converts u_boot_console to be a function-scoped fixture, so
that pytest attempts to re-create the object for each test invocation.
This allows the fixture factory function to ensure that U-Boot is spawned
prior to every test. In practice, the same object is returned each time
so there is essentially no additional overhead due to this change.
This allows us to remove:
- The explicit ensure_spawned() call from test_sleep, since the core now
ensures that the spawn happens before the test code is executed.
- The laxy calls to ensure_spawned() in the u_boot_console_*
implementations.
The one downside is that test_env's "state_ttest_env" fixture must be
converted to a function-scoped fixture too, since a module-scoped fixture
cannot use a function-scoped fixture. To avoid overhead, we use the same
trick of returning the same object each time.
Signed-off-by: Stephen Warren <swarren@nvidia.com>
Acked-by: Simon Glass <sjg@chromium.org>
2016-01-22 19:30:08 +00:00
|
|
|
global ste
|
|
|
|
if not ste:
|
|
|
|
ste = StateTestEnv(u_boot_console)
|
|
|
|
return ste
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
def unset_var(state_test_env, var):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Unset an environment variable.
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
This both executes a U-Boot shell command and updates a StateTestEnv
|
|
|
|
object.
|
|
|
|
|
|
|
|
Args:
|
2016-01-28 17:18:03 +00:00
|
|
|
state_test_env: The StateTestEnv object to update.
|
2016-01-15 18:15:26 +00:00
|
|
|
var: The variable name to unset.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
state_test_env.u_boot_console.run_command('setenv %s' % var)
|
|
|
|
if var in state_test_env.env:
|
|
|
|
del state_test_env.env[var]
|
|
|
|
|
|
|
|
def set_var(state_test_env, var, value):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Set an environment variable.
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
This both executes a U-Boot shell command and updates a StateTestEnv
|
|
|
|
object.
|
|
|
|
|
|
|
|
Args:
|
2016-01-28 17:18:03 +00:00
|
|
|
state_test_env: The StateTestEnv object to update.
|
2016-01-15 18:15:26 +00:00
|
|
|
var: The variable name to set.
|
|
|
|
value: The value to set the variable to.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
2017-11-10 10:59:15 +00:00
|
|
|
bc = state_test_env.u_boot_console.config.buildconfig
|
|
|
|
if bc.get('config_hush_parser', None):
|
|
|
|
quote = '"'
|
|
|
|
else:
|
|
|
|
quote = ''
|
|
|
|
if ' ' in value:
|
|
|
|
pytest.skip('Space in variable value on non-Hush shell')
|
|
|
|
|
|
|
|
state_test_env.u_boot_console.run_command(
|
|
|
|
'setenv %s %s%s%s' % (var, quote, value, quote))
|
2016-01-15 18:15:26 +00:00
|
|
|
state_test_env.env[var] = value
|
|
|
|
|
|
|
|
def validate_empty(state_test_env, var):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Validate that a variable is not set, using U-Boot shell commands.
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
var: The variable name to test.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
2020-09-10 10:09:03 +00:00
|
|
|
response = state_test_env.u_boot_console.run_command('echo ${%s}' % var)
|
2016-01-15 18:15:26 +00:00
|
|
|
assert response == ''
|
|
|
|
|
|
|
|
def validate_set(state_test_env, var, value):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Validate that a variable is set, using U-Boot shell commands.
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
var: The variable name to test.
|
|
|
|
value: The value the variable is expected to have.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
# echo does not preserve leading, internal, or trailing whitespace in the
|
|
|
|
# value. printenv does, and hence allows more complete testing.
|
|
|
|
response = state_test_env.u_boot_console.run_command('printenv %s' % var)
|
|
|
|
assert response == ('%s=%s' % (var, value))
|
|
|
|
|
2022-12-09 12:09:56 +00:00
|
|
|
@pytest.mark.boardspec('sandbox')
|
|
|
|
def test_env_initial_env_file(u_boot_console):
|
|
|
|
"""Test that the u-boot-initial-env make target works"""
|
|
|
|
cons = u_boot_console
|
|
|
|
builddir = 'O=' + cons.config.build_dir
|
|
|
|
envfile = cons.config.build_dir + '/u-boot-initial-env'
|
|
|
|
|
|
|
|
# remove if already exists from an older run
|
|
|
|
try:
|
|
|
|
os.remove(envfile)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
u_boot_utils.run_and_log(cons, ['make', builddir, 'u-boot-initial-env'])
|
|
|
|
|
|
|
|
assert os.path.exists(envfile)
|
|
|
|
|
|
|
|
# assume that every environment has a board variable, e.g. board=sandbox
|
|
|
|
with open(envfile, 'r') as file:
|
|
|
|
env = file.read()
|
|
|
|
regex = re.compile('board=.+\\n')
|
|
|
|
assert re.search(regex, env)
|
|
|
|
|
2016-01-15 18:15:26 +00:00
|
|
|
def test_env_echo_exists(state_test_env):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Test echoing a variable that exists."""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
var = state_test_env.get_existent_var()
|
|
|
|
value = state_test_env.env[var]
|
|
|
|
validate_set(state_test_env, var, value)
|
|
|
|
|
2017-05-15 12:29:02 +00:00
|
|
|
@pytest.mark.buildconfigspec('cmd_echo')
|
2016-01-15 18:15:26 +00:00
|
|
|
def test_env_echo_non_existent(state_test_env):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Test echoing a variable that doesn't exist."""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
var = state_test_env.set_var
|
|
|
|
validate_empty(state_test_env, var)
|
|
|
|
|
|
|
|
def test_env_printenv_non_existent(state_test_env):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Test printenv error message for non-existant variables."""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
var = state_test_env.set_var
|
|
|
|
c = state_test_env.u_boot_console
|
|
|
|
with c.disable_check('error_notification'):
|
|
|
|
response = c.run_command('printenv %s' % var)
|
2021-11-26 22:30:27 +00:00
|
|
|
assert response == '## Error: "%s" not defined' % var
|
2016-01-15 18:15:26 +00:00
|
|
|
|
2017-05-15 12:29:02 +00:00
|
|
|
@pytest.mark.buildconfigspec('cmd_echo')
|
2016-01-15 18:15:26 +00:00
|
|
|
def test_env_unset_non_existent(state_test_env):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Test unsetting a nonexistent variable."""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
var = state_test_env.get_non_existent_var()
|
|
|
|
unset_var(state_test_env, var)
|
|
|
|
validate_empty(state_test_env, var)
|
|
|
|
|
|
|
|
def test_env_set_non_existent(state_test_env):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Test set a non-existant variable."""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
var = state_test_env.set_var
|
|
|
|
value = 'foo'
|
|
|
|
set_var(state_test_env, var, value)
|
|
|
|
validate_set(state_test_env, var, value)
|
|
|
|
|
|
|
|
def test_env_set_existing(state_test_env):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Test setting an existant variable."""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
var = state_test_env.set_var
|
|
|
|
value = 'bar'
|
|
|
|
set_var(state_test_env, var, value)
|
|
|
|
validate_set(state_test_env, var, value)
|
|
|
|
|
2017-05-15 12:29:02 +00:00
|
|
|
@pytest.mark.buildconfigspec('cmd_echo')
|
2016-01-15 18:15:26 +00:00
|
|
|
def test_env_unset_existing(state_test_env):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Test unsetting a variable."""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
var = state_test_env.set_var
|
|
|
|
unset_var(state_test_env, var)
|
|
|
|
validate_empty(state_test_env, var)
|
|
|
|
|
|
|
|
def test_env_expansion_spaces(state_test_env):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Test expanding a variable that contains a space in its value."""
|
2016-01-15 18:15:26 +00:00
|
|
|
|
|
|
|
var_space = None
|
|
|
|
var_test = None
|
|
|
|
try:
|
|
|
|
var_space = state_test_env.get_non_existent_var()
|
|
|
|
set_var(state_test_env, var_space, ' ')
|
|
|
|
|
|
|
|
var_test = state_test_env.get_non_existent_var()
|
|
|
|
value = ' 1${%(var_space)s}${%(var_space)s} 2 ' % locals()
|
|
|
|
set_var(state_test_env, var_test, value)
|
|
|
|
value = ' 1 2 '
|
|
|
|
validate_set(state_test_env, var_test, value)
|
|
|
|
finally:
|
|
|
|
if var_space:
|
|
|
|
unset_var(state_test_env, var_space)
|
|
|
|
if var_test:
|
|
|
|
unset_var(state_test_env, var_test)
|
test/py: add test for whitelist of variables while importing environment
This tests that the importing of an environment with a specified
whitelist works as intended.
If there are variables passed as parameter to the env import command,
those only should be imported in the current environment.
For each variable passed as parameter, if
- foo is bar in current env and bar2 in exported env, after importing
exported env, foo shall be bar2,
- foo does not exist in current env and foo is bar2 in exported env,
after importing exported env, foo shall be bar2,
- foo is bar in current env and does not exist in exported env (but is
passed as parameter), after importing exported env, foo shall be empty
ONLY if the -d option is passed to env import, otherwise foo shall be
bar,
Any variable not passed as parameter should be left untouched.
Two other tests are made to test that size cannot be '-' if the checksum
protection is enabled.
Signed-off-by: Quentin Schulz <quentin.schulz@bootlin.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Stephen Warren <swarren@nvidia.com>
Tested-by: Stephen Warren <swarren@nvidia.com>
2018-07-09 17:16:30 +00:00
|
|
|
|
|
|
|
@pytest.mark.buildconfigspec('cmd_importenv')
|
|
|
|
def test_env_import_checksum_no_size(state_test_env):
|
|
|
|
"""Test that omitted ('-') size parameter with checksum validation fails the
|
|
|
|
env import function.
|
|
|
|
"""
|
|
|
|
c = state_test_env.u_boot_console
|
|
|
|
ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console)
|
|
|
|
addr = '%08x' % ram_base
|
|
|
|
|
|
|
|
with c.disable_check('error_notification'):
|
|
|
|
response = c.run_command('env import -c %s -' % addr)
|
2021-11-26 22:30:27 +00:00
|
|
|
assert response == '## Error: external checksum format must pass size'
|
test/py: add test for whitelist of variables while importing environment
This tests that the importing of an environment with a specified
whitelist works as intended.
If there are variables passed as parameter to the env import command,
those only should be imported in the current environment.
For each variable passed as parameter, if
- foo is bar in current env and bar2 in exported env, after importing
exported env, foo shall be bar2,
- foo does not exist in current env and foo is bar2 in exported env,
after importing exported env, foo shall be bar2,
- foo is bar in current env and does not exist in exported env (but is
passed as parameter), after importing exported env, foo shall be empty
ONLY if the -d option is passed to env import, otherwise foo shall be
bar,
Any variable not passed as parameter should be left untouched.
Two other tests are made to test that size cannot be '-' if the checksum
protection is enabled.
Signed-off-by: Quentin Schulz <quentin.schulz@bootlin.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Stephen Warren <swarren@nvidia.com>
Tested-by: Stephen Warren <swarren@nvidia.com>
2018-07-09 17:16:30 +00:00
|
|
|
|
|
|
|
@pytest.mark.buildconfigspec('cmd_importenv')
|
|
|
|
def test_env_import_whitelist_checksum_no_size(state_test_env):
|
|
|
|
"""Test that omitted ('-') size parameter with checksum validation fails the
|
|
|
|
env import function when variables are passed as parameters.
|
|
|
|
"""
|
|
|
|
c = state_test_env.u_boot_console
|
|
|
|
ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console)
|
|
|
|
addr = '%08x' % ram_base
|
|
|
|
|
|
|
|
with c.disable_check('error_notification'):
|
|
|
|
response = c.run_command('env import -c %s - foo1 foo2 foo4' % addr)
|
2021-11-26 22:30:27 +00:00
|
|
|
assert response == '## Error: external checksum format must pass size'
|
test/py: add test for whitelist of variables while importing environment
This tests that the importing of an environment with a specified
whitelist works as intended.
If there are variables passed as parameter to the env import command,
those only should be imported in the current environment.
For each variable passed as parameter, if
- foo is bar in current env and bar2 in exported env, after importing
exported env, foo shall be bar2,
- foo does not exist in current env and foo is bar2 in exported env,
after importing exported env, foo shall be bar2,
- foo is bar in current env and does not exist in exported env (but is
passed as parameter), after importing exported env, foo shall be empty
ONLY if the -d option is passed to env import, otherwise foo shall be
bar,
Any variable not passed as parameter should be left untouched.
Two other tests are made to test that size cannot be '-' if the checksum
protection is enabled.
Signed-off-by: Quentin Schulz <quentin.schulz@bootlin.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Stephen Warren <swarren@nvidia.com>
Tested-by: Stephen Warren <swarren@nvidia.com>
2018-07-09 17:16:30 +00:00
|
|
|
|
|
|
|
@pytest.mark.buildconfigspec('cmd_exportenv')
|
|
|
|
@pytest.mark.buildconfigspec('cmd_importenv')
|
|
|
|
def test_env_import_whitelist(state_test_env):
|
|
|
|
"""Test importing only a handful of env variables from an environment."""
|
|
|
|
c = state_test_env.u_boot_console
|
|
|
|
ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console)
|
|
|
|
addr = '%08x' % ram_base
|
|
|
|
|
|
|
|
set_var(state_test_env, 'foo1', 'bar1')
|
|
|
|
set_var(state_test_env, 'foo2', 'bar2')
|
|
|
|
set_var(state_test_env, 'foo3', 'bar3')
|
|
|
|
|
|
|
|
c.run_command('env export %s' % addr)
|
|
|
|
|
|
|
|
unset_var(state_test_env, 'foo1')
|
|
|
|
set_var(state_test_env, 'foo2', 'test2')
|
|
|
|
set_var(state_test_env, 'foo4', 'bar4')
|
|
|
|
|
|
|
|
# no foo1 in current env, foo2 overridden, foo3 should be of the value
|
|
|
|
# before exporting and foo4 should be of the value before importing.
|
|
|
|
c.run_command('env import %s - foo1 foo2 foo4' % addr)
|
|
|
|
|
|
|
|
validate_set(state_test_env, 'foo1', 'bar1')
|
|
|
|
validate_set(state_test_env, 'foo2', 'bar2')
|
|
|
|
validate_set(state_test_env, 'foo3', 'bar3')
|
|
|
|
validate_set(state_test_env, 'foo4', 'bar4')
|
|
|
|
|
|
|
|
# Cleanup test environment
|
|
|
|
unset_var(state_test_env, 'foo1')
|
|
|
|
unset_var(state_test_env, 'foo2')
|
|
|
|
unset_var(state_test_env, 'foo3')
|
|
|
|
unset_var(state_test_env, 'foo4')
|
|
|
|
|
|
|
|
@pytest.mark.buildconfigspec('cmd_exportenv')
|
|
|
|
@pytest.mark.buildconfigspec('cmd_importenv')
|
|
|
|
def test_env_import_whitelist_delete(state_test_env):
|
|
|
|
|
|
|
|
"""Test importing only a handful of env variables from an environment, with.
|
|
|
|
deletion if a var A that is passed to env import is not in the
|
|
|
|
environment to be imported.
|
|
|
|
"""
|
|
|
|
c = state_test_env.u_boot_console
|
|
|
|
ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console)
|
|
|
|
addr = '%08x' % ram_base
|
|
|
|
|
|
|
|
set_var(state_test_env, 'foo1', 'bar1')
|
|
|
|
set_var(state_test_env, 'foo2', 'bar2')
|
|
|
|
set_var(state_test_env, 'foo3', 'bar3')
|
|
|
|
|
|
|
|
c.run_command('env export %s' % addr)
|
|
|
|
|
|
|
|
unset_var(state_test_env, 'foo1')
|
|
|
|
set_var(state_test_env, 'foo2', 'test2')
|
|
|
|
set_var(state_test_env, 'foo4', 'bar4')
|
|
|
|
|
|
|
|
# no foo1 in current env, foo2 overridden, foo3 should be of the value
|
|
|
|
# before exporting and foo4 should be empty.
|
|
|
|
c.run_command('env import -d %s - foo1 foo2 foo4' % addr)
|
|
|
|
|
|
|
|
validate_set(state_test_env, 'foo1', 'bar1')
|
|
|
|
validate_set(state_test_env, 'foo2', 'bar2')
|
|
|
|
validate_set(state_test_env, 'foo3', 'bar3')
|
|
|
|
validate_empty(state_test_env, 'foo4')
|
|
|
|
|
|
|
|
# Cleanup test environment
|
|
|
|
unset_var(state_test_env, 'foo1')
|
|
|
|
unset_var(state_test_env, 'foo2')
|
|
|
|
unset_var(state_test_env, 'foo3')
|
|
|
|
unset_var(state_test_env, 'foo4')
|
2020-06-19 12:03:37 +00:00
|
|
|
|
|
|
|
@pytest.mark.buildconfigspec('cmd_nvedit_info')
|
|
|
|
def test_env_info(state_test_env):
|
|
|
|
|
|
|
|
"""Test 'env info' command with all possible options.
|
|
|
|
"""
|
|
|
|
c = state_test_env.u_boot_console
|
|
|
|
|
|
|
|
response = c.run_command('env info')
|
|
|
|
nb_line = 0
|
|
|
|
for l in response.split('\n'):
|
|
|
|
if 'env_valid = ' in l:
|
|
|
|
assert '= invalid' in l or '= valid' in l or '= redundant' in l
|
|
|
|
nb_line += 1
|
|
|
|
elif 'env_ready =' in l or 'env_use_default =' in l:
|
|
|
|
assert '= true' in l or '= false' in l
|
|
|
|
nb_line += 1
|
|
|
|
else:
|
2021-11-26 22:30:27 +00:00
|
|
|
assert True
|
2020-06-19 12:03:37 +00:00
|
|
|
assert nb_line == 3
|
|
|
|
|
|
|
|
response = c.run_command('env info -p -d')
|
2021-11-26 22:30:27 +00:00
|
|
|
assert 'Default environment is used' in response or \
|
|
|
|
"Environment was loaded from persistent storage" in response
|
|
|
|
assert 'Environment can be persisted' in response or \
|
|
|
|
"Environment cannot be persisted" in response
|
2020-06-19 12:03:37 +00:00
|
|
|
|
|
|
|
response = c.run_command('env info -p -d -q')
|
|
|
|
assert response == ""
|
|
|
|
|
|
|
|
response = c.run_command('env info -p -q')
|
|
|
|
assert response == ""
|
|
|
|
|
|
|
|
response = c.run_command('env info -d -q')
|
|
|
|
assert response == ""
|
|
|
|
|
|
|
|
@pytest.mark.boardspec('sandbox')
|
|
|
|
@pytest.mark.buildconfigspec('cmd_nvedit_info')
|
|
|
|
@pytest.mark.buildconfigspec('cmd_echo')
|
|
|
|
def test_env_info_sandbox(state_test_env):
|
|
|
|
"""Test 'env info' command result with several options on sandbox
|
|
|
|
with a known ENV configuration: ready & default & persistent
|
|
|
|
"""
|
|
|
|
c = state_test_env.u_boot_console
|
|
|
|
|
|
|
|
response = c.run_command('env info')
|
|
|
|
assert 'env_ready = true' in response
|
|
|
|
assert 'env_use_default = true' in response
|
|
|
|
|
|
|
|
response = c.run_command('env info -p -d')
|
|
|
|
assert 'Default environment is used' in response
|
|
|
|
assert 'Environment cannot be persisted' in response
|
|
|
|
|
|
|
|
response = c.run_command('env info -d -q')
|
|
|
|
response = c.run_command('echo $?')
|
|
|
|
assert response == "0"
|
|
|
|
|
|
|
|
response = c.run_command('env info -p -q')
|
|
|
|
response = c.run_command('echo $?')
|
|
|
|
assert response == "1"
|
|
|
|
|
|
|
|
response = c.run_command('env info -d -p -q')
|
|
|
|
response = c.run_command('echo $?')
|
|
|
|
assert response == "1"
|
2020-07-28 09:51:24 +00:00
|
|
|
|
|
|
|
def mk_env_ext4(state_test_env):
|
|
|
|
|
|
|
|
"""Create a empty ext4 file system volume."""
|
|
|
|
c = state_test_env.u_boot_console
|
|
|
|
filename = 'env.ext4.img'
|
|
|
|
persistent = c.config.persistent_data_dir + '/' + filename
|
|
|
|
fs_img = c.config.result_dir + '/' + filename
|
|
|
|
|
|
|
|
if os.path.exists(persistent):
|
|
|
|
c.log.action('Disk image file ' + persistent + ' already exists')
|
|
|
|
else:
|
2021-02-11 14:40:09 +00:00
|
|
|
# Some distributions do not add /sbin to the default PATH, where mkfs.ext4 lives
|
|
|
|
os.environ["PATH"] += os.pathsep + '/sbin'
|
2020-07-28 09:51:24 +00:00
|
|
|
try:
|
|
|
|
u_boot_utils.run_and_log(c, 'dd if=/dev/zero of=%s bs=1M count=16' % persistent)
|
2020-08-04 17:28:33 +00:00
|
|
|
u_boot_utils.run_and_log(c, 'mkfs.ext4 %s' % persistent)
|
|
|
|
sb_content = u_boot_utils.run_and_log(c, 'tune2fs -l %s' % persistent)
|
|
|
|
if 'metadata_csum' in sb_content:
|
|
|
|
u_boot_utils.run_and_log(c, 'tune2fs -O ^metadata_csum %s' % persistent)
|
2020-07-28 09:51:24 +00:00
|
|
|
except CalledProcessError:
|
|
|
|
call('rm -f %s' % persistent, shell=True)
|
|
|
|
raise
|
|
|
|
|
|
|
|
u_boot_utils.run_and_log(c, ['cp', '-f', persistent, fs_img])
|
|
|
|
return fs_img
|
|
|
|
|
|
|
|
@pytest.mark.boardspec('sandbox')
|
|
|
|
@pytest.mark.buildconfigspec('cmd_echo')
|
|
|
|
@pytest.mark.buildconfigspec('cmd_nvedit_info')
|
|
|
|
@pytest.mark.buildconfigspec('cmd_nvedit_load')
|
|
|
|
@pytest.mark.buildconfigspec('cmd_nvedit_select')
|
|
|
|
@pytest.mark.buildconfigspec('env_is_in_ext4')
|
|
|
|
def test_env_ext4(state_test_env):
|
|
|
|
|
|
|
|
"""Test ENV in EXT4 on sandbox."""
|
|
|
|
c = state_test_env.u_boot_console
|
|
|
|
fs_img = ''
|
|
|
|
try:
|
|
|
|
fs_img = mk_env_ext4(state_test_env)
|
|
|
|
|
|
|
|
c.run_command('host bind 0 %s' % fs_img)
|
|
|
|
|
|
|
|
response = c.run_command('ext4ls host 0:0')
|
|
|
|
assert 'uboot.env' not in response
|
|
|
|
|
|
|
|
# force env location: EXT4 (prio 1 in sandbox)
|
|
|
|
response = c.run_command('env select EXT4')
|
|
|
|
assert 'Select Environment on EXT4: OK' in response
|
|
|
|
|
|
|
|
response = c.run_command('env save')
|
|
|
|
assert 'Saving Environment to EXT4' in response
|
|
|
|
|
|
|
|
response = c.run_command('env load')
|
|
|
|
assert 'Loading Environment from EXT4... OK' in response
|
|
|
|
|
|
|
|
response = c.run_command('ext4ls host 0:0')
|
|
|
|
assert '8192 uboot.env' in response
|
|
|
|
|
|
|
|
response = c.run_command('env info')
|
|
|
|
assert 'env_valid = valid' in response
|
|
|
|
assert 'env_ready = true' in response
|
|
|
|
assert 'env_use_default = false' in response
|
|
|
|
|
|
|
|
response = c.run_command('env info -p -d')
|
|
|
|
assert 'Environment was loaded from persistent storage' in response
|
|
|
|
assert 'Environment can be persisted' in response
|
|
|
|
|
|
|
|
response = c.run_command('env info -d -q')
|
|
|
|
assert response == ""
|
|
|
|
response = c.run_command('echo $?')
|
|
|
|
assert response == "1"
|
|
|
|
|
|
|
|
response = c.run_command('env info -p -q')
|
|
|
|
assert response == ""
|
|
|
|
response = c.run_command('echo $?')
|
|
|
|
assert response == "0"
|
|
|
|
|
2020-07-28 09:51:27 +00:00
|
|
|
response = c.run_command('env erase')
|
|
|
|
assert 'OK' in response
|
|
|
|
|
|
|
|
response = c.run_command('env load')
|
|
|
|
assert 'Loading Environment from EXT4... ' in response
|
|
|
|
assert 'bad CRC, using default environment' in response
|
|
|
|
|
|
|
|
response = c.run_command('env info')
|
|
|
|
assert 'env_valid = invalid' in response
|
|
|
|
assert 'env_ready = true' in response
|
|
|
|
assert 'env_use_default = true' in response
|
|
|
|
|
|
|
|
response = c.run_command('env info -p -d')
|
|
|
|
assert 'Default environment is used' in response
|
|
|
|
assert 'Environment can be persisted' in response
|
|
|
|
|
2020-07-28 09:51:24 +00:00
|
|
|
# restore env location: NOWHERE (prio 0 in sandbox)
|
|
|
|
response = c.run_command('env select nowhere')
|
|
|
|
assert 'Select Environment on nowhere: OK' in response
|
|
|
|
|
|
|
|
response = c.run_command('env load')
|
|
|
|
assert 'Loading Environment from nowhere... OK' in response
|
|
|
|
|
|
|
|
response = c.run_command('env info')
|
|
|
|
assert 'env_valid = invalid' in response
|
|
|
|
assert 'env_ready = true' in response
|
|
|
|
assert 'env_use_default = true' in response
|
|
|
|
|
|
|
|
response = c.run_command('env info -p -d')
|
|
|
|
assert 'Default environment is used' in response
|
|
|
|
assert 'Environment cannot be persisted' in response
|
|
|
|
|
|
|
|
finally:
|
|
|
|
if fs_img:
|
|
|
|
call('rm -f %s' % fs_img, shell=True)
|
2021-10-22 03:08:46 +00:00
|
|
|
|
|
|
|
def test_env_text(u_boot_console):
|
|
|
|
"""Test the script that converts the environment to a text file"""
|
|
|
|
|
|
|
|
def check_script(intext, expect_val):
|
|
|
|
"""Check a test case
|
|
|
|
|
|
|
|
Args:
|
|
|
|
intext: Text to pass to the script
|
|
|
|
expect_val: Expected value of the CONFIG_EXTRA_ENV_TEXT string, or
|
|
|
|
None if we expect it not to be defined
|
|
|
|
"""
|
|
|
|
with tempfile.TemporaryDirectory() as path:
|
|
|
|
fname = os.path.join(path, 'infile')
|
|
|
|
with open(fname, 'w') as inf:
|
|
|
|
print(intext, file=inf)
|
|
|
|
result = u_boot_utils.run_and_log(cons, ['awk', '-f', script, fname])
|
|
|
|
if expect_val is not None:
|
|
|
|
expect = '#define CONFIG_EXTRA_ENV_TEXT "%s"\n' % expect_val
|
|
|
|
assert result == expect
|
|
|
|
else:
|
|
|
|
assert result == ''
|
|
|
|
|
|
|
|
cons = u_boot_console
|
|
|
|
script = os.path.join(cons.config.source_dir, 'scripts', 'env2string.awk')
|
|
|
|
|
|
|
|
# simple script with a single var
|
|
|
|
check_script('fred=123', 'fred=123\\0')
|
|
|
|
|
|
|
|
# no vars
|
|
|
|
check_script('', None)
|
|
|
|
|
|
|
|
# two vars
|
|
|
|
check_script('''fred=123
|
2022-03-13 05:47:49 +00:00
|
|
|
mary=456''', 'fred=123\\0mary=456\\0')
|
2021-10-22 03:08:46 +00:00
|
|
|
|
|
|
|
# blank lines
|
|
|
|
check_script('''fred=123
|
|
|
|
|
|
|
|
|
2022-03-13 05:47:49 +00:00
|
|
|
mary=456
|
2021-10-22 03:08:46 +00:00
|
|
|
|
2022-03-13 05:47:49 +00:00
|
|
|
''', 'fred=123\\0mary=456\\0')
|
2021-10-22 03:08:46 +00:00
|
|
|
|
|
|
|
# append
|
|
|
|
check_script('''fred=123
|
2022-03-13 05:47:49 +00:00
|
|
|
mary=456
|
|
|
|
fred+= 456''', 'fred=123 456\\0mary=456\\0')
|
2021-10-22 03:08:46 +00:00
|
|
|
|
|
|
|
# append from empty
|
|
|
|
check_script('''fred=
|
2022-03-13 05:47:49 +00:00
|
|
|
mary=456
|
|
|
|
fred+= 456''', 'fred= 456\\0mary=456\\0')
|
2021-10-22 03:08:46 +00:00
|
|
|
|
|
|
|
# variable with + in it
|
2022-03-13 05:47:49 +00:00
|
|
|
check_script('fred+mary=123', 'fred+mary=123\\0')
|
2021-10-22 03:08:46 +00:00
|
|
|
|
|
|
|
# ignores variables that are empty
|
|
|
|
check_script('''fred=
|
|
|
|
fred+=
|
2022-03-13 05:47:49 +00:00
|
|
|
mary=456''', 'mary=456\\0')
|
2021-10-22 03:08:46 +00:00
|
|
|
|
|
|
|
# single-character env name
|
2022-03-13 05:47:49 +00:00
|
|
|
check_script('''m=123
|
2021-10-22 03:08:46 +00:00
|
|
|
e=456
|
2022-03-13 05:47:49 +00:00
|
|
|
m+= 456''', 'e=456\\0m=123 456\\0')
|
2021-10-22 03:08:46 +00:00
|
|
|
|
|
|
|
# contains quotes
|
|
|
|
check_script('''fred="my var"
|
2022-03-13 05:47:49 +00:00
|
|
|
mary=another"''', 'fred=\\"my var\\"\\0mary=another\\"\\0')
|
2021-10-22 03:08:46 +00:00
|
|
|
|
|
|
|
# variable name ending in +
|
|
|
|
check_script('''fred\\+=my var
|
|
|
|
fred++= again''', 'fred+=my var again\\0')
|
|
|
|
|
|
|
|
# variable name containing +
|
|
|
|
check_script('''fred+jane=both
|
|
|
|
fred+jane+=again
|
2022-03-13 05:47:49 +00:00
|
|
|
mary=456''', 'fred+jane=bothagain\\0mary=456\\0')
|
2021-10-22 03:08:46 +00:00
|
|
|
|
|
|
|
# multi-line vars - new vars always start at column 1
|
|
|
|
check_script('''fred=first
|
|
|
|
second
|
|
|
|
\tthird with tab
|
|
|
|
|
|
|
|
after blank
|
|
|
|
confusing=oops
|
2022-03-13 05:47:49 +00:00
|
|
|
mary=another"''', 'fred=first second third with tab after blank confusing=oops\\0mary=another\\"\\0')
|
2021-10-22 03:08:46 +00:00
|
|
|
|
|
|
|
# real-world example
|
|
|
|
check_script('''ubifs_boot=
|
|
|
|
env exists bootubipart ||
|
|
|
|
env set bootubipart UBI;
|
|
|
|
env exists bootubivol ||
|
|
|
|
env set bootubivol boot;
|
|
|
|
if ubi part ${bootubipart} &&
|
|
|
|
ubifsmount ubi${devnum}:${bootubivol};
|
|
|
|
then
|
|
|
|
devtype=ubi;
|
|
|
|
run scan_dev_for_boot;
|
|
|
|
fi
|
|
|
|
''',
|
|
|
|
'ubifs_boot=env exists bootubipart || env set bootubipart UBI; '
|
|
|
|
'env exists bootubivol || env set bootubivol boot; '
|
|
|
|
'if ubi part ${bootubipart} && ubifsmount ubi${devnum}:${bootubivol}; '
|
|
|
|
'then devtype=ubi; run scan_dev_for_boot; fi\\0')
|