ut_rpc.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 cfg_file_auth_test(proto,d):
  11. qmsg(cyan(f'\n Testing authentication with credentials from {d.cfg_file}:'))
  12. d.remove_datadir() # removes cookie file to force authentication from cfg file
  13. os.makedirs(d.network_datadir)
  14. cf = os.path.join(d.datadir,d.cfg_file)
  15. open(cf,'a').write('\nrpcuser = ut_rpc\nrpcpassword = ut_rpc_passw0rd\n')
  16. d.add_flag('keep_cfg_file')
  17. d.start()
  18. async def do():
  19. rpc = await rpc_init(proto)
  20. assert rpc.auth.user == 'ut_rpc', f'{rpc.auth.user}: user is not ut_rpc!'
  21. run_session(do())
  22. d.stop()
  23. def do_msg(rpc):
  24. qmsg(' Testing backend {!r}'.format(type(rpc.backend).__name__))
  25. class init_test:
  26. async def btc(proto,backend):
  27. rpc = await rpc_init(proto,backend)
  28. do_msg(rpc)
  29. bh = (await rpc.call('getblockchaininfo',timeout=300))['bestblockhash']
  30. await rpc.gathered_call('getblock',((bh,),(bh,1)),timeout=300)
  31. await rpc.gathered_call(None,(('getblock',(bh,)),('getblock',(bh,1))),timeout=300)
  32. async def bch(proto,backend):
  33. rpc = await rpc_init(proto,backend)
  34. do_msg(rpc)
  35. ltc = bch
  36. async def eth(proto,backend):
  37. rpc = await rpc_init(proto,backend)
  38. do_msg(rpc)
  39. await rpc.call('eth_blockNumber',timeout=300)
  40. etc = eth
  41. def run_test(network_ids,test_cf_auth): # TODO: run all available networks simultaneously
  42. for network_id in network_ids:
  43. proto = init_proto(network_id=network_id)
  44. d = CoinDaemon(proto=proto,test_suite=True)
  45. if not opt.no_daemon_stop:
  46. d.stop()
  47. if not opt.no_daemon_autostart:
  48. d.remove_datadir()
  49. d.start()
  50. for backend in g.autoset_opts['rpc_backend'].choices:
  51. run_session(getattr(init_test,proto.coin.lower())(proto,backend),backend=backend)
  52. if not opt.no_daemon_stop:
  53. d.stop()
  54. if test_cf_auth and g.platform != 'win':
  55. cfg_file_auth_test(proto,d)
  56. qmsg('')
  57. return True
  58. class unit_tests:
  59. altcoin_deps = ('ltc','bch','eth','etc','xmrwallet')
  60. def btc(self,name,ut):
  61. return run_test(['btc','btc_tn'],test_cf_auth=True)
  62. def ltc(self,name,ut):
  63. return run_test(['ltc','ltc_tn'],test_cf_auth=True)
  64. def bch(self,name,ut):
  65. return run_test(['bch','bch_tn'],test_cf_auth=True)
  66. def eth(self,name,ut):
  67. return run_test(['eth'],test_cf_auth=False)
  68. def etc(self,name,ut):
  69. return run_test(['etc'],test_cf_auth=False)
  70. def xmrwallet(self,name,ut):
  71. async def run():
  72. networks = init_proto('xmr').networks
  73. daemons = [(
  74. CoinDaemon(proto=proto,test_suite=True),
  75. MoneroWalletDaemon(
  76. proto = proto,
  77. test_suite = True,
  78. wallet_dir = 'test/trash',
  79. passwd = 'ut_rpc_passw0rd' )
  80. ) for proto in (init_proto('xmr',network=network) for network in networks) ]
  81. for md,wd in daemons:
  82. if not opt.no_daemon_autostart:
  83. md.start()
  84. wd.start()
  85. c = MoneroWalletRPCClient(
  86. host = wd.host,
  87. port = wd.rpc_port,
  88. user = wd.user,
  89. passwd = wd.passwd )
  90. await c.call('get_version')
  91. for md,wd in daemons:
  92. wd.wait = False
  93. wd.stop()
  94. if not opt.no_daemon_stop:
  95. md.wait = False
  96. md.stop()
  97. gmsg('OK')
  98. run_session(run())
  99. return True