python: Add more tests.

* lang/python/tests/Makefile.am (py_tests): Add new tests.
* lang/python/tests/support.py (print_data): New function.
* lang/python/tests/t-decrypt.py: Use new function.
* lang/python/tests/t-encrypt.py: Likewise.
* lang/python/tests/t-sign.py: New file.
* lang/python/tests/t-encrypt-sym.py: Likewise.

Signed-off-by: Justus Winter <justus@gnupg.org>
This commit is contained in:
Justus Winter 2016-05-18 15:33:36 +02:00
parent c1c893e338
commit 464d404c88
6 changed files with 157 additions and 8 deletions

View File

@ -31,6 +31,8 @@ TESTS_ENVIRONMENT = GNUPGHOME=$(abs_builddir) \
py_tests = t-wrapper.py \ py_tests = t-wrapper.py \
t-data.py \ t-data.py \
t-encrypt.py \ t-encrypt.py \
t-encrypt-sym.py \
t-sign.py \
t-decrypt.py t-decrypt.py
TESTS = $(top_srcdir)/tests/gpg/initial.test \ TESTS = $(top_srcdir)/tests/gpg/initial.test \

View File

@ -15,6 +15,7 @@
# You should have received a copy of the GNU Lesser General Public # 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/>. # License along with this program; if not, see <http://www.gnu.org/licenses/>.
import sys
import os import os
from pyme import core from pyme import core
@ -24,3 +25,8 @@ def make_filename(name):
def init_gpgme(proto): def init_gpgme(proto):
core.check_version() core.check_version()
core.engine_check_version(proto) 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())

View File

@ -17,8 +17,6 @@
# You should have received a copy of the GNU Lesser General Public # 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/>. # License along with this program; if not, see <http://www.gnu.org/licenses/>.
import sys
import os
from pyme import core, constants from pyme import core, constants
import support import support
@ -33,5 +31,4 @@ result = c.op_decrypt_result()
assert not result.unsupported_algorithm, \ assert not result.unsupported_algorithm, \
"Unsupported algorithm: {}".format(result.unsupported_algorithm) "Unsupported algorithm: {}".format(result.unsupported_algorithm)
sink.seek(0, os.SEEK_SET) support.print_data(sink)
sys.stdout.buffer.write(sink.read())

View File

@ -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 <http://www.gnu.org/licenses/>.
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)

View File

@ -17,8 +17,6 @@
# You should have received a copy of the GNU Lesser General Public # 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/>. # License along with this program; if not, see <http://www.gnu.org/licenses/>.
import sys
import os
from pyme import core, constants from pyme import core, constants
import support import support
@ -38,5 +36,4 @@ result = c.op_encrypt_result()
assert not result.invalid_recipients, \ assert not result.invalid_recipients, \
"Invalid recipient encountered: {}".format(result.invalid_recipients.fpr) "Invalid recipient encountered: {}".format(result.invalid_recipients.fpr)
sink.seek(0, os.SEEK_SET) support.print_data(sink)
sys.stdout.buffer.write(sink.read())

84
lang/python/tests/t-sign.py Executable file
View File

@ -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 <http://www.gnu.org/licenses/>.
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)