ut_rpc.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. def run_test(coin,auth):
  49. proto = init_proto(coin)
  50. d = CoinDaemon(proto=proto,test_suite=True)
  51. if not opt.no_daemon_stop:
  52. d.stop()
  53. if not opt.no_daemon_autostart:
  54. d.remove_datadir()
  55. d.start()
  56. for backend in g.autoset_opts['rpc_backend'].choices:
  57. run_session(getattr(init_test,coin)(proto,backend),backend=backend)
  58. if not opt.no_daemon_stop:
  59. d.stop()
  60. if auth:
  61. auth_test(proto,d)
  62. return True
  63. class unit_tests:
  64. altcoin_deps = ('ltc','bch','eth','xmr_wallet')
  65. def btc(self,name,ut):
  66. return run_test('btc',auth=True)
  67. def ltc(self,name,ut):
  68. return run_test('ltc',auth=True)
  69. def bch(self,name,ut):
  70. return run_test('bch',auth=True)
  71. def eth(self,name,ut):
  72. return run_test('eth',auth=False)
  73. def xmr_wallet(self,name,ut):
  74. async def run():
  75. md = CoinDaemon('xmr',test_suite=True)
  76. if not opt.no_daemon_autostart:
  77. md.start()
  78. wd = MoneroWalletDaemon(
  79. wallet_dir = 'test/trash',
  80. passwd = 'ut_rpc_passw0rd',
  81. test_suite = True )
  82. wd.start()
  83. c = MoneroWalletRPCClient(
  84. host = wd.host,
  85. port = wd.rpc_port,
  86. user = wd.user,
  87. passwd = wd.passwd )
  88. await c.call('get_version')
  89. gmsg('OK')
  90. wd.wait = False
  91. wd.stop()
  92. if not opt.no_daemon_stop:
  93. md.wait = False
  94. md.stop()
  95. run_session(run())
  96. return True