common.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2022 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. common.py: Shared routines and data for the MMGen test suites
  20. """
  21. class TestSuiteException(Exception): pass
  22. class TestSuiteFatalException(Exception): pass
  23. import os
  24. from mmgen.common import *
  25. from mmgen.devtools import *
  26. from mmgen.fileutil import write_data_to_file,get_data_from_file
  27. def strip_ansi_escapes(s):
  28. import re
  29. return re.sub('\x1b' + r'\[[;0-9]+?m','',s)
  30. ascii_uc = ''.join(map(chr,list(range(65,91)))) # 26 chars
  31. ascii_lc = ''.join(map(chr,list(range(97,123)))) # 26 chars
  32. lat_accent = ''.join(map(chr,list(range(192,383)))) # 191 chars, L,S
  33. ru_uc = ''.join(map(chr,list(range(1040,1072)))) # 32 chars
  34. gr_uc = ''.join(map(chr,list(range(913,930)) + list(range(931,940)))) # 26 chars (930 is ctrl char)
  35. gr_uc_w_ctrl = ''.join(map(chr,list(range(913,940)))) # 27 chars, L,C
  36. lat_cyr_gr = lat_accent[:130:5] + ru_uc + gr_uc # 84 chars
  37. ascii_cyr_gr = ascii_uc + ru_uc + gr_uc # 84 chars
  38. utf8_text = '[α-$ample UTF-8 text-ω]' * 10 # 230 chars, L,N,P,S,Z
  39. utf8_combining = '[α-$ámple UTF-8 téxt-ω]' * 10 # L,N,P,S,Z,M
  40. utf8_ctrl = '[α-$ample\nUTF-8\ntext-ω]' * 10 # L,N,P,S,Z,C
  41. text_jp = '必要なのは、信用ではなく暗号化された証明に基づく電子取引システムであり、これにより希望する二者が信用できる第三者機関を介さずに直接取引できるよう' # 72 chars ('W'ide)
  42. text_zh = '所以,我們非常需要這樣一種電子支付系統,它基於密碼學原理而不基於信用,使得任何達成一致的雙方,能夠直接進行支付,從而不需要協力廠商仲介的參與。。' # 72 chars ('F'ull + 'W'ide)
  43. sample_text = 'The Times 03/Jan/2009 Chancellor on brink of second bailout for banks'
  44. sample_mn = {
  45. 'mmgen': { # 'able': 0, 'youth': 1625, 'after' == 'afternoon'[:5]
  46. 'mn': 'able cast forgive master funny gaze after afternoon million paint moral youth',
  47. 'hex': '0005685ab4e94cbe3b228cf92112bc5f',
  48. },
  49. 'bip39': { # len('sun') < uniq_ss_len
  50. 'mn': 'vessel ladder alter error federal sibling chat ability sun glass valve picture',
  51. 'hex': 'f30f8c1da665478f49b001d94c5fc452',
  52. },
  53. 'xmrseed': {
  54. 'mn': 'viewpoint donuts ardent template unveil agile meant unafraid urgent athlete rustled mime azure jaded hawk baby jagged haystack baby jagged haystack ramped oncoming point template',
  55. 'hex': 'e8164dda6d42bd1e261a3406b2038dcbddadbeefdeadbeefdeadbeefdeadbe0f',
  56. },
  57. }
  58. ref_kafile_pass = 'kafile password'
  59. ref_kafile_hash_preset = '1'
  60. def getrand(n):
  61. if g.test_suite_deterministic:
  62. from mmgen.crypto import fake_urandom
  63. return fake_urandom(n)
  64. else:
  65. return os.urandom(n)
  66. def getrandnum(n):
  67. return int(getrand(n).hex(),16)
  68. def getrandhex(n):
  69. return getrand(n).hex()
  70. def getrandnum_range(nbytes,rn_max):
  71. while True:
  72. rn = int(getrand(nbytes).hex(),16)
  73. if rn < rn_max:
  74. return rn
  75. def getrandstr(num_chars,no_space=False):
  76. n,m = (94,33) if no_space else (95,32)
  77. return ''.join( chr(i % n + m) for i in list(getrand(num_chars)) )
  78. def get_data_dir():
  79. return os.path.join('test','data_dir' + ('','-α')[bool(os.getenv('MMGEN_DEBUG_UTF8'))])
  80. # Windows uses non-UTF8 encodings in filesystem, so use raw bytes here
  81. def cleandir(d,do_msg=False):
  82. d_enc = d.encode()
  83. try: files = os.listdir(d_enc)
  84. except: return
  85. from shutil import rmtree
  86. if do_msg:
  87. gmsg(f'Cleaning directory {d!r}')
  88. for f in files:
  89. try:
  90. os.unlink(os.path.join(d_enc,f))
  91. except:
  92. rmtree(os.path.join(d_enc,f),ignore_errors=True)
  93. def mk_tmpdir(d):
  94. try: os.mkdir(d,0o755)
  95. except OSError as e:
  96. if e.errno != 17:
  97. raise
  98. else:
  99. vmsg(f'Created directory {d!r}')
  100. def get_tmpfile(cfg,fn):
  101. return os.path.join(cfg['tmpdir'],fn)
  102. def write_to_file(fn,data,binary=False):
  103. write_data_to_file(
  104. fn,
  105. data,
  106. quiet = True,
  107. binary = binary,
  108. ignore_opt_outdir = True )
  109. def write_to_tmpfile(cfg,fn,data,binary=False):
  110. write_to_file( os.path.join(cfg['tmpdir'],fn), data=data, binary=binary )
  111. def read_from_file(fn,binary=False):
  112. return get_data_from_file(fn,quiet=True,binary=binary)
  113. def read_from_tmpfile(cfg,fn,binary=False):
  114. return read_from_file(os.path.join(cfg['tmpdir'],fn),binary=binary)
  115. def joinpath(*args,**kwargs):
  116. return os.path.join(*args,**kwargs)
  117. def ok():
  118. if opt.profile:
  119. return
  120. if opt.verbose or opt.exact_output:
  121. gmsg('OK')
  122. else:
  123. msg(' OK')
  124. def cmp_or_die(s,t,desc=None):
  125. if s != t:
  126. die( 'TestSuiteFatalException',
  127. (f'For {desc}:\n' if desc else '') +
  128. f'ERROR: recoded data:\n{t!r}\ndiffers from original data:\n{s!r}'
  129. )
  130. def init_coverage():
  131. coverdir = os.path.join('test','trace')
  132. acc_file = os.path.join('test','trace.acc')
  133. try: os.mkdir(coverdir,0o755)
  134. except: pass
  135. return coverdir,acc_file
  136. def silence():
  137. if not (opt.verbose or opt.exact_output):
  138. devnull_fn = ('/dev/null','null.out')[g.platform == 'win']
  139. g.stdout = g.stderr = open(devnull_fn,'w')
  140. def end_silence():
  141. if not (opt.verbose or opt.exact_output):
  142. g.stdout.close()
  143. g.stdout = sys.stdout
  144. g.stderr = sys.stderr
  145. def omsg(s):
  146. sys.stderr.write(s + '\n')
  147. def omsg_r(s):
  148. sys.stderr.write(s)
  149. sys.stderr.flush()
  150. def imsg(s):
  151. if opt.verbose or opt.exact_output:
  152. omsg(s)
  153. def imsg_r(s):
  154. if opt.verbose or opt.exact_output:
  155. omsg_r(s)
  156. def iqmsg(s):
  157. if not opt.quiet:
  158. omsg(s)
  159. def iqmsg_r(s):
  160. if not opt.quiet:
  161. omsg_r(s)
  162. def oqmsg(s):
  163. if not (opt.verbose or opt.exact_output):
  164. omsg(s)
  165. def oqmsg_r(s):
  166. if not (opt.verbose or opt.exact_output):
  167. omsg_r(s)
  168. def end_msg(t):
  169. omsg(green(
  170. 'All requested tests finished OK' +
  171. ('' if g.test_suite_deterministic else f', elapsed time: {t//60:02d}:{t%60:02d}')
  172. ))
  173. def start_test_daemons(*network_ids,remove_datadir=False):
  174. if not opt.no_daemon_autostart:
  175. return test_daemons_ops(*network_ids,op='start',remove_datadir=remove_datadir)
  176. def stop_test_daemons(*network_ids,force=False,remove_datadir=False):
  177. if force or not opt.no_daemon_stop:
  178. return test_daemons_ops(*network_ids,op='stop',remove_datadir=remove_datadir)
  179. def restart_test_daemons(*network_ids,remove_datadir=False):
  180. if not stop_test_daemons(*network_ids):
  181. return False
  182. return start_test_daemons(*network_ids,remove_datadir=remove_datadir)
  183. def test_daemons_ops(*network_ids,op,remove_datadir=False):
  184. if not opt.no_daemon_autostart:
  185. from mmgen.daemon import CoinDaemon
  186. silent = not (opt.verbose or opt.exact_output)
  187. ret = False
  188. for network_id in network_ids:
  189. d = CoinDaemon(network_id,test_suite=True,daemon_id=g.daemon_id)
  190. if remove_datadir:
  191. d.stop(silent=True)
  192. d.remove_datadir()
  193. ret = d.cmd(op,silent=silent)
  194. return ret