ut_dep.py 4.4 KB

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