Commit b7a7c4113c231e73a1719204cefe8d82d0047e96

Authored by Quentin Schulz
Committed by Tom Rini
1 parent eaf734724f

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>

Showing 1 changed file with 97 additions and 0 deletions Side-by-side Diff

test/py/tests/test_env.py
... ... @@ -5,6 +5,7 @@
5 5 # Test operation of shell commands relating to environment variables.
6 6  
7 7 import pytest
  8 +import u_boot_utils
8 9  
9 10 # FIXME: This might be useful for other tests;
10 11 # perhaps refactor it into ConsoleBase or some other state object?
... ... @@ -239,4 +240,100 @@
239 240 unset_var(state_test_env, var_space)
240 241 if var_test:
241 242 unset_var(state_test_env, var_test)
  243 +
  244 +@pytest.mark.buildconfigspec('cmd_importenv')
  245 +def test_env_import_checksum_no_size(state_test_env):
  246 + """Test that omitted ('-') size parameter with checksum validation fails the
  247 + env import function.
  248 + """
  249 + c = state_test_env.u_boot_console
  250 + ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console)
  251 + addr = '%08x' % ram_base
  252 +
  253 + with c.disable_check('error_notification'):
  254 + response = c.run_command('env import -c %s -' % addr)
  255 + assert(response == '## Error: external checksum format must pass size')
  256 +
  257 +@pytest.mark.buildconfigspec('cmd_importenv')
  258 +def test_env_import_whitelist_checksum_no_size(state_test_env):
  259 + """Test that omitted ('-') size parameter with checksum validation fails the
  260 + env import function when variables are passed as parameters.
  261 + """
  262 + c = state_test_env.u_boot_console
  263 + ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console)
  264 + addr = '%08x' % ram_base
  265 +
  266 + with c.disable_check('error_notification'):
  267 + response = c.run_command('env import -c %s - foo1 foo2 foo4' % addr)
  268 + assert(response == '## Error: external checksum format must pass size')
  269 +
  270 +@pytest.mark.buildconfigspec('cmd_exportenv')
  271 +@pytest.mark.buildconfigspec('cmd_importenv')
  272 +def test_env_import_whitelist(state_test_env):
  273 + """Test importing only a handful of env variables from an environment."""
  274 + c = state_test_env.u_boot_console
  275 + ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console)
  276 + addr = '%08x' % ram_base
  277 +
  278 + set_var(state_test_env, 'foo1', 'bar1')
  279 + set_var(state_test_env, 'foo2', 'bar2')
  280 + set_var(state_test_env, 'foo3', 'bar3')
  281 +
  282 + c.run_command('env export %s' % addr)
  283 +
  284 + unset_var(state_test_env, 'foo1')
  285 + set_var(state_test_env, 'foo2', 'test2')
  286 + set_var(state_test_env, 'foo4', 'bar4')
  287 +
  288 + # no foo1 in current env, foo2 overridden, foo3 should be of the value
  289 + # before exporting and foo4 should be of the value before importing.
  290 + c.run_command('env import %s - foo1 foo2 foo4' % addr)
  291 +
  292 + validate_set(state_test_env, 'foo1', 'bar1')
  293 + validate_set(state_test_env, 'foo2', 'bar2')
  294 + validate_set(state_test_env, 'foo3', 'bar3')
  295 + validate_set(state_test_env, 'foo4', 'bar4')
  296 +
  297 + # Cleanup test environment
  298 + unset_var(state_test_env, 'foo1')
  299 + unset_var(state_test_env, 'foo2')
  300 + unset_var(state_test_env, 'foo3')
  301 + unset_var(state_test_env, 'foo4')
  302 +
  303 +@pytest.mark.buildconfigspec('cmd_exportenv')
  304 +@pytest.mark.buildconfigspec('cmd_importenv')
  305 +def test_env_import_whitelist_delete(state_test_env):
  306 +
  307 + """Test importing only a handful of env variables from an environment, with.
  308 + deletion if a var A that is passed to env import is not in the
  309 + environment to be imported.
  310 + """
  311 + c = state_test_env.u_boot_console
  312 + ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console)
  313 + addr = '%08x' % ram_base
  314 +
  315 + set_var(state_test_env, 'foo1', 'bar1')
  316 + set_var(state_test_env, 'foo2', 'bar2')
  317 + set_var(state_test_env, 'foo3', 'bar3')
  318 +
  319 + c.run_command('env export %s' % addr)
  320 +
  321 + unset_var(state_test_env, 'foo1')
  322 + set_var(state_test_env, 'foo2', 'test2')
  323 + set_var(state_test_env, 'foo4', 'bar4')
  324 +
  325 + # no foo1 in current env, foo2 overridden, foo3 should be of the value
  326 + # before exporting and foo4 should be empty.
  327 + c.run_command('env import -d %s - foo1 foo2 foo4' % addr)
  328 +
  329 + validate_set(state_test_env, 'foo1', 'bar1')
  330 + validate_set(state_test_env, 'foo2', 'bar2')
  331 + validate_set(state_test_env, 'foo3', 'bar3')
  332 + validate_empty(state_test_env, 'foo4')
  333 +
  334 + # Cleanup test environment
  335 + unset_var(state_test_env, 'foo1')
  336 + unset_var(state_test_env, 'foo2')
  337 + unset_var(state_test_env, 'foo3')
  338 + unset_var(state_test_env, 'foo4')