2018-10-30 16:23:12 +00:00
|
|
|
#!/usr/bin/env python3
|
2015-01-09 21:02:16 +03:00
|
|
|
#
|
|
|
|
|
# mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
|
2019-02-12 18:35:12 +00:00
|
|
|
# Copyright (C)2013-2019 The MMGen Project <mmgen@tuta.io>
|
2015-01-09 21:02:16 +03:00
|
|
|
#
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
"""
|
2019-03-06 20:58:59 +00:00
|
|
|
common.py: Shared routines and data for the MMGen test suites
|
2015-01-09 21:02:16 +03:00
|
|
|
"""
|
|
|
|
|
|
2019-03-06 20:58:59 +00:00
|
|
|
sample_text = 'The Times 03/Jan/2009 Chancellor on brink of second bailout for banks'
|
|
|
|
|
|
|
|
|
|
ref_kafile_pass = 'kafile password'
|
|
|
|
|
ref_kafile_hash_preset = '1'
|
|
|
|
|
|
2019-03-02 18:27:53 +00:00
|
|
|
class TestSuiteException(Exception): pass
|
|
|
|
|
class TestSuiteFatalException(Exception): pass
|
|
|
|
|
|
2016-02-28 16:41:43 +03:00
|
|
|
import os
|
|
|
|
|
from mmgen.common import *
|
2015-01-09 21:02:16 +03:00
|
|
|
|
2019-03-17 10:33:55 +00:00
|
|
|
def getrandnum(n): return int(os.urandom(n).hex(),16)
|
|
|
|
|
def getrandhex(n): return os.urandom(n).hex()
|
2019-03-02 18:27:53 +00:00
|
|
|
def getrandnum_range(nbytes,rn_max):
|
|
|
|
|
while True:
|
2019-03-17 10:33:55 +00:00
|
|
|
rn = int(os.urandom(nbytes).hex(),16)
|
2019-03-02 18:27:53 +00:00
|
|
|
if rn < rn_max: return rn
|
|
|
|
|
|
|
|
|
|
def getrandstr(num_chars,no_space=False):
|
|
|
|
|
n,m = 95,32
|
|
|
|
|
if no_space: n,m = 94,33
|
|
|
|
|
return ''.join([chr(i%n+m) for i in list(os.urandom(num_chars))])
|
|
|
|
|
|
2018-05-12 15:26:54 +00:00
|
|
|
# Windows uses non-UTF8 encodings in filesystem, so use raw bytes here
|
2019-03-02 18:27:53 +00:00
|
|
|
def cleandir(d,do_msg=False):
|
2018-10-31 14:16:00 +00:00
|
|
|
d_enc = d.encode()
|
2018-05-12 15:26:54 +00:00
|
|
|
|
|
|
|
|
try: files = os.listdir(d_enc)
|
2015-01-09 21:02:16 +03:00
|
|
|
except: return
|
|
|
|
|
|
2018-05-12 15:26:54 +00:00
|
|
|
from shutil import rmtree
|
2019-03-02 18:27:53 +00:00
|
|
|
if do_msg: gmsg("Cleaning directory '{}'".format(d))
|
2015-01-09 21:02:16 +03:00
|
|
|
for f in files:
|
2016-12-08 18:02:28 +03:00
|
|
|
try:
|
2018-05-12 15:26:54 +00:00
|
|
|
os.unlink(os.path.join(d_enc,f))
|
2016-12-08 18:02:28 +03:00
|
|
|
except:
|
2019-05-20 15:44:30 +00:00
|
|
|
rmtree(os.path.join(d_enc,f),ignore_errors=True)
|
2015-01-09 21:02:16 +03:00
|
|
|
|
2016-11-21 19:59:03 +03:00
|
|
|
def mk_tmpdir(d):
|
2018-10-30 16:31:14 +00:00
|
|
|
try: os.mkdir(d,0o755)
|
2015-01-09 21:02:16 +03:00
|
|
|
except OSError as e:
|
|
|
|
|
if e.errno != 17: raise
|
2017-07-27 22:55:52 +03:00
|
|
|
else:
|
2018-10-30 16:31:14 +00:00
|
|
|
vmsg("Created directory '{}'".format(d))
|
2016-06-25 18:27:45 +03:00
|
|
|
|
2019-03-02 18:27:53 +00:00
|
|
|
# def mk_tmpdir_path(path,cfg):
|
|
|
|
|
# try:
|
|
|
|
|
# name = os.path.split(cfg['tmpdir'])[-1]
|
|
|
|
|
# src = os.path.join(path,name)
|
|
|
|
|
# try:
|
|
|
|
|
# os.unlink(cfg['tmpdir'])
|
|
|
|
|
# except OSError as e:
|
|
|
|
|
# if e.errno != 2: raise
|
|
|
|
|
# finally:
|
|
|
|
|
# os.mkdir(src)
|
|
|
|
|
# os.symlink(src,cfg['tmpdir'])
|
|
|
|
|
# except OSError as e:
|
|
|
|
|
# if e.errno != 17: raise
|
|
|
|
|
# else: msg("Created directory '{}'".format(cfg['tmpdir']))
|
|
|
|
|
|
|
|
|
|
def get_tmpfile(cfg,fn):
|
2015-01-09 21:02:16 +03:00
|
|
|
return os.path.join(cfg['tmpdir'],fn)
|
|
|
|
|
|
2019-03-02 18:27:53 +00:00
|
|
|
def write_to_file(fn,data,binary=False):
|
|
|
|
|
write_data_to_file( fn,
|
|
|
|
|
data,
|
2019-03-20 17:10:46 +00:00
|
|
|
quiet = True,
|
2019-03-02 18:27:53 +00:00
|
|
|
binary = binary,
|
|
|
|
|
ignore_opt_outdir = True )
|
|
|
|
|
|
2015-10-25 13:04:30 +03:00
|
|
|
def write_to_tmpfile(cfg,fn,data,binary=False):
|
2019-03-02 18:27:53 +00:00
|
|
|
write_to_file( os.path.join(cfg['tmpdir'],fn), data=data, binary=binary )
|
2015-01-09 21:02:16 +03:00
|
|
|
|
2015-10-25 13:04:30 +03:00
|
|
|
def read_from_file(fn,binary=False):
|
2015-01-09 21:02:16 +03:00
|
|
|
from mmgen.util import get_data_from_file
|
2019-03-20 17:10:46 +00:00
|
|
|
return get_data_from_file(fn,quiet=True,binary=binary)
|
2015-01-09 21:02:16 +03:00
|
|
|
|
2015-10-25 13:04:30 +03:00
|
|
|
def read_from_tmpfile(cfg,fn,binary=False):
|
|
|
|
|
return read_from_file(os.path.join(cfg['tmpdir'],fn),binary=binary)
|
2015-01-09 21:02:16 +03:00
|
|
|
|
2019-03-02 18:27:53 +00:00
|
|
|
def joinpath(*args,**kwargs):
|
|
|
|
|
return os.path.join(*args,**kwargs)
|
|
|
|
|
|
2015-01-09 21:02:16 +03:00
|
|
|
def ok():
|
2016-11-11 16:05:27 +03:00
|
|
|
if opt.profile: return
|
2015-01-10 18:52:30 +03:00
|
|
|
if opt.verbose or opt.exact_output:
|
2018-03-07 09:59:35 +00:00
|
|
|
gmsg('OK')
|
2016-02-28 16:41:43 +03:00
|
|
|
else: msg(' OK')
|
2015-01-09 21:02:16 +03:00
|
|
|
|
2019-03-02 18:27:53 +00:00
|
|
|
def cmp_or_die(s,t,desc=None):
|
|
|
|
|
if s != t:
|
|
|
|
|
m = 'ERROR: recoded data:\n{!r}\ndiffers from original data:\n{!r}'
|
|
|
|
|
if desc: m = 'For {}:\n{}'.format(desc,m)
|
|
|
|
|
raise TestSuiteFatalException(m.format(t,s))
|
2018-02-24 14:39:49 +03:00
|
|
|
|
|
|
|
|
def init_coverage():
|
|
|
|
|
coverdir = os.path.join('test','trace')
|
|
|
|
|
acc_file = os.path.join('test','trace.acc')
|
2018-10-30 16:31:14 +00:00
|
|
|
try: os.mkdir(coverdir,0o755)
|
2018-02-24 14:39:49 +03:00
|
|
|
except: pass
|
|
|
|
|
return coverdir,acc_file
|