ts_cfg.py 7.4 KB

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