ut_rpc.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.flag.keep_cfg_file = True
  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,daemon):
  27. rpc = await rpc_init(proto,backend,daemon)
  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,daemon):
  33. rpc = await rpc_init(proto,backend,daemon)
  34. do_msg(rpc)
  35. ltc = bch
  36. async def eth(proto,backend,daemon):
  37. rpc = await rpc_init(proto,backend,daemon)
  38. do_msg(rpc)
  39. await rpc.call('eth_blockNumber',timeout=300)
  40. etc = eth
  41. def run_test(network_ids,test_cf_auth=False,daemon_ids=None):
  42. def do(d):
  43. if not opt.no_daemon_stop:
  44. d.stop()
  45. if not opt.no_daemon_autostart:
  46. d.remove_datadir()
  47. d.start()
  48. for backend in g.autoset_opts['rpc_backend'].choices:
  49. test = getattr(init_test,d.proto.coin.lower())
  50. run_session(test(d.proto,backend,d),backend=backend)
  51. if not opt.no_daemon_stop:
  52. d.stop()
  53. if test_cf_auth and g.platform != 'win':
  54. cfg_file_auth_test(d.proto,d)
  55. qmsg('')
  56. for network_id in network_ids:
  57. proto = init_proto(network_id=network_id)
  58. daemon_ids = (lambda x:
  59. set(daemon_ids) & set(x) if daemon_ids else x
  60. )(CoinDaemon.coins[proto.coin].daemon_ids)
  61. for daemon_id in daemon_ids:
  62. do( CoinDaemon(proto=proto,test_suite=True,daemon_id=daemon_id) )
  63. return True
  64. class unit_tests:
  65. altcoin_deps = ('ltc','bch','eth','etc','xmrwallet')
  66. def btc(self,name,ut):
  67. return run_test(['btc','btc_tn'],test_cf_auth=True)
  68. def ltc(self,name,ut):
  69. return run_test(['ltc','ltc_tn'],test_cf_auth=True)
  70. def bch(self,name,ut):
  71. return run_test(['bch','bch_tn'],test_cf_auth=True)
  72. def eth(self,name,ut):
  73. run_test(['eth','eth_tn','eth_rt'],daemon_ids=['openethereum','erigon'])
  74. return run_test(['eth_tn','eth_rt'],daemon_ids=['geth']) # mainnet returns EIP-155 error on empty blockchain
  75. def etc(self,name,ut):
  76. return run_test(['etc'])
  77. def xmrwallet(self,name,ut):
  78. async def run():
  79. networks = init_proto('xmr').networks
  80. daemons = [(
  81. CoinDaemon(proto=proto,test_suite=True),
  82. MoneroWalletDaemon(
  83. proto = proto,
  84. test_suite = True,
  85. wallet_dir = 'test/trash2',
  86. passwd = 'ut_rpc_passw0rd' )
  87. ) for proto in (init_proto('xmr',network=network) for network in networks) ]
  88. for md,wd in daemons:
  89. if not opt.no_daemon_autostart:
  90. md.start()
  91. wd.start()
  92. c = MoneroWalletRPCClient(daemon=wd)
  93. await c.call('get_version')
  94. for md,wd in daemons:
  95. wd.wait = False
  96. wd.stop()
  97. if not opt.no_daemon_stop:
  98. md.wait = False
  99. md.stop()
  100. gmsg('OK')
  101. import shutil
  102. shutil.rmtree('test/trash2',ignore_errors=True)
  103. os.makedirs('test/trash2')
  104. run_session(run())
  105. return True