diff --git a/lang/python/tests/Makefile.am b/lang/python/tests/Makefile.am index 87fb8d1f..c4fc6029 100644 --- a/lang/python/tests/Makefile.am +++ b/lang/python/tests/Makefile.am @@ -31,6 +31,8 @@ TESTS_ENVIRONMENT = GNUPGHOME=$(abs_builddir) \ py_tests = t-wrapper.py \ t-data.py \ t-encrypt.py \ + t-encrypt-sym.py \ + t-sign.py \ t-decrypt.py TESTS = $(top_srcdir)/tests/gpg/initial.test \ diff --git a/lang/python/tests/support.py b/lang/python/tests/support.py index 36b3c888..a57d5816 100644 --- a/lang/python/tests/support.py +++ b/lang/python/tests/support.py @@ -15,6 +15,7 @@ # You should have received a copy of the GNU Lesser General Public # License along with this program; if not, see . +import sys import os from pyme import core @@ -24,3 +25,8 @@ def make_filename(name): def init_gpgme(proto): core.check_version() core.engine_check_version(proto) + +def print_data(data): + if int(os.environ.get('verbose', 0)) > 1: + data.seek(0, os.SEEK_SET) + sys.stdout.buffer.write(data.read()) diff --git a/lang/python/tests/t-decrypt.py b/lang/python/tests/t-decrypt.py index da62ba42..bd7b59fb 100755 --- a/lang/python/tests/t-decrypt.py +++ b/lang/python/tests/t-decrypt.py @@ -17,8 +17,6 @@ # You should have received a copy of the GNU Lesser General Public # License along with this program; if not, see . -import sys -import os from pyme import core, constants import support @@ -33,5 +31,4 @@ result = c.op_decrypt_result() assert not result.unsupported_algorithm, \ "Unsupported algorithm: {}".format(result.unsupported_algorithm) -sink.seek(0, os.SEEK_SET) -sys.stdout.buffer.write(sink.read()) +support.print_data(sink) diff --git a/lang/python/tests/t-encrypt-sym.py b/lang/python/tests/t-encrypt-sym.py new file mode 100755 index 00000000..0b24fd52 --- /dev/null +++ b/lang/python/tests/t-encrypt-sym.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python3 + +# 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 . + +import os +from pyme import core, constants +import support + +support.init_gpgme(constants.PROTOCOL_OpenPGP) + +for passphrase in ("abc", b"abc"): + c = core.Context() + c.set_armor(True) + c.set_pinentry_mode(constants.PINENTRY_MODE_LOOPBACK) + + source = core.Data("Hallo Leute\n") + cipher = core.Data() + + passphrase_cb_called = 0 + def passphrase_cb(hint, desc, prev_bad, hook=None): + global passphrase_cb_called + passphrase_cb_called += 1 + return passphrase + + c.set_passphrase_cb(passphrase_cb, None) + + c.op_encrypt([], 0, source, cipher) + assert passphrase_cb_called == 1, \ + "Callback called {} times".format(passphrase_cb_called) + support.print_data(cipher) + + c = core.Context() + c.set_armor(True) + c.set_pinentry_mode(constants.PINENTRY_MODE_LOOPBACK) + c.set_passphrase_cb(passphrase_cb, None) + plain = core.Data() + cipher.seek(0, os.SEEK_SET) + + c.op_decrypt(cipher, plain) + # Seems like the passphrase is cached. + #assert passphrase_cb_called == 2, \ + # "Callback called {} times".format(passphrase_cb_called) + support.print_data(plain) + + plain.seek(0, os.SEEK_SET) + plaintext = plain.read() + assert plaintext == b"Hallo Leute\n", \ + "Wrong plaintext {!r}".format(plaintext) diff --git a/lang/python/tests/t-encrypt.py b/lang/python/tests/t-encrypt.py index 3bed752b..24869fcd 100755 --- a/lang/python/tests/t-encrypt.py +++ b/lang/python/tests/t-encrypt.py @@ -17,8 +17,6 @@ # You should have received a copy of the GNU Lesser General Public # License along with this program; if not, see . -import sys -import os from pyme import core, constants import support @@ -38,5 +36,4 @@ result = c.op_encrypt_result() assert not result.invalid_recipients, \ "Invalid recipient encountered: {}".format(result.invalid_recipients.fpr) -sink.seek(0, os.SEEK_SET) -sys.stdout.buffer.write(sink.read()) +support.print_data(sink) diff --git a/lang/python/tests/t-sign.py b/lang/python/tests/t-sign.py new file mode 100755 index 00000000..a721f03d --- /dev/null +++ b/lang/python/tests/t-sign.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 + +# 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 . + +import sys +import os +from pyme import core, constants +import support + +def check_result(r, typ): + if r.invalid_signers: + sys.exit("Invalid signer found: {}".format(r.invalid_signers.fpr)) + + if len(r.signatures) != 1: + sys.exit("Unexpected number of signatures created") + + signature = r.signatures[0] + if signature.type != typ: + sys.exit("Wrong type of signature created") + + if signature.pubkey_algo != constants.PK_DSA: + sys.exit("Wrong pubkey algorithm reported: {}".format( + signature.pubkey_algo)) + + if signature.hash_algo != constants.MD_SHA1: + sys.exit("Wrong hash algorithm reported: {}".format( + signature.hash_algo)) + + if signature.sig_class != 1: + sys.exit("Wrong signature class reported: {}".format( + signature.sig_class)) + + if signature.fpr != "A0FF4590BB6122EDEF6E3C542D727CC768697734": + sys.exit("Wrong fingerprint reported: {}".format(signature.fpr)) + + +support.init_gpgme(constants.PROTOCOL_OpenPGP) +c = core.Context() +c.set_textmode(True) +c.set_armor(True) + +source = core.Data("Hallo Leute\n") +sink = core.Data() + +c.op_sign(source, sink, constants.SIG_MODE_NORMAL) + +result = c.op_sign_result() +check_result(result, constants.SIG_MODE_NORMAL) +support.print_data(sink) + +# Now a detached signature. +source.seek(0, os.SEEK_SET) +sink = core.Data() + +c.op_sign(source, sink, constants.SIG_MODE_DETACH) + +result = c.op_sign_result() +check_result(result, constants.SIG_MODE_DETACH) +support.print_data(sink) + +# And finally a cleartext signature. */ +source.seek(0, os.SEEK_SET) +sink = core.Data() + +c.op_sign(source, sink, constants.SIG_MODE_CLEAR) + +result = c.op_sign_result() +check_result(result, constants.SIG_MODE_CLEAR) +support.print_data(sink)