ut_dep.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env python3
  2. """
  3. test.modtest_d.ut_dep: dependency unit tests for the MMGen suite
  4. Test whether dependencies are installed and functional.
  5. No data verification is performed.
  6. """
  7. from subprocess import run, PIPE
  8. from mmgen.util import msg, rmsg, ymsg, gmsg
  9. from mmgen.exception import NoLEDSupport
  10. from ..include.common import cfg, vmsg, check_solc_ver
  11. class unit_tests:
  12. altcoin_deps = ('py_ecc', 'solc', 'keccak', 'pysocks')
  13. win_skip = ('led',)
  14. def led(self, name, ut):
  15. from mmgen.led import LEDControl
  16. try:
  17. LEDControl(enabled=True)
  18. except NoLEDSupport:
  19. ymsg('Warning: no LED support on this platform')
  20. else:
  21. gmsg('LED support found!')
  22. return True
  23. def keccak(self, name, ut): # used by ETH, XMR
  24. from mmgen.util2 import get_keccak
  25. try:
  26. get_keccak()
  27. except Exception as e:
  28. rmsg(str(e))
  29. return False
  30. else:
  31. return True
  32. def py_ecc(self, name, ut): # ETH
  33. from py_ecc.secp256k1 import privtopub
  34. return True
  35. def pysocks(self, name, ut):
  36. import requests, urllib3
  37. urllib3.disable_warnings()
  38. session = requests.Session()
  39. session.trust_env = False
  40. session.proxies.update({'https':'socks5h://127.243.172.8:20677'})
  41. try:
  42. session.get('https://127.188.29.17', timeout=1)
  43. except Exception as e:
  44. if type(e).__name__ in ('ConnectionError', 'ConnectTimeout'):
  45. return True
  46. else:
  47. ymsg('{}: {}'.format(type(e).__name__, e))
  48. msg('Is the ‘pysocks’ package installed?')
  49. return False
  50. def secp256k1(self, name, ut):
  51. from mmgen.proto.secp256k1.secp256k1 import pubkey_gen
  52. pubkey_gen(bytes.fromhex('deadbeef'*8), 1)
  53. return True
  54. def cryptography(self, name, ut):
  55. from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
  56. from cryptography.hazmat.backends import default_backend
  57. c = Cipher(algorithms.AES(b'deadbeef'*4), modes.CTR(b'deadbeef'*2), backend=default_backend())
  58. encryptor = c.encryptor()
  59. encryptor.update(b'foo') + encryptor.finalize()
  60. return True
  61. def ecdsa(self, name, ut):
  62. import ecdsa
  63. pko = ecdsa.SigningKey.from_secret_exponent(12345678901234, curve=ecdsa.SECP256k1)
  64. pko.get_verifying_key().to_string().hex()
  65. return True
  66. def ripemd160(self, name, ut):
  67. import hashlib
  68. if hashlib.new.__name__ == 'hashlib_new_wrapper':
  69. ymsg('Warning: RIPEMD160 missing in hashlib, falling back on pure-Python implementation')
  70. hashlib.new('ripemd160')
  71. return True
  72. def gmpy(self, name, ut):
  73. from gmpy2 import context, set_context, sqrt, cbrt
  74. # context() parameters are platform-dependent!
  75. set_context(context(precision=75, round=1)) # OK for gmp 6.1.2 / gmpy 2.1.0
  76. return True
  77. def aiohttp(self, name, ut):
  78. import asyncio, aiohttp
  79. async def do():
  80. async with aiohttp.ClientSession(
  81. headers = {'Content-Type': 'application/json'},
  82. connector = aiohttp.TCPConnector(),
  83. ):
  84. pass
  85. asyncio.run(do())
  86. return True
  87. def pexpect(self, name, ut):
  88. import pexpect
  89. from pexpect.popen_spawn import PopenSpawn
  90. return True
  91. def scrypt(self, name, ut):
  92. passwd, salt = b'foo', b'bar'
  93. N, r, p = 4, 8, 16
  94. buflen = 64
  95. vmsg('Testing builtin scrypt module (hashlib)')
  96. from hashlib import scrypt # max N == 14!!
  97. scrypt(password=passwd, salt=salt, n=2**N, r=r, p=p, maxmem=0, dklen=buflen)
  98. vmsg('Testing standalone scrypt module')
  99. import scrypt
  100. scrypt.hash(passwd, salt, N=2**N, r=r, p=p, buflen=buflen)
  101. return True
  102. def solc(self, name, ut):
  103. from mmgen.protocol import init_proto
  104. solc_ok = check_solc_ver()
  105. if solc_ok:
  106. cmd = [
  107. 'python3',
  108. 'scripts/create-token.py',
  109. '--coin=ETH',
  110. '--name=My Fake Token',
  111. '--symbol=FAKE',
  112. '--supply=100000000000000000000000000',
  113. '--decimals=18',
  114. '--stdout',
  115. init_proto(cfg, 'eth').checksummed_addr('deadbeef'*5),
  116. ]
  117. cp = run(cmd, stdout=PIPE, stderr=PIPE)
  118. vmsg(cp.stderr.decode())
  119. if cp.returncode:
  120. msg(cp.stderr.decode())
  121. return False
  122. return True