ut_tx.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python3
  2. """
  3. test.modtest_d.ut_tx: TX unit tests for the MMGen suite
  4. """
  5. import os
  6. from mmgen.tx import CompletedTX, UnsignedTX
  7. from mmgen.tx.file import MMGenTxFile
  8. from mmgen.cfg import Config
  9. from ..include.common import cfg, qmsg, vmsg
  10. async def do_txfile_test(desc, fns, cfg=cfg, check=False):
  11. qmsg(f' Testing CompletedTX initializer ({desc})')
  12. for fn in fns:
  13. qmsg(f' parsing: {os.path.basename(fn)}')
  14. fpath = os.path.join('test', 'ref', fn)
  15. tx = await CompletedTX(cfg=cfg, filename=fpath, quiet_open=True)
  16. vmsg('\n' + tx.info.format())
  17. f = MMGenTxFile(tx)
  18. fn_gen = f.make_filename()
  19. if cfg.debug_utf8:
  20. fn_gen = fn_gen.replace('-α', '')
  21. assert fn_gen == os.path.basename(fn), f'{fn_gen} != {fn}'
  22. if check:
  23. text = f.format()
  24. with open(fpath) as fh:
  25. text_chk = fh.read()
  26. assert text == text_chk, f'\nformatted text:\n{text}\n !=\noriginal file:\n{text_chk}'
  27. qmsg(' OK')
  28. return True
  29. class unit_tests:
  30. altcoin_deps = ('txfile_alt', 'txfile_alt_legacy')
  31. async def txfile(self, name, ut):
  32. return await do_txfile_test(
  33. 'Bitcoin',
  34. (
  35. 'tx/7A8157[6.65227,34].rawtx',
  36. 'tx/BB3FD2[7.57134314,123].sigtx',
  37. 'tx/0A869F[1.23456,32].regtest.asubtx',
  38. ),
  39. check = True
  40. )
  41. async def txfile_alt(self, name, ut):
  42. return await do_txfile_test(
  43. 'altcoins',
  44. (
  45. 'tx/C09D73-LTC[981.73747,2000].testnet.rawtx',
  46. 'tx/91060A-BCH[1.23456].regtest.arawtx',
  47. 'tx/D850C6-MM1[43.21,50000].subtx', # token tx
  48. ),
  49. # token resolved by tracking wallet under data_dir:
  50. cfg = Config({'data_dir': 'test/ref/data_dir'}),
  51. check = True
  52. )
  53. async def txfile_legacy(self, name, ut):
  54. return await do_txfile_test(
  55. 'Bitcoin - legacy file format',
  56. (
  57. '0B8D5A[15.31789,14,tl=1320969600].rawtx',
  58. '542169[5.68152,34].sigtx',
  59. '0C7115[15.86255,14,tl=1320969600].testnet.rawtx',
  60. '25EFA3[2.34].testnet.rawtx',
  61. )
  62. )
  63. async def txfile_alt_legacy(self, name, ut):
  64. return await do_txfile_test(
  65. 'altcoins - legacy file format',
  66. (
  67. '460D4D-BCH[10.19764,tl=1320969600].rawtx',
  68. 'ethereum/5881D2-MM1[1.23456,50000].rawtx',
  69. 'ethereum/6BDB25-MM1[1.23456,50000].testnet.rawtx',
  70. 'ethereum/88FEFD-ETH[23.45495,40000].rawtx',
  71. 'ethereum/B472BD-ETH[23.45495,40000].testnet.rawtx',
  72. 'ethereum/B472BD-ETH[23.45495,40000].testnet.sigtx',
  73. 'litecoin/A5A1E0-LTC[1454.64322,1453,tl=1320969600].testnet.rawtx',
  74. 'litecoin/AF3CDF-LTC[620.76194,1453,tl=1320969600].rawtx',
  75. )
  76. )
  77. def errors(self, name, ut):
  78. async def bad1():
  79. await CompletedTX(cfg, filename='foo')
  80. def bad2():
  81. UnsignedTX(cfg, filename='foo')
  82. bad_data = (
  83. ('forbidden positional args', 'TypeError', 'positional arguments', bad1),
  84. ('forbidden positional args', 'TypeError', 'positional arguments', bad2),
  85. )
  86. ut.process_bad_data(bad_data)
  87. return True