ut_rpc.py 3.4 KB

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