ut_dep.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. 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')
  14. win_skip = ('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 keccak(self,name,ut): # used by ETH, XMR
  25. from mmgen.util2 import get_keccak
  26. try:
  27. get_keccak()
  28. except Exception as e:
  29. rmsg(str(e))
  30. return False
  31. else:
  32. return True
  33. def py_ecc(self,name,ut): # ETH
  34. from py_ecc.secp256k1 import privtopub
  35. return True
  36. def pysocks(self,name,ut):
  37. import requests,urllib3
  38. urllib3.disable_warnings()
  39. session = requests.Session()
  40. session.trust_env = False
  41. session.proxies.update({'https':'socks5h://127.243.172.8:20677'})
  42. try:
  43. session.get('https://127.188.29.17')
  44. except Exception as e:
  45. if type(e).__name__ == 'ConnectionError':
  46. return True
  47. else:
  48. print(e)
  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