ct_opts.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2025 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_good25', (41, 'good cmdline opt (--btc-rpc-host)', [])),
  46. ('opt_good26', (41, 'good cmdline opt (--btc-rpc-port)', [])),
  47. ('opt_good27', (41, 'good cmdline opt (--btc-ignore-daemon-version)', [])),
  48. ('opt_good28', (41, 'good cmdline opt (--bch-cashaddr)', [])),
  49. ('opt_good29', (41, 'good cmdline opt (--etc-max-tx-fee=0.1)', [])),
  50. ('opt_good30', (41, 'good cmdline opt (--eth-chain-names=foo,bar)', [])),
  51. ('opt_good31', (41, 'good cmdline opt (--xmr-rpc-port=28081)', [])),
  52. ('opt_bad_param', (41, 'bad global opt (--pager=1)', [])),
  53. ('opt_bad_infile', (41, 'bad infile parameter', [])),
  54. ('opt_bad_outdir', (41, 'bad outdir parameter', [])),
  55. ('opt_bad_incompatible', (41, 'incompatible opts', [])),
  56. ('opt_bad_autoset', (41, 'invalid autoset value', [])),
  57. ('opt_invalid_1', (41, 'invalid cmdline opt ‘--x’', [])),
  58. ('opt_invalid_2', (41, 'invalid cmdline opt ‘---’', [])),
  59. ('opt_invalid_5', (41, 'invalid cmdline opt (missing parameter)', [])),
  60. ('opt_invalid_6', (41, 'invalid cmdline opt (missing parameter)', [])),
  61. ('opt_invalid_7', (41, 'invalid cmdline opt (parameter not required)', [])),
  62. ('opt_invalid_8', (41, 'invalid cmdline opt (non-existent option)', [])),
  63. ('opt_invalid_9', (41, 'invalid cmdline opt (non-existent option)', [])),
  64. ('opt_invalid_10', (41, 'invalid cmdline opt (missing parameter)', [])),
  65. ('opt_invalid_11', (41, 'invalid cmdline opt (missing parameter)', [])),
  66. ('opt_invalid_12', (41, 'invalid cmdline opt (non-existent option)', [])),
  67. ('opt_invalid_13', (41, 'invalid cmdline opt (ambiguous long opt substring)', [])),
  68. ('opt_invalid_14', (41, 'invalid cmdline opt (long opt substring too short)', [])),
  69. ('opt_invalid_15', (41, 'invalid cmdline (too many args)', [])),
  70. ('opt_invalid_16', (41, 'invalid cmdline (overlong arg)', [])),
  71. ('opt_invalid_17', (41, 'invalid cmdline opt (--btc-rpc-host without ‘need_proto’)', [])),
  72. ('opt_invalid_18', (41, 'invalid cmdline opt (--btc-rpc-port without ‘need_proto’)', [])),
  73. ('opt_invalid_19', (41, 'invalid cmdline opt (--btc-rpc-port with non-integer param)', [])),
  74. ('opt_invalid_21', (41, 'invalid cmdline opt (--btc-foo)', [])),
  75. ('opt_invalid_22', (41, 'invalid cmdline opt (--btc-rpc-host with missing param)', [])),
  76. ('opt_invalid_23', (41, 'invalid cmdline opt (--btc-ignore-daemon-version with param)', [])),
  77. ('opt_invalid_24', (41, 'invalid cmdline opt (--bch-cashaddr without ‘need_proto’)', [])),
  78. ('opt_invalid_25', (41, 'invalid cmdline opt (--bch-cashaddr without parameter)', [])),
  79. ('opt_invalid_26', (41, 'invalid cmdline opt (--bch-cashaddr with non-bool parameter)', [])),
  80. ('opt_invalid_27', (41, 'invalid cmdline opt (--ltc-cashaddr)', [])),
  81. ('opt_invalid_28', (41, 'invalid cmdline opt (--xmr-max-tx-fee)', [])),
  82. ('opt_invalid_29', (41, 'invalid cmdline opt (--eth-max-tx-fee without parameter)', [])),
  83. ('opt_invalid_30', (41, 'invalid cmdline opt (--eth-max-tx-fee with non-numeric parameter)', [])),
  84. ('opt_invalid_31', (41, 'invalid cmdline opt (--bch-cashaddr without --coin=bch)', [])),
  85. ('opt_invalid_32', (41, 'invalid cmdline opt (--eth-chain-names without --coin=eth)', [])),
  86. ('opt_invalid_33', (41, 'invalid cmdline opt (--xmr-rpc-host)', [])),
  87. ('opt_invalid_34', (41, 'invalid cmdline opt (--eth-rpc-user)', [])),
  88. )
  89. def spawn_prog(self, args, opts=[], exit_val=None, need_proto=False):
  90. return self.spawn(
  91. 'test/misc/opts.py',
  92. opts + args,
  93. cmd_dir = '.',
  94. exit_val = exit_val,
  95. env = {'TEST_MISC_OPTS_NEEDS_PROTO': '1' if need_proto else ''})
  96. def check_vals(self, args, vals, check=True, need_proto=False):
  97. show_opts = [a.removeprefix('cfg.') for a, b in vals if a.startswith('cfg.')]
  98. t = self.spawn_prog(
  99. args,
  100. opts = ['--show-opts=' + ','.join(show_opts)] if show_opts else [],
  101. need_proto = need_proto)
  102. if check:
  103. for k, v in vals:
  104. t.expect(rf'{k}:\s+{v}', regex=True)
  105. return t
  106. def do_run(self, args, expect, exit_val, regex=False):
  107. t = self.spawn_prog(args, exit_val=exit_val or None)
  108. t.expect(expect, regex=regex)
  109. return t
  110. def opt_helpscreen(self):
  111. expect = r'OPTS.PY: Opts test.*USAGE:\s+opts.py'
  112. if not cfg.pexpect_spawn:
  113. expect += r'.*--minconf.*NOTES FOR THIS.*a note'
  114. t = self.do_run(['--help'], expect, 0, regex=True)
  115. if t.pexpect_spawn:
  116. time.sleep(0.4)
  117. t.send('q')
  118. return t
  119. def opt_noargs(self):
  120. return self.check_vals(
  121. [],
  122. (
  123. ('cfg.foo', 'None'), # added opt
  124. ('cfg.print_checksum', 'None'), # sets 'quiet'
  125. ('cfg.quiet', 'False'), # _incompatible_opts
  126. ('cfg.verbose', 'False'), # _incompatible_opts
  127. ('cfg.passwd_file', ''), # _infile_opts - check_infile()
  128. ('cfg.outdir', ''), # check_outdir()
  129. ('cfg.cached_balances', 'False'),
  130. ('cfg.minconf', '1'),
  131. ('cfg.coin', 'BTC'),
  132. ('cfg.pager', 'False'),
  133. ('cfg.fee_estimate_mode', 'conservative'), # _autoset_opts
  134. ))
  135. def opt_good1(self):
  136. pf_base = 'testfile'
  137. pf = os.path.join(self.tmpdir, pf_base)
  138. self.write_to_tmpfile(pf_base, '')
  139. return self.check_vals(
  140. [
  141. '--print-checksum',
  142. '--fee-estimate-mode=E',
  143. '--passwd-file='+pf,
  144. '--outdir='+self.tmpdir,
  145. '--cached-balances',
  146. f'--hidden-incog-input-params={pf},123',
  147. ], (
  148. ('cfg.print_checksum', 'True'),
  149. ('cfg.quiet', 'True'), # set by print_checksum
  150. ('cfg.passwd_file', pf),
  151. ('cfg.outdir', self.tmpdir),
  152. ('cfg.cached_balances', 'True'),
  153. ('cfg.hidden_incog_input_params', pf+',123'),
  154. ('cfg.fee_estimate_mode', 'economical'),
  155. ))
  156. def opt_good2(self):
  157. return self.check_vals(
  158. [
  159. '--print-checksum',
  160. '-qX',
  161. f'--outdir={self.tmpdir}',
  162. '-p5',
  163. '-m', '0',
  164. '--seed-len=256',
  165. '-L--my-label',
  166. '--seed-len', '128',
  167. '--min-temp=-30',
  168. '-T-10',
  169. '--',
  170. 'x', 'y', '12345'
  171. ], (
  172. ('cfg.print_checksum', 'True'),
  173. ('cfg.quiet', 'True'),
  174. ('cfg.outdir', self.tmpdir),
  175. ('cfg.cached_balances', 'True'),
  176. ('cfg.minconf', '0'),
  177. ('cfg.keep_label', 'None'),
  178. ('cfg.seed_len', '128'),
  179. ('cfg.hash_preset', '5'),
  180. ('cfg.label', '--my-label'),
  181. ('cfg.min_temp', '-30'),
  182. ('cfg.max_temp', '-10'),
  183. ('arg1', 'x'),
  184. ('arg2', 'y'),
  185. ('arg3', '12345'),
  186. ))
  187. def opt_good3(self):
  188. return self.check_vals(['m'] * 256, (('arg256', 'm'),))
  189. def opt_good4(self):
  190. return self.check_vals(['e' * 4096], (('arg1', 'e' * 4096),))
  191. def opt_good5(self):
  192. return self.check_vals(['--minc=7'], (('cfg.minconf', '7'),))
  193. def opt_good6(self):
  194. if cfg.no_altcoin:
  195. return 'skip'
  196. return self.check_vals(['--coin=xmr'], (('cfg.coin', 'XMR'),))
  197. def opt_good7(self):
  198. if cfg.no_altcoin:
  199. return 'skip'
  200. return self.check_vals(['--coin', 'xmr'], (('cfg.coin', 'XMR'),))
  201. def opt_good8(self):
  202. return self.check_vals(['--pager'], (('cfg.pager', 'True'),))
  203. def opt_good9(self):
  204. return self.check_vals(['-'], (('arg1', '-'),))
  205. def opt_good10(self):
  206. return self.check_vals(['-', '-x'], (('arg1', '-'), ('arg2', '-x')))
  207. def opt_good11(self):
  208. return self.check_vals(['-q', '-', '-x'], (('arg1', '-'), ('arg2', '-x')))
  209. def opt_good12(self):
  210. return self.check_vals(['-l128'], (('cfg.seed_len', '128'),))
  211. def opt_good13(self):
  212. return self.check_vals(['-l', '128'], (('cfg.seed_len', '128'),))
  213. def opt_good14(self):
  214. return self.check_vals(['-kl128'], (('cfg.keep_label', 'True'), ('cfg.seed_len', '128')))
  215. def opt_good15(self):
  216. return self.check_vals(['-kl', '128'], (('cfg.keep_label', 'True'), ('cfg.seed_len', '128')))
  217. def opt_good16(self):
  218. return self.check_vals(['--point=x=1,y=2,z=3'], (('cfg.point', 'x=1,y=2,z=3'),))
  219. def opt_good17(self):
  220. return self.check_vals(['--point', 'x=1,y=2,z=3'], (('cfg.point', 'x=1,y=2,z=3'),))
  221. def opt_good18(self):
  222. return self.check_vals(['-xx=1,y=2,z=3'], (('cfg.point', 'x=1,y=2,z=3'),))
  223. def opt_good19(self):
  224. return self.check_vals(['-x', 'x=1,y=2,z=3'], (('cfg.point', 'x=1,y=2,z=3'),))
  225. def opt_good20(self):
  226. return self.check_vals(['--pager', '--no-pager'], (('cfg.pager', 'False'),))
  227. def opt_good21(self):
  228. return self.check_vals(['--foobleize'], (('cfg.no_foobleize', 'False'),))
  229. def opt_good22(self):
  230. return self.check_vals(['--quiet', '--no-q'], (('cfg.quiet', 'False'),))
  231. def opt_good23(self):
  232. return self.check_vals(['--foobl'], (('cfg.no_foobleize', 'False'),))
  233. def opt_good24(self):
  234. return self.check_vals(['--no-pag', '--pag'], (('cfg.pager', 'True'),))
  235. def opt_good25(self):
  236. return self.check_vals(
  237. ['--btc-rpc-host=pi5'],
  238. (('cfg.btc_rpc_host', 'pi5'), ('proto.rpc_host', 'pi5')),
  239. need_proto=True)
  240. def opt_good26(self):
  241. return self.check_vals(
  242. ['--btc-rpc-port=7272'],
  243. (('cfg.btc_rpc_port', '7272'), ('proto.rpc_port', '7272')),
  244. need_proto=True)
  245. def opt_good27(self):
  246. return self.check_vals(
  247. ['--btc-ignore-daemon-version'],
  248. (('cfg.btc_ignore_daemon_version', 'True'), ('proto.ignore_daemon_version', 'True'),),
  249. need_proto = True)
  250. def opt_good28(self):
  251. return self.check_vals(
  252. ['--coin=bch', '--bch-cashaddr=yes'],
  253. (('cfg.bch_cashaddr', 'True'), ('proto.cashaddr', 'True'),),
  254. need_proto = True)
  255. def opt_good29(self):
  256. return self.check_vals(['--etc-max-tx-fee=0.1'], (('cfg.etc_max_tx_fee', '0.1'),), need_proto=True)
  257. def opt_good30(self):
  258. return self.check_vals(
  259. ['--coin=eth', '--eth-mainnet-chain-names=foo,bar'],
  260. (('cfg.eth_mainnet_chain_names', r"\['foo', 'bar'\]"), ('proto.chain_names', r"\['foo', 'bar'\]")),
  261. need_proto = True)
  262. def opt_good31(self):
  263. return self.check_vals(
  264. ['--coin=xmr', '--xmr-rpc-port=28081'],
  265. (('cfg.xmr_rpc_port', '28081'),('proto.rpc_port', '28081'),),
  266. need_proto = True)
  267. def opt_bad_param(self):
  268. return self.do_run(['--pager=1'], 'no parameter', 1)
  269. def opt_bad_infile(self):
  270. pf = os.path.join(self.tmpdir, 'fubar')
  271. return self.do_run(['--passwd-file='+pf], 'not found', 1)
  272. def opt_bad_outdir(self):
  273. bo = self.tmpdir+'_fubar'
  274. return self.do_run(['--outdir='+bo], 'not found', 1)
  275. def opt_bad_incompatible(self):
  276. return self.do_run(['--label=Label', '--keep-label'], 'Conflicting options', 1)
  277. def opt_bad_autoset(self):
  278. return self.do_run(['--fee-estimate-mode=Fubar'], 'not unique substring', 1)
  279. def opt_invalid(self, args, expect, opts=[], need_proto=False, exit_val=1):
  280. t = self.spawn_prog(args, opts=opts, exit_val=exit_val, need_proto=need_proto)
  281. t.expect(expect)
  282. return t
  283. def opt_invalid_1(self):
  284. return self.opt_invalid(['--x'], 'must be at least')
  285. def opt_invalid_2(self):
  286. return self.opt_invalid(['---'], 'must be at least')
  287. def opt_invalid_5(self):
  288. return self.opt_invalid(['-l'], 'missing parameter')
  289. def opt_invalid_6(self):
  290. return self.opt_invalid(['-l', '-k'], 'missing parameter')
  291. def opt_invalid_7(self):
  292. return self.opt_invalid(['--quiet=1'], 'requires no parameter')
  293. def opt_invalid_8(self):
  294. return self.opt_invalid(['-w'], 'unrecognized option')
  295. def opt_invalid_9(self):
  296. return self.opt_invalid(['--frobnicate'], 'unrecognized option')
  297. def opt_invalid_10(self):
  298. return self.opt_invalid(['--label', '-q'], 'missing parameter')
  299. def opt_invalid_11(self):
  300. return self.opt_invalid(['-T', '-10'], 'missing parameter')
  301. def opt_invalid_12(self):
  302. return self.opt_invalid(['-q', '-10'], 'unrecognized option')
  303. def opt_invalid_13(self):
  304. return self.opt_invalid(['--mi=3'], 'ambiguous option')
  305. def opt_invalid_14(self):
  306. return self.opt_invalid(['--m=3'], 'must be at least')
  307. def opt_invalid_15(self):
  308. return self.opt_invalid(['m'] * 257, 'too many')
  309. def opt_invalid_16(self):
  310. return self.opt_invalid(['e' * 4097], 'too long')
  311. def opt_invalid_17(self):
  312. return self.opt_invalid(['--btc-rpc-host'], 'unrecognized option')
  313. def opt_invalid_18(self):
  314. return self.opt_invalid(['--btc-rpc-port'], 'unrecognized option')
  315. def opt_invalid_19(self):
  316. return self.opt_invalid(['--btc-rpc-port=foo'], "must be of type 'int'", need_proto=True)
  317. def opt_invalid_21(self):
  318. return self.opt_invalid(['--btc-foo'], 'unrecognized option')
  319. def opt_invalid_22(self):
  320. return self.opt_invalid(['--btc-rpc-host'], 'missing parameter', need_proto=True)
  321. def opt_invalid_23(self):
  322. return self.opt_invalid(['--btc-ignore-daemon-version=1'], 'requires no parameter', need_proto=True)
  323. def opt_invalid_24(self):
  324. return self.opt_invalid(['--bch-cashaddr'], 'unrecognized option')
  325. def opt_invalid_25(self):
  326. return self.opt_invalid(['--bch-cashaddr'], 'missing parameter', need_proto=True)
  327. def opt_invalid_26(self):
  328. return self.opt_invalid(['--bch-cashaddr=foo'], "must be of type 'bool'", need_proto=True)
  329. def opt_invalid_27(self):
  330. return self.opt_invalid(['--ltc-cashaddr'], 'unrecognized option', need_proto=True)
  331. def opt_invalid_28(self):
  332. return self.opt_invalid(['--xmr-max-tx-fee=0.1'], 'unrecognized option', need_proto=True)
  333. def opt_invalid_29(self):
  334. return self.opt_invalid(['--eth-max-tx-fee'], 'missing parameter', need_proto=True)
  335. def opt_invalid_30(self):
  336. return self.opt_invalid(['--eth-max-tx-fee=true'], 'must be of type', need_proto=True)
  337. def opt_invalid_31(self):
  338. return self.opt_invalid(['--bch-cashaddr=true'], 'has no attribute', opts=['--show-opts=bch_cashaddr'], need_proto=True)
  339. def opt_invalid_32(self):
  340. return self.opt_invalid(['--eth-chain-names=foo,bar'], 'unrecognized option', need_proto=True)
  341. def opt_invalid_33(self):
  342. return self.opt_invalid(['--xmr-rpc-host=solaris'], 'unrecognized option', need_proto=True)
  343. def opt_invalid_34(self):
  344. return self.opt_invalid(['--eth-rpc-user=bob'], 'unrecognized option', need_proto=True)