unit_tests.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2023 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. test/unit_tests.py: Unit tests for the MMGen suite
  20. """
  21. import sys,os,time,importlib,platform
  22. from include.tests_header import repo_root
  23. from mmgen.devinit import init_dev
  24. init_dev()
  25. from mmgen.common import *
  26. from test.include.common import set_globals,end_msg
  27. opts_data = {
  28. 'text': {
  29. 'desc': "Unit tests for the MMGen suite",
  30. 'usage':'[options] [test | test.subtest]...',
  31. 'options': """
  32. -h, --help Print this help message
  33. -a, --no-altcoin-deps Skip tests requiring altcoin daemons, libs or utils
  34. -A, --no-daemon-autostart Don't start and stop daemons automatically
  35. -D, --no-daemon-stop Don't stop auto-started daemons after running tests
  36. -f, --fast Speed up execution by reducing rounds on some tests
  37. -l, --list List available tests
  38. -L, --list-subtests List available tests and subtests
  39. -n, --names Print command names instead of descriptions
  40. -q, --quiet Produce quieter output
  41. -x, --exclude=T Exclude tests 'T' (comma-separated)
  42. -v, --verbose Produce more verbose output
  43. """,
  44. 'notes': """
  45. If no test is specified, all available tests are run
  46. """
  47. }
  48. }
  49. sys.argv.insert(1,'--skip-cfg-file')
  50. cfg = Config(opts_data=opts_data)
  51. type(cfg)._reset_ok += ('use_internal_keccak_module','debug_addrlist')
  52. set_globals(cfg)
  53. os.environ['PYTHONPATH'] = repo_root
  54. file_pfx = 'ut_'
  55. tests_d = os.path.join(repo_root,'test','unit_tests_d')
  56. all_tests = sorted(fn[len(file_pfx):-len('.py')] for fn in os.listdir(tests_d) if fn.startswith(file_pfx))
  57. exclude = cfg.exclude.split(',') if cfg.exclude else []
  58. for e in exclude:
  59. if e not in all_tests:
  60. die(1,f'{e!r}: invalid parameter for --exclude (no such test)')
  61. start_time = int(time.time())
  62. if cfg.list:
  63. Msg(' '.join(all_tests))
  64. sys.exit(0)
  65. if cfg.list_subtests:
  66. def gen():
  67. for test in all_tests:
  68. mod = importlib.import_module(f'test.unit_tests_d.{file_pfx}{test}')
  69. if hasattr(mod,'unit_tests'):
  70. t = getattr(mod,'unit_tests')
  71. subtests = [k for k,v in t.__dict__.items() if type(v).__name__ == 'function' and k[0] != '_']
  72. yield fs.format( test, ' '.join(f'{subtest}' for subtest in subtests) )
  73. else:
  74. yield test
  75. fs = '{:%s} {}' % max(len(t) for t in all_tests)
  76. Msg( fs.format('TEST','SUBTESTS') + '\n' + '\n'.join(gen()) )
  77. sys.exit(0)
  78. class UnitTestHelpers:
  79. def __init__(self,subtest_name):
  80. self.subtest_name = subtest_name
  81. def skip_msg(self,desc):
  82. cfg._util.qmsg(gray(f'Skipping subtest {self.subtest_name.replace("_","-")!r} for {desc}'))
  83. def process_bad_data(self,data):
  84. if os.getenv('PYTHONOPTIMIZE'):
  85. ymsg('PYTHONOPTIMIZE set, skipping error handling tests')
  86. return
  87. import re
  88. desc_w = max(len(e[0]) for e in data)
  89. exc_w = max(len(e[1]) for e in data)
  90. m_exc = '{!r}: incorrect exception type (expected {!r})'
  91. m_err = '{!r}: incorrect error msg (should match {!r}'
  92. m_noraise = "\nillegal action 'bad {}' failed to raise an exception (expected {!r})"
  93. for (desc,exc_chk,emsg_chk,func) in data:
  94. try:
  95. cfg._util.vmsg_r(' bad {:{w}}'.format( desc+':', w=desc_w+1 ))
  96. func()
  97. except Exception as e:
  98. exc = type(e).__name__
  99. emsg = e.args[0]
  100. cfg._util.vmsg(' {:{w}} [{}]'.format( exc, emsg, w=exc_w ))
  101. assert exc == exc_chk, m_exc.format(exc,exc_chk)
  102. assert re.search(emsg_chk,emsg), m_err.format(emsg,emsg_chk)
  103. else:
  104. die(4,m_noraise.format(desc,exc_chk))
  105. tests_seen = []
  106. def run_test(test,subtest=None):
  107. mod = importlib.import_module(f'test.unit_tests_d.{file_pfx}{test}')
  108. def run_subtest(subtest):
  109. subtest_disp = subtest.replace('_','-')
  110. msg(f'Running unit subtest {test}.{subtest_disp}')
  111. t = getattr(mod,'unit_tests')()
  112. if hasattr(t,'_pre_subtest'):
  113. getattr(t,'_pre_subtest')(test,subtest,UnitTestHelpers(subtest))
  114. ret = getattr(t,subtest.replace('-','_'))(test,UnitTestHelpers(subtest))
  115. if hasattr(t,'_post_subtest'):
  116. getattr(t,'_post_subtest')(test,subtest,UnitTestHelpers(subtest))
  117. if type(ret).__name__ == 'coroutine':
  118. ret = async_run(ret)
  119. if not ret:
  120. die(4,f'Unit subtest {subtest_disp!r} failed')
  121. if test not in tests_seen:
  122. gmsg(f'Running unit test {test}')
  123. tests_seen.append(test)
  124. if subtest:
  125. run_subtest(subtest)
  126. else:
  127. if hasattr(mod,'unit_tests'): # new class-based API
  128. t = getattr(mod,'unit_tests')
  129. altcoin_deps = getattr(t,'altcoin_deps',())
  130. win_skip = getattr(t,'win_skip',())
  131. arm_skip = getattr(t,'arm_skip',())
  132. subtests = [k for k,v in t.__dict__.items() if type(v).__name__ == 'function' and k[0] != '_']
  133. for subtest in subtests:
  134. subtest_disp = subtest.replace('_','-')
  135. if cfg.no_altcoin_deps and subtest in altcoin_deps:
  136. cfg._util.qmsg(gray(f'Invoked with --no-altcoin-deps, so skipping subtest {subtest_disp!r}'))
  137. continue
  138. if gc.platform == 'win' and subtest in win_skip:
  139. cfg._util.qmsg(gray(f'Skipping subtest {subtest_disp!r} for Windows platform'))
  140. continue
  141. elif platform.machine() == 'aarch64' and subtest in arm_skip:
  142. cfg._util.qmsg(gray(f'Skipping subtest {subtest_disp!r} for ARM platform'))
  143. continue
  144. run_subtest(subtest)
  145. else:
  146. if not mod.unit_test().run_test(test,UnitTestHelpers(test)):
  147. die(4,'Unit test {test!r} failed')
  148. try:
  149. for test in (cfg._args or all_tests):
  150. if '.' in test:
  151. test,subtest = test.split('.')
  152. else:
  153. subtest = None
  154. if test not in all_tests:
  155. die(1,f'{test!r}: test not recognized')
  156. if test not in exclude:
  157. run_test(test,subtest=subtest)
  158. end_msg(int(time.time()) - start_time)
  159. except KeyboardInterrupt:
  160. die(1,green('\nExiting at user request'))