ts_cfg.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2022 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. ts_misc.py: 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=''):
  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. def path(self,id_str):
  43. return {
  44. 'ref': 'test/ref/mmgen.cfg',
  45. 'data_dir': '{}/data_dir'.format(self.tmpdir),
  46. 'shared_data': '{}/data_dir/{}'.format(self.tmpdir,CfgFileSampleSys.test_fn_subdir),
  47. 'usr': '{}/data_dir/mmgen.cfg'.format(self.tmpdir),
  48. 'sys': '{}/data_dir/{}/mmgen.cfg'.format(self.tmpdir,CfgFileSampleSys.test_fn_subdir),
  49. 'sample': '{}/data_dir/mmgen.cfg.sample'.format(os.path.abspath(self.tmpdir)),
  50. }[id_str]
  51. def copy_sys_sample(self):
  52. os.makedirs(self.path('shared_data'),exist_ok=True)
  53. shutil.copy2(self.path('ref'),self.path('sys'))
  54. def sysfile(self):
  55. self.copy_sys_sample()
  56. t = self.spawn_test()
  57. t.read()
  58. u = read_from_file(self.path('usr'))
  59. S = read_from_file(self.path('sys'))
  60. assert u[-1] == '\n', u
  61. assert u.replace('\r\n','\n') == S, 'u != S'
  62. self.check_replaced_sample()
  63. return t
  64. def check_replaced_sample(self):
  65. S = read_from_file(self.path('sys'))
  66. s = read_from_file(self.path('sample'))
  67. assert s[-1] == '\n', s
  68. assert S.splitlines() == s.splitlines()[:-1], 'sys != sample[:-1]'
  69. def bad_sample(self,s,e):
  70. write_to_file(self.path('sample'),s)
  71. t = self.spawn_test()
  72. t.expect(e)
  73. t.read()
  74. self.check_replaced_sample()
  75. return t
  76. def no_metadata_sample(self):
  77. self.copy_sys_sample()
  78. s = read_from_file(self.path('sys'))
  79. e = CfgFileSampleUsr.out_of_date_fs.format(self.path('sample'))
  80. return self.bad_sample(s,e)
  81. def altered_sample(self):
  82. s = '\n'.join(read_from_file(self.path('sample')).splitlines()[1:]) + '\n'
  83. e = CfgFileSampleUsr.altered_by_user_fs.format(self.path('sample'))
  84. return self.bad_sample(s,e)
  85. def old_sample_common(self,old_set=False,args=[]):
  86. if opt.pexpect_spawn: # FIXME: get_char() is blocking
  87. return 'skip'
  88. s = read_from_file(self.path('sys'))
  89. d = s.replace('monero_','zcash_').splitlines()
  90. a1 = ['','# Uncomment to make foo true:','# foo true']
  91. a2 = ['','# Uncomment to make bar false:','# bar false']
  92. d = d + a1 + a2
  93. chk = CfgFileSample.cls_make_metadata(d)
  94. write_to_file(self.path('sample'),'\n'.join(d+chk) + '\n')
  95. t = self.spawn_test(args=args)
  96. t.expect('options have changed')
  97. for s in ('have been added','monero_','have been removed','zcash_','foo','bar'):
  98. t.expect(s)
  99. if old_set:
  100. for s in ('must be deleted','bar','foo'):
  101. t.expect(s)
  102. cp = CfgFileSampleUsr.details_confirm_prompt + ' (y/N): '
  103. t.expect(cp,'y')
  104. for s in ('CHANGES','Removed','# zcash_','# foo','# bar','Added','# monero_'):
  105. t.expect(s)
  106. t.expect(cp,'n')
  107. if old_set:
  108. t.expect('unrecognized option')
  109. t.req_exit_val = 1
  110. if args == ['parse_test']:
  111. t.expect('parsed chunks: 29')
  112. t.expect('usr cfg: testnet=true rpc_password=passwOrd')
  113. if not old_set:
  114. self.check_replaced_sample()
  115. return t
  116. def old_sample(self):
  117. d = ['testnet true','rpc_password passwOrd']
  118. write_to_file(self.path('usr'),'\n'.join(d) + '\n')
  119. return self.old_sample_common(args=['parse_test'])
  120. def old_sample_bad_var(self):
  121. d = ['foo true','bar false']
  122. write_to_file(self.path('usr'),'\n'.join(d) + '\n')
  123. return self.old_sample_common(old_set=True)
  124. def coin_specific_vars(self):
  125. """
  126. ensure that derived classes explicitly set these variables
  127. """
  128. d = [
  129. 'btc_max_tx_fee 1.2345',
  130. 'eth_max_tx_fee 5.4321',
  131. 'btc_ignore_daemon_version true',
  132. 'eth_ignore_daemon_version true'
  133. ]
  134. write_to_file(self.path('usr'),'\n'.join(d) + '\n')
  135. imsg(yellow('Wrote cfg file:\n {}'.format('\n '.join(d))))
  136. for coin,res1_chk,res2_chk,res2_chk_eq in (
  137. ('BTC','True', '1.2345',True),
  138. ('LTC','False','1.2345',False),
  139. ('BCH','False','1.2345',False),
  140. ('ETH','True', '5.4321',True),
  141. ('ETC','False','5.4321',False)
  142. ):
  143. if opt.no_altcoin and coin != 'BTC':
  144. continue
  145. t = self.spawn_test(
  146. args = [
  147. f'--coin={coin}',
  148. 'coin_specific_vars',
  149. 'ignore_daemon_version',
  150. 'max_tx_fee'
  151. ],
  152. extra_desc=f'({coin})' )
  153. res1 = t.expect_getend('ignore_daemon_version: ')
  154. res2 = t.expect_getend('max_tx_fee: ')
  155. assert res1 == res1_chk, f'{res1} != {res1_chk}'
  156. if res2_chk_eq:
  157. assert res2 == res2_chk, f'{res2} != {res2_chk}'
  158. else:
  159. assert res2 != res2_chk, f'{res2} == {res2_chk}'
  160. t.read()
  161. t.ok()
  162. t.skip_ok = True
  163. return t
  164. def mnemonic_entry_modes(self):
  165. def run(modes_chk):
  166. t = self.spawn_test(args=['mnemonic_entry_modes'])
  167. modes = t.expect_getend('mnemonic_entry_modes: ')
  168. assert modes_chk == modes, f'{modes_chk} != {modes}'
  169. return t
  170. txt = 'mnemonic_entry_modes mmgen:full bip39:short'
  171. write_to_file(self.path('usr'),txt+'\n')
  172. imsg(yellow(f'Wrote cfg file: {txt!r}'))
  173. return run("{'mmgen': 'full', 'bip39': 'short'}")
  174. def chain_names(self):
  175. if opt.no_altcoin:
  176. return 'skip'
  177. def run(chk,testnet):
  178. for coin,chain_chk in (('ETH',chk),('ETC',None)):
  179. t = self.spawn_test(
  180. args = [f'--coin={coin}',f'--testnet={(0,1)[testnet]}','coin_specific_vars','chain_names'],
  181. extra_desc = f'({coin} testnet={testnet!r:5} chain_names={chain_chk})' )
  182. chain = t.expect_getend('chain_names: ')
  183. if chain_chk:
  184. assert chain == chain_chk, f'{chain} != {chain_chk}'
  185. else:
  186. assert chain != chain_chk, f'{chain} == {chain_chk}'
  187. t.read()
  188. t.ok()
  189. return t
  190. txt = 'eth_mainnet_chain_names istanbul constantinople'
  191. write_to_file(self.path('usr'),txt+'\n')
  192. imsg(yellow(f'Wrote cfg file: {txt!r}'))
  193. t = run("['istanbul', 'constantinople']",False)
  194. t = run(None,True)
  195. txt = 'eth_testnet_chain_names rinkeby'
  196. write_to_file(self.path('usr'),txt+'\n')
  197. imsg(yellow(f'Wrote cfg file: {txt!r}'))
  198. t = run(None,False)
  199. t = run("['rinkeby']",True)
  200. t.skip_ok = True
  201. return t