ts_cfgfile.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. # Project source code repository: https://github.com/mmgen/mmgen
  7. # Licensed according to the terms of GPL Version 3. See LICENSE for details.
  8. """
  9. test.test_py_d.ts_cfgfile: CfgFile tests for the MMGen test.py test suite
  10. """
  11. import os,time,shutil
  12. from mmgen.cfg import gc
  13. from mmgen.color import yellow
  14. from mmgen.cfgfile import CfgFileSampleSys,CfgFileSampleUsr,cfg_file_sample
  15. from ..include.common import cfg,read_from_file,write_to_file,imsg
  16. from .ts_base import TestSuiteBase
  17. class TestSuiteCfgFile(TestSuiteBase):
  18. 'CfgFile API'
  19. networks = ('btc',)
  20. tmpdir_nums = [40]
  21. base_passthru_opts = ()
  22. color = True
  23. cmd_group = (
  24. ('sysfile', (40,'init with system cfg sample file in place', [])),
  25. ('no_metadata_sample', (40,'init with unversioned cfg sample file', [])),
  26. ('altered_sample', (40,'init with user-modified cfg sample file', [])),
  27. ('old_sample', (40,'init with old v2 cfg sample file', [])),
  28. ('old_sample_bad_var', (40,'init with old v2 cfg sample file and bad variable in mmgen.cfg', [])),
  29. ('autoset_opts', (40,'setting autoset opts', [])),
  30. ('autoset_opts_cmdline', (40,'setting autoset opts (override on cmdline)', [])),
  31. ('autoset_opts_bad', (40,'setting autoset opts (bad value in cfg file)', [])),
  32. ('autoset_opts_bad_cmdline', (40,'setting autoset opts (bad param on cmdline)', [])),
  33. ('coin_specific_vars', (40,'setting coin-specific vars', [])),
  34. ('chain_names', (40,'setting chain names', [])),
  35. ('mnemonic_entry_modes', (40,'setting mnemonic entry modes', [])),
  36. )
  37. def __init__(self,trunner,cfgs,spawn):
  38. TestSuiteBase.__init__(self,trunner,cfgs,spawn)
  39. self.spawn_env['MMGEN_TEST_SUITE_CFGTEST'] = '1'
  40. def spawn_test(self,args=[],extra_desc='',pexpect_spawn=None):
  41. return self.spawn(
  42. 'test/misc/cfg.py',
  43. [f'--data-dir={self.path("data_dir")}'] + args,
  44. cmd_dir = '.',
  45. extra_desc = extra_desc,
  46. pexpect_spawn = pexpect_spawn )
  47. def path(self,id_str):
  48. return {
  49. 'ref': 'test/ref/mmgen.cfg',
  50. 'data_dir': '{}/data_dir'.format(self.tmpdir),
  51. 'shared_data': '{}/data_dir/{}'.format(self.tmpdir,CfgFileSampleSys.test_fn_subdir),
  52. 'usr': '{}/data_dir/mmgen.cfg'.format(self.tmpdir),
  53. 'sys': '{}/data_dir/{}/mmgen.cfg'.format(self.tmpdir,CfgFileSampleSys.test_fn_subdir),
  54. 'sample': '{}/data_dir/mmgen.cfg.sample'.format(os.path.abspath(self.tmpdir)),
  55. }[id_str]
  56. def copy_sys_sample(self):
  57. os.makedirs(self.path('shared_data'),exist_ok=True)
  58. shutil.copy2(self.path('ref'),self.path('sys'))
  59. def sysfile(self):
  60. self.copy_sys_sample()
  61. t = self.spawn_test()
  62. t.read()
  63. u = read_from_file(self.path('usr'))
  64. S = read_from_file(self.path('sys'))
  65. assert u[-1] == '\n', u
  66. assert u.replace('\r\n','\n') == S, 'u != S'
  67. self.check_replaced_sample()
  68. return t
  69. def check_replaced_sample(self):
  70. S = read_from_file(self.path('sys'))
  71. s = read_from_file(self.path('sample'))
  72. assert s[-1] == '\n', s
  73. assert S.splitlines() == s.splitlines()[:-1], 'sys != sample[:-1]'
  74. def bad_sample(self,s,e):
  75. write_to_file(self.path('sample'),s)
  76. t = self.spawn_test()
  77. t.expect(e)
  78. t.read()
  79. self.check_replaced_sample()
  80. return t
  81. def no_metadata_sample(self):
  82. self.copy_sys_sample()
  83. s = read_from_file(self.path('sys'))
  84. e = CfgFileSampleUsr.out_of_date_fs.format(self.path('sample'))
  85. return self.bad_sample(s,e)
  86. def altered_sample(self):
  87. s = '\n'.join(read_from_file(self.path('sample')).splitlines()[1:]) + '\n'
  88. e = CfgFileSampleUsr.altered_by_user_fs.format(self.path('sample'))
  89. return self.bad_sample(s,e)
  90. def old_sample_common(self,old_set=False,args=[],pexpect_spawn=False):
  91. s = read_from_file(self.path('sys'))
  92. d = s.replace('monero_','zcash_').splitlines()
  93. a1 = ['','# Uncomment to make foo true:','# foo true']
  94. a2 = ['','# Uncomment to make bar false:','# bar false']
  95. d = d + a1 + a2
  96. chk = cfg_file_sample.cls_make_metadata(d)
  97. write_to_file(self.path('sample'),'\n'.join(d+chk) + '\n')
  98. t = self.spawn_test(args=args,pexpect_spawn=pexpect_spawn)
  99. t.expect('options have changed')
  100. for s in ('have been added','monero_','have been removed','zcash_','foo','bar'):
  101. t.expect(s)
  102. if old_set:
  103. for s in ('must be deleted','bar','foo'):
  104. t.expect(s)
  105. cp = CfgFileSampleUsr.details_confirm_prompt + ' (y/N): '
  106. t.expect(cp,'y')
  107. for s in ('CHANGES','Removed','# zcash_','# foo','# bar','Added','# monero_'):
  108. t.expect(s)
  109. if t.pexpect_spawn: # view and exit pager
  110. time.sleep(1 if cfg.exact_output else t.send_delay)
  111. t.send('q')
  112. t.expect(cp,'n')
  113. if old_set:
  114. t.expect('unrecognized option')
  115. t.req_exit_val = 1
  116. if args == ['parse_test']:
  117. t.expect('parsed chunks: 29')
  118. t.expect('usr cfg: testnet=true rpc_password=passwOrd')
  119. if not old_set:
  120. self.check_replaced_sample()
  121. return t
  122. def old_sample(self):
  123. d = ['testnet true','rpc_password passwOrd']
  124. write_to_file(self.path('usr'),'\n'.join(d) + '\n')
  125. return self.old_sample_common(args=['parse_test'])
  126. def old_sample_bad_var(self):
  127. d = ['foo true','bar false']
  128. write_to_file(self.path('usr'),'\n'.join(d) + '\n')
  129. return self.old_sample_common(
  130. old_set = True,
  131. pexpect_spawn = False if gc.platform == 'win' else True )
  132. def _autoset_opts(self,args=[],text='rpc_backend aiohttp\n'):
  133. write_to_file( self.path('usr'), text )
  134. imsg(yellow(f'Wrote cfg file:\n {text}'))
  135. return self.spawn_test(args=args)
  136. def autoset_opts(self):
  137. return self._autoset_opts(args=['autoset_opts'])
  138. def autoset_opts_cmdline(self):
  139. return self._autoset_opts(args=['--rpc-backend=curl','autoset_opts_cmdline'])
  140. def _autoset_opts_bad(self,kwargs):
  141. t = self._autoset_opts(**kwargs)
  142. t.req_exit_val = 1
  143. return t
  144. def autoset_opts_bad(self):
  145. return self._autoset_opts_bad({'text':'rpc_backend foo\n'})
  146. def autoset_opts_bad_cmdline(self):
  147. return self._autoset_opts_bad({'args':['--rpc-backend=foo']})
  148. def coin_specific_vars(self):
  149. """
  150. ensure that derived classes explicitly set these variables
  151. """
  152. d = [
  153. 'btc_max_tx_fee 1.2345',
  154. 'eth_max_tx_fee 5.4321',
  155. 'btc_ignore_daemon_version true',
  156. 'eth_ignore_daemon_version true'
  157. ]
  158. write_to_file(self.path('usr'),'\n'.join(d) + '\n')
  159. imsg(yellow('Wrote cfg file:\n {}'.format('\n '.join(d))))
  160. for coin,res1_chk,res2_chk,res2_chk_eq in (
  161. ('BTC','True', '1.2345',True),
  162. ('LTC','False','1.2345',False),
  163. ('BCH','False','1.2345',False),
  164. ('ETH','True', '5.4321',True),
  165. ('ETC','False','5.4321',False)
  166. ):
  167. if cfg.no_altcoin and coin != 'BTC':
  168. continue
  169. t = self.spawn_test(
  170. args = [
  171. f'--coin={coin}',
  172. 'coin_specific_vars',
  173. 'ignore_daemon_version',
  174. 'max_tx_fee'
  175. ],
  176. extra_desc=f'({coin})' )
  177. res1 = t.expect_getend('ignore_daemon_version: ')
  178. res2 = t.expect_getend('max_tx_fee: ')
  179. assert res1 == res1_chk, f'{res1} != {res1_chk}'
  180. if res2_chk_eq:
  181. assert res2 == res2_chk, f'{res2} != {res2_chk}'
  182. else:
  183. assert res2 != res2_chk, f'{res2} == {res2_chk}'
  184. t.read()
  185. t.ok()
  186. t.skip_ok = True
  187. return t
  188. def mnemonic_entry_modes(self):
  189. def run(modes_chk):
  190. t = self.spawn_test(args=['mnemonic_entry_modes'])
  191. modes = t.expect_getend('mnemonic_entry_modes: ')
  192. assert modes_chk == modes, f'{modes_chk} != {modes}'
  193. return t
  194. txt = 'mnemonic_entry_modes mmgen:full bip39:short'
  195. write_to_file(self.path('usr'),txt+'\n')
  196. imsg(yellow(f'Wrote cfg file: {txt!r}'))
  197. t = run("{'mmgen': 'full', 'bip39': 'short'}")
  198. # check that set_dfl_entry_mode() set the mode correctly:
  199. t.expect('mmgen: full')
  200. t.expect('bip39: short')
  201. return t
  202. def chain_names(self):
  203. if cfg.no_altcoin:
  204. return 'skip'
  205. def run(chk,testnet):
  206. for coin,chain_chk in (('ETH',chk),('ETC',None)):
  207. t = self.spawn_test(
  208. args = [f'--coin={coin}',f'--testnet={(0,1)[testnet]}','coin_specific_vars','chain_names'],
  209. extra_desc = f'({coin} testnet={testnet!r:5} chain_names={chain_chk})' )
  210. chain = t.expect_getend('chain_names: ')
  211. if chain_chk:
  212. assert chain == chain_chk, f'{chain} != {chain_chk}'
  213. else:
  214. assert chain != chain_chk, f'{chain} == {chain_chk}'
  215. t.read()
  216. t.ok()
  217. return t
  218. txt = 'eth_mainnet_chain_names istanbul constantinople'
  219. write_to_file(self.path('usr'),txt+'\n')
  220. imsg(yellow(f'Wrote cfg file: {txt!r}'))
  221. t = run("['istanbul', 'constantinople']",False)
  222. t = run(None,True)
  223. txt = 'eth_testnet_chain_names rinkeby'
  224. write_to_file(self.path('usr'),txt+'\n')
  225. imsg(yellow(f'Wrote cfg file: {txt!r}'))
  226. t = run(None,False)
  227. t = run("['rinkeby']",True)
  228. t.skip_ok = True
  229. return t