diff options
Diffstat (limited to 'lang/python/tests')
| -rw-r--r-- | lang/python/tests/Makefile.am | 3 | ||||
| -rwxr-xr-x | lang/python/tests/t-callbacks.py | 33 | ||||
| -rwxr-xr-x | lang/python/tests/t-edit.py | 62 | 
3 files changed, 97 insertions, 1 deletions
| diff --git a/lang/python/tests/Makefile.am b/lang/python/tests/Makefile.am index 32dc4323..236354ff 100644 --- a/lang/python/tests/Makefile.am +++ b/lang/python/tests/Makefile.am @@ -38,7 +38,8 @@ py_tests = t-wrapper.py \  	t-sign.py \  	t-signers.py \  	t-decrypt.py \ -	t-export.py +	t-export.py \ +	t-edit.py  TESTS = $(top_srcdir)/tests/gpg/initial.test \  	$(py_tests) \ diff --git a/lang/python/tests/t-callbacks.py b/lang/python/tests/t-callbacks.py index 70f641d7..d962dc41 100755 --- a/lang/python/tests/t-callbacks.py +++ b/lang/python/tests/t-callbacks.py @@ -113,3 +113,36 @@ except Exception as e:      assert e == myException  else:      assert False, "Expected an error, got none" + + +# Test the edit callback. +c = core.Context() +c.set_pinentry_mode(constants.PINENTRY_MODE_LOOPBACK) +c.set_passphrase_cb(lambda *args: "abc") +sink = core.Data() +alpha = c.get_key("A0FF4590BB6122EDEF6E3C542D727CC768697734", False) + +cookie = object() +edit_cb_called = False +def edit_cb(status, args, hook): +    global edit_cb_called +    edit_cb_called = True +    assert hook == cookie +    return "quit" if args == "keyedit.prompt" else None +c.op_edit(alpha, edit_cb, cookie, sink) +assert edit_cb_called + +# Test exceptions. +c = core.Context() +c.set_pinentry_mode(constants.PINENTRY_MODE_LOOPBACK) +c.set_passphrase_cb(lambda *args: "abc") +sink = core.Data() + +def edit_cb(status, args): +    raise myException +try: +    c.op_edit(alpha, edit_cb, None, sink) +except Exception as e: +    assert e == myException +else: +    assert False, "Expected an error, got none" diff --git a/lang/python/tests/t-edit.py b/lang/python/tests/t-edit.py new file mode 100755 index 00000000..64255c95 --- /dev/null +++ b/lang/python/tests/t-edit.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2005 Igor Belyi <[email protected]> +# Copyright (C) 2016 g10 Code GmbH +# +# This file is part of GPGME. +# +# GPGME 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 2 of the License, or +# (at your option) any later version. +# +# GPGME 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 Lesser General +# Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program; if not, see <http://www.gnu.org/licenses/>. + +import sys +import os +from pyme import core, constants +import support + +class KeyEditor(object): +    def __init__(self): +        self.steps = ["fpr", "expire", "1", "primary", "quit"] +        self.step = 0 +        self.done = False +        self.verbose = int(os.environ.get('verbose', 0)) > 1 + +    def edit_fnc(self, status, args, out): +        if args == "keyedit.prompt": +            result = self.steps[self.step] +            self.step += 1 +        elif args == "keyedit.save.okay": +            result = "Y" +            self.done = self.step == len(self.steps) +        elif args == "keygen.valid": +            result = "0" +        else: +            result = None + +        if self.verbose: +            sys.stderr.write("Code: {}, args: {!r}, Returning: {!r}\n" +                             .format(status, args, result)) + +        return result + +support.init_gpgme(constants.PROTOCOL_OpenPGP) + +c = core.Context() +c.set_pinentry_mode(constants.PINENTRY_MODE_LOOPBACK) +c.set_passphrase_cb(lambda *args: "abc") +c.set_armor(True) +sink = core.Data() + +editor = KeyEditor() +c.op_edit(c.get_key("A0FF4590BB6122EDEF6E3C542D727CC768697734", False), +          editor.edit_fnc, sink, sink) +assert editor.done | 
