ut_rpc.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.exception import *
  7. from mmgen.protocol import init_proto
  8. from mmgen.rpc import rpc_init,MoneroWalletRPCClient
  9. from mmgen.daemon import CoinDaemon,MoneroWalletDaemon
  10. def auth_test(proto,d):
  11. d.stop()
  12. if g.platform != 'win':
  13. qmsg(f'\n Testing authentication with credentials from bitcoin.conf:')
  14. d.remove_datadir()
  15. os.makedirs(d.datadir)
  16. cf = os.path.join(d.datadir,'bitcoin.conf')
  17. open(cf,'a').write('\nrpcuser = ut_rpc\nrpcpassword = ut_rpc_passw0rd\n')
  18. d.add_flag('keep_cfg_file')
  19. d.start()
  20. async def do():
  21. rpc = await rpc_init(proto)
  22. assert rpc.auth.user == 'ut_rpc', 'user is not ut_rpc!'
  23. run_session(do())
  24. d.stop()
  25. def do_msg(rpc):
  26. qmsg(' Testing backend {!r}'.format(type(rpc.backend).__name__))
  27. class init_test:
  28. async def btc(proto,backend):
  29. rpc = await rpc_init(proto,backend)
  30. do_msg(rpc)
  31. addrs = (
  32. ('bc1qvmqas4maw7lg9clqu6kqu9zq9cluvlln5hw97q','test address #1'), # deadbeef * 8
  33. ('bc1qe50rj25cldtskw5huxam335kyshtqtlrf4pt9x','test address #2'), # deadbeef * 7 + deadbeee
  34. )
  35. await rpc.batch_call('importaddress',addrs,timeout=120)
  36. ret = await rpc.batch_call('getaddressesbylabel',[(l,) for a,l in addrs])
  37. assert list(ret[0].keys())[0] == addrs[0][0]
  38. bh = (await rpc.call('getblockchaininfo',timeout=300))['bestblockhash']
  39. await rpc.gathered_call('getblock',((bh,),(bh,1)),timeout=300)
  40. await rpc.gathered_call(None,(('getblock',(bh,)),('getblock',(bh,1))),timeout=300)
  41. async def bch(proto,backend):
  42. rpc = await rpc_init(proto,backend)
  43. do_msg(rpc)
  44. async def eth(proto,backend):
  45. rpc = await rpc_init(proto,backend)
  46. do_msg(rpc)
  47. await rpc.call('parity_versionInfo',timeout=300)
  48. def run_test(coin,auth):
  49. proto = init_proto(coin,network=('mainnet','regtest')[coin=='eth']) # FIXME CoinDaemon's network handling broken
  50. d = CoinDaemon(network_id=coin,test_suite=True)
  51. if auth:
  52. d.remove_datadir()
  53. d.start()
  54. for backend in g.autoset_opts['rpc_backend'].choices:
  55. run_session(getattr(init_test,coin)(proto,backend),backend=backend)
  56. if auth:
  57. auth_test(proto,d)
  58. qmsg(' OK')
  59. return True
  60. class unit_tests:
  61. def bch(self,name,ut):
  62. return run_test('bch',auth=True)
  63. def btc(self,name,ut):
  64. return run_test('btc',auth=True)
  65. def eth(self,name,ut):
  66. return run_test('eth',auth=False)
  67. def xmr_wallet(self,name,ut):
  68. async def run():
  69. md = CoinDaemon('xmr',test_suite=True)
  70. if not opt.no_daemon_autostart:
  71. md.start()
  72. g.monero_wallet_rpc_password = 'passwOrd'
  73. mwd = MoneroWalletDaemon(wallet_dir='test/trash',test_suite=True)
  74. mwd.start()
  75. c = MoneroWalletRPCClient(
  76. host = g.monero_wallet_rpc_host,
  77. port = mwd.rpc_port,
  78. user = g.monero_wallet_rpc_user,
  79. passwd = g.monero_wallet_rpc_password)
  80. await c.call('get_version')
  81. gmsg('OK')
  82. mwd.wait = False
  83. mwd.stop()
  84. if not opt.no_daemon_stop:
  85. md.wait = False
  86. md.stop()
  87. run_session(run())
  88. return True