1# SPDX-License-Identifier: GPL-2.0
2# Copyright (c) 2015 Stephen Warren
3# Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
4
5"""
6Test operation of shell commands relating to environment variables.
7"""
8
9import os
10import os.path
11import re
12from subprocess import call, CalledProcessError
13import tempfile
14
15import pytest
16import utils
17
18# FIXME: This might be useful for other tests;
19# perhaps refactor it into ConsoleBase or some other state object?
20class StateTestEnv(object):
21    """Container that represents the state of all U-Boot environment variables.
22    This enables quick determination of existant/non-existant variable
23    names.
24    """
25
26    def __init__(self, ubman):
27        """Initialize a new StateTestEnv object.
28
29        Args:
30            ubman: A U-Boot console.
31
32        Returns:
33            Nothing.
34        """
35
36        self.ubman = ubman
37        self.get_env()
38        self.set_var = self.get_non_existent_var()
39
40    def get_env(self):
41        """Read all current environment variables from U-Boot.
42
43        Args:
44            None.
45
46        Returns:
47            Nothing.
48        """
49
50        if self.ubman.config.buildconfig.get(
51                'config_version_variable', 'n') == 'y':
52            with self.ubman.disable_check('main_signon'):
53                response = self.ubman.run_command('printenv')
54        else:
55            response = self.ubman.run_command('printenv')
56        self.env = {}
57        for l in response.splitlines():
58            if not '=' in l:
59                continue
60            (var, value) = l.split('=', 1)
61            self.env[var] = value
62
63    def get_existent_var(self):
64        """Return the name of an environment variable that exists.
65
66        Args:
67            None.
68
69        Returns:
70            The name of an environment variable.
71        """
72
73        for var in self.env:
74            return var
75
76    def get_non_existent_var(self):
77        """Return the name of an environment variable that does not exist.
78
79        Args:
80            None.
81
82        Returns:
83            The name of an environment variable.
84        """
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
93ste = None
94@pytest.fixture(scope='function')
95def state_test_env(ubman):
96    """pytest fixture to provide a StateTestEnv object to tests."""
97
98    global ste
99    if not ste:
100        ste = StateTestEnv(ubman)
101    return ste
102
103def unset_var(state_test_env, var):
104    """Unset an environment variable.
105
106    This both executes a U-Boot shell command and updates a StateTestEnv
107    object.
108
109    Args:
110        state_test_env: The StateTestEnv object to update.
111        var: The variable name to unset.
112
113    Returns:
114        Nothing.
115    """
116
117    state_test_env.ubman.run_command('setenv %s' % var)
118    if var in state_test_env.env:
119        del state_test_env.env[var]
120
121def set_var(state_test_env, var, value):
122    """Set an environment variable.
123
124    This both executes a U-Boot shell command and updates a StateTestEnv
125    object.
126
127    Args:
128        state_test_env: The StateTestEnv object to update.
129        var: The variable name to set.
130        value: The value to set the variable to.
131
132    Returns:
133        Nothing.
134    """
135
136    bc = state_test_env.ubman.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.ubman.run_command(
145        'setenv %s %s%s%s' % (var, quote, value, quote))
146    state_test_env.env[var] = value
147
148def validate_empty(state_test_env, var):
149    """Validate that a variable is not set, using U-Boot shell commands.
150
151    Args:
152        var: The variable name to test.
153
154    Returns:
155        Nothing.
156    """
157
158    response = state_test_env.ubman.run_command('echo ${%s}' % var)
159    assert response == ''
160
161def validate_set(state_test_env, var, value):
162    """Validate that a variable is set, using U-Boot shell commands.
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.
170    """
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.ubman.run_command('printenv %s' % var)
175    assert response == ('%s=%s' % (var, value))
176
177@pytest.mark.boardspec('sandbox')
178def test_env_initial_env_file(ubman):
179    """Test that the u-boot-initial-env make target works"""
180    builddir = 'O=' + ubman.config.build_dir
181    envfile = ubman.config.build_dir + '/u-boot-initial-env'
182
183    # remove if already exists from an older run
184    try:
185        os.remove(envfile)
186    except:
187        pass
188
189    utils.run_and_log(ubman, ['make', builddir, 'u-boot-initial-env'])
190
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
199def test_env_echo_exists(state_test_env):
200    """Test echoing a variable that exists."""
201
202    var = state_test_env.get_existent_var()
203    value = state_test_env.env[var]
204    validate_set(state_test_env, var, value)
205
206@pytest.mark.buildconfigspec('cmd_echo')
207def test_env_echo_non_existent(state_test_env):
208    """Test echoing a variable that doesn't exist."""
209
210    var = state_test_env.set_var
211    validate_empty(state_test_env, var)
212
213def test_env_printenv_non_existent(state_test_env):
214    """Test printenv error message for non-existant variables."""
215
216    var = state_test_env.set_var
217    c = state_test_env.ubman
218    with c.disable_check('error_notification'):
219        response = c.run_command('printenv %s' % var)
220    assert response == '## Error: "%s" not defined' % var
221
222@pytest.mark.buildconfigspec('cmd_echo')
223def test_env_unset_non_existent(state_test_env):
224    """Test unsetting a nonexistent variable."""
225
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):
231    """Test set a non-existant variable."""
232
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):
239    """Test setting an existant variable."""
240
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
246@pytest.mark.buildconfigspec('cmd_echo')
247def test_env_unset_existing(state_test_env):
248    """Test unsetting a variable."""
249
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):
255    """Test expanding a variable that contains a space in its value."""
256
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)
273
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    """
279    c = state_test_env.ubman
280    ram_base = utils.find_ram_base(state_test_env.ubman)
281    addr = '%08x' % ram_base
282
283    with c.disable_check('error_notification'):
284        response = c.run_command('env import -c %s -' % addr)
285    assert response == '## Error: external checksum format must pass size'
286
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    """
292    c = state_test_env.ubman
293    ram_base = utils.find_ram_base(state_test_env.ubman)
294    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)
298    assert response == '## Error: external checksum format must pass size'
299
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."""
304    c = state_test_env.ubman
305    ram_base = utils.find_ram_base(state_test_env.ubman)
306    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    """
341    c = state_test_env.ubman
342    ram_base = utils.find_ram_base(state_test_env.ubman)
343    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')
369
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    """
375    c = state_test_env.ubman
376
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:
387            assert True
388    assert nb_line == 3
389
390    response = c.run_command('env info -p -d')
391    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
395
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):
409    """Test 'env info' command result with several options on sandbox
410       with a known ENV configuration: ready & default & persistent
411    """
412    c = state_test_env.ubman
413
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"
433
434def mk_env_ext4(state_test_env):
435
436    """Create a empty ext4 file system volume."""
437    c = state_test_env.ubman
438    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:
445        # Some distributions do not add /sbin to the default PATH, where mkfs.ext4 lives
446        os.environ["PATH"] += os.pathsep + '/sbin'
447        try:
448            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)
451            if 'metadata_csum' in sb_content:
452                utils.run_and_log(c, 'tune2fs -O ^metadata_csum %s' % persistent)
453        except CalledProcessError:
454            call('rm -f %s' % persistent, shell=True)
455            raise
456
457    utils.run_and_log(c, ['cp',  '-f', persistent, fs_img])
458    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."""
469    c = state_test_env.ubman
470    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')
490        assert '8192   uboot.env' in response
491
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
511        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
527        # 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)
546
547def test_env_text(ubman):
548    """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)
562            result = utils.run_and_log(ubman, ['awk', '-f', script, fname])
563            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
569    script = os.path.join(ubman.config.source_dir, 'scripts', 'env2string.awk')
570
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
579mary=456''', 'fred=123\\0mary=456\\0')
580
581    # blank lines
582    check_script('''fred=123
583
584
585mary=456
586
587''', 'fred=123\\0mary=456\\0')
588
589    # append
590    check_script('''fred=123
591mary=456
592fred+= 456''', 'fred=123 456\\0mary=456\\0')
593
594    # append from empty
595    check_script('''fred=
596mary=456
597fred+= 456''', 'fred= 456\\0mary=456\\0')
598
599    # variable with + in it
600    check_script('fred+mary=123', 'fred+mary=123\\0')
601
602    # ignores variables that are empty
603    check_script('''fred=
604fred+=
605mary=456''', 'mary=456\\0')
606
607    # single-character env name
608    check_script('''m=123
609e=456
610m+= 456''', 'e=456\\0m=123 456\\0')
611
612    # contains quotes
613    check_script('''fred="my var"
614mary=another"''', 'fred=\\"my var\\"\\0mary=another\\"\\0')
615
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
623mary=456''', 'fred+jane=bothagain\\0mary=456\\0')
624
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
632mary=another"''', 'fred=first second third with tab after blank confusing=oops\\0mary=another\\"\\0')
633
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')
651