dexter has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/38606?usp=email )
Change subject: pySim-shell: allow checking of APDU responses ......................................................................
pySim-shell: allow checking of APDU responses
The "apdu" command allows us to send custom APDUs to a card. This command is often used in low level initialization scripts or tests. To stop the script execution in case of an error, the command allows us to specify a status word that must match the status word of the response. But we have no such mechanism for the response itsself. Let's add another parameter where we can pass a regex that the response must match.
Related: OS#6367 Change-Id: I97bbcdf37bdcf00ad50a875b96940c211de7073d --- M pySim-shell.py 1 file changed, 6 insertions(+), 0 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/06/38606/1
diff --git a/pySim-shell.py b/pySim-shell.py index 37d58ae..f53efd3 100755 --- a/pySim-shell.py +++ b/pySim-shell.py @@ -21,6 +21,7 @@
import json import traceback +import re
import cmd2 from packaging import version @@ -239,6 +240,7 @@
apdu_cmd_parser = argparse.ArgumentParser() apdu_cmd_parser.add_argument('--expect-sw', help='expect a specified status word', type=str, default=None) + apdu_cmd_parser.add_argument('--expect-response-regex', help='match response against regex', type=str, default=None) apdu_cmd_parser.add_argument('--raw', help='Bypass the logical channel (and secure channel)', action='store_true') apdu_cmd_parser.add_argument('APDU', type=is_hexstr, help='APDU as hex string')
@@ -264,6 +266,10 @@ if opts.expect_sw: if not sw_match(sw, opts.expect_sw): raise SwMatchError(sw, opts.expect_sw) + if opts.expect_response_regex: + response_regex_compiled = re.compile(opts.expect_response_regex) + if re.match(response_regex_compiled, data) is None: + raise ValueError("RESP does not match regex '%s'" % opts.expect_response_regex)
@cmd2.with_category(CUSTOM_CATEGORY) def do_reset(self, opts):