2019-03-02 18:27:53 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
#
|
|
|
|
|
# mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
|
2024-01-19 11:05:10 +00:00
|
|
|
# Copyright (C)2013-2024 The MMGen Project <mmgen@tuta.io>
|
2019-03-02 18:27:53 +00: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/>.
|
|
|
|
|
|
|
|
|
|
"""
|
2022-11-14 09:54:07 +00:00
|
|
|
test.include.pexpect: pexpect implementation for MMGen test suites
|
2019-03-02 18:27:53 +00:00
|
|
|
"""
|
|
|
|
|
|
2023-10-11 12:58:51 +00:00
|
|
|
import sys,time
|
2023-10-03 14:27:57 +00:00
|
|
|
from mmgen.color import red,yellow,green,cyan
|
|
|
|
|
from mmgen.util import msg,msg_r,rmsg,die
|
2023-03-28 18:14:37 +00:00
|
|
|
from .common import cfg,vmsg,vmsg_r,getrandstr,strip_ansi_escapes
|
2019-03-02 18:27:53 +00:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
import pexpect
|
|
|
|
|
from pexpect.popen_spawn import PopenSpawn
|
2019-10-23 10:01:11 +00:00
|
|
|
except ImportError as e:
|
2021-09-29 21:17:57 +00:00
|
|
|
die(2,red(f'Pexpect module is missing. Cannnot run test suite ({e!r})'))
|
2019-03-02 18:27:53 +00:00
|
|
|
|
|
|
|
|
def debug_pexpect_msg(p):
|
2022-10-29 20:10:23 +00:00
|
|
|
msg('\n{}{}{}'.format( red('BEFORE ['), p.before, red(']') ))
|
|
|
|
|
msg('{}{}{}'.format( red('MATCH ['), p.after, red(']') ))
|
2019-03-02 18:27:53 +00:00
|
|
|
|
2019-03-25 10:07:04 +00:00
|
|
|
NL = '\n'
|
2019-03-02 18:27:53 +00:00
|
|
|
|
2022-10-28 11:35:13 +00:00
|
|
|
class MMGenPexpect:
|
2019-03-02 18:27:53 +00:00
|
|
|
|
2023-05-06 15:14:06 +00:00
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
args,
|
|
|
|
|
no_output = False,
|
2023-05-23 12:12:32 +00:00
|
|
|
spawn_env = None,
|
2023-05-06 15:14:06 +00:00
|
|
|
pexpect_spawn = False,
|
|
|
|
|
send_delay = None,
|
2023-05-06 15:14:06 +00:00
|
|
|
timeout = None,
|
|
|
|
|
direct_exec = False ):
|
2019-03-02 18:27:53 +00:00
|
|
|
|
2022-10-28 11:35:13 +00:00
|
|
|
self.pexpect_spawn = pexpect_spawn
|
2022-10-29 20:10:24 +00:00
|
|
|
self.send_delay = send_delay
|
2022-10-28 11:35:13 +00:00
|
|
|
self.skip_ok = False
|
|
|
|
|
self.sent_value = None
|
2023-05-23 12:12:33 +00:00
|
|
|
self.spawn_env = spawn_env
|
2022-10-28 11:35:13 +00:00
|
|
|
|
2023-05-06 15:14:06 +00:00
|
|
|
if direct_exec or cfg.direct_exec:
|
|
|
|
|
from subprocess import Popen,DEVNULL
|
|
|
|
|
redir = DEVNULL if (no_output or not cfg.exact_output) else None
|
2023-05-23 12:12:33 +00:00
|
|
|
self.ep = Popen([args[0]] + args[1:], stderr=redir, env=spawn_env )
|
2019-03-02 18:27:53 +00:00
|
|
|
else:
|
2023-03-28 18:14:37 +00:00
|
|
|
timeout = int(timeout or cfg.pexpect_timeout or 0) or (60,5)[bool(cfg.debug_pexpect)]
|
2022-10-28 11:35:13 +00:00
|
|
|
if pexpect_spawn:
|
2023-05-23 12:12:32 +00:00
|
|
|
self.p = pexpect.spawn(args[0],args[1:],encoding='utf8',timeout=timeout,env=spawn_env)
|
2019-03-02 18:27:53 +00:00
|
|
|
else:
|
2023-05-23 12:12:32 +00:00
|
|
|
self.p = PopenSpawn(args,encoding='utf8',timeout=timeout,env=spawn_env)
|
2019-03-02 18:27:53 +00:00
|
|
|
|
2023-03-28 18:14:37 +00:00
|
|
|
if cfg.exact_output:
|
2021-10-03 17:40:02 +00:00
|
|
|
self.p.logfile = sys.stdout
|
2019-03-02 18:27:53 +00:00
|
|
|
|
2023-10-11 12:58:51 +00:00
|
|
|
def do_decrypt_ka_data(
|
|
|
|
|
self,
|
|
|
|
|
pw,
|
|
|
|
|
desc = 'key-address data',
|
|
|
|
|
check = True,
|
|
|
|
|
have_yes_opt = False):
|
2019-03-02 18:27:53 +00:00
|
|
|
self.passphrase(desc,pw)
|
2019-03-06 20:58:59 +00:00
|
|
|
if not have_yes_opt:
|
|
|
|
|
self.expect('Check key-to-address validity? (y/N): ',('n','y')[check])
|
2019-03-02 18:27:53 +00:00
|
|
|
|
|
|
|
|
def view_tx(self,view):
|
2022-01-06 20:24:20 +00:00
|
|
|
self.expect(r'View.* transaction.*\? .*: ',view,regex=True)
|
2019-03-02 18:27:53 +00:00
|
|
|
if view not in 'n\n':
|
|
|
|
|
self.expect('to continue: ','\n')
|
|
|
|
|
|
|
|
|
|
def do_comment(self,add_comment,has_label=False):
|
|
|
|
|
p = ('Add a comment to transaction','Edit transaction comment')[has_label]
|
2021-09-29 21:17:57 +00:00
|
|
|
self.expect(f'{p}? (y/N): ',('n','y')[bool(add_comment)])
|
2019-03-02 18:27:53 +00:00
|
|
|
if add_comment:
|
|
|
|
|
self.expect('Comment: ',add_comment+'\n')
|
|
|
|
|
|
2024-03-08 14:13:23 +00:00
|
|
|
def ok(self, exit_val=None):
|
2022-10-28 11:35:13 +00:00
|
|
|
if not self.pexpect_spawn:
|
2022-01-15 14:00:10 +00:00
|
|
|
self.p.sendeof()
|
2022-01-06 20:24:22 +00:00
|
|
|
self.p.read()
|
2019-03-02 18:27:53 +00:00
|
|
|
ret = self.p.wait()
|
2024-03-08 14:13:23 +00:00
|
|
|
if ret != (exit_val or 0) and not cfg.coverage:
|
2023-09-29 12:24:22 +00:00
|
|
|
die( 'TestSuiteSpawnedScriptException', f'Spawned script exited with value {ret}' )
|
2023-03-28 18:14:37 +00:00
|
|
|
if cfg.profile:
|
2020-05-26 14:53:44 +00:00
|
|
|
return
|
2019-03-02 18:27:53 +00:00
|
|
|
if not self.skip_ok:
|
2022-08-04 13:44:31 +00:00
|
|
|
m = 'OK\n' if ret == 0 else f'OK[{ret}]\n'
|
2023-03-28 18:14:37 +00:00
|
|
|
sys.stderr.write( green(m) if cfg.exact_output or cfg.verbose else ' '+m )
|
2019-03-02 18:27:53 +00:00
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
def license(self):
|
2023-05-23 12:12:33 +00:00
|
|
|
if self.spawn_env.get('MMGEN_NO_LICENSE'):
|
|
|
|
|
return
|
2019-03-02 18:27:53 +00:00
|
|
|
self.expect("'w' for conditions and warranty info, or 'c' to continue: ",'c')
|
|
|
|
|
|
|
|
|
|
def label(self,label='Test Label (UTF-8) α'):
|
|
|
|
|
self.expect('Enter a wallet label, or hit ENTER for no label: ',label+'\n')
|
|
|
|
|
|
|
|
|
|
def usr_rand(self,num_chars):
|
2023-03-28 18:14:37 +00:00
|
|
|
if cfg.usr_random:
|
2019-03-02 18:27:53 +00:00
|
|
|
self.interactive()
|
|
|
|
|
self.send('\n')
|
|
|
|
|
else:
|
|
|
|
|
rand_chars = list(getrandstr(num_chars,no_space=True))
|
|
|
|
|
vmsg_r('SEND ')
|
|
|
|
|
while rand_chars:
|
|
|
|
|
ch = rand_chars.pop(0)
|
2023-03-28 18:14:37 +00:00
|
|
|
msg_r(yellow(ch)+' ' if cfg.verbose else '+')
|
2023-10-11 12:58:52 +00:00
|
|
|
self.expect('left: ',ch,delay=0.005)
|
2019-03-02 18:27:53 +00:00
|
|
|
self.expect('ENTER to continue: ','\n')
|
|
|
|
|
|
|
|
|
|
def passphrase_new(self,desc,passphrase):
|
2021-09-29 21:17:57 +00:00
|
|
|
self.expect(f'Enter passphrase for {desc}: ',passphrase+'\n')
|
2019-03-02 18:27:53 +00:00
|
|
|
self.expect('Repeat passphrase: ',passphrase+'\n')
|
|
|
|
|
|
|
|
|
|
def passphrase(self,desc,passphrase,pwtype=''):
|
2023-10-11 12:58:51 +00:00
|
|
|
if pwtype:
|
|
|
|
|
pwtype += ' '
|
2021-09-29 21:17:57 +00:00
|
|
|
self.expect(f'Enter {pwtype}passphrase for {desc}.*?: ',passphrase+'\n',regex=True)
|
2019-03-02 18:27:53 +00:00
|
|
|
|
|
|
|
|
def hash_preset(self,desc,preset=''):
|
2021-09-29 21:17:57 +00:00
|
|
|
self.expect(f'Enter hash preset for {desc}')
|
2019-03-02 18:27:53 +00:00
|
|
|
self.expect('or hit ENTER .*?:',str(preset)+'\n',regex=True)
|
|
|
|
|
|
2023-10-11 12:58:52 +00:00
|
|
|
def written_to_file(self,desc,overwrite_unlikely=False,query='Overwrite? '):
|
2021-09-29 21:17:57 +00:00
|
|
|
s1 = f'{desc} written to file '
|
2019-03-02 18:27:53 +00:00
|
|
|
s2 = query + "Type uppercase 'YES' to confirm: "
|
|
|
|
|
ret = self.expect(([s1,s2],s1)[overwrite_unlikely])
|
|
|
|
|
if ret == 1:
|
|
|
|
|
self.send('YES\n')
|
|
|
|
|
return self.expect_getend("Overwriting file '").rstrip("'")
|
|
|
|
|
self.expect(NL,nonl=True)
|
|
|
|
|
outfile = self.p.before.strip().strip("'")
|
2023-03-28 18:14:37 +00:00
|
|
|
if cfg.debug_pexpect:
|
2021-09-29 21:17:57 +00:00
|
|
|
rmsg(f'Outfile [{outfile}]')
|
|
|
|
|
vmsg('{} file: {}'.format( desc, cyan(outfile.replace('"',"")) ))
|
2019-03-02 18:27:53 +00:00
|
|
|
return outfile
|
|
|
|
|
|
2019-10-10 19:53:42 +00:00
|
|
|
def hincog_create(self,hincog_bytes):
|
|
|
|
|
ret = self.expect(['Create? (Y/n): ',"'YES' to confirm: "])
|
|
|
|
|
if ret == 0:
|
|
|
|
|
self.send('\n')
|
|
|
|
|
self.expect('Enter file size: ',str(hincog_bytes)+'\n')
|
|
|
|
|
else:
|
|
|
|
|
self.send('YES\n')
|
|
|
|
|
return ret
|
|
|
|
|
|
2019-03-02 18:27:53 +00:00
|
|
|
def no_overwrite(self):
|
|
|
|
|
self.expect("Overwrite? Type uppercase 'YES' to confirm: ",'\n')
|
|
|
|
|
self.expect('Exiting at user request')
|
|
|
|
|
|
|
|
|
|
def expect_getend(self,s,regex=False):
|
2023-10-11 12:58:52 +00:00
|
|
|
self.expect(s,regex=regex,nonl=True)
|
2023-03-28 18:14:37 +00:00
|
|
|
if cfg.debug_pexpect:
|
2022-10-29 20:10:23 +00:00
|
|
|
debug_pexpect_msg(self.p)
|
2019-03-02 18:27:53 +00:00
|
|
|
# readline() of partial lines doesn't work with PopenSpawn, so do this instead:
|
|
|
|
|
self.expect(NL,nonl=True,silent=True)
|
2023-03-28 18:14:37 +00:00
|
|
|
if cfg.debug_pexpect:
|
2022-10-29 20:10:23 +00:00
|
|
|
debug_pexpect_msg(self.p)
|
2019-03-26 13:02:09 +00:00
|
|
|
end = self.p.before.rstrip()
|
2023-03-28 18:14:37 +00:00
|
|
|
if not cfg.debug:
|
2021-09-29 21:17:57 +00:00
|
|
|
vmsg(f' ==> {cyan(end)}')
|
2019-03-02 18:27:53 +00:00
|
|
|
return end
|
|
|
|
|
|
|
|
|
|
def interactive(self):
|
|
|
|
|
return self.p.interact() # interact() not available with popen_spawn
|
|
|
|
|
|
|
|
|
|
def kill(self,signal):
|
|
|
|
|
return self.p.kill(signal)
|
|
|
|
|
|
2022-08-04 13:44:31 +00:00
|
|
|
def match_expect_list(self,expect_list,greedy=False):
|
|
|
|
|
allrep = '.*' if greedy else '.*?'
|
|
|
|
|
expect = (
|
|
|
|
|
r'(\b|\s)' +
|
|
|
|
|
fr'\s{allrep}\s'.join(s.replace(r'.',r'\.').replace(' ',r'\s+') for s in expect_list) +
|
|
|
|
|
r'(\b|\s)' )
|
2023-03-28 18:14:37 +00:00
|
|
|
import re
|
2023-04-30 10:01:24 +00:00
|
|
|
m = re.search( expect, self.read(strip_color=True), re.DOTALL )
|
2022-08-04 13:44:31 +00:00
|
|
|
assert m, f'No match found for regular expression {expect!r}'
|
|
|
|
|
return m
|
|
|
|
|
|
2019-03-02 18:27:53 +00:00
|
|
|
def expect(self,s,t='',delay=None,regex=False,nonl=False,silent=False):
|
|
|
|
|
|
|
|
|
|
if not silent:
|
2023-03-28 18:14:37 +00:00
|
|
|
if cfg.verbose:
|
2019-03-23 14:30:47 +00:00
|
|
|
msg_r('EXPECT ' + yellow(str(s)))
|
2023-03-28 18:14:37 +00:00
|
|
|
elif not cfg.exact_output:
|
2022-10-29 20:10:23 +00:00
|
|
|
msg_r('+')
|
2019-03-02 18:27:53 +00:00
|
|
|
|
|
|
|
|
try:
|
2022-10-29 20:10:23 +00:00
|
|
|
ret = (self.p.expect_exact,self.p.expect)[bool(regex)](s) if s else 0
|
2023-10-11 12:58:53 +00:00
|
|
|
except pexpect.TIMEOUT as e:
|
2023-03-28 18:14:37 +00:00
|
|
|
if cfg.debug_pexpect:
|
2022-02-05 13:32:56 +00:00
|
|
|
raise
|
|
|
|
|
m1 = f'\nERROR. Expect {s!r} timed out. Exiting\n'
|
2021-09-29 21:17:57 +00:00
|
|
|
m2 = f'before: [{self.p.before}]\n'
|
2023-10-11 12:58:52 +00:00
|
|
|
m3 = f'sent value: [{self.sent_value}]' if self.sent_value is not None else ''
|
2023-10-11 12:58:53 +00:00
|
|
|
raise pexpect.TIMEOUT(m1+m2+m3) from e
|
2019-03-02 18:27:53 +00:00
|
|
|
|
2023-03-28 18:14:37 +00:00
|
|
|
if cfg.debug_pexpect:
|
2022-10-29 20:10:23 +00:00
|
|
|
debug_pexpect_msg(self.p)
|
2019-03-02 18:27:53 +00:00
|
|
|
|
2023-10-11 12:58:51 +00:00
|
|
|
if cfg.verbose and not isinstance(s,str):
|
2021-09-29 21:17:57 +00:00
|
|
|
msg_r(f' ==> {ret} ')
|
2019-03-02 18:27:53 +00:00
|
|
|
|
|
|
|
|
if ret == -1:
|
2022-02-05 13:32:56 +00:00
|
|
|
die(4,f'Error. Expect returned {ret}')
|
2019-03-02 18:27:53 +00:00
|
|
|
else:
|
2022-10-29 20:10:23 +00:00
|
|
|
if t:
|
2019-03-02 18:27:53 +00:00
|
|
|
self.send(t,delay,s)
|
2022-10-29 20:10:23 +00:00
|
|
|
else:
|
|
|
|
|
if not nonl and not silent:
|
|
|
|
|
vmsg('')
|
2019-03-02 18:27:53 +00:00
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
def send(self,t,delay=None,s=False):
|
2022-10-29 20:10:24 +00:00
|
|
|
delay = delay or self.send_delay
|
2022-10-29 20:10:23 +00:00
|
|
|
if delay:
|
|
|
|
|
time.sleep(delay)
|
2019-03-02 18:27:53 +00:00
|
|
|
ret = self.p.send(t) # returns num bytes written
|
2022-10-29 20:10:23 +00:00
|
|
|
self.sent_value = t if ret else None
|
2023-03-28 18:14:37 +00:00
|
|
|
if cfg.demo and delay:
|
2022-10-29 20:10:23 +00:00
|
|
|
time.sleep(delay)
|
2023-03-28 18:14:37 +00:00
|
|
|
if cfg.verbose:
|
|
|
|
|
ls = '' if cfg.debug or not s else ' '
|
2021-09-29 21:17:57 +00:00
|
|
|
es = '' if s else ' '
|
|
|
|
|
yt = yellow('{!r}'.format( t.replace('\n',r'\n') ))
|
|
|
|
|
msg(f'{ls}SEND {es}{yt}')
|
2019-03-02 18:27:53 +00:00
|
|
|
return ret
|
|
|
|
|
|
2023-04-30 10:01:24 +00:00
|
|
|
def read(self,n=-1,strip_color=False):
|
|
|
|
|
return strip_ansi_escapes(self.p.read(n)).replace('\r','') if strip_color else self.p.read(n)
|
2019-03-02 18:27:53 +00:00
|
|
|
|
|
|
|
|
def close(self):
|
2022-10-28 11:35:13 +00:00
|
|
|
if self.pexpect_spawn:
|
2019-03-02 18:27:53 +00:00
|
|
|
self.p.close()
|