def __init__(self, args, no_output=False): if opt.direct_exec: msg('') from subprocess import call, check_output f = (call, check_output)[bool(no_output)] ret = f([args[0]] + args[1:]) if f == call and ret != 0: die( 1, red('ERROR: process returned a non-zero exit status ({})'. format(ret))) else: if opt.pexpect_spawn: self.p = pexpect.spawn(args[0], args[1:], encoding='utf8') self.p.delaybeforesend = 0 else: self.p = PopenSpawn(args, encoding='utf8') # self.p.delaybeforesend = 0 # TODO: try this here too if opt.exact_output: self.p.logfile = sys.stdout self.req_exit_val = 0 self.skip_ok = False self.timeout = int(opt.pexpect_timeout or 0) or (60, 5)[bool( opt.debug_pexpect)] self.sent_value = None
def expect(self,s,t='',delay=None,regex=False,nonl=False,silent=False): delay = delay or (0,0.3)[bool(opt.buf_keypress)] if not silent: if opt.verbose: msg_r('EXPECT ' + yellow(str(s))) elif not opt.exact_output: msg_r('+') try: if s == '': ret = 0 else: f = (self.p.expect_exact,self.p.expect)[bool(regex)] ret = f(s,self.timeout) except pexpect.TIMEOUT: if opt.debug_pexpect: raise m1 = red('\nERROR. Expect {!r} timed out. Exiting\n'.format(s)) m2 = 'before: [{}]\n'.format(self.p.before) m3 = 'sent value: [{}]'.format(self.sent_value) if self.sent_value != None else '' rdie(1,m1+m2+m3) debug_pexpect_msg(self.p) if opt.verbose and type(s) != str: msg_r(' ==> {} '.format(ret)) if ret == -1: rdie(1,'Error. Expect returned {}'.format(ret)) else: if t == '': if not nonl and not silent: vmsg('') else: self.send(t,delay,s) return ret
def __call__(self, *args): self.__idcnt += 1 postdata = json.dumps({ 'version': '1.1', 'method': self.__serviceName, 'params': args, 'id': self.__idcnt}) try: self.__conn.request('POST', self.__url.path, postdata, { 'Host' : self.__url.hostname, 'User-Agent' : USER_AGENT, 'Authorization' : self.__authhdr, 'Content-type' : 'application/json' }) except: from mmgen.util import die,red die(1,red("Unable to connect to bitcoind")) httpresp = self.__conn.getresponse() if httpresp is None: raise JSONRPCException({ 'code' : -342, 'message' : 'missing HTTP response from server'}) resp = httpresp.read() resp = resp.decode('utf8') resp = json.loads(resp, parse_float=decimal.Decimal) if 'error' in resp and resp['error'] != None: raise JSONRPCException(resp['error']) elif 'result' not in resp: raise JSONRPCException({ 'code' : -343, 'message' : 'missing JSON-RPC result'}) else: return resp['result']
def cmp_or_die(s,t,skip_ok=False): if s == t: if not skip_ok: ok() else: sys.stderr.write(red( "ERROR: recoded data:\n%s\ndiffers from original data:\n%s\n" % (repr(t),repr(s)))) sys.exit(3)
def ok_or_die(val,chk_func,s,skip_ok=False): try: ret = chk_func(val) except: ret = False if ret: if not skip_ok: ok() else: msg(red("Returned value '%s' is not a %s" % (val,s))) sys.exit(3)
def ok(self): ret = self.p.wait() if ret != self.req_exit_val and not opt.coverage: die(1,red('test.py: spawned program exited with value {}'.format(ret))) if opt.profile: return if not self.skip_ok: sys.stderr.write(green('OK\n') if opt.exact_output or opt.verbose else (' OK\n')) return self
def ok(self): ret = self.p.wait() if ret != self.req_exit_val and not opt.coverage: die( 1, red('test.py: spawned program exited with value {}'.format( ret))) if opt.profile: return if not self.skip_ok: sys.stderr.write( green('OK\n') if opt.exact_output or opt.verbose else ( ' OK\n')) return self
def expect(self, s, t='', delay=None, regex=False, nonl=False, silent=False): delay = delay or (0, 0.3)[bool(opt.buf_keypress)] if not silent: if opt.verbose: msg_r('EXPECT ' + yellow(str(s))) elif not opt.exact_output: msg_r('+') try: if s == '': ret = 0 else: f = (self.p.expect_exact, self.p.expect)[bool(regex)] ret = f(s, self.timeout) except pexpect.TIMEOUT: if opt.debug_pexpect: raise m1 = red('\nERROR. Expect {!r} timed out. Exiting\n'.format(s)) m2 = 'before: [{}]\n'.format(self.p.before) m3 = 'sent value: [{}]'.format( self.sent_value) if self.sent_value != None else '' rdie(1, m1 + m2 + m3) debug_pexpect_msg(self.p) if opt.verbose and type(s) != str: msg_r(' ==> {} '.format(ret)) if ret == -1: rdie(1, 'Error. Expect returned {}'.format(ret)) else: if t == '': if not nonl and not silent: vmsg('') else: self.send(t, delay, s) return ret
def __init__(self,args,no_output=False): if opt.direct_exec: msg('') from subprocess import call,check_output f = (call,check_output)[bool(no_output)] ret = f([args[0]] + args[1:]) if f == call and ret != 0: die(1,red('ERROR: process returned a non-zero exit status ({})'.format(ret))) else: if opt.pexpect_spawn: self.p = pexpect.spawn(args[0],args[1:],encoding='utf8') self.p.delaybeforesend = 0 else: self.p = PopenSpawn(args,encoding='utf8') # self.p.delaybeforesend = 0 # TODO: try this here too if opt.exact_output: self.p.logfile = sys.stdout self.req_exit_val = 0 self.skip_ok = False self.timeout = int(opt.pexpect_timeout or 0) or (60,5)[bool(opt.debug_pexpect)] self.sent_value = None
if opt.system: sys.path.pop(0) from mmgen.addr import test_for_keyconv if not test_for_keyconv(silent=True): msg( "To run this test, you must install 'keyconv' from the vanitygen package.") sys.exit(1) msg(green("Comparing {}'s internally generated addresses against output of 'keyconv'").format(g.proj_name)) from subprocess import check_output for i in range(1,rounds+1): msg_r("\rRound %s/%s " % (i,rounds)) sec = hexlify(os.urandom(32)) wif = hextowif(sec) a = privnum2addr(int(sec,16)) vmsg("\nkey: %s\naddr: %s\n" % (wif,a)) b = check_output(["keyconv", wif]).split()[1] if a != b: msg_r(red("\nERROR: Addresses do not match!")) msg(""" sec key: {} WIF key: {} {pnm}: {} keyconv: {} """.format(sec,wif,a,b,pnm=g.proj_name).rstrip()) sys.exit(3) msg(green("%sOK" % ("" if opt.verbose else "\n")))
def debug_pexpect_msg(p): if opt.debug_pexpect: msg('\n{}{}{}'.format(red('BEFORE ['),p.before,red(']'))) msg('{}{}{}'.format(red('MATCH ['),p.after,red(']')))
""" test/pexpect.py: pexpect implementation for MMGen test suites """ import sys,os,time from mmgen.globalvars import g from mmgen.opts import opt from mmgen.util import msg,msg_r,vmsg,vmsg_r,rmsg,red,yellow,green,cyan,die,rdie from test.common import getrandstr try: import pexpect from pexpect.popen_spawn import PopenSpawn except: die(2,red('Pexpect module is missing. Cannnot run test suite')) def debug_pexpect_msg(p): if opt.debug_pexpect: msg('\n{}{}{}'.format(red('BEFORE ['),p.before,red(']'))) msg('{}{}{}'.format(red('MATCH ['),p.after,red(']'))) NL = '\n' class MMGenPexpect(object): def __init__(self,args,no_output=False): if opt.direct_exec: msg('') from subprocess import call,check_output
def debug_pexpect_msg(p): if opt.debug_pexpect: msg('\n{}{}{}'.format(red('BEFORE ['), p.before, red(']'))) msg('{}{}{}'.format(red('MATCH ['), p.after, red(']')))
# along with this program. If not, see <http://www.gnu.org/licenses/>. """ test/pexpect.py: pexpect implementation for MMGen test suites """ import sys, os, time from mmgen.globalvars import g from mmgen.opts import opt from mmgen.util import msg, msg_r, vmsg, vmsg_r, rmsg, red, yellow, green, cyan, die, rdie from test.common import getrandstr try: import pexpect from pexpect.popen_spawn import PopenSpawn except: die(2, red('Pexpect module is missing. Cannnot run test suite')) def debug_pexpect_msg(p): if opt.debug_pexpect: msg('\n{}{}{}'.format(red('BEFORE ['), p.before, red(']'))) msg('{}{}{}'.format(red('MATCH ['), p.after, red(']'))) NL = '\n' class MMGenPexpect(object): def __init__(self, args, no_output=False): if opt.direct_exec:
test/pexpect.py: pexpect implementation for MMGen test suites """ import sys, os, time from mmgen.globalvars import g from mmgen.opts import opt from mmgen.util import msg, msg_r, vmsg, vmsg_r, rmsg, red, yellow, green, cyan, die, rdie from .common import * try: import pexpect from pexpect.popen_spawn import PopenSpawn except ImportError as e: die( 2, red('Pexpect module is missing. Cannnot run test suite ({!r})'.format( e))) def debug_pexpect_msg(p): if opt.debug_pexpect: msg('\n{}{}{}'.format(red('BEFORE ['), p.before, red(']'))) msg('{}{}{}'.format(red('MATCH ['), p.after, red(']'))) NL = '\n' class MMGenPexpect(object): def __init__(self, args, no_output=False): if opt.direct_exec: