ts_cfg.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2020 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. cmd_group = (
  21. ('nosysfile', (40,'init with missing system cfg sample file', [])),
  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. )
  28. def __init__(self,trunner,cfgs,spawn):
  29. os.environ['MMGEN_TEST_SUITE_CFGTEST'] = '1'
  30. TestSuiteBase.__init__(self,trunner,cfgs,spawn)
  31. def spawn_test(self,args=[]):
  32. return self.spawn('test/misc/cfg.py',['--data-dir={}'.format(self.path('data_dir'))]+args,cmd_dir='.')
  33. def path(self,id_str):
  34. return {
  35. 'ref': 'test/ref/mmgen.cfg',
  36. 'data_dir': '{}/data_dir'.format(self.tmpdir),
  37. 'shared_data': '{}/data_dir/{}'.format(self.tmpdir,CfgFileSampleSys.test_fn_subdir),
  38. 'usr': '{}/data_dir/mmgen.cfg'.format(self.tmpdir),
  39. 'sys': '{}/data_dir/{}/mmgen.cfg'.format(self.tmpdir,CfgFileSampleSys.test_fn_subdir),
  40. 'sample': '{}/data_dir/mmgen.cfg.sample'.format(self.tmpdir),
  41. }[id_str]
  42. def nosysfile(self):
  43. t = self.spawn_test()
  44. errstr = CfgFile.file_not_found_fs.format(CfgFileSampleSys.desc,self.path('shared_data')+'/mmgen.cfg')
  45. for i in (1,2,3,4,5):
  46. t.expect(errstr)
  47. for k in ('usr','sys','sample'):
  48. t.expect('{} cfg: {}'.format(k,self.path(k)))
  49. assert not os.path.exists(self.path(k)), self.path(k)
  50. t.read()
  51. return t
  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=[]):
  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)
  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. t.expect(cp,'n')
  106. if old_set:
  107. t.expect('unrecognized option')
  108. t.req_exit_val = 2
  109. if args == ['parse_test']:
  110. t.expect('parsed chunks: 29')
  111. t.expect('usr cfg: testnet=true rpc_password=passwOrd')
  112. t.read()
  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)