ut_dep.py 4.2 KB

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