ut_rpc.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 {d.cfg_file}:')
  14. d.remove_datadir()
  15. os.makedirs(d.datadir)
  16. cf = os.path.join(d.datadir,d.cfg_file)
  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', f'{rpc.auth.user}: 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. ltc = bch
  45. async def eth(proto,backend):
  46. rpc = await rpc_init(proto,backend)
  47. do_msg(rpc)
  48. await rpc.call('parity_versionInfo',timeout=300)
  49. def run_test(coin,auth):
  50. proto = init_proto(coin,network=('mainnet','regtest')[coin=='eth']) # FIXME CoinDaemon's network handling broken
  51. d = CoinDaemon(network_id=coin,test_suite=True)
  52. if auth:
  53. d.remove_datadir()
  54. d.start()
  55. for backend in g.autoset_opts['rpc_backend'].choices:
  56. run_session(getattr(init_test,coin)(proto,backend),backend=backend)
  57. if auth:
  58. auth_test(proto,d)
  59. qmsg(' OK')
  60. return True
  61. class unit_tests:
  62. altcoin_deps = ('ltc','bch','eth','xmr_wallet')
  63. def btc(self,name,ut):
  64. return run_test('btc',auth=True)
  65. def ltc(self,name,ut):
  66. return run_test('ltc',auth=True)
  67. def bch(self,name,ut):
  68. return run_test('bch',auth=True)
  69. def eth(self,name,ut):
  70. return run_test('eth',auth=False)
  71. def xmr_wallet(self,name,ut):
  72. async def run():
  73. md = CoinDaemon('xmr',test_suite=True)
  74. if not opt.no_daemon_autostart:
  75. md.start()
  76. wd = MoneroWalletDaemon(
  77. wallet_dir = 'test/trash',
  78. passwd = 'ut_rpc_passw0rd',
  79. test_suite = True )
  80. wd.start()
  81. c = MoneroWalletRPCClient(
  82. host = wd.host,
  83. port = wd.rpc_port,
  84. user = wd.user,
  85. passwd = wd.passwd )
  86. await c.call('get_version')
  87. gmsg('OK')
  88. wd.wait = False
  89. wd.stop()
  90. if not opt.no_daemon_stop:
  91. md.wait = False
  92. md.stop()
  93. run_session(run())
  94. return True