ut_rpc.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. 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 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','geth','erigon','openethereum','parity','xmrwallet')
  66. win_skip = ('xmrwallet',) # FIXME - wallet doesn't open
  67. arm_skip = ('openethereum','parity') # no prebuilt binaries for ARM
  68. def btc(self,name,ut):
  69. return run_test(['btc','btc_tn'],test_cf_auth=True)
  70. def ltc(self,name,ut):
  71. return run_test(['ltc','ltc_tn'],test_cf_auth=True)
  72. def bch(self,name,ut):
  73. return run_test(['bch','bch_tn'],test_cf_auth=True)
  74. def geth(self,name,ut):
  75. return run_test(['eth_tn','eth_rt'],daemon_ids=['geth']) # mainnet returns EIP-155 error on empty blockchain
  76. def erigon(self,name,ut):
  77. return run_test(['eth','eth_tn','eth_rt'],daemon_ids=['erigon'])
  78. def openethereum(self,name,ut):
  79. return run_test(['eth','eth_tn','eth_rt'],daemon_ids=['openethereum'])
  80. def parity(self,name,ut):
  81. return run_test(['etc'])
  82. def xmrwallet(self,name,ut):
  83. async def run():
  84. networks = init_proto('xmr').networks
  85. daemons = [(
  86. CoinDaemon(proto=proto,test_suite=True),
  87. MoneroWalletDaemon(
  88. proto = proto,
  89. test_suite = True,
  90. wallet_dir = 'test/trash2',
  91. passwd = 'ut_rpc_passw0rd' )
  92. ) for proto in (init_proto('xmr',network=network) for network in networks) ]
  93. for md,wd in daemons:
  94. if not opt.no_daemon_autostart:
  95. md.start()
  96. wd.start()
  97. c = MoneroWalletRPCClient(daemon=wd)
  98. fn = f'monero-{wd.network}-junk-wallet'
  99. qmsg(f'Creating {wd.network} wallet')
  100. await c.call(
  101. 'restore_deterministic_wallet',
  102. filename = fn,
  103. password = 'foo',
  104. seed = baseconv.fromhex('beadface'*8,'xmrseed',tostr=True) )
  105. qmsg(f'Opening {wd.network} wallet')
  106. await c.call( 'open_wallet', filename=fn, password='foo' )
  107. for md,wd in daemons:
  108. wd.wait = False
  109. await wd.rpc.stop_daemon()
  110. if not opt.no_daemon_stop:
  111. md.wait = False
  112. await md.rpc.stop_daemon()
  113. gmsg('OK')
  114. from mmgen.baseconv import baseconv
  115. import shutil
  116. shutil.rmtree('test/trash2',ignore_errors=True)
  117. os.makedirs('test/trash2')
  118. run_session(run())
  119. return True