ct_opts.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2024 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # Project source code repository: https://github.com/mmgen/mmgen-wallet
  7. # Licensed according to the terms of GPL Version 3. See LICENSE for details.
  8. """
  9. test.cmdtest_d.ct_opts: options processing tests for the MMGen cmdtest.py test suite
  10. """
  11. import os, time
  12. from ..include.common import cfg
  13. from .ct_base import CmdTestBase
  14. class CmdTestOpts(CmdTestBase):
  15. 'command-line options parsing and processing'
  16. networks = ('btc',)
  17. tmpdir_nums = [41]
  18. cmd_group = (
  19. ('opt_helpscreen', (41, 'helpscreen output', [])),
  20. ('opt_noargs', (41, 'invocation with no user options or arguments', [])),
  21. ('opt_good1', (41, 'good opts (long opts only)', [])),
  22. ('opt_good2', (41, 'good opts (mixed short and long opts)', [])),
  23. ('opt_good3', (41, 'good opts (max arg count)', [])),
  24. ('opt_good4', (41, 'good opts (maxlen arg)', [])),
  25. ('opt_good5', (41, 'good opts (long opt substring)', [])),
  26. ('opt_good6', (41, 'good global opt (--coin=xmr)', [])),
  27. ('opt_good7', (41, 'good global opt (--coin xmr)', [])),
  28. ('opt_good8', (41, 'good global opt (--pager)', [])),
  29. ('opt_good9', (41, 'good cmdline arg ‘-’', [])),
  30. ('opt_good10', (41, 'good cmdline arg ‘-’ with arg', [])),
  31. ('opt_good11', (41, 'good cmdline arg ‘-’ with option', [])),
  32. ('opt_good12', (41, 'good cmdline opt (short opt with option)', [])),
  33. ('opt_good13', (41, 'good cmdline opt (short opt with option)', [])),
  34. ('opt_good14', (41, 'good cmdline opt (combined short opt with option)', [])),
  35. ('opt_good15', (41, 'good cmdline opt (combined short opt with option)', [])),
  36. ('opt_good16', (41, 'good cmdline opt (param with equals signs)', [])),
  37. ('opt_good17', (41, 'good cmdline opt (param with equals signs)', [])),
  38. ('opt_good18', (41, 'good cmdline opt (param with equals signs)', [])),
  39. ('opt_good19', (41, 'good cmdline opt (param with equals signs)', [])),
  40. ('opt_good20', (41, 'good cmdline opt (opt + negated opt)', [])),
  41. ('opt_good21', (41, 'good cmdline opt (negated negative opt)', [])),
  42. ('opt_good22', (41, 'good cmdline opt (opt + negated opt [substring])', [])),
  43. ('opt_good23', (41, 'good cmdline opt (negated negative opt [substring])', [])),
  44. ('opt_good24', (41, 'good cmdline opt (negated opt + opt [substring])', [])),
  45. ('opt_bad_param', (41, 'bad global opt (--pager=1)', [])),
  46. ('opt_bad_infile', (41, 'bad infile parameter', [])),
  47. ('opt_bad_outdir', (41, 'bad outdir parameter', [])),
  48. ('opt_bad_incompatible', (41, 'incompatible opts', [])),
  49. ('opt_bad_autoset', (41, 'invalid autoset value', [])),
  50. ('opt_invalid_1', (41, 'invalid cmdline opt ‘--x’', [])),
  51. ('opt_invalid_2', (41, 'invalid cmdline opt ‘---’', [])),
  52. ('opt_invalid_5', (41, 'invalid cmdline opt (missing parameter)', [])),
  53. ('opt_invalid_6', (41, 'invalid cmdline opt (missing parameter)', [])),
  54. ('opt_invalid_7', (41, 'invalid cmdline opt (parameter not required)', [])),
  55. ('opt_invalid_8', (41, 'invalid cmdline opt (non-existent option)', [])),
  56. ('opt_invalid_9', (41, 'invalid cmdline opt (non-existent option)', [])),
  57. ('opt_invalid_10', (41, 'invalid cmdline opt (missing parameter)', [])),
  58. ('opt_invalid_11', (41, 'invalid cmdline opt (missing parameter)', [])),
  59. ('opt_invalid_12', (41, 'invalid cmdline opt (non-existent option)', [])),
  60. ('opt_invalid_13', (41, 'invalid cmdline opt (ambiguous long opt substring)', [])),
  61. ('opt_invalid_14', (41, 'invalid cmdline opt (long opt substring too short)', [])),
  62. ('opt_invalid_15', (41, 'invalid cmdline (too many args)', [])),
  63. ('opt_invalid_16', (41, 'invalid cmdline (overlong arg)', [])),
  64. )
  65. def spawn_prog(self, args, exit_val=None):
  66. return self.spawn('test/misc/opts.py', args, cmd_dir='.', exit_val=exit_val)
  67. def check_vals(self, args, vals):
  68. t = self.spawn_prog(args)
  69. for k, v in vals:
  70. t.expect(rf'{k}:\s+{v}', regex=True)
  71. return t
  72. def do_run(self, args, expect, exit_val, regex=False):
  73. t = self.spawn_prog(args, exit_val=exit_val or None)
  74. t.expect(expect, regex=regex)
  75. return t
  76. def opt_helpscreen(self):
  77. expect = r'OPTS.PY: Opts test.*USAGE:\s+opts.py'
  78. if not cfg.pexpect_spawn:
  79. expect += r'.*--minconf.*NOTES FOR THIS.*a note'
  80. t = self.do_run(['--help'], expect, 0, regex=True)
  81. if t.pexpect_spawn:
  82. time.sleep(0.4)
  83. t.send('q')
  84. return t
  85. def opt_noargs(self):
  86. return self.check_vals(
  87. [],
  88. (
  89. ('cfg.foo', 'None'), # added opt
  90. ('cfg.print_checksum', 'None'), # sets 'quiet'
  91. ('cfg.quiet', 'False'), # _incompatible_opts
  92. ('cfg.verbose', 'False'), # _incompatible_opts
  93. ('cfg.passwd_file', ''), # _infile_opts - check_infile()
  94. ('cfg.outdir', ''), # check_outdir()
  95. ('cfg.cached_balances', 'False'),
  96. ('cfg.minconf', '1'),
  97. ('cfg.coin', 'BTC'),
  98. ('cfg.pager', 'False'),
  99. ('cfg.fee_estimate_mode', 'conservative'), # _autoset_opts
  100. ))
  101. def opt_good1(self):
  102. pf_base = 'testfile'
  103. pf = os.path.join(self.tmpdir, pf_base)
  104. self.write_to_tmpfile(pf_base, '')
  105. return self.check_vals(
  106. [
  107. '--print-checksum',
  108. '--fee-estimate-mode=E',
  109. '--passwd-file='+pf,
  110. '--outdir='+self.tmpdir,
  111. '--cached-balances',
  112. f'--hidden-incog-input-params={pf},123',
  113. ], (
  114. ('cfg.print_checksum', 'True'),
  115. ('cfg.quiet', 'True'), # set by print_checksum
  116. ('cfg.passwd_file', pf),
  117. ('cfg.outdir', self.tmpdir),
  118. ('cfg.cached_balances', 'True'),
  119. ('cfg.hidden_incog_input_params', pf+',123'),
  120. ('cfg.fee_estimate_mode', 'economical'),
  121. ))
  122. def opt_good2(self):
  123. return self.check_vals(
  124. [
  125. '--print-checksum',
  126. '-qX',
  127. f'--outdir={self.tmpdir}',
  128. '-p5',
  129. '-m', '0',
  130. '--seed-len=256',
  131. '-L--my-label',
  132. '--seed-len', '128',
  133. '--min-temp=-30',
  134. '-T-10',
  135. '--',
  136. 'x', 'y', '12345'
  137. ], (
  138. ('cfg.print_checksum', 'True'),
  139. ('cfg.quiet', 'True'),
  140. ('cfg.outdir', self.tmpdir),
  141. ('cfg.cached_balances', 'True'),
  142. ('cfg.minconf', '0'),
  143. ('cfg.keep_label', 'None'),
  144. ('cfg.seed_len', '128'),
  145. ('cfg.hash_preset', '5'),
  146. ('cfg.label', '--my-label'),
  147. ('cfg.min_temp', '-30'),
  148. ('cfg.max_temp', '-10'),
  149. ('arg1', 'x'),
  150. ('arg2', 'y'),
  151. ('arg3', '12345'),
  152. ))
  153. def opt_good3(self):
  154. return self.check_vals(['m'] * 256, (('arg256', 'm'),))
  155. def opt_good4(self):
  156. return self.check_vals(['e' * 4096], (('arg1', 'e' * 4096),))
  157. def opt_good5(self):
  158. return self.check_vals(['--minc=7'], (('cfg.minconf', '7'),))
  159. def opt_good6(self):
  160. return self.check_vals(['--coin=xmr'], (('cfg.coin', 'XMR'),))
  161. def opt_good7(self):
  162. return self.check_vals(['--coin', 'xmr'], (('cfg.coin', 'XMR'),))
  163. def opt_good8(self):
  164. return self.check_vals(['--pager'], (('cfg.pager', 'True'),))
  165. def opt_good9(self):
  166. return self.check_vals(['-'], (('arg1', '-'),))
  167. def opt_good10(self):
  168. return self.check_vals(['-', '-x'], (('arg1', '-'), ('arg2', '-x')))
  169. def opt_good11(self):
  170. return self.check_vals(['-q', '-', '-x'], (('arg1', '-'), ('arg2', '-x')))
  171. def opt_good12(self):
  172. return self.check_vals(['-l128'], (('cfg.seed_len', '128'),))
  173. def opt_good13(self):
  174. return self.check_vals(['-l', '128'], (('cfg.seed_len', '128'),))
  175. def opt_good14(self):
  176. return self.check_vals(['-kl128'], (('cfg.keep_label', 'True'), ('cfg.seed_len', '128')))
  177. def opt_good15(self):
  178. return self.check_vals(['-kl', '128'], (('cfg.keep_label', 'True'), ('cfg.seed_len', '128')))
  179. def opt_good16(self):
  180. return self.check_vals(['--point=x=1,y=2,z=3'], (('cfg.point', 'x=1,y=2,z=3'),))
  181. def opt_good17(self):
  182. return self.check_vals(['--point', 'x=1,y=2,z=3'], (('cfg.point', 'x=1,y=2,z=3'),))
  183. def opt_good18(self):
  184. return self.check_vals(['-xx=1,y=2,z=3'], (('cfg.point', 'x=1,y=2,z=3'),))
  185. def opt_good19(self):
  186. return self.check_vals(['-x', 'x=1,y=2,z=3'], (('cfg.point', 'x=1,y=2,z=3'),))
  187. def opt_good20(self):
  188. return self.check_vals(['--pager', '--no-pager'], (('cfg.pager', 'False'),))
  189. def opt_good21(self):
  190. return self.check_vals(['--foobleize'], (('cfg.no_foobleize', 'False'),))
  191. def opt_good22(self):
  192. return self.check_vals(['--quiet', '--no-q'], (('cfg.quiet', 'False'),))
  193. def opt_good23(self):
  194. return self.check_vals(['--foobl'], (('cfg.no_foobleize', 'False'),))
  195. def opt_good24(self):
  196. return self.check_vals(['--no-pag', '--pag'], (('cfg.pager', 'True'),))
  197. def opt_bad_param(self):
  198. return self.do_run(['--pager=1'], 'no parameter', 1)
  199. def opt_bad_infile(self):
  200. pf = os.path.join(self.tmpdir, 'fubar')
  201. return self.do_run(['--passwd-file='+pf], 'not found', 1)
  202. def opt_bad_outdir(self):
  203. bo = self.tmpdir+'_fubar'
  204. return self.do_run(['--outdir='+bo], 'not found', 1)
  205. def opt_bad_incompatible(self):
  206. return self.do_run(['--label=Label', '--keep-label'], 'Conflicting options', 1)
  207. def opt_bad_autoset(self):
  208. return self.do_run(['--fee-estimate-mode=Fubar'], 'not unique substring', 1)
  209. def opt_invalid(self, args, expect, exit_val):
  210. t = self.spawn_prog(args, exit_val=exit_val)
  211. t.expect(expect)
  212. return t
  213. def opt_invalid_1(self):
  214. return self.opt_invalid(['--x'], 'must be at least', 1)
  215. def opt_invalid_2(self):
  216. return self.opt_invalid(['---'], 'must be at least', 1)
  217. def opt_invalid_5(self):
  218. return self.opt_invalid(['-l'], 'missing parameter', 1)
  219. def opt_invalid_6(self):
  220. return self.opt_invalid(['-l', '-k'], 'missing parameter', 1)
  221. def opt_invalid_7(self):
  222. return self.opt_invalid(['--quiet=1'], 'requires no parameter', 1)
  223. def opt_invalid_8(self):
  224. return self.opt_invalid(['-w'], 'unrecognized option', 1)
  225. def opt_invalid_9(self):
  226. return self.opt_invalid(['--frobnicate'], 'unrecognized option', 1)
  227. def opt_invalid_10(self):
  228. return self.opt_invalid(['--label', '-q'], 'missing parameter', 1)
  229. def opt_invalid_11(self):
  230. return self.opt_invalid(['-T', '-10'], 'missing parameter', 1)
  231. def opt_invalid_12(self):
  232. return self.opt_invalid(['-q', '-10'], 'unrecognized option', 1)
  233. def opt_invalid_13(self):
  234. return self.opt_invalid(['--mi=3'], 'ambiguous option', 1)
  235. def opt_invalid_14(self):
  236. return self.opt_invalid(['--m=3'], 'must be at least', 1)
  237. def opt_invalid_15(self):
  238. return self.opt_invalid(['m'] * 257, 'too many', 1)
  239. def opt_invalid_16(self):
  240. return self.opt_invalid(['e' * 4097], 'too long', 1)