ut_rpc.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/usr/bin/env python3
  2. """
  3. test.unit_tests_d.ut_rpc: RPC unit test for the MMGen suite
  4. """
  5. from mmgen.common import *
  6. from mmgen.protocol import init_proto
  7. from mmgen.rpc import rpc_init
  8. from mmgen.daemon import CoinDaemon
  9. from mmgen.base_proto.monero.rpc import MoneroWalletRPCClient
  10. from mmgen.base_proto.monero.daemon import MoneroWalletDaemon
  11. def cfg_file_auth_test(proto,d):
  12. qmsg(cyan(f'\n Testing authentication with credentials from {d.cfg_file}:'))
  13. d.remove_datadir() # removes cookie file to force authentication from cfg file
  14. os.makedirs(d.network_datadir)
  15. cf = os.path.join(d.datadir,d.cfg_file)
  16. with open(cf,'a') as fp:
  17. fp.write('\nrpcuser = ut_rpc\nrpcpassword = ut_rpc_passw0rd\n')
  18. d.flag.keep_cfg_file = True
  19. d.start()
  20. async def do():
  21. rpc = await rpc_init(proto)
  22. assert rpc.auth.user == 'ut_rpc', f'{rpc.auth.user}: user is not ut_rpc!'
  23. run_session(do())
  24. d.stop()
  25. def do_msg(rpc,backend):
  26. bname = type(rpc.backend).__name__
  27. qmsg(' Testing backend {!r}{}'.format( bname, '' if backend == bname else f' [{backend}]' ))
  28. class init_test:
  29. async def btc(proto,backend,daemon):
  30. rpc = await rpc_init(proto,backend,daemon)
  31. do_msg(rpc,backend)
  32. bh = (await rpc.call('getblockchaininfo',timeout=300))['bestblockhash']
  33. await rpc.gathered_call('getblock',((bh,),(bh,1)),timeout=300)
  34. await rpc.gathered_call(None,(('getblock',(bh,)),('getblock',(bh,1))),timeout=300)
  35. async def bch(proto,backend,daemon):
  36. rpc = await rpc_init(proto,backend,daemon)
  37. do_msg(rpc,backend)
  38. ltc = bch
  39. async def eth(proto,backend,daemon):
  40. rpc = await rpc_init(proto,backend,daemon)
  41. do_msg(rpc,backend)
  42. await rpc.call('eth_blockNumber',timeout=300)
  43. etc = eth
  44. def run_test(network_ids,test_cf_auth=False,daemon_ids=None):
  45. def do(d):
  46. if not opt.no_daemon_stop:
  47. d.stop()
  48. if not opt.no_daemon_autostart:
  49. d.remove_datadir()
  50. d.start()
  51. for backend in g.autoset_opts['rpc_backend'].choices:
  52. test = getattr(init_test,d.proto.coin.lower())
  53. run_session(test(d.proto,backend,d),backend=backend)
  54. if not opt.no_daemon_stop:
  55. d.stop()
  56. if test_cf_auth and g.platform != 'win':
  57. cfg_file_auth_test(d.proto,d)
  58. qmsg('')
  59. for network_id in network_ids:
  60. proto = init_proto(network_id=network_id)
  61. ids = (lambda x:
  62. set(daemon_ids) & set(x) if daemon_ids else x
  63. )(CoinDaemon.coins[proto.coin].daemon_ids)
  64. for daemon_id in ids:
  65. do( CoinDaemon(proto=proto,test_suite=True,daemon_id=daemon_id) )
  66. return True
  67. class unit_tests:
  68. altcoin_deps = ('ltc','bch','geth','erigon','openethereum','parity','xmrwallet')
  69. win_skip = ('xmrwallet',) # FIXME - wallet doesn't open
  70. arm_skip = ('openethereum','parity') # no prebuilt binaries for ARM
  71. def btc(self,name,ut):
  72. return run_test(['btc','btc_tn'],test_cf_auth=True)
  73. def ltc(self,name,ut):
  74. return run_test(['ltc','ltc_tn'],test_cf_auth=True)
  75. def bch(self,name,ut):
  76. return run_test(['bch','bch_tn'],test_cf_auth=True)
  77. def geth(self,name,ut):
  78. return run_test(['eth_tn','eth_rt'],daemon_ids=['geth']) # mainnet returns EIP-155 error on empty blockchain
  79. def erigon(self,name,ut):
  80. return run_test(['eth','eth_tn','eth_rt'],daemon_ids=['erigon'])
  81. def openethereum(self,name,ut):
  82. return run_test(['eth','eth_tn','eth_rt'],daemon_ids=['openethereum'])
  83. def parity(self,name,ut):
  84. return run_test(['etc'])
  85. def xmrwallet(self,name,ut):
  86. async def run():
  87. networks = init_proto('xmr').networks
  88. daemons = [(
  89. CoinDaemon(proto=proto,test_suite=True),
  90. MoneroWalletDaemon(
  91. proto = proto,
  92. test_suite = True,
  93. wallet_dir = 'test/trash2',
  94. passwd = 'ut_rpc_passw0rd' )
  95. ) for proto in (init_proto( 'xmr', network=network ) for network in networks) ]
  96. for md,wd in daemons:
  97. if not opt.no_daemon_autostart:
  98. md.start()
  99. wd.start()
  100. c = MoneroWalletRPCClient(daemon=wd)
  101. fn = f'monero-{wd.network}-junk-wallet'
  102. qmsg(f'Creating {wd.network} wallet')
  103. await c.call(
  104. 'restore_deterministic_wallet',
  105. filename = fn,
  106. password = 'foo',
  107. seed = xmrseed().fromhex('beadface'*8,tostr=True) )
  108. qmsg(f'Opening {wd.network} wallet')
  109. await c.call( 'open_wallet', filename=fn, password='foo' )
  110. for md,wd in daemons:
  111. wd.wait = False
  112. await wd.rpc.stop_daemon()
  113. if not opt.no_daemon_stop:
  114. md.wait = False
  115. await md.rpc.stop_daemon()
  116. gmsg('OK')
  117. from mmgen.xmrseed import xmrseed
  118. import shutil
  119. shutil.rmtree('test/trash2',ignore_errors=True)
  120. os.makedirs('test/trash2')
  121. run_session(run())
  122. return True