blob: 6d08565f0b52e6461aa13fa3fa602fda5fa1d6da [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
Heinrich Schuchardtd1896032021-11-26 23:30:27 +010011from subprocess import call, CalledProcessError
Simon Glass7327fe72021-10-21 21:08:46 -060012import tempfile
Patrick Delaunay57951c52020-07-28 11:51:24 +020013
Stephen Warrend6310622016-01-15 11:15:26 -070014import pytest
Quentin Schulze74def52018-07-09 19:16:30 +020015import u_boot_utils
Stephen Warrend6310622016-01-15 11:15:26 -070016
17# FIXME: This might be useful for other tests;
18# perhaps refactor it into ConsoleBase or some other state object?
19class StateTestEnv(object):
Stephen Warren75e731e2016-01-26 13:41:30 -070020 """Container that represents the state of all U-Boot environment variables.
Stephen Warrend6310622016-01-15 11:15:26 -070021 This enables quick determination of existant/non-existant variable
22 names.
Stephen Warren75e731e2016-01-26 13:41:30 -070023 """
Stephen Warrend6310622016-01-15 11:15:26 -070024
25 def __init__(self, u_boot_console):
Stephen Warren75e731e2016-01-26 13:41:30 -070026 """Initialize a new StateTestEnv object.
Stephen Warrend6310622016-01-15 11:15:26 -070027
28 Args:
29 u_boot_console: A U-Boot console.
30
31 Returns:
32 Nothing.
Stephen Warren75e731e2016-01-26 13:41:30 -070033 """
Stephen Warrend6310622016-01-15 11:15:26 -070034
35 self.u_boot_console = u_boot_console
36 self.get_env()
37 self.set_var = self.get_non_existent_var()
38
39 def get_env(self):
Stephen Warren75e731e2016-01-26 13:41:30 -070040 """Read all current environment variables from U-Boot.
Stephen Warrend6310622016-01-15 11:15:26 -070041
42 Args:
43 None.
44
45 Returns:
46 Nothing.
Stephen Warren75e731e2016-01-26 13:41:30 -070047 """
Stephen Warrend6310622016-01-15 11:15:26 -070048
Stephen Warren9c401e42016-06-16 12:59:34 -060049 if self.u_boot_console.config.buildconfig.get(
50 'config_version_variable', 'n') == 'y':
Heiko Schocher52e05e92016-06-07 08:31:15 +020051 with self.u_boot_console.disable_check('main_signon'):
52 response = self.u_boot_console.run_command('printenv')
53 else:
54 response = self.u_boot_console.run_command('printenv')
Stephen Warrend6310622016-01-15 11:15:26 -070055 self.env = {}
56 for l in response.splitlines():
57 if not '=' in l:
58 continue
Stephen Warren66b5b9e2019-12-18 11:37:21 -070059 (var, value) = l.split('=', 1)
Stephen Warrend6310622016-01-15 11:15:26 -070060 self.env[var] = value
61
62 def get_existent_var(self):
Stephen Warren75e731e2016-01-26 13:41:30 -070063 """Return the name of an environment variable that exists.
Stephen Warrend6310622016-01-15 11:15:26 -070064
65 Args:
66 None.
67
68 Returns:
69 The name of an environment variable.
Stephen Warren75e731e2016-01-26 13:41:30 -070070 """
Stephen Warrend6310622016-01-15 11:15:26 -070071
72 for var in self.env:
73 return var
74
75 def get_non_existent_var(self):
Stephen Warren75e731e2016-01-26 13:41:30 -070076 """Return the name of an environment variable that does not exist.
Stephen Warrend6310622016-01-15 11:15:26 -070077
78 Args:
79 None.
80
81 Returns:
82 The name of an environment variable.
Stephen Warren75e731e2016-01-26 13:41:30 -070083 """
Stephen Warrend6310622016-01-15 11:15:26 -070084
85 n = 0
86 while True:
87 var = 'test_env_' + str(n)
88 if var not in self.env:
89 return var
90 n += 1
91
Stephen Warrene1d24d02016-01-22 12:30:08 -070092ste = None
93@pytest.fixture(scope='function')
Stephen Warrend6310622016-01-15 11:15:26 -070094def state_test_env(u_boot_console):
Stephen Warren75e731e2016-01-26 13:41:30 -070095 """pytest fixture to provide a StateTestEnv object to tests."""
Stephen Warrend6310622016-01-15 11:15:26 -070096
Stephen Warrene1d24d02016-01-22 12:30:08 -070097 global ste
98 if not ste:
99 ste = StateTestEnv(u_boot_console)
100 return ste
Stephen Warrend6310622016-01-15 11:15:26 -0700101
102def unset_var(state_test_env, var):
Stephen Warren75e731e2016-01-26 13:41:30 -0700103 """Unset an environment variable.
Stephen Warrend6310622016-01-15 11:15:26 -0700104
105 This both executes a U-Boot shell command and updates a StateTestEnv
106 object.
107
108 Args:
Stephen Warren2b467242016-01-28 10:18:03 -0700109 state_test_env: The StateTestEnv object to update.
Stephen Warrend6310622016-01-15 11:15:26 -0700110 var: The variable name to unset.
111
112 Returns:
113 Nothing.
Stephen Warren75e731e2016-01-26 13:41:30 -0700114 """
Stephen Warrend6310622016-01-15 11:15:26 -0700115
116 state_test_env.u_boot_console.run_command('setenv %s' % var)
117 if var in state_test_env.env:
118 del state_test_env.env[var]
119
120def set_var(state_test_env, var, value):
Stephen Warren75e731e2016-01-26 13:41:30 -0700121 """Set an environment variable.
Stephen Warrend6310622016-01-15 11:15:26 -0700122
123 This both executes a U-Boot shell command and updates a StateTestEnv
124 object.
125
126 Args:
Stephen Warren2b467242016-01-28 10:18:03 -0700127 state_test_env: The StateTestEnv object to update.
Stephen Warrend6310622016-01-15 11:15:26 -0700128 var: The variable name to set.
129 value: The value to set the variable to.
130
131 Returns:
132 Nothing.
Stephen Warren75e731e2016-01-26 13:41:30 -0700133 """
Stephen Warrend6310622016-01-15 11:15:26 -0700134
Stephen Warren16abcd92017-11-10 11:59:15 +0100135 bc = state_test_env.u_boot_console.config.buildconfig
136 if bc.get('config_hush_parser', None):
137 quote = '"'
138 else:
139 quote = ''
140 if ' ' in value:
141 pytest.skip('Space in variable value on non-Hush shell')
142
143 state_test_env.u_boot_console.run_command(
144 'setenv %s %s%s%s' % (var, quote, value, quote))
Stephen Warrend6310622016-01-15 11:15:26 -0700145 state_test_env.env[var] = value
146
147def validate_empty(state_test_env, var):
Stephen Warren75e731e2016-01-26 13:41:30 -0700148 """Validate that a variable is not set, using U-Boot shell commands.
Stephen Warrend6310622016-01-15 11:15:26 -0700149
150 Args:
151 var: The variable name to test.
152
153 Returns:
154 Nothing.
Stephen Warren75e731e2016-01-26 13:41:30 -0700155 """
Stephen Warrend6310622016-01-15 11:15:26 -0700156
Heinrich Schuchardtff3fbc62020-09-10 12:09:03 +0200157 response = state_test_env.u_boot_console.run_command('echo ${%s}' % var)
Stephen Warrend6310622016-01-15 11:15:26 -0700158 assert response == ''
159
160def validate_set(state_test_env, var, value):
Stephen Warren75e731e2016-01-26 13:41:30 -0700161 """Validate that a variable is set, using U-Boot shell commands.
Stephen Warrend6310622016-01-15 11:15:26 -0700162
163 Args:
164 var: The variable name to test.
165 value: The value the variable is expected to have.
166
167 Returns:
168 Nothing.
Stephen Warren75e731e2016-01-26 13:41:30 -0700169 """
Stephen Warrend6310622016-01-15 11:15:26 -0700170
171 # echo does not preserve leading, internal, or trailing whitespace in the
172 # value. printenv does, and hence allows more complete testing.
173 response = state_test_env.u_boot_console.run_command('printenv %s' % var)
174 assert response == ('%s=%s' % (var, value))
175
176def test_env_echo_exists(state_test_env):
Stephen Warren75e731e2016-01-26 13:41:30 -0700177 """Test echoing a variable that exists."""
Stephen Warrend6310622016-01-15 11:15:26 -0700178
179 var = state_test_env.get_existent_var()
180 value = state_test_env.env[var]
181 validate_set(state_test_env, var, value)
182
Michal Simeke710ab72017-05-15 14:29:02 +0200183@pytest.mark.buildconfigspec('cmd_echo')
Stephen Warrend6310622016-01-15 11:15:26 -0700184def test_env_echo_non_existent(state_test_env):
Stephen Warren75e731e2016-01-26 13:41:30 -0700185 """Test echoing a variable that doesn't exist."""
Stephen Warrend6310622016-01-15 11:15:26 -0700186
187 var = state_test_env.set_var
188 validate_empty(state_test_env, var)
189
190def test_env_printenv_non_existent(state_test_env):
Stephen Warren75e731e2016-01-26 13:41:30 -0700191 """Test printenv error message for non-existant variables."""
Stephen Warrend6310622016-01-15 11:15:26 -0700192
193 var = state_test_env.set_var
194 c = state_test_env.u_boot_console
195 with c.disable_check('error_notification'):
196 response = c.run_command('printenv %s' % var)
Heinrich Schuchardtd1896032021-11-26 23:30:27 +0100197 assert response == '## Error: "%s" not defined' % var
Stephen Warrend6310622016-01-15 11:15:26 -0700198
Michal Simeke710ab72017-05-15 14:29:02 +0200199@pytest.mark.buildconfigspec('cmd_echo')
Stephen Warrend6310622016-01-15 11:15:26 -0700200def test_env_unset_non_existent(state_test_env):
Stephen Warren75e731e2016-01-26 13:41:30 -0700201 """Test unsetting a nonexistent variable."""
Stephen Warrend6310622016-01-15 11:15:26 -0700202
203 var = state_test_env.get_non_existent_var()
204 unset_var(state_test_env, var)
205 validate_empty(state_test_env, var)
206
207def test_env_set_non_existent(state_test_env):
Stephen Warren75e731e2016-01-26 13:41:30 -0700208 """Test set a non-existant variable."""
Stephen Warrend6310622016-01-15 11:15:26 -0700209
210 var = state_test_env.set_var
211 value = 'foo'
212 set_var(state_test_env, var, value)
213 validate_set(state_test_env, var, value)
214
215def test_env_set_existing(state_test_env):
Stephen Warren75e731e2016-01-26 13:41:30 -0700216 """Test setting an existant variable."""
Stephen Warrend6310622016-01-15 11:15:26 -0700217
218 var = state_test_env.set_var
219 value = 'bar'
220 set_var(state_test_env, var, value)
221 validate_set(state_test_env, var, value)
222
Michal Simeke710ab72017-05-15 14:29:02 +0200223@pytest.mark.buildconfigspec('cmd_echo')
Stephen Warrend6310622016-01-15 11:15:26 -0700224def test_env_unset_existing(state_test_env):
Stephen Warren75e731e2016-01-26 13:41:30 -0700225 """Test unsetting a variable."""
Stephen Warrend6310622016-01-15 11:15:26 -0700226
227 var = state_test_env.set_var
228 unset_var(state_test_env, var)
229 validate_empty(state_test_env, var)
230
231def test_env_expansion_spaces(state_test_env):
Stephen Warren75e731e2016-01-26 13:41:30 -0700232 """Test expanding a variable that contains a space in its value."""
Stephen Warrend6310622016-01-15 11:15:26 -0700233
234 var_space = None
235 var_test = None
236 try:
237 var_space = state_test_env.get_non_existent_var()
238 set_var(state_test_env, var_space, ' ')
239
240 var_test = state_test_env.get_non_existent_var()
241 value = ' 1${%(var_space)s}${%(var_space)s} 2 ' % locals()
242 set_var(state_test_env, var_test, value)
243 value = ' 1 2 '
244 validate_set(state_test_env, var_test, value)
245 finally:
246 if var_space:
247 unset_var(state_test_env, var_space)
248 if var_test:
249 unset_var(state_test_env, var_test)
Quentin Schulze74def52018-07-09 19:16:30 +0200250
251@pytest.mark.buildconfigspec('cmd_importenv')
252def test_env_import_checksum_no_size(state_test_env):
253 """Test that omitted ('-') size parameter with checksum validation fails the
254 env import function.
255 """
256 c = state_test_env.u_boot_console
257 ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console)
258 addr = '%08x' % ram_base
259
260 with c.disable_check('error_notification'):
261 response = c.run_command('env import -c %s -' % addr)
Heinrich Schuchardtd1896032021-11-26 23:30:27 +0100262 assert response == '## Error: external checksum format must pass size'
Quentin Schulze74def52018-07-09 19:16:30 +0200263
264@pytest.mark.buildconfigspec('cmd_importenv')
265def test_env_import_whitelist_checksum_no_size(state_test_env):
266 """Test that omitted ('-') size parameter with checksum validation fails the
267 env import function when variables are passed as parameters.
268 """
269 c = state_test_env.u_boot_console
270 ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console)
271 addr = '%08x' % ram_base
272
273 with c.disable_check('error_notification'):
274 response = c.run_command('env import -c %s - foo1 foo2 foo4' % addr)
Heinrich Schuchardtd1896032021-11-26 23:30:27 +0100275 assert response == '## Error: external checksum format must pass size'
Quentin Schulze74def52018-07-09 19:16:30 +0200276
277@pytest.mark.buildconfigspec('cmd_exportenv')
278@pytest.mark.buildconfigspec('cmd_importenv')
279def test_env_import_whitelist(state_test_env):
280 """Test importing only a handful of env variables from an environment."""
281 c = state_test_env.u_boot_console
282 ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console)
283 addr = '%08x' % ram_base
284
285 set_var(state_test_env, 'foo1', 'bar1')
286 set_var(state_test_env, 'foo2', 'bar2')
287 set_var(state_test_env, 'foo3', 'bar3')
288
289 c.run_command('env export %s' % addr)
290
291 unset_var(state_test_env, 'foo1')
292 set_var(state_test_env, 'foo2', 'test2')
293 set_var(state_test_env, 'foo4', 'bar4')
294
295 # no foo1 in current env, foo2 overridden, foo3 should be of the value
296 # before exporting and foo4 should be of the value before importing.
297 c.run_command('env import %s - foo1 foo2 foo4' % addr)
298
299 validate_set(state_test_env, 'foo1', 'bar1')
300 validate_set(state_test_env, 'foo2', 'bar2')
301 validate_set(state_test_env, 'foo3', 'bar3')
302 validate_set(state_test_env, 'foo4', 'bar4')
303
304 # Cleanup test environment
305 unset_var(state_test_env, 'foo1')
306 unset_var(state_test_env, 'foo2')
307 unset_var(state_test_env, 'foo3')
308 unset_var(state_test_env, 'foo4')
309
310@pytest.mark.buildconfigspec('cmd_exportenv')
311@pytest.mark.buildconfigspec('cmd_importenv')
312def test_env_import_whitelist_delete(state_test_env):
313
314 """Test importing only a handful of env variables from an environment, with.
315 deletion if a var A that is passed to env import is not in the
316 environment to be imported.
317 """
318 c = state_test_env.u_boot_console
319 ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console)
320 addr = '%08x' % ram_base
321
322 set_var(state_test_env, 'foo1', 'bar1')
323 set_var(state_test_env, 'foo2', 'bar2')
324 set_var(state_test_env, 'foo3', 'bar3')
325
326 c.run_command('env export %s' % addr)
327
328 unset_var(state_test_env, 'foo1')
329 set_var(state_test_env, 'foo2', 'test2')
330 set_var(state_test_env, 'foo4', 'bar4')
331
332 # no foo1 in current env, foo2 overridden, foo3 should be of the value
333 # before exporting and foo4 should be empty.
334 c.run_command('env import -d %s - foo1 foo2 foo4' % addr)
335
336 validate_set(state_test_env, 'foo1', 'bar1')
337 validate_set(state_test_env, 'foo2', 'bar2')
338 validate_set(state_test_env, 'foo3', 'bar3')
339 validate_empty(state_test_env, 'foo4')
340
341 # Cleanup test environment
342 unset_var(state_test_env, 'foo1')
343 unset_var(state_test_env, 'foo2')
344 unset_var(state_test_env, 'foo3')
345 unset_var(state_test_env, 'foo4')
Patrick Delaunaydc4d1c52020-06-19 14:03:37 +0200346
347@pytest.mark.buildconfigspec('cmd_nvedit_info')
348def test_env_info(state_test_env):
349
350 """Test 'env info' command with all possible options.
351 """
352 c = state_test_env.u_boot_console
353
354 response = c.run_command('env info')
355 nb_line = 0
356 for l in response.split('\n'):
357 if 'env_valid = ' in l:
358 assert '= invalid' in l or '= valid' in l or '= redundant' in l
359 nb_line += 1
360 elif 'env_ready =' in l or 'env_use_default =' in l:
361 assert '= true' in l or '= false' in l
362 nb_line += 1
363 else:
Heinrich Schuchardtd1896032021-11-26 23:30:27 +0100364 assert True
Patrick Delaunaydc4d1c52020-06-19 14:03:37 +0200365 assert nb_line == 3
366
367 response = c.run_command('env info -p -d')
Heinrich Schuchardtd1896032021-11-26 23:30:27 +0100368 assert 'Default environment is used' in response or \
369 "Environment was loaded from persistent storage" in response
370 assert 'Environment can be persisted' in response or \
371 "Environment cannot be persisted" in response
Patrick Delaunaydc4d1c52020-06-19 14:03:37 +0200372
373 response = c.run_command('env info -p -d -q')
374 assert response == ""
375
376 response = c.run_command('env info -p -q')
377 assert response == ""
378
379 response = c.run_command('env info -d -q')
380 assert response == ""
381
382@pytest.mark.boardspec('sandbox')
383@pytest.mark.buildconfigspec('cmd_nvedit_info')
384@pytest.mark.buildconfigspec('cmd_echo')
385def test_env_info_sandbox(state_test_env):
Patrick Delaunaydc4d1c52020-06-19 14:03:37 +0200386 """Test 'env info' command result with several options on sandbox
387 with a known ENV configuration: ready & default & persistent
388 """
389 c = state_test_env.u_boot_console
390
391 response = c.run_command('env info')
392 assert 'env_ready = true' in response
393 assert 'env_use_default = true' in response
394
395 response = c.run_command('env info -p -d')
396 assert 'Default environment is used' in response
397 assert 'Environment cannot be persisted' in response
398
399 response = c.run_command('env info -d -q')
400 response = c.run_command('echo $?')
401 assert response == "0"
402
403 response = c.run_command('env info -p -q')
404 response = c.run_command('echo $?')
405 assert response == "1"
406
407 response = c.run_command('env info -d -p -q')
408 response = c.run_command('echo $?')
409 assert response == "1"
Patrick Delaunay57951c52020-07-28 11:51:24 +0200410
411def mk_env_ext4(state_test_env):
412
413 """Create a empty ext4 file system volume."""
414 c = state_test_env.u_boot_console
415 filename = 'env.ext4.img'
416 persistent = c.config.persistent_data_dir + '/' + filename
417 fs_img = c.config.result_dir + '/' + filename
418
419 if os.path.exists(persistent):
420 c.log.action('Disk image file ' + persistent + ' already exists')
421 else:
Andy Shevchenko9cdba9b2021-02-11 16:40:09 +0200422 # Some distributions do not add /sbin to the default PATH, where mkfs.ext4 lives
423 os.environ["PATH"] += os.pathsep + '/sbin'
Patrick Delaunay57951c52020-07-28 11:51:24 +0200424 try:
425 u_boot_utils.run_and_log(c, 'dd if=/dev/zero of=%s bs=1M count=16' % persistent)
Stephen Warrenf7d10932020-08-04 11:28:33 -0600426 u_boot_utils.run_and_log(c, 'mkfs.ext4 %s' % persistent)
427 sb_content = u_boot_utils.run_and_log(c, 'tune2fs -l %s' % persistent)
428 if 'metadata_csum' in sb_content:
429 u_boot_utils.run_and_log(c, 'tune2fs -O ^metadata_csum %s' % persistent)
Patrick Delaunay57951c52020-07-28 11:51:24 +0200430 except CalledProcessError:
431 call('rm -f %s' % persistent, shell=True)
432 raise
433
434 u_boot_utils.run_and_log(c, ['cp', '-f', persistent, fs_img])
435 return fs_img
436
437@pytest.mark.boardspec('sandbox')
438@pytest.mark.buildconfigspec('cmd_echo')
439@pytest.mark.buildconfigspec('cmd_nvedit_info')
440@pytest.mark.buildconfigspec('cmd_nvedit_load')
441@pytest.mark.buildconfigspec('cmd_nvedit_select')
442@pytest.mark.buildconfigspec('env_is_in_ext4')
443def test_env_ext4(state_test_env):
444
445 """Test ENV in EXT4 on sandbox."""
446 c = state_test_env.u_boot_console
447 fs_img = ''
448 try:
449 fs_img = mk_env_ext4(state_test_env)
450
451 c.run_command('host bind 0 %s' % fs_img)
452
453 response = c.run_command('ext4ls host 0:0')
454 assert 'uboot.env' not in response
455
456 # force env location: EXT4 (prio 1 in sandbox)
457 response = c.run_command('env select EXT4')
458 assert 'Select Environment on EXT4: OK' in response
459
460 response = c.run_command('env save')
461 assert 'Saving Environment to EXT4' in response
462
463 response = c.run_command('env load')
464 assert 'Loading Environment from EXT4... OK' in response
465
466 response = c.run_command('ext4ls host 0:0')
467 assert '8192 uboot.env' in response
468
469 response = c.run_command('env info')
470 assert 'env_valid = valid' in response
471 assert 'env_ready = true' in response
472 assert 'env_use_default = false' in response
473
474 response = c.run_command('env info -p -d')
475 assert 'Environment was loaded from persistent storage' in response
476 assert 'Environment can be persisted' in response
477
478 response = c.run_command('env info -d -q')
479 assert response == ""
480 response = c.run_command('echo $?')
481 assert response == "1"
482
483 response = c.run_command('env info -p -q')
484 assert response == ""
485 response = c.run_command('echo $?')
486 assert response == "0"
487
Patrick Delaunayfe6e1f12020-07-28 11:51:27 +0200488 response = c.run_command('env erase')
489 assert 'OK' in response
490
491 response = c.run_command('env load')
492 assert 'Loading Environment from EXT4... ' in response
493 assert 'bad CRC, using default environment' in response
494
495 response = c.run_command('env info')
496 assert 'env_valid = invalid' in response
497 assert 'env_ready = true' in response
498 assert 'env_use_default = true' in response
499
500 response = c.run_command('env info -p -d')
501 assert 'Default environment is used' in response
502 assert 'Environment can be persisted' in response
503
Patrick Delaunay57951c52020-07-28 11:51:24 +0200504 # restore env location: NOWHERE (prio 0 in sandbox)
505 response = c.run_command('env select nowhere')
506 assert 'Select Environment on nowhere: OK' in response
507
508 response = c.run_command('env load')
509 assert 'Loading Environment from nowhere... OK' in response
510
511 response = c.run_command('env info')
512 assert 'env_valid = invalid' in response
513 assert 'env_ready = true' in response
514 assert 'env_use_default = true' in response
515
516 response = c.run_command('env info -p -d')
517 assert 'Default environment is used' in response
518 assert 'Environment cannot be persisted' in response
519
520 finally:
521 if fs_img:
522 call('rm -f %s' % fs_img, shell=True)
Simon Glass7327fe72021-10-21 21:08:46 -0600523
524def test_env_text(u_boot_console):
525 """Test the script that converts the environment to a text file"""
526
527 def check_script(intext, expect_val):
528 """Check a test case
529
530 Args:
531 intext: Text to pass to the script
532 expect_val: Expected value of the CONFIG_EXTRA_ENV_TEXT string, or
533 None if we expect it not to be defined
534 """
535 with tempfile.TemporaryDirectory() as path:
536 fname = os.path.join(path, 'infile')
537 with open(fname, 'w') as inf:
538 print(intext, file=inf)
539 result = u_boot_utils.run_and_log(cons, ['awk', '-f', script, fname])
540 if expect_val is not None:
541 expect = '#define CONFIG_EXTRA_ENV_TEXT "%s"\n' % expect_val
542 assert result == expect
543 else:
544 assert result == ''
545
546 cons = u_boot_console
547 script = os.path.join(cons.config.source_dir, 'scripts', 'env2string.awk')
548
549 # simple script with a single var
550 check_script('fred=123', 'fred=123\\0')
551
552 # no vars
553 check_script('', None)
554
555 # two vars
556 check_script('''fred=123
Simon Glass6e638af2022-03-12 22:47:49 -0700557mary=456''', 'fred=123\\0mary=456\\0')
Simon Glass7327fe72021-10-21 21:08:46 -0600558
559 # blank lines
560 check_script('''fred=123
561
562
Simon Glass6e638af2022-03-12 22:47:49 -0700563mary=456
Simon Glass7327fe72021-10-21 21:08:46 -0600564
Simon Glass6e638af2022-03-12 22:47:49 -0700565''', 'fred=123\\0mary=456\\0')
Simon Glass7327fe72021-10-21 21:08:46 -0600566
567 # append
568 check_script('''fred=123
Simon Glass6e638af2022-03-12 22:47:49 -0700569mary=456
570fred+= 456''', 'fred=123 456\\0mary=456\\0')
Simon Glass7327fe72021-10-21 21:08:46 -0600571
572 # append from empty
573 check_script('''fred=
Simon Glass6e638af2022-03-12 22:47:49 -0700574mary=456
575fred+= 456''', 'fred= 456\\0mary=456\\0')
Simon Glass7327fe72021-10-21 21:08:46 -0600576
577 # variable with + in it
Simon Glass6e638af2022-03-12 22:47:49 -0700578 check_script('fred+mary=123', 'fred+mary=123\\0')
Simon Glass7327fe72021-10-21 21:08:46 -0600579
580 # ignores variables that are empty
581 check_script('''fred=
582fred+=
Simon Glass6e638af2022-03-12 22:47:49 -0700583mary=456''', 'mary=456\\0')
Simon Glass7327fe72021-10-21 21:08:46 -0600584
585 # single-character env name
Simon Glass6e638af2022-03-12 22:47:49 -0700586 check_script('''m=123
Simon Glass7327fe72021-10-21 21:08:46 -0600587e=456
Simon Glass6e638af2022-03-12 22:47:49 -0700588m+= 456''', 'e=456\\0m=123 456\\0')
Simon Glass7327fe72021-10-21 21:08:46 -0600589
590 # contains quotes
591 check_script('''fred="my var"
Simon Glass6e638af2022-03-12 22:47:49 -0700592mary=another"''', 'fred=\\"my var\\"\\0mary=another\\"\\0')
Simon Glass7327fe72021-10-21 21:08:46 -0600593
594 # variable name ending in +
595 check_script('''fred\\+=my var
596fred++= again''', 'fred+=my var again\\0')
597
598 # variable name containing +
599 check_script('''fred+jane=both
600fred+jane+=again
Simon Glass6e638af2022-03-12 22:47:49 -0700601mary=456''', 'fred+jane=bothagain\\0mary=456\\0')
Simon Glass7327fe72021-10-21 21:08:46 -0600602
603 # multi-line vars - new vars always start at column 1
604 check_script('''fred=first
605 second
606\tthird with tab
607
608 after blank
609 confusing=oops
Simon Glass6e638af2022-03-12 22:47:49 -0700610mary=another"''', 'fred=first second third with tab after blank confusing=oops\\0mary=another\\"\\0')
Simon Glass7327fe72021-10-21 21:08:46 -0600611
612 # real-world example
613 check_script('''ubifs_boot=
614 env exists bootubipart ||
615 env set bootubipart UBI;
616 env exists bootubivol ||
617 env set bootubivol boot;
618 if ubi part ${bootubipart} &&
619 ubifsmount ubi${devnum}:${bootubivol};
620 then
621 devtype=ubi;
622 run scan_dev_for_boot;
623 fi
624''',
625 'ubifs_boot=env exists bootubipart || env set bootubipart UBI; '
626 'env exists bootubivol || env set bootubivol boot; '
627 'if ubi part ${bootubipart} && ubifsmount ubi${devnum}:${bootubivol}; '
628 'then devtype=ubi; run scan_dev_for_boot; fi\\0')