test_env.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 pytest
  6. import u_boot_utils
  7. # FIXME: This might be useful for other tests;
  8. # perhaps refactor it into ConsoleBase or some other state object?
  9. class StateTestEnv(object):
  10. """Container that represents the state of all U-Boot environment variables.
  11. This enables quick determination of existant/non-existant variable
  12. names.
  13. """
  14. def __init__(self, u_boot_console):
  15. """Initialize a new StateTestEnv object.
  16. Args:
  17. u_boot_console: A U-Boot console.
  18. Returns:
  19. Nothing.
  20. """
  21. self.u_boot_console = u_boot_console
  22. self.get_env()
  23. self.set_var = self.get_non_existent_var()
  24. def get_env(self):
  25. """Read all current environment variables from U-Boot.
  26. Args:
  27. None.
  28. Returns:
  29. Nothing.
  30. """
  31. if self.u_boot_console.config.buildconfig.get(
  32. 'config_version_variable', 'n') == 'y':
  33. with self.u_boot_console.disable_check('main_signon'):
  34. response = self.u_boot_console.run_command('printenv')
  35. else:
  36. response = self.u_boot_console.run_command('printenv')
  37. self.env = {}
  38. for l in response.splitlines():
  39. if not '=' in l:
  40. continue
  41. (var, value) = l.split('=', 1)
  42. self.env[var] = value
  43. def get_existent_var(self):
  44. """Return the name of an environment variable that exists.
  45. Args:
  46. None.
  47. Returns:
  48. The name of an environment variable.
  49. """
  50. for var in self.env:
  51. return var
  52. def get_non_existent_var(self):
  53. """Return the name of an environment variable that does not exist.
  54. Args:
  55. None.
  56. Returns:
  57. The name of an environment variable.
  58. """
  59. n = 0
  60. while True:
  61. var = 'test_env_' + str(n)
  62. if var not in self.env:
  63. return var
  64. n += 1
  65. ste = None
  66. @pytest.fixture(scope='function')
  67. def state_test_env(u_boot_console):
  68. """pytest fixture to provide a StateTestEnv object to tests."""
  69. global ste
  70. if not ste:
  71. ste = StateTestEnv(u_boot_console)
  72. return ste
  73. def unset_var(state_test_env, var):
  74. """Unset an environment variable.
  75. This both executes a U-Boot shell command and updates a StateTestEnv
  76. object.
  77. Args:
  78. state_test_env: The StateTestEnv object to update.
  79. var: The variable name to unset.
  80. Returns:
  81. Nothing.
  82. """
  83. state_test_env.u_boot_console.run_command('setenv %s' % var)
  84. if var in state_test_env.env:
  85. del state_test_env.env[var]
  86. def set_var(state_test_env, var, value):
  87. """Set an environment variable.
  88. This both executes a U-Boot shell command and updates a StateTestEnv
  89. object.
  90. Args:
  91. state_test_env: The StateTestEnv object to update.
  92. var: The variable name to set.
  93. value: The value to set the variable to.
  94. Returns:
  95. Nothing.
  96. """
  97. bc = state_test_env.u_boot_console.config.buildconfig
  98. if bc.get('config_hush_parser', None):
  99. quote = '"'
  100. else:
  101. quote = ''
  102. if ' ' in value:
  103. pytest.skip('Space in variable value on non-Hush shell')
  104. state_test_env.u_boot_console.run_command(
  105. 'setenv %s %s%s%s' % (var, quote, value, quote))
  106. state_test_env.env[var] = value
  107. def validate_empty(state_test_env, var):
  108. """Validate that a variable is not set, using U-Boot shell commands.
  109. Args:
  110. var: The variable name to test.
  111. Returns:
  112. Nothing.
  113. """
  114. response = state_test_env.u_boot_console.run_command('echo $%s' % var)
  115. assert response == ''
  116. def validate_set(state_test_env, var, value):
  117. """Validate that a variable is set, using U-Boot shell commands.
  118. Args:
  119. var: The variable name to test.
  120. value: The value the variable is expected to have.
  121. Returns:
  122. Nothing.
  123. """
  124. # echo does not preserve leading, internal, or trailing whitespace in the
  125. # value. printenv does, and hence allows more complete testing.
  126. response = state_test_env.u_boot_console.run_command('printenv %s' % var)
  127. assert response == ('%s=%s' % (var, value))
  128. def test_env_echo_exists(state_test_env):
  129. """Test echoing a variable that exists."""
  130. var = state_test_env.get_existent_var()
  131. value = state_test_env.env[var]
  132. validate_set(state_test_env, var, value)
  133. @pytest.mark.buildconfigspec('cmd_echo')
  134. def test_env_echo_non_existent(state_test_env):
  135. """Test echoing a variable that doesn't exist."""
  136. var = state_test_env.set_var
  137. validate_empty(state_test_env, var)
  138. def test_env_printenv_non_existent(state_test_env):
  139. """Test printenv error message for non-existant variables."""
  140. var = state_test_env.set_var
  141. c = state_test_env.u_boot_console
  142. with c.disable_check('error_notification'):
  143. response = c.run_command('printenv %s' % var)
  144. assert(response == '## Error: "%s" not defined' % var)
  145. @pytest.mark.buildconfigspec('cmd_echo')
  146. def test_env_unset_non_existent(state_test_env):
  147. """Test unsetting a nonexistent variable."""
  148. var = state_test_env.get_non_existent_var()
  149. unset_var(state_test_env, var)
  150. validate_empty(state_test_env, var)
  151. def test_env_set_non_existent(state_test_env):
  152. """Test set a non-existant variable."""
  153. var = state_test_env.set_var
  154. value = 'foo'
  155. set_var(state_test_env, var, value)
  156. validate_set(state_test_env, var, value)
  157. def test_env_set_existing(state_test_env):
  158. """Test setting an existant variable."""
  159. var = state_test_env.set_var
  160. value = 'bar'
  161. set_var(state_test_env, var, value)
  162. validate_set(state_test_env, var, value)
  163. @pytest.mark.buildconfigspec('cmd_echo')
  164. def test_env_unset_existing(state_test_env):
  165. """Test unsetting a variable."""
  166. var = state_test_env.set_var
  167. unset_var(state_test_env, var)
  168. validate_empty(state_test_env, var)
  169. def test_env_expansion_spaces(state_test_env):
  170. """Test expanding a variable that contains a space in its value."""
  171. var_space = None
  172. var_test = None
  173. try:
  174. var_space = state_test_env.get_non_existent_var()
  175. set_var(state_test_env, var_space, ' ')
  176. var_test = state_test_env.get_non_existent_var()
  177. value = ' 1${%(var_space)s}${%(var_space)s} 2 ' % locals()
  178. set_var(state_test_env, var_test, value)
  179. value = ' 1 2 '
  180. validate_set(state_test_env, var_test, value)
  181. finally:
  182. if var_space:
  183. unset_var(state_test_env, var_space)
  184. if var_test:
  185. unset_var(state_test_env, var_test)
  186. @pytest.mark.buildconfigspec('cmd_importenv')
  187. def test_env_import_checksum_no_size(state_test_env):
  188. """Test that omitted ('-') size parameter with checksum validation fails the
  189. env import function.
  190. """
  191. c = state_test_env.u_boot_console
  192. ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console)
  193. addr = '%08x' % ram_base
  194. with c.disable_check('error_notification'):
  195. response = c.run_command('env import -c %s -' % addr)
  196. assert(response == '## Error: external checksum format must pass size')
  197. @pytest.mark.buildconfigspec('cmd_importenv')
  198. def test_env_import_whitelist_checksum_no_size(state_test_env):
  199. """Test that omitted ('-') size parameter with checksum validation fails the
  200. env import function when variables are passed as parameters.
  201. """
  202. c = state_test_env.u_boot_console
  203. ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console)
  204. addr = '%08x' % ram_base
  205. with c.disable_check('error_notification'):
  206. response = c.run_command('env import -c %s - foo1 foo2 foo4' % addr)
  207. assert(response == '## Error: external checksum format must pass size')
  208. @pytest.mark.buildconfigspec('cmd_exportenv')
  209. @pytest.mark.buildconfigspec('cmd_importenv')
  210. def test_env_import_whitelist(state_test_env):
  211. """Test importing only a handful of env variables from an environment."""
  212. c = state_test_env.u_boot_console
  213. ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console)
  214. addr = '%08x' % ram_base
  215. set_var(state_test_env, 'foo1', 'bar1')
  216. set_var(state_test_env, 'foo2', 'bar2')
  217. set_var(state_test_env, 'foo3', 'bar3')
  218. c.run_command('env export %s' % addr)
  219. unset_var(state_test_env, 'foo1')
  220. set_var(state_test_env, 'foo2', 'test2')
  221. set_var(state_test_env, 'foo4', 'bar4')
  222. # no foo1 in current env, foo2 overridden, foo3 should be of the value
  223. # before exporting and foo4 should be of the value before importing.
  224. c.run_command('env import %s - foo1 foo2 foo4' % addr)
  225. validate_set(state_test_env, 'foo1', 'bar1')
  226. validate_set(state_test_env, 'foo2', 'bar2')
  227. validate_set(state_test_env, 'foo3', 'bar3')
  228. validate_set(state_test_env, 'foo4', 'bar4')
  229. # Cleanup test environment
  230. unset_var(state_test_env, 'foo1')
  231. unset_var(state_test_env, 'foo2')
  232. unset_var(state_test_env, 'foo3')
  233. unset_var(state_test_env, 'foo4')
  234. @pytest.mark.buildconfigspec('cmd_exportenv')
  235. @pytest.mark.buildconfigspec('cmd_importenv')
  236. def test_env_import_whitelist_delete(state_test_env):
  237. """Test importing only a handful of env variables from an environment, with.
  238. deletion if a var A that is passed to env import is not in the
  239. environment to be imported.
  240. """
  241. c = state_test_env.u_boot_console
  242. ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console)
  243. addr = '%08x' % ram_base
  244. set_var(state_test_env, 'foo1', 'bar1')
  245. set_var(state_test_env, 'foo2', 'bar2')
  246. set_var(state_test_env, 'foo3', 'bar3')
  247. c.run_command('env export %s' % addr)
  248. unset_var(state_test_env, 'foo1')
  249. set_var(state_test_env, 'foo2', 'test2')
  250. set_var(state_test_env, 'foo4', 'bar4')
  251. # no foo1 in current env, foo2 overridden, foo3 should be of the value
  252. # before exporting and foo4 should be empty.
  253. c.run_command('env import -d %s - foo1 foo2 foo4' % addr)
  254. validate_set(state_test_env, 'foo1', 'bar1')
  255. validate_set(state_test_env, 'foo2', 'bar2')
  256. validate_set(state_test_env, 'foo3', 'bar3')
  257. validate_empty(state_test_env, 'foo4')
  258. # Cleanup test environment
  259. unset_var(state_test_env, 'foo1')
  260. unset_var(state_test_env, 'foo2')
  261. unset_var(state_test_env, 'foo3')
  262. unset_var(state_test_env, 'foo4')