blob: 9bdaef9373fd85c3715c62f4ed339f45e0a2768a [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
5# Test operation of shell commands relating to environment variables.
6
7import pytest
Quentin Schulze74def52018-07-09 19:16:30 +02008import u_boot_utils
Stephen Warrend6310622016-01-15 11:15:26 -07009
10# FIXME: This might be useful for other tests;
11# perhaps refactor it into ConsoleBase or some other state object?
12class StateTestEnv(object):
Stephen Warren75e731e2016-01-26 13:41:30 -070013 """Container that represents the state of all U-Boot environment variables.
Stephen Warrend6310622016-01-15 11:15:26 -070014 This enables quick determination of existant/non-existant variable
15 names.
Stephen Warren75e731e2016-01-26 13:41:30 -070016 """
Stephen Warrend6310622016-01-15 11:15:26 -070017
18 def __init__(self, u_boot_console):
Stephen Warren75e731e2016-01-26 13:41:30 -070019 """Initialize a new StateTestEnv object.
Stephen Warrend6310622016-01-15 11:15:26 -070020
21 Args:
22 u_boot_console: A U-Boot console.
23
24 Returns:
25 Nothing.
Stephen Warren75e731e2016-01-26 13:41:30 -070026 """
Stephen Warrend6310622016-01-15 11:15:26 -070027
28 self.u_boot_console = u_boot_console
29 self.get_env()
30 self.set_var = self.get_non_existent_var()
31
32 def get_env(self):
Stephen Warren75e731e2016-01-26 13:41:30 -070033 """Read all current environment variables from U-Boot.
Stephen Warrend6310622016-01-15 11:15:26 -070034
35 Args:
36 None.
37
38 Returns:
39 Nothing.
Stephen Warren75e731e2016-01-26 13:41:30 -070040 """
Stephen Warrend6310622016-01-15 11:15:26 -070041
Stephen Warren9c401e42016-06-16 12:59:34 -060042 if self.u_boot_console.config.buildconfig.get(
43 'config_version_variable', 'n') == 'y':
Heiko Schocher52e05e92016-06-07 08:31:15 +020044 with self.u_boot_console.disable_check('main_signon'):
45 response = self.u_boot_console.run_command('printenv')
46 else:
47 response = self.u_boot_console.run_command('printenv')
Stephen Warrend6310622016-01-15 11:15:26 -070048 self.env = {}
49 for l in response.splitlines():
50 if not '=' in l:
51 continue
52 (var, value) = l.strip().split('=', 1)
53 self.env[var] = value
54
55 def get_existent_var(self):
Stephen Warren75e731e2016-01-26 13:41:30 -070056 """Return the name of an environment variable that exists.
Stephen Warrend6310622016-01-15 11:15:26 -070057
58 Args:
59 None.
60
61 Returns:
62 The name of an environment variable.
Stephen Warren75e731e2016-01-26 13:41:30 -070063 """
Stephen Warrend6310622016-01-15 11:15:26 -070064
65 for var in self.env:
66 return var
67
68 def get_non_existent_var(self):
Stephen Warren75e731e2016-01-26 13:41:30 -070069 """Return the name of an environment variable that does not exist.
Stephen Warrend6310622016-01-15 11:15:26 -070070
71 Args:
72 None.
73
74 Returns:
75 The name of an environment variable.
Stephen Warren75e731e2016-01-26 13:41:30 -070076 """
Stephen Warrend6310622016-01-15 11:15:26 -070077
78 n = 0
79 while True:
80 var = 'test_env_' + str(n)
81 if var not in self.env:
82 return var
83 n += 1
84
Stephen Warrene1d24d02016-01-22 12:30:08 -070085ste = None
86@pytest.fixture(scope='function')
Stephen Warrend6310622016-01-15 11:15:26 -070087def state_test_env(u_boot_console):
Stephen Warren75e731e2016-01-26 13:41:30 -070088 """pytest fixture to provide a StateTestEnv object to tests."""
Stephen Warrend6310622016-01-15 11:15:26 -070089
Stephen Warrene1d24d02016-01-22 12:30:08 -070090 global ste
91 if not ste:
92 ste = StateTestEnv(u_boot_console)
93 return ste
Stephen Warrend6310622016-01-15 11:15:26 -070094
95def unset_var(state_test_env, var):
Stephen Warren75e731e2016-01-26 13:41:30 -070096 """Unset an environment variable.
Stephen Warrend6310622016-01-15 11:15:26 -070097
98 This both executes a U-Boot shell command and updates a StateTestEnv
99 object.
100
101 Args:
Stephen Warren2b467242016-01-28 10:18:03 -0700102 state_test_env: The StateTestEnv object to update.
Stephen Warrend6310622016-01-15 11:15:26 -0700103 var: The variable name to unset.
104
105 Returns:
106 Nothing.
Stephen Warren75e731e2016-01-26 13:41:30 -0700107 """
Stephen Warrend6310622016-01-15 11:15:26 -0700108
109 state_test_env.u_boot_console.run_command('setenv %s' % var)
110 if var in state_test_env.env:
111 del state_test_env.env[var]
112
113def set_var(state_test_env, var, value):
Stephen Warren75e731e2016-01-26 13:41:30 -0700114 """Set an environment variable.
Stephen Warrend6310622016-01-15 11:15:26 -0700115
116 This both executes a U-Boot shell command and updates a StateTestEnv
117 object.
118
119 Args:
Stephen Warren2b467242016-01-28 10:18:03 -0700120 state_test_env: The StateTestEnv object to update.
Stephen Warrend6310622016-01-15 11:15:26 -0700121 var: The variable name to set.
122 value: The value to set the variable to.
123
124 Returns:
125 Nothing.
Stephen Warren75e731e2016-01-26 13:41:30 -0700126 """
Stephen Warrend6310622016-01-15 11:15:26 -0700127
Stephen Warren16abcd92017-11-10 11:59:15 +0100128 bc = state_test_env.u_boot_console.config.buildconfig
129 if bc.get('config_hush_parser', None):
130 quote = '"'
131 else:
132 quote = ''
133 if ' ' in value:
134 pytest.skip('Space in variable value on non-Hush shell')
135
136 state_test_env.u_boot_console.run_command(
137 'setenv %s %s%s%s' % (var, quote, value, quote))
Stephen Warrend6310622016-01-15 11:15:26 -0700138 state_test_env.env[var] = value
139
140def validate_empty(state_test_env, var):
Stephen Warren75e731e2016-01-26 13:41:30 -0700141 """Validate that a variable is not set, using U-Boot shell commands.
Stephen Warrend6310622016-01-15 11:15:26 -0700142
143 Args:
144 var: The variable name to test.
145
146 Returns:
147 Nothing.
Stephen Warren75e731e2016-01-26 13:41:30 -0700148 """
Stephen Warrend6310622016-01-15 11:15:26 -0700149
150 response = state_test_env.u_boot_console.run_command('echo $%s' % var)
151 assert response == ''
152
153def validate_set(state_test_env, var, value):
Stephen Warren75e731e2016-01-26 13:41:30 -0700154 """Validate that a variable is set, using U-Boot shell commands.
Stephen Warrend6310622016-01-15 11:15:26 -0700155
156 Args:
157 var: The variable name to test.
158 value: The value the variable is expected to have.
159
160 Returns:
161 Nothing.
Stephen Warren75e731e2016-01-26 13:41:30 -0700162 """
Stephen Warrend6310622016-01-15 11:15:26 -0700163
164 # echo does not preserve leading, internal, or trailing whitespace in the
165 # value. printenv does, and hence allows more complete testing.
166 response = state_test_env.u_boot_console.run_command('printenv %s' % var)
167 assert response == ('%s=%s' % (var, value))
168
169def test_env_echo_exists(state_test_env):
Stephen Warren75e731e2016-01-26 13:41:30 -0700170 """Test echoing a variable that exists."""
Stephen Warrend6310622016-01-15 11:15:26 -0700171
172 var = state_test_env.get_existent_var()
173 value = state_test_env.env[var]
174 validate_set(state_test_env, var, value)
175
Michal Simeke710ab72017-05-15 14:29:02 +0200176@pytest.mark.buildconfigspec('cmd_echo')
Stephen Warrend6310622016-01-15 11:15:26 -0700177def test_env_echo_non_existent(state_test_env):
Stephen Warren75e731e2016-01-26 13:41:30 -0700178 """Test echoing a variable that doesn't exist."""
Stephen Warrend6310622016-01-15 11:15:26 -0700179
180 var = state_test_env.set_var
181 validate_empty(state_test_env, var)
182
183def test_env_printenv_non_existent(state_test_env):
Stephen Warren75e731e2016-01-26 13:41:30 -0700184 """Test printenv error message for non-existant variables."""
Stephen Warrend6310622016-01-15 11:15:26 -0700185
186 var = state_test_env.set_var
187 c = state_test_env.u_boot_console
188 with c.disable_check('error_notification'):
189 response = c.run_command('printenv %s' % var)
190 assert(response == '## Error: "%s" not defined' % var)
191
Michal Simeke710ab72017-05-15 14:29:02 +0200192@pytest.mark.buildconfigspec('cmd_echo')
Stephen Warrend6310622016-01-15 11:15:26 -0700193def test_env_unset_non_existent(state_test_env):
Stephen Warren75e731e2016-01-26 13:41:30 -0700194 """Test unsetting a nonexistent variable."""
Stephen Warrend6310622016-01-15 11:15:26 -0700195
196 var = state_test_env.get_non_existent_var()
197 unset_var(state_test_env, var)
198 validate_empty(state_test_env, var)
199
200def test_env_set_non_existent(state_test_env):
Stephen Warren75e731e2016-01-26 13:41:30 -0700201 """Test set a non-existant variable."""
Stephen Warrend6310622016-01-15 11:15:26 -0700202
203 var = state_test_env.set_var
204 value = 'foo'
205 set_var(state_test_env, var, value)
206 validate_set(state_test_env, var, value)
207
208def test_env_set_existing(state_test_env):
Stephen Warren75e731e2016-01-26 13:41:30 -0700209 """Test setting an existant variable."""
Stephen Warrend6310622016-01-15 11:15:26 -0700210
211 var = state_test_env.set_var
212 value = 'bar'
213 set_var(state_test_env, var, value)
214 validate_set(state_test_env, var, value)
215
Michal Simeke710ab72017-05-15 14:29:02 +0200216@pytest.mark.buildconfigspec('cmd_echo')
Stephen Warrend6310622016-01-15 11:15:26 -0700217def test_env_unset_existing(state_test_env):
Stephen Warren75e731e2016-01-26 13:41:30 -0700218 """Test unsetting a variable."""
Stephen Warrend6310622016-01-15 11:15:26 -0700219
220 var = state_test_env.set_var
221 unset_var(state_test_env, var)
222 validate_empty(state_test_env, var)
223
224def test_env_expansion_spaces(state_test_env):
Stephen Warren75e731e2016-01-26 13:41:30 -0700225 """Test expanding a variable that contains a space in its value."""
Stephen Warrend6310622016-01-15 11:15:26 -0700226
227 var_space = None
228 var_test = None
229 try:
230 var_space = state_test_env.get_non_existent_var()
231 set_var(state_test_env, var_space, ' ')
232
233 var_test = state_test_env.get_non_existent_var()
234 value = ' 1${%(var_space)s}${%(var_space)s} 2 ' % locals()
235 set_var(state_test_env, var_test, value)
236 value = ' 1 2 '
237 validate_set(state_test_env, var_test, value)
238 finally:
239 if var_space:
240 unset_var(state_test_env, var_space)
241 if var_test:
242 unset_var(state_test_env, var_test)
Quentin Schulze74def52018-07-09 19:16:30 +0200243
244@pytest.mark.buildconfigspec('cmd_importenv')
245def 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')
258def 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')
272def 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')
305def 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')