ts_cfg.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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='',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=None):
  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(old_set=True,pexpect_spawn=True)
  126. def coin_specific_vars(self):
  127. """
  128. ensure that derived classes explicitly set these variables
  129. """
  130. d = [
  131. 'btc_max_tx_fee 1.2345',
  132. 'eth_max_tx_fee 5.4321',
  133. 'btc_ignore_daemon_version true',
  134. 'eth_ignore_daemon_version true'
  135. ]
  136. write_to_file(self.path('usr'),'\n'.join(d) + '\n')
  137. imsg(yellow('Wrote cfg file:\n {}'.format('\n '.join(d))))
  138. for coin,res1_chk,res2_chk,res2_chk_eq in (
  139. ('BTC','True', '1.2345',True),
  140. ('LTC','False','1.2345',False),
  141. ('BCH','False','1.2345',False),
  142. ('ETH','True', '5.4321',True),
  143. ('ETC','False','5.4321',False)
  144. ):
  145. if opt.no_altcoin and coin != 'BTC':
  146. continue
  147. t = self.spawn_test(
  148. args = [
  149. f'--coin={coin}',
  150. 'coin_specific_vars',
  151. 'ignore_daemon_version',
  152. 'max_tx_fee'
  153. ],
  154. extra_desc=f'({coin})' )
  155. res1 = t.expect_getend('ignore_daemon_version: ')
  156. res2 = t.expect_getend('max_tx_fee: ')
  157. assert res1 == res1_chk, f'{res1} != {res1_chk}'
  158. if res2_chk_eq:
  159. assert res2 == res2_chk, f'{res2} != {res2_chk}'
  160. else:
  161. assert res2 != res2_chk, f'{res2} == {res2_chk}'
  162. t.read()
  163. t.ok()
  164. t.skip_ok = True
  165. return t
  166. def mnemonic_entry_modes(self):
  167. def run(modes_chk):
  168. t = self.spawn_test(args=['mnemonic_entry_modes'])
  169. modes = t.expect_getend('mnemonic_entry_modes: ')
  170. assert modes_chk == modes, f'{modes_chk} != {modes}'
  171. return t
  172. txt = 'mnemonic_entry_modes mmgen:full bip39:short'
  173. write_to_file(self.path('usr'),txt+'\n')
  174. imsg(yellow(f'Wrote cfg file: {txt!r}'))
  175. return run("{'mmgen': 'full', 'bip39': 'short'}")
  176. def chain_names(self):
  177. if opt.no_altcoin:
  178. return 'skip'
  179. def run(chk,testnet):
  180. for coin,chain_chk in (('ETH',chk),('ETC',None)):
  181. t = self.spawn_test(
  182. args = [f'--coin={coin}',f'--testnet={(0,1)[testnet]}','coin_specific_vars','chain_names'],
  183. extra_desc = f'({coin} testnet={testnet!r:5} chain_names={chain_chk})' )
  184. chain = t.expect_getend('chain_names: ')
  185. if chain_chk:
  186. assert chain == chain_chk, f'{chain} != {chain_chk}'
  187. else:
  188. assert chain != chain_chk, f'{chain} == {chain_chk}'
  189. t.read()
  190. t.ok()
  191. return t
  192. txt = 'eth_mainnet_chain_names istanbul constantinople'
  193. write_to_file(self.path('usr'),txt+'\n')
  194. imsg(yellow(f'Wrote cfg file: {txt!r}'))
  195. t = run("['istanbul', 'constantinople']",False)
  196. t = run(None,True)
  197. txt = 'eth_testnet_chain_names rinkeby'
  198. write_to_file(self.path('usr'),txt+'\n')
  199. imsg(yellow(f'Wrote cfg file: {txt!r}'))
  200. t = run(None,False)
  201. t = run("['rinkeby']",True)
  202. t.skip_ok = True
  203. return t