test_env.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. # SPDX-License-Identifier: GPL-2.0
  2. # Copyright (c) 2015 Stephen Warren
  3. # Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
  4. # Test operation of shell commands relating to environment variables.
  5. import os
  6. import os.path
  7. from subprocess import call, check_call, CalledProcessError
  8. import tempfile
  9. import pytest
  10. import u_boot_utils
  11. # FIXME: This might be useful for other tests;
  12. # perhaps refactor it into ConsoleBase or some other state object?
  13. class StateTestEnv(object):
  14. """Container that represents the state of all U-Boot environment variables.
  15. This enables quick determination of existant/non-existant variable
  16. names.
  17. """
  18. def __init__(self, u_boot_console):
  19. """Initialize a new StateTestEnv object.
  20. Args:
  21. u_boot_console: A U-Boot console.
  22. Returns:
  23. Nothing.
  24. """
  25. self.u_boot_console = u_boot_console
  26. self.get_env()
  27. self.set_var = self.get_non_existent_var()
  28. def get_env(self):
  29. """Read all current environment variables from U-Boot.
  30. Args:
  31. None.
  32. Returns:
  33. Nothing.
  34. """
  35. if self.u_boot_console.config.buildconfig.get(
  36. 'config_version_variable', 'n') == 'y':
  37. with self.u_boot_console.disable_check('main_signon'):
  38. response = self.u_boot_console.run_command('printenv')
  39. else:
  40. response = self.u_boot_console.run_command('printenv')
  41. self.env = {}
  42. for l in response.splitlines():
  43. if not '=' in l:
  44. continue
  45. (var, value) = l.split('=', 1)
  46. self.env[var] = value
  47. def get_existent_var(self):
  48. """Return the name of an environment variable that exists.
  49. Args:
  50. None.
  51. Returns:
  52. The name of an environment variable.
  53. """
  54. for var in self.env:
  55. return var
  56. def get_non_existent_var(self):
  57. """Return the name of an environment variable that does not exist.
  58. Args:
  59. None.
  60. Returns:
  61. The name of an environment variable.
  62. """
  63. n = 0
  64. while True:
  65. var = 'test_env_' + str(n)
  66. if var not in self.env:
  67. return var
  68. n += 1
  69. ste = None
  70. @pytest.fixture(scope='function')
  71. def state_test_env(u_boot_console):
  72. """pytest fixture to provide a StateTestEnv object to tests."""
  73. global ste
  74. if not ste:
  75. ste = StateTestEnv(u_boot_console)
  76. return ste
  77. def unset_var(state_test_env, var):
  78. """Unset an environment variable.
  79. This both executes a U-Boot shell command and updates a StateTestEnv
  80. object.
  81. Args:
  82. state_test_env: The StateTestEnv object to update.
  83. var: The variable name to unset.
  84. Returns:
  85. Nothing.
  86. """
  87. state_test_env.u_boot_console.run_command('setenv %s' % var)
  88. if var in state_test_env.env:
  89. del state_test_env.env[var]
  90. def set_var(state_test_env, var, value):
  91. """Set an environment variable.
  92. This both executes a U-Boot shell command and updates a StateTestEnv
  93. object.
  94. Args:
  95. state_test_env: The StateTestEnv object to update.
  96. var: The variable name to set.
  97. value: The value to set the variable to.
  98. Returns:
  99. Nothing.
  100. """
  101. bc = state_test_env.u_boot_console.config.buildconfig
  102. if bc.get('config_hush_parser', None):
  103. quote = '"'
  104. else:
  105. quote = ''
  106. if ' ' in value:
  107. pytest.skip('Space in variable value on non-Hush shell')
  108. state_test_env.u_boot_console.run_command(
  109. 'setenv %s %s%s%s' % (var, quote, value, quote))
  110. state_test_env.env[var] = value
  111. def validate_empty(state_test_env, var):
  112. """Validate that a variable is not set, using U-Boot shell commands.
  113. Args:
  114. var: The variable name to test.
  115. Returns:
  116. Nothing.
  117. """
  118. response = state_test_env.u_boot_console.run_command('echo ${%s}' % var)
  119. assert response == ''
  120. def validate_set(state_test_env, var, value):
  121. """Validate that a variable is set, using U-Boot shell commands.
  122. Args:
  123. var: The variable name to test.
  124. value: The value the variable is expected to have.
  125. Returns:
  126. Nothing.
  127. """
  128. # echo does not preserve leading, internal, or trailing whitespace in the
  129. # value. printenv does, and hence allows more complete testing.
  130. response = state_test_env.u_boot_console.run_command('printenv %s' % var)
  131. assert response == ('%s=%s' % (var, value))
  132. def test_env_echo_exists(state_test_env):
  133. """Test echoing a variable that exists."""
  134. var = state_test_env.get_existent_var()
  135. value = state_test_env.env[var]
  136. validate_set(state_test_env, var, value)
  137. @pytest.mark.buildconfigspec('cmd_echo')
  138. def test_env_echo_non_existent(state_test_env):
  139. """Test echoing a variable that doesn't exist."""
  140. var = state_test_env.set_var
  141. validate_empty(state_test_env, var)
  142. def test_env_printenv_non_existent(state_test_env):
  143. """Test printenv error message for non-existant variables."""
  144. var = state_test_env.set_var
  145. c = state_test_env.u_boot_console
  146. with c.disable_check('error_notification'):
  147. response = c.run_command('printenv %s' % var)
  148. assert(response == '## Error: "%s" not defined' % var)
  149. @pytest.mark.buildconfigspec('cmd_echo')
  150. def test_env_unset_non_existent(state_test_env):
  151. """Test unsetting a nonexistent variable."""
  152. var = state_test_env.get_non_existent_var()
  153. unset_var(state_test_env, var)
  154. validate_empty(state_test_env, var)
  155. def test_env_set_non_existent(state_test_env):
  156. """Test set a non-existant variable."""
  157. var = state_test_env.set_var
  158. value = 'foo'
  159. set_var(state_test_env, var, value)
  160. validate_set(state_test_env, var, value)
  161. def test_env_set_existing(state_test_env):
  162. """Test setting an existant variable."""
  163. var = state_test_env.set_var
  164. value = 'bar'
  165. set_var(state_test_env, var, value)
  166. validate_set(state_test_env, var, value)
  167. @pytest.mark.buildconfigspec('cmd_echo')
  168. def test_env_unset_existing(state_test_env):
  169. """Test unsetting a variable."""
  170. var = state_test_env.set_var
  171. unset_var(state_test_env, var)
  172. validate_empty(state_test_env, var)
  173. def test_env_expansion_spaces(state_test_env):
  174. """Test expanding a variable that contains a space in its value."""
  175. var_space = None
  176. var_test = None
  177. try:
  178. var_space = state_test_env.get_non_existent_var()
  179. set_var(state_test_env, var_space, ' ')
  180. var_test = state_test_env.get_non_existent_var()
  181. value = ' 1${%(var_space)s}${%(var_space)s} 2 ' % locals()
  182. set_var(state_test_env, var_test, value)
  183. value = ' 1 2 '
  184. validate_set(state_test_env, var_test, value)
  185. finally:
  186. if var_space:
  187. unset_var(state_test_env, var_space)
  188. if var_test:
  189. unset_var(state_test_env, var_test)
  190. @pytest.mark.buildconfigspec('cmd_importenv')
  191. def test_env_import_checksum_no_size(state_test_env):
  192. """Test that omitted ('-') size parameter with checksum validation fails the
  193. env import function.
  194. """
  195. c = state_test_env.u_boot_console
  196. ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console)
  197. addr = '%08x' % ram_base
  198. with c.disable_check('error_notification'):
  199. response = c.run_command('env import -c %s -' % addr)
  200. assert(response == '## Error: external checksum format must pass size')
  201. @pytest.mark.buildconfigspec('cmd_importenv')
  202. def test_env_import_whitelist_checksum_no_size(state_test_env):
  203. """Test that omitted ('-') size parameter with checksum validation fails the
  204. env import function when variables are passed as parameters.
  205. """
  206. c = state_test_env.u_boot_console
  207. ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console)
  208. addr = '%08x' % ram_base
  209. with c.disable_check('error_notification'):
  210. response = c.run_command('env import -c %s - foo1 foo2 foo4' % addr)
  211. assert(response == '## Error: external checksum format must pass size')
  212. @pytest.mark.buildconfigspec('cmd_exportenv')
  213. @pytest.mark.buildconfigspec('cmd_importenv')
  214. def test_env_import_whitelist(state_test_env):
  215. """Test importing only a handful of env variables from an environment."""
  216. c = state_test_env.u_boot_console
  217. ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console)
  218. addr = '%08x' % ram_base
  219. set_var(state_test_env, 'foo1', 'bar1')
  220. set_var(state_test_env, 'foo2', 'bar2')
  221. set_var(state_test_env, 'foo3', 'bar3')
  222. c.run_command('env export %s' % addr)
  223. unset_var(state_test_env, 'foo1')
  224. set_var(state_test_env, 'foo2', 'test2')
  225. set_var(state_test_env, 'foo4', 'bar4')
  226. # no foo1 in current env, foo2 overridden, foo3 should be of the value
  227. # before exporting and foo4 should be of the value before importing.
  228. c.run_command('env import %s - foo1 foo2 foo4' % addr)
  229. validate_set(state_test_env, 'foo1', 'bar1')
  230. validate_set(state_test_env, 'foo2', 'bar2')
  231. validate_set(state_test_env, 'foo3', 'bar3')
  232. validate_set(state_test_env, 'foo4', 'bar4')
  233. # Cleanup test environment
  234. unset_var(state_test_env, 'foo1')
  235. unset_var(state_test_env, 'foo2')
  236. unset_var(state_test_env, 'foo3')
  237. unset_var(state_test_env, 'foo4')
  238. @pytest.mark.buildconfigspec('cmd_exportenv')
  239. @pytest.mark.buildconfigspec('cmd_importenv')
  240. def test_env_import_whitelist_delete(state_test_env):
  241. """Test importing only a handful of env variables from an environment, with.
  242. deletion if a var A that is passed to env import is not in the
  243. environment to be imported.
  244. """
  245. c = state_test_env.u_boot_console
  246. ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console)
  247. addr = '%08x' % ram_base
  248. set_var(state_test_env, 'foo1', 'bar1')
  249. set_var(state_test_env, 'foo2', 'bar2')
  250. set_var(state_test_env, 'foo3', 'bar3')
  251. c.run_command('env export %s' % addr)
  252. unset_var(state_test_env, 'foo1')
  253. set_var(state_test_env, 'foo2', 'test2')
  254. set_var(state_test_env, 'foo4', 'bar4')
  255. # no foo1 in current env, foo2 overridden, foo3 should be of the value
  256. # before exporting and foo4 should be empty.
  257. c.run_command('env import -d %s - foo1 foo2 foo4' % addr)
  258. validate_set(state_test_env, 'foo1', 'bar1')
  259. validate_set(state_test_env, 'foo2', 'bar2')
  260. validate_set(state_test_env, 'foo3', 'bar3')
  261. validate_empty(state_test_env, 'foo4')
  262. # Cleanup test environment
  263. unset_var(state_test_env, 'foo1')
  264. unset_var(state_test_env, 'foo2')
  265. unset_var(state_test_env, 'foo3')
  266. unset_var(state_test_env, 'foo4')
  267. @pytest.mark.buildconfigspec('cmd_nvedit_info')
  268. def test_env_info(state_test_env):
  269. """Test 'env info' command with all possible options.
  270. """
  271. c = state_test_env.u_boot_console
  272. response = c.run_command('env info')
  273. nb_line = 0
  274. for l in response.split('\n'):
  275. if 'env_valid = ' in l:
  276. assert '= invalid' in l or '= valid' in l or '= redundant' in l
  277. nb_line += 1
  278. elif 'env_ready =' in l or 'env_use_default =' in l:
  279. assert '= true' in l or '= false' in l
  280. nb_line += 1
  281. else:
  282. assert true
  283. assert nb_line == 3
  284. response = c.run_command('env info -p -d')
  285. assert 'Default environment is used' in response or "Environment was loaded from persistent storage" in response
  286. assert 'Environment can be persisted' in response or "Environment cannot be persisted" in response
  287. response = c.run_command('env info -p -d -q')
  288. assert response == ""
  289. response = c.run_command('env info -p -q')
  290. assert response == ""
  291. response = c.run_command('env info -d -q')
  292. assert response == ""
  293. @pytest.mark.boardspec('sandbox')
  294. @pytest.mark.buildconfigspec('cmd_nvedit_info')
  295. @pytest.mark.buildconfigspec('cmd_echo')
  296. def test_env_info_sandbox(state_test_env):
  297. """Test 'env info' command result with several options on sandbox
  298. with a known ENV configuration: ready & default & persistent
  299. """
  300. c = state_test_env.u_boot_console
  301. response = c.run_command('env info')
  302. assert 'env_ready = true' in response
  303. assert 'env_use_default = true' in response
  304. response = c.run_command('env info -p -d')
  305. assert 'Default environment is used' in response
  306. assert 'Environment cannot be persisted' in response
  307. response = c.run_command('env info -d -q')
  308. response = c.run_command('echo $?')
  309. assert response == "0"
  310. response = c.run_command('env info -p -q')
  311. response = c.run_command('echo $?')
  312. assert response == "1"
  313. response = c.run_command('env info -d -p -q')
  314. response = c.run_command('echo $?')
  315. assert response == "1"
  316. def mk_env_ext4(state_test_env):
  317. """Create a empty ext4 file system volume."""
  318. c = state_test_env.u_boot_console
  319. filename = 'env.ext4.img'
  320. persistent = c.config.persistent_data_dir + '/' + filename
  321. fs_img = c.config.result_dir + '/' + filename
  322. if os.path.exists(persistent):
  323. c.log.action('Disk image file ' + persistent + ' already exists')
  324. else:
  325. # Some distributions do not add /sbin to the default PATH, where mkfs.ext4 lives
  326. os.environ["PATH"] += os.pathsep + '/sbin'
  327. try:
  328. u_boot_utils.run_and_log(c, 'dd if=/dev/zero of=%s bs=1M count=16' % persistent)
  329. u_boot_utils.run_and_log(c, 'mkfs.ext4 %s' % persistent)
  330. sb_content = u_boot_utils.run_and_log(c, 'tune2fs -l %s' % persistent)
  331. if 'metadata_csum' in sb_content:
  332. u_boot_utils.run_and_log(c, 'tune2fs -O ^metadata_csum %s' % persistent)
  333. except CalledProcessError:
  334. call('rm -f %s' % persistent, shell=True)
  335. raise
  336. u_boot_utils.run_and_log(c, ['cp', '-f', persistent, fs_img])
  337. return fs_img
  338. @pytest.mark.boardspec('sandbox')
  339. @pytest.mark.buildconfigspec('cmd_echo')
  340. @pytest.mark.buildconfigspec('cmd_nvedit_info')
  341. @pytest.mark.buildconfigspec('cmd_nvedit_load')
  342. @pytest.mark.buildconfigspec('cmd_nvedit_select')
  343. @pytest.mark.buildconfigspec('env_is_in_ext4')
  344. def test_env_ext4(state_test_env):
  345. """Test ENV in EXT4 on sandbox."""
  346. c = state_test_env.u_boot_console
  347. fs_img = ''
  348. try:
  349. fs_img = mk_env_ext4(state_test_env)
  350. c.run_command('host bind 0 %s' % fs_img)
  351. response = c.run_command('ext4ls host 0:0')
  352. assert 'uboot.env' not in response
  353. # force env location: EXT4 (prio 1 in sandbox)
  354. response = c.run_command('env select EXT4')
  355. assert 'Select Environment on EXT4: OK' in response
  356. response = c.run_command('env save')
  357. assert 'Saving Environment to EXT4' in response
  358. response = c.run_command('env load')
  359. assert 'Loading Environment from EXT4... OK' in response
  360. response = c.run_command('ext4ls host 0:0')
  361. assert '8192 uboot.env' in response
  362. response = c.run_command('env info')
  363. assert 'env_valid = valid' in response
  364. assert 'env_ready = true' in response
  365. assert 'env_use_default = false' in response
  366. response = c.run_command('env info -p -d')
  367. assert 'Environment was loaded from persistent storage' in response
  368. assert 'Environment can be persisted' in response
  369. response = c.run_command('env info -d -q')
  370. assert response == ""
  371. response = c.run_command('echo $?')
  372. assert response == "1"
  373. response = c.run_command('env info -p -q')
  374. assert response == ""
  375. response = c.run_command('echo $?')
  376. assert response == "0"
  377. response = c.run_command('env erase')
  378. assert 'OK' in response
  379. response = c.run_command('env load')
  380. assert 'Loading Environment from EXT4... ' in response
  381. assert 'bad CRC, using default environment' in response
  382. response = c.run_command('env info')
  383. assert 'env_valid = invalid' in response
  384. assert 'env_ready = true' in response
  385. assert 'env_use_default = true' in response
  386. response = c.run_command('env info -p -d')
  387. assert 'Default environment is used' in response
  388. assert 'Environment can be persisted' in response
  389. # restore env location: NOWHERE (prio 0 in sandbox)
  390. response = c.run_command('env select nowhere')
  391. assert 'Select Environment on nowhere: OK' in response
  392. response = c.run_command('env load')
  393. assert 'Loading Environment from nowhere... OK' in response
  394. response = c.run_command('env info')
  395. assert 'env_valid = invalid' in response
  396. assert 'env_ready = true' in response
  397. assert 'env_use_default = true' in response
  398. response = c.run_command('env info -p -d')
  399. assert 'Default environment is used' in response
  400. assert 'Environment cannot be persisted' in response
  401. finally:
  402. if fs_img:
  403. call('rm -f %s' % fs_img, shell=True)
  404. def test_env_text(u_boot_console):
  405. """Test the script that converts the environment to a text file"""
  406. def check_script(intext, expect_val):
  407. """Check a test case
  408. Args:
  409. intext: Text to pass to the script
  410. expect_val: Expected value of the CONFIG_EXTRA_ENV_TEXT string, or
  411. None if we expect it not to be defined
  412. """
  413. with tempfile.TemporaryDirectory() as path:
  414. fname = os.path.join(path, 'infile')
  415. with open(fname, 'w') as inf:
  416. print(intext, file=inf)
  417. result = u_boot_utils.run_and_log(cons, ['awk', '-f', script, fname])
  418. if expect_val is not None:
  419. expect = '#define CONFIG_EXTRA_ENV_TEXT "%s"\n' % expect_val
  420. assert result == expect
  421. else:
  422. assert result == ''
  423. cons = u_boot_console
  424. script = os.path.join(cons.config.source_dir, 'scripts', 'env2string.awk')
  425. # simple script with a single var
  426. check_script('fred=123', 'fred=123\\0')
  427. # no vars
  428. check_script('', None)
  429. # two vars
  430. check_script('''fred=123
  431. ernie=456''', 'fred=123\\0ernie=456\\0')
  432. # blank lines
  433. check_script('''fred=123
  434. ernie=456
  435. ''', 'fred=123\\0ernie=456\\0')
  436. # append
  437. check_script('''fred=123
  438. ernie=456
  439. fred+= 456''', 'fred=123 456\\0ernie=456\\0')
  440. # append from empty
  441. check_script('''fred=
  442. ernie=456
  443. fred+= 456''', 'fred= 456\\0ernie=456\\0')
  444. # variable with + in it
  445. check_script('fred+ernie=123', 'fred+ernie=123\\0')
  446. # ignores variables that are empty
  447. check_script('''fred=
  448. fred+=
  449. ernie=456''', 'ernie=456\\0')
  450. # single-character env name
  451. check_script('''f=123
  452. e=456
  453. f+= 456''', 'e=456\\0f=123 456\\0')
  454. # contains quotes
  455. check_script('''fred="my var"
  456. ernie=another"''', 'fred=\\"my var\\"\\0ernie=another\\"\\0')
  457. # variable name ending in +
  458. check_script('''fred\\+=my var
  459. fred++= again''', 'fred+=my var again\\0')
  460. # variable name containing +
  461. check_script('''fred+jane=both
  462. fred+jane+=again
  463. ernie=456''', 'fred+jane=bothagain\\0ernie=456\\0')
  464. # multi-line vars - new vars always start at column 1
  465. check_script('''fred=first
  466. second
  467. \tthird with tab
  468. after blank
  469. confusing=oops
  470. ernie=another"''', 'fred=first second third with tab after blank confusing=oops\\0ernie=another\\"\\0')
  471. # real-world example
  472. check_script('''ubifs_boot=
  473. env exists bootubipart ||
  474. env set bootubipart UBI;
  475. env exists bootubivol ||
  476. env set bootubivol boot;
  477. if ubi part ${bootubipart} &&
  478. ubifsmount ubi${devnum}:${bootubivol};
  479. then
  480. devtype=ubi;
  481. run scan_dev_for_boot;
  482. fi
  483. ''',
  484. 'ubifs_boot=env exists bootubipart || env set bootubipart UBI; '
  485. 'env exists bootubivol || env set bootubivol boot; '
  486. 'if ubi part ${bootubipart} && ubifsmount ubi${devnum}:${bootubivol}; '
  487. 'then devtype=ubi; run scan_dev_for_boot; fi\\0')