ts_cfg.py 7.3 KB

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