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. 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('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. d.stop()
  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. d.stop()
  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