python: Add more tests.
authorJustus Winter <justus@gnupg.org>
Wed, 18 May 2016 13:33:36 +0000 (15:33 +0200)
committerJustus Winter <justus@gnupg.org>
Thu, 19 May 2016 09:09:45 +0000 (11:09 +0200)
* 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>
lang/python/tests/Makefile.am
lang/python/tests/support.py
lang/python/tests/t-decrypt.py
lang/python/tests/t-encrypt-sym.py [new file with mode: 0755]
lang/python/tests/t-encrypt.py
lang/python/tests/t-sign.py [new file with mode: 0755]

index 87fb8d1..c4fc602 100644 (file)
@@ -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 \
index 36b3c88..a57d581 100644 (file)
@@ -15,6 +15,7 @@
 # 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
 
@@ -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())
index da62ba4..bd7b59f 100755 (executable)
@@ -17,8 +17,6 @@
 # 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
 
@@ -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 (executable)
index 0000000..0b24fd5
--- /dev/null
@@ -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)
index 3bed752..24869fc 100755 (executable)
@@ -17,8 +17,6 @@
 # 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
 
@@ -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 (executable)
index 0000000..a721f03
--- /dev/null
@@ -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)