From e64bffe0307d14204b00a177a472cd4f99c07561 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Thu, 12 May 2016 15:27:50 +0200 Subject: python: Add a test suite. * configure.ac: Add new Makefile. * lang/python/Makefile.am: Add subdirectory. * lang/python/tests/Makefile.am: New file. * lang/python/tests/t-wrapper.py: Likewise. Signed-off-by: Justus Winter --- lang/python/tests/Makefile.am | 22 ++++++++++++++++++++++ lang/python/tests/t-wrapper.py | 23 +++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 lang/python/tests/Makefile.am create mode 100755 lang/python/tests/t-wrapper.py (limited to 'lang/python/tests') diff --git a/lang/python/tests/Makefile.am b/lang/python/tests/Makefile.am new file mode 100644 index 00000000..56ff74ce --- /dev/null +++ b/lang/python/tests/Makefile.am @@ -0,0 +1,22 @@ +# Makefile.am for the tests of the Python bindings. +# 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 . + +TESTS_ENVIRONMENT = GNUPGHOME=$(abs_builddir) \ + PYTHONPATH=`echo $(abs_builddir)/../build/lib.*` + +TESTS = t-wrapper.py diff --git a/lang/python/tests/t-wrapper.py b/lang/python/tests/t-wrapper.py new file mode 100755 index 00000000..acc2ecfd --- /dev/null +++ b/lang/python/tests/t-wrapper.py @@ -0,0 +1,23 @@ +#!/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 . + +from pyme import core + +d0 = core.Data() +assert d0.seek == d0.seek, "Generated wrapper functions are not cached" -- cgit From f7094d8358e933f3ce074eade7a40b2a7d291180 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Thu, 12 May 2016 17:44:54 +0200 Subject: python: Fix writing to data buffers. * lang/python/gpgme.i: Add typemap for buffers. * lang/python/pyme/core.py (Data.write): Fix function. * lang/python/tests/Makefile.am: Add new test. * lang/python/tests/t-data.py: New file. Signed-off-by: Justus Winter --- lang/python/gpgme.i | 17 +++++++++++++++ lang/python/pyme/core.py | 6 ++++-- lang/python/tests/Makefile.am | 2 +- lang/python/tests/t-data.py | 49 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 3 deletions(-) create mode 100755 lang/python/tests/t-data.py (limited to 'lang/python/tests') diff --git a/lang/python/gpgme.i b/lang/python/gpgme.i index 7c11943c..c5b5bc97 100644 --- a/lang/python/gpgme.i +++ b/lang/python/gpgme.i @@ -173,6 +173,23 @@ PyObject* object_to_gpgme_t(PyObject* input, const char* objtype, int argnum) { free($1); } +/* For gpgme_data_write, but should be universal. */ +%typemap(in) (const void *buffer, size_t size) { + if ($input == Py_None) + $1 = NULL, $2 = 0; + else if (PyUnicode_Check($input)) + $1 = PyUnicode_AsUTF8AndSize($input, (size_t *) &$2); + else if (PyBytes_Check($input)) + PyBytes_AsStringAndSize($input, (char **) &$1, (size_t *) &$2); + else { + PyErr_Format(PyExc_TypeError, + "arg %d: expected str, bytes, or None, got %s", + $argnum, $input->ob_type->tp_name); + return NULL; + } +} +%typemap(freearg) (const void *buffer, size_t size) ""; + // Make types containing 'next' field to be lists %ignore next; %typemap(out) gpgme_sig_notation_t, gpgme_engine_info_t, gpgme_subkey_t, gpgme_key_sig_t, diff --git a/lang/python/pyme/core.py b/lang/python/pyme/core.py index e822704a..dafbd9b3 100644 --- a/lang/python/pyme/core.py +++ b/lang/python/pyme/core.py @@ -391,8 +391,10 @@ class Data(GpgmeWrapper): self.new_from_fd(file) def write(self, buffer): - """Write buffer given as bytes.""" - errorcheck(pygpgme.gpgme_data_write(self.wrapped, buffer, len(buffer))) + """Write buffer given as string or bytes. + + If a string is given, it is implicitly encoded using UTF-8.""" + return pygpgme.gpgme_data_write(self.wrapped, buffer) def read(self, size = -1): """Read at most size bytes, returned as bytes. diff --git a/lang/python/tests/Makefile.am b/lang/python/tests/Makefile.am index 56ff74ce..50c78e35 100644 --- a/lang/python/tests/Makefile.am +++ b/lang/python/tests/Makefile.am @@ -19,4 +19,4 @@ TESTS_ENVIRONMENT = GNUPGHOME=$(abs_builddir) \ PYTHONPATH=`echo $(abs_builddir)/../build/lib.*` -TESTS = t-wrapper.py +TESTS = t-wrapper.py t-data.py diff --git a/lang/python/tests/t-data.py b/lang/python/tests/t-data.py new file mode 100755 index 00000000..af2eb986 --- /dev/null +++ b/lang/python/tests/t-data.py @@ -0,0 +1,49 @@ +#!/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 + +data = core.Data('Hello world!') +assert data.read() == b'Hello world!' +assert data.read() == b'' + +data.seek(0, os.SEEK_SET) +assert data.read() == b'Hello world!' +assert data.read() == b'' + +data = core.Data(b'Hello world!') +assert data.read() == b'Hello world!' + +data = core.Data() +data.write('Hello world!') +data.seek(0, os.SEEK_SET) +assert data.read() == b'Hello world!' + +data = core.Data() +data.write(b'Hello world!') +data.seek(0, os.SEEK_SET) +assert data.read() == b'Hello world!' + +binjunk = bytes(range(256)) +data = core.Data() +data.write(binjunk) +data.seek(0, os.SEEK_SET) +assert data.read() == binjunk -- cgit From 11314f0db6e57597e3f56351a86fdb36a7a17dd7 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Thu, 12 May 2016 18:29:04 +0200 Subject: python: Share generated methods between objects. * lang/python/pyme/util.py (GpgmeWrapper.__getattr__): Monkey-patch the class. * lang/python/tests/t-wrapper.py: Demonstrate the sharing. Signed-off-by: Justus Winter --- lang/python/pyme/util.py | 26 ++++++++++++++++---------- lang/python/tests/t-wrapper.py | 2 ++ 2 files changed, 18 insertions(+), 10 deletions(-) (limited to 'lang/python/tests') diff --git a/lang/python/pyme/util.py b/lang/python/pyme/util.py index a8560992..a9fa19d2 100644 --- a/lang/python/pyme/util.py +++ b/lang/python/pyme/util.py @@ -74,19 +74,25 @@ class GpgmeWrapper(object): if key[0] == '_' or self._getnameprepend() == None: return None name = self._getnameprepend() + key + func = getattr(pygpgme, name) + if self._errorcheck(name): - def _funcwrap(*args, **kwargs): - args = [self.wrapped] + list(args) - return errorcheck(getattr(pygpgme, name)(*args, **kwargs), + def _funcwrap(slf, *args, **kwargs): + return errorcheck(func(slf.wrapped, *args, **kwargs), "Invocation of " + name) else: - def _funcwrap(*args, **kwargs): - args = [self.wrapped] + list(args) - return getattr(pygpgme, name)(*args, **kwargs) + def _funcwrap(slf, *args, **kwargs): + return func(slf.wrapped, *args, **kwargs) + + _funcwrap.__doc__ = getattr(func, "__doc__") + + # Monkey-patch the class. + setattr(self.__class__, key, _funcwrap) - _funcwrap.__doc__ = getattr(getattr(pygpgme, name), "__doc__") + # Bind the method to 'self'. + def wrapper(*args, **kwargs): + return _funcwrap(self, *args, **kwargs) + _funcwrap.__doc__ = getattr(func, "__doc__") - # Cache the wrapper function. - setattr(self, key, _funcwrap) - return _funcwrap + return wrapper diff --git a/lang/python/tests/t-wrapper.py b/lang/python/tests/t-wrapper.py index acc2ecfd..fab0d811 100755 --- a/lang/python/tests/t-wrapper.py +++ b/lang/python/tests/t-wrapper.py @@ -20,4 +20,6 @@ from pyme import core d0 = core.Data() +d0.seek # trigger on-demand-wrapping assert d0.seek == d0.seek, "Generated wrapper functions are not cached" +assert hasattr(core.Data, 'seek'), "Generated wrapper functions are not shared" -- cgit From 9ceaec25918c6c5f2dfafe4e20181b83ce78f6ce Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Tue, 17 May 2016 13:46:44 +0200 Subject: python: Port more tests. * lang/python/Makefile.am: Add bits from the c test suite. * lang/python/support.py: New file. * lang/python/t-decrypt.py: Likewise. * lang/python/t-encrypt.py: Likewise. Signed-off-by: Justus Winter --- lang/python/tests/Makefile.am | 62 +++++++++++++++++++++++++++++++++++++++++- lang/python/tests/support.py | 26 ++++++++++++++++++ lang/python/tests/t-decrypt.py | 37 +++++++++++++++++++++++++ lang/python/tests/t-encrypt.py | 42 ++++++++++++++++++++++++++++ 4 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 lang/python/tests/support.py create mode 100755 lang/python/tests/t-decrypt.py create mode 100755 lang/python/tests/t-encrypt.py (limited to 'lang/python/tests') diff --git a/lang/python/tests/Makefile.am b/lang/python/tests/Makefile.am index 50c78e35..87fb8d1f 100644 --- a/lang/python/tests/Makefile.am +++ b/lang/python/tests/Makefile.am @@ -16,7 +16,67 @@ # You should have received a copy of the GNU Lesser General Public # License along with this program; if not, see . +GPG = gpg +GPG_AGENT = gpg-agent +export GNUPGHOME := $(abs_builddir) +export GPG_AGENT_INFO := + +test_srcdir = $(top_srcdir)/tests/gpg + TESTS_ENVIRONMENT = GNUPGHOME=$(abs_builddir) \ + LC_ALL=C GPG_AGENT_INFO= \ + top_srcdir=$(top_srcdir) \ PYTHONPATH=`echo $(abs_builddir)/../build/lib.*` -TESTS = t-wrapper.py t-data.py +py_tests = t-wrapper.py \ + t-data.py \ + t-encrypt.py \ + t-decrypt.py + +TESTS = $(top_srcdir)/tests/gpg/initial.test \ + $(py_tests) \ + $(top_srcdir)/tests/gpg/final.test + +CLEANFILES = secring.gpg pubring.gpg pubring.kbx trustdb.gpg dirmngr.conf \ + gpg-agent.conf pubring.kbx~ gpg.conf pubring.gpg~ \ + random_seed .gpg-v21-migrated pubring-stamp + +private_keys = \ + $(test_srcdir)/13CD0F3BDF24BE53FE192D62F18737256FF6E4FD \ + $(test_srcdir)/76F7E2B35832976B50A27A282D9B87E44577EB66 \ + $(test_srcdir)/A0747D5F9425E6664F4FFBEED20FBCA79FDED2BD \ + $(test_srcdir)/13CBE3758AFE42B5E5E2AE4CED27AFA455E3F87F \ + $(test_srcdir)/7A030357C0F253A5BBCD282FFC4E521B37558F5C + +clean-local: + -$(top_srcdir)/tests/start-stop-agent --stop + -rm -fR -- private-keys-v1.d openpgp-revocs.d S.gpg-agent sshcontrol + +check-local: ./gpg.conf ./gpg-agent.conf ./pubring-stamp \ + ./private-keys-v1.d/gpg-sample.stamp + +# To guarantee that check-local is run before any tests we +# add this dependency: +$(top_srcdir)/tests/gpg/initial.test: check-local + +./private-keys-v1.d/gpg-sample.stamp: $(private_keys) + test -d ./private-keys-v1.d || mkdir ./private-keys-v1.d + for k in $(private_keys); do \ + cp $$k private-keys-v1.d/`basename $$k`.key; \ + done + echo x > ./private-keys-v1.d/gpg-sample.stamp + +./pubring-stamp: $(test_srcdir)/pubdemo.asc + $(GPG) --no-permission-warning \ + --import $(test_srcdir)/pubdemo.asc + -$(GPG) --no-permission-warning \ + --import $(test_srcdir)/secdemo.asc + touch ./pubring-stamp + +./gpg.conf: +# This is required for t-sig-notations. + echo no-force-v3-sigs > ./gpg.conf + +./gpg-agent.conf: +# This is required for gpg2, which does not support command fd. + echo pinentry-program $(abs_top_srcdir)/tests/gpg/pinentry > ./gpg-agent.conf diff --git a/lang/python/tests/support.py b/lang/python/tests/support.py new file mode 100644 index 00000000..36b3c888 --- /dev/null +++ b/lang/python/tests/support.py @@ -0,0 +1,26 @@ +# 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 + +def make_filename(name): + return os.path.join(os.environ['top_srcdir'], 'tests', 'gpg', name) + +def init_gpgme(proto): + core.check_version() + core.engine_check_version(proto) diff --git a/lang/python/tests/t-decrypt.py b/lang/python/tests/t-decrypt.py new file mode 100755 index 00000000..da62ba42 --- /dev/null +++ b/lang/python/tests/t-decrypt.py @@ -0,0 +1,37 @@ +#!/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 + +support.init_gpgme(constants.PROTOCOL_OpenPGP) +c = core.Context() + +source = core.Data(file=support.make_filename("cipher-1.asc")) +sink = core.Data() + +c.op_decrypt(source, sink) +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()) diff --git a/lang/python/tests/t-encrypt.py b/lang/python/tests/t-encrypt.py new file mode 100755 index 00000000..3bed752b --- /dev/null +++ b/lang/python/tests/t-encrypt.py @@ -0,0 +1,42 @@ +#!/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 + +support.init_gpgme(constants.PROTOCOL_OpenPGP) +c = core.Context() +c.set_armor(True) + +source = core.Data("Hallo Leute\n") +sink = core.Data() + +keys = [] +keys.append(c.get_key("A0FF4590BB6122EDEF6E3C542D727CC768697734", False)) +keys.append(c.get_key("D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2", False)) + +c.op_encrypt(keys, constants.ENCRYPT_ALWAYS_TRUST, source, sink) +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()) -- cgit