python: Import the topmost module in tests and examples.

* examples/verifydetails.py: Only import the topmost module 'gpg' and
update the code accordingly.
* tests/support.py: Likewise.
* tests/t-callbacks.py: Likewise.
* tests/t-data.py: Likewise.
* tests/t-decrypt-verify.py: Likewise.
* tests/t-decrypt.py: Likewise.
* tests/t-edit.py: Likewise.
* tests/t-encrypt-large.py: Likewise.
* tests/t-encrypt-sign.py: Likewise.
* tests/t-encrypt-sym.py: Likewise.
* tests/t-encrypt.py: Likewise.
* tests/t-export.py: Likewise.
* tests/t-file-name.py: Likewise.
* tests/t-import.py: Likewise.
* tests/t-keylist.py: Likewise.
* tests/t-sig-notation.py: Likewise.
* tests/t-sign.py: Likewise.
* tests/t-signers.py: Likewise.
* tests/t-trustlist.py: Likewise.
* tests/t-verify.py: Likewise.
* tests/t-wait.py: Likewise.
* tests/t-wrapper.py: Likewise.

Signed-off-by: Justus Winter <justus@g10code.com>
This commit is contained in:
Justus Winter 2016-10-31 14:42:26 +01:00
parent 70b7064e5c
commit 20dc37a0e7
22 changed files with 215 additions and 225 deletions

View File

@ -21,24 +21,23 @@ from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import sys
from gpg import core
from gpg.constants import protocol
import gpg
def print_engine_infos():
print("gpgme version:", core.check_version(None))
print("gpgme version:", gpg.core.check_version(None))
print("engines:")
for engine in core.get_engine_info():
for engine in gpg.core.get_engine_info():
print(engine.file_name, engine.version)
for proto in [protocol.OpenPGP, protocol.CMS]:
print("Have {}? {}".format(core.get_protocol_name(proto),
core.engine_check_version(proto)))
for proto in [gpg.constants.protocol.OpenPGP, gpg.constants.protocol.CMS]:
print("Have {}? {}".format(gpg.core.get_protocol_name(proto),
gpg.core.engine_check_version(proto)))
def verifyprintdetails(filename, detached_sig_filename=None):
"""Verify a signature, print a lot of details."""
with core.Context() as c:
with gpg.Context() as c:
# Verify.
data, result = c.verify(open(filename),

View File

@ -20,7 +20,7 @@ del absolute_import, print_function, unicode_literals
import sys
import os
from gpg import core
import gpg
# known keys
alpha = "A0FF4590BB6122EDEF6E3C542D727CC768697734"
@ -36,7 +36,7 @@ def in_srcdir(name):
return os.path.join(os.environ['srcdir'], name)
def init_gpgme(proto):
core.engine_check_version(proto)
gpg.core.engine_check_version(proto)
verbose = int(os.environ.get('verbose', 0)) > 1
def print_data(data):
@ -66,5 +66,5 @@ def mark_key_trusted(ctx, key):
else:
result = None
return result
with core.Data() as sink:
with gpg.Data() as sink:
ctx.op_edit(key, Editor().edit, sink, sink)

View File

@ -21,16 +21,16 @@ from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import os
from gpg import core, constants
import gpg
import support
support.init_gpgme(constants.PROTOCOL_OpenPGP)
support.init_gpgme(gpg.constants.PROTOCOL_OpenPGP)
c = core.Context()
c.set_pinentry_mode(constants.PINENTRY_MODE_LOOPBACK)
c = gpg.Context()
c.set_pinentry_mode(gpg.constants.PINENTRY_MODE_LOOPBACK)
source = core.Data("Hallo Leute\n")
sink = core.Data()
source = gpg.Data("Hallo Leute\n")
sink = gpg.Data()
# Valid passphrases, both as string and bytes.
for passphrase in ('foo', b'foo'):
@ -100,7 +100,7 @@ def progress_cb(what, typ, current, total, hook=None):
"PROGRESS UPDATE: what = {}, type = {}, current = {}, total = {}"
.format(what, typ, current, total))
c = core.Context()
c = gpg.Context()
c.set_progress_cb(progress_cb, messages)
c.op_genkey(parms, None, None)
assert len(messages) > 0
@ -109,7 +109,7 @@ assert len(messages) > 0
def progress_cb(what, typ, current, total, hook=None):
raise myException
c = core.Context()
c = gpg.Context()
c.set_progress_cb(progress_cb, None)
try:
c.op_genkey(parms, None, None)
@ -120,10 +120,10 @@ else:
# Test the edit callback.
c = core.Context()
c.set_pinentry_mode(constants.PINENTRY_MODE_LOOPBACK)
c = gpg.Context()
c.set_pinentry_mode(gpg.constants.PINENTRY_MODE_LOOPBACK)
c.set_passphrase_cb(lambda *args: "abc")
sink = core.Data()
sink = gpg.Data()
alpha = c.get_key("A0FF4590BB6122EDEF6E3C542D727CC768697734", False)
cookie = object()
@ -137,10 +137,10 @@ 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 = gpg.Context()
c.set_pinentry_mode(gpg.constants.PINENTRY_MODE_LOOPBACK)
c.set_passphrase_cb(lambda *args: "abc")
sink = core.Data()
sink = gpg.Data()
def edit_cb(status, args):
raise myException
@ -154,8 +154,8 @@ else:
# Test the status callback.
source = core.Data("Hallo Leute\n")
sink = core.Data()
source = gpg.Data("Hallo Leute\n")
sink = gpg.Data()
status_cb_called = False
def status_cb(keyword, args, hook=None):
@ -163,24 +163,24 @@ def status_cb(keyword, args, hook=None):
status_cb_called = True
assert hook == cookie
c = core.Context()
c = gpg.Context()
c.set_status_cb(status_cb, cookie)
c.set_ctx_flag("full-status", "1")
c.op_encrypt([alpha], constants.ENCRYPT_ALWAYS_TRUST, source, sink)
c.op_encrypt([alpha], gpg.constants.ENCRYPT_ALWAYS_TRUST, source, sink)
assert status_cb_called
# Test exceptions.
source = core.Data("Hallo Leute\n")
sink = core.Data()
source = gpg.Data("Hallo Leute\n")
sink = gpg.Data()
def status_cb(keyword, args):
raise myException
c = core.Context()
c = gpg.Context()
c.set_status_cb(status_cb, None)
c.set_ctx_flag("full-status", "1")
try:
c.op_encrypt([alpha], constants.ENCRYPT_ALWAYS_TRUST, source, sink)
c.op_encrypt([alpha], gpg.constants.ENCRYPT_ALWAYS_TRUST, source, sink)
except Exception as e:
assert e == myException
else:
@ -194,7 +194,7 @@ def read_cb(amount, hook=None):
return 0
def release_cb(hook=None):
assert hook == cookie
data = core.Data(cbs=(read_cb, None, None, release_cb, cookie))
data = gpg.Data(cbs=(read_cb, None, None, release_cb, cookie))
try:
data.read()
except Exception as e:
@ -204,7 +204,7 @@ else:
def read_cb(amount):
raise myException
data = core.Data(cbs=(read_cb, None, None, lambda: None))
data = gpg.Data(cbs=(read_cb, None, None, lambda: None))
try:
data.read()
except Exception as e:
@ -216,7 +216,7 @@ else:
def write_cb(what, hook=None):
assert hook == cookie
return "wrong type"
data = core.Data(cbs=(None, write_cb, None, release_cb, cookie))
data = gpg.Data(cbs=(None, write_cb, None, release_cb, cookie))
try:
data.write(b'stuff')
except Exception as e:
@ -226,7 +226,7 @@ else:
def write_cb(what):
raise myException
data = core.Data(cbs=(None, write_cb, None, lambda: None))
data = gpg.Data(cbs=(None, write_cb, None, lambda: None))
try:
data.write(b'stuff')
except Exception as e:
@ -238,7 +238,7 @@ else:
def seek_cb(offset, whence, hook=None):
assert hook == cookie
return "wrong type"
data = core.Data(cbs=(None, None, seek_cb, release_cb, cookie))
data = gpg.Data(cbs=(None, None, seek_cb, release_cb, cookie))
try:
data.seek(0, os.SEEK_SET)
except Exception as e:
@ -248,7 +248,7 @@ else:
def seek_cb(offset, whence):
raise myException
data = core.Data(cbs=(None, None, seek_cb, lambda: None))
data = gpg.Data(cbs=(None, None, seek_cb, lambda: None))
try:
data.seek(0, os.SEEK_SET)
except Exception as e:

View File

@ -23,9 +23,9 @@ del absolute_import, print_function, unicode_literals
import io
import os
import tempfile
from gpg import core
import gpg
data = core.Data('Hello world!')
data = gpg.Data('Hello world!')
assert data.read() == b'Hello world!'
assert data.read() == b''
@ -33,29 +33,29 @@ data.seek(0, os.SEEK_SET)
assert data.read() == b'Hello world!'
assert data.read() == b''
data = core.Data(b'Hello world!')
data = gpg.Data(b'Hello world!')
assert data.read() == b'Hello world!'
data = core.Data(b'Hello world!', copy=False)
data = gpg.Data(b'Hello world!', copy=False)
assert data.read() == b'Hello world!'
data = core.Data()
data = gpg.Data()
data.write('Hello world!')
data.seek(0, os.SEEK_SET)
assert data.read() == b'Hello world!'
data = core.Data()
data = gpg.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 = gpg.Data()
data.write(binjunk)
data.seek(0, os.SEEK_SET)
assert data.read() == binjunk
data = core.Data()
data = gpg.Data()
data.set_file_name("foobar")
assert data.get_file_name() == "foobar"
@ -66,26 +66,26 @@ with tempfile.NamedTemporaryFile() as tmp:
tmp.seek(0)
# Open using name.
data = core.Data(file=tmp.name)
data = gpg.Data(file=tmp.name)
assert data.read() == binjunk
# Open using name, without copying.
if False:
# delayed reads are not yet supported
data = core.Data(file=tmp.name, copy=False)
data = gpg.Data(file=tmp.name, copy=False)
assert data.read() == binjunk
# Open using stream.
tmp.seek(0)
data = core.Data(file=tmp)
data = gpg.Data(file=tmp)
assert data.read() == binjunk
# Open using stream, offset, and length.
data = core.Data(file=tmp, offset=0, length=42)
data = gpg.Data(file=tmp, offset=0, length=42)
assert data.read() == binjunk[:42]
# Open using name, offset, and length.
data = core.Data(file=tmp.name, offset=23, length=42)
data = gpg.Data(file=tmp.name, offset=23, length=42)
assert data.read() == binjunk[23:23+42]
# Test callbacks.
@ -112,7 +112,7 @@ class DataObject(object):
do = DataObject()
cookie = object()
data = core.Data(cbs=(do.read, do.write, do.seek, do.release, cookie))
data = gpg.Data(cbs=(do.read, do.write, do.seek, do.release, cookie))
data.write('Hello world!')
data.seek(0, os.SEEK_SET)
assert data.read() == b'Hello world!'
@ -121,7 +121,7 @@ assert do.released
# Again, without the cookie.
do = DataObject()
data = core.Data(cbs=(do.read, do.write, do.seek, do.release))
data = gpg.Data(cbs=(do.read, do.write, do.seek, do.release))
data.write('Hello world!')
data.seek(0, os.SEEK_SET)
assert data.read() == b'Hello world!'

View File

@ -21,7 +21,6 @@ from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import gpg
from gpg import core, constants, errors
import support
def check_verify_result(result, summary, fpr, status):
@ -29,17 +28,17 @@ def check_verify_result(result, summary, fpr, status):
sig = result.signatures[0]
assert sig.summary == summary, "Unexpected signature summary"
assert sig.fpr == fpr
assert errors.GPGMEError(sig.status).getcode() == status
assert gpg.errors.GPGMEError(sig.status).getcode() == status
assert len(sig.notations) == 0
assert not sig.wrong_key_usage
assert sig.validity == constants.VALIDITY_FULL
assert errors.GPGMEError(sig.validity_reason).getcode() == errors.NO_ERROR
assert sig.validity == gpg.constants.VALIDITY_FULL
assert gpg.errors.GPGMEError(sig.validity_reason).getcode() == gpg.errors.NO_ERROR
support.init_gpgme(constants.PROTOCOL_OpenPGP)
c = core.Context()
support.init_gpgme(gpg.constants.PROTOCOL_OpenPGP)
c = gpg.Context()
source = core.Data(file=support.make_filename("cipher-2.asc"))
sink = core.Data()
source = gpg.Data(file=support.make_filename("cipher-2.asc"))
sink = gpg.Data()
c.op_decrypt_verify(source, sink)
result = c.op_decrypt_result()
@ -50,9 +49,9 @@ support.print_data(sink)
verify_result = c.op_verify_result()
check_verify_result(verify_result,
constants.SIGSUM_VALID | constants.SIGSUM_GREEN,
gpg.constants.SIGSUM_VALID | gpg.constants.SIGSUM_GREEN,
"A0FF4590BB6122EDEF6E3C542D727CC768697734",
errors.NO_ERROR)
gpg.errors.NO_ERROR)
# Idiomatic interface.
with gpg.Context() as c:
@ -63,14 +62,14 @@ with gpg.Context() as c:
assert plaintext.find(b'Wenn Sie dies lesen k') >= 0, \
'Plaintext not found'
check_verify_result(verify_result,
constants.SIGSUM_VALID | constants.SIGSUM_GREEN,
gpg.constants.SIGSUM_VALID | gpg.constants.SIGSUM_GREEN,
"A0FF4590BB6122EDEF6E3C542D727CC768697734",
errors.NO_ERROR)
gpg.errors.NO_ERROR)
try:
c.decrypt(open(support.make_filename("cipher-2.asc")),
verify=[alpha, bob])
except errors.MissingSignatures as e:
except gpg.errors.MissingSignatures as e:
assert len(e.missing) == 1
assert e.missing[0] == bob
else:

View File

@ -21,14 +21,13 @@ from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import gpg
from gpg import core, constants
import support
support.init_gpgme(constants.PROTOCOL_OpenPGP)
c = core.Context()
support.init_gpgme(gpg.constants.PROTOCOL_OpenPGP)
c = gpg.Context()
source = core.Data(file=support.make_filename("cipher-1.asc"))
sink = core.Data()
source = gpg.Data(file=support.make_filename("cipher-1.asc"))
sink = gpg.Data()
c.op_decrypt(source, sink)
result = c.op_decrypt_result()

View File

@ -23,7 +23,7 @@ del absolute_import, print_function, unicode_literals
import sys
import os
from gpg import core, constants
import gpg
import support
class KeyEditor(object):
@ -51,10 +51,10 @@ class KeyEditor(object):
return result
support.init_gpgme(constants.PROTOCOL_OpenPGP)
support.init_gpgme(gpg.constants.PROTOCOL_OpenPGP)
c = core.Context()
c.set_pinentry_mode(constants.PINENTRY_MODE_LOOPBACK)
c = gpg.Context()
c.set_pinentry_mode(gpg.constants.PINENTRY_MODE_LOOPBACK)
c.set_passphrase_cb(lambda *args: "abc")
c.set_armor(True)
@ -65,7 +65,7 @@ c.interact(c.get_key("A0FF4590BB6122EDEF6E3C542D727CC768697734", False),
assert editor.done
# The deprecated interface.
sink = core.Data()
sink = gpg.Data()
editor = KeyEditor()
c.op_edit(c.get_key("A0FF4590BB6122EDEF6E3C542D727CC768697734", False),
editor.edit_fnc, sink, sink)

View File

@ -22,7 +22,7 @@ del absolute_import, print_function, unicode_literals
import sys
import random
from gpg import core, constants
import gpg
import support
if len(sys.argv) == 2:
@ -30,8 +30,8 @@ if len(sys.argv) == 2:
else:
nbytes = 100000
support.init_gpgme(constants.PROTOCOL_OpenPGP)
c = core.Context()
support.init_gpgme(gpg.constants.PROTOCOL_OpenPGP)
c = gpg.Context()
ntoread = nbytes
def read_cb(amount):
@ -48,14 +48,14 @@ def write_cb(data):
nwritten += len(data)
return len(data)
source = core.Data(cbs=(read_cb, None, None, lambda: None))
sink = core.Data(cbs=(None, write_cb, None, lambda: None))
source = gpg.Data(cbs=(read_cb, None, None, lambda: None))
sink = gpg.Data(cbs=(None, write_cb, None, lambda: None))
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)
c.op_encrypt(keys, gpg.constants.ENCRYPT_ALWAYS_TRUST, source, sink)
result = c.op_encrypt_result()
assert not result.invalid_recipients, \
"Invalid recipient encountered: {}".format(result.invalid_recipients.fpr)

View File

@ -22,11 +22,10 @@ del absolute_import, print_function, unicode_literals
import sys
import gpg
from gpg import core, constants
import support
support.init_gpgme(constants.PROTOCOL_OpenPGP)
c = core.Context()
support.init_gpgme(gpg.constants.PROTOCOL_OpenPGP)
c = gpg.Context()
c.set_armor(True)
def check_result(r, typ):
@ -40,11 +39,11 @@ def check_result(r, typ):
if signature.type != typ:
sys.exit("Wrong type of signature created")
if signature.pubkey_algo != constants.PK_DSA:
if signature.pubkey_algo != gpg.constants.PK_DSA:
sys.exit("Wrong pubkey algorithm reported: {}".format(
signature.pubkey_algo))
if signature.hash_algo not in (constants.MD_SHA1, constants.MD_RMD160):
if signature.hash_algo not in (gpg.constants.MD_SHA1, gpg.constants.MD_RMD160):
sys.exit("Wrong hash algorithm reported: {}".format(
signature.hash_algo))
@ -60,17 +59,17 @@ keys.append(c.get_key("A0FF4590BB6122EDEF6E3C542D727CC768697734", False))
keys.append(c.get_key("D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2", False))
for recipients in (keys, []):
source = core.Data("Hallo Leute\n")
sink = core.Data()
source = gpg.Data("Hallo Leute\n")
sink = gpg.Data()
c.op_encrypt_sign(recipients, constants.ENCRYPT_ALWAYS_TRUST, source, sink)
c.op_encrypt_sign(recipients, gpg.constants.ENCRYPT_ALWAYS_TRUST, source, sink)
result = c.op_encrypt_result()
assert not result.invalid_recipients, \
"Invalid recipient encountered: {}".format(
result.invalid_recipients.fpr)
result = c.op_sign_result()
check_result(result, constants.SIG_MODE_NORMAL)
check_result(result, gpg.constants.SIG_MODE_NORMAL)
support.print_data(sink)
@ -83,7 +82,7 @@ with gpg.Context(armor=True) as c:
always_trust=True)
assert len(ciphertext) > 0
assert ciphertext.find(b'BEGIN PGP MESSAGE') > 0, 'Marker not found'
check_result(sig_result, constants.SIG_MODE_NORMAL)
check_result(sig_result, gpg.constants.SIG_MODE_NORMAL)
c.signers = [c.get_key(support.sign_only, True)]
c.encrypt(message, recipients=keys, always_trust=True)

View File

@ -22,18 +22,17 @@ del absolute_import, print_function, unicode_literals
import os
import gpg
from gpg import core, constants
import support
support.init_gpgme(constants.PROTOCOL_OpenPGP)
support.init_gpgme(gpg.constants.PROTOCOL_OpenPGP)
for passphrase in ("abc", b"abc"):
c = core.Context()
c = gpg.Context()
c.set_armor(True)
c.set_pinentry_mode(constants.PINENTRY_MODE_LOOPBACK)
c.set_pinentry_mode(gpg.constants.PINENTRY_MODE_LOOPBACK)
source = core.Data("Hallo Leute\n")
cipher = core.Data()
source = gpg.Data("Hallo Leute\n")
cipher = gpg.Data()
passphrase_cb_called = 0
def passphrase_cb(hint, desc, prev_bad, hook=None):
@ -48,11 +47,11 @@ for passphrase in ("abc", b"abc"):
"Callback called {} times".format(passphrase_cb_called)
support.print_data(cipher)
c = core.Context()
c = gpg.Context()
c.set_armor(True)
c.set_pinentry_mode(constants.PINENTRY_MODE_LOOPBACK)
c.set_pinentry_mode(gpg.constants.PINENTRY_MODE_LOOPBACK)
c.set_passphrase_cb(passphrase_cb, None)
plain = core.Data()
plain = gpg.Data()
cipher.seek(0, os.SEEK_SET)
c.op_decrypt(cipher, plain)

View File

@ -21,21 +21,20 @@ from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import gpg
from gpg import core, constants
import support
support.init_gpgme(constants.PROTOCOL_OpenPGP)
c = core.Context()
support.init_gpgme(gpg.constants.PROTOCOL_OpenPGP)
c = gpg.Context()
c.set_armor(True)
source = core.Data("Hallo Leute\n")
sink = core.Data()
source = gpg.Data("Hallo Leute\n")
sink = gpg.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)
c.op_encrypt(keys, gpg.constants.ENCRYPT_ALWAYS_TRUST, source, sink)
result = c.op_encrypt_result()
assert not result.invalid_recipients, \
"Invalid recipients: {}".format(", ".join(r.fpr for r in result.recipients))

View File

@ -20,14 +20,14 @@
from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
from gpg import core, constants
import gpg
import support
support.init_gpgme(constants.PROTOCOL_OpenPGP)
c = core.Context()
support.init_gpgme(gpg.constants.PROTOCOL_OpenPGP)
c = gpg.Context()
c.set_armor(True)
sink = core.Data()
sink = gpg.Data()
c.op_export_ext(['Alpha', 'Bob'], 0, sink)
support.print_data(sink)
@ -35,6 +35,6 @@ support.print_data(sink)
keys = []
keys.append(c.get_key("0x68697734", False)) # Alpha
keys.append(c.get_key("0xA9E3B0B2", False)) # Bob
sink = core.Data()
sink = gpg.Data()
c.op_export_keys(keys, 0, sink)
support.print_data(sink)

View File

@ -21,24 +21,24 @@ from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import os
from gpg import core, constants
import gpg
import support
testname = "abcde12345"
support.init_gpgme(constants.PROTOCOL_OpenPGP)
c = core.Context()
support.init_gpgme(gpg.constants.PROTOCOL_OpenPGP)
c = gpg.Context()
c.set_armor(True)
source = core.Data("Hallo Leute\n")
source = gpg.Data("Hallo Leute\n")
source.set_file_name(testname)
cipher = core.Data()
plain = core.Data()
cipher = gpg.Data()
plain = gpg.Data()
keys = []
keys.append(c.get_key("A0FF4590BB6122EDEF6E3C542D727CC768697734", False))
c.op_encrypt(keys, constants.ENCRYPT_ALWAYS_TRUST, source, cipher)
c.op_encrypt(keys, gpg.constants.ENCRYPT_ALWAYS_TRUST, source, cipher)
cipher.seek(0, os.SEEK_SET)
c.op_decrypt(cipher, plain)
result = c.op_decrypt_result()

View File

@ -20,7 +20,7 @@
from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
from gpg import core, constants
import gpg
import support
def check_result(result, fpr, secret):
@ -67,13 +67,13 @@ def check_result(result, fpr, secret):
assert len(result.imports) == 1 or fpr == result.imports[1].fpr
assert result.imports[0].result == 0
support.init_gpgme(constants.PROTOCOL_OpenPGP)
c = core.Context()
support.init_gpgme(gpg.constants.PROTOCOL_OpenPGP)
c = gpg.Context()
c.op_import(core.Data(file=support.make_filename("pubkey-1.asc")))
c.op_import(gpg.Data(file=support.make_filename("pubkey-1.asc")))
result = c.op_import_result()
check_result(result, "ADAB7FCC1F4DE2616ECFA402AF82244F9CD9FD55", False)
c.op_import(core.Data(file=support.make_filename("seckey-1.asc")))
c.op_import(gpg.Data(file=support.make_filename("seckey-1.asc")))
result = c.op_import_result()
check_result(result, "ADAB7FCC1F4DE2616ECFA402AF82244F9CD9FD55", True)

View File

@ -21,11 +21,10 @@ from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import gpg
from gpg import core, constants
import support
support.init_gpgme(constants.PROTOCOL_OpenPGP)
c = core.Context()
support.init_gpgme(gpg.constants.PROTOCOL_OpenPGP)
c = gpg.Context()
# Check expration of keys. This test assumes three subkeys of which
# 2 are expired; it is used with the "Whisky" test key. It has
@ -109,7 +108,7 @@ def check_global(key, uids, n_subkeys):
assert key.can_sign, "Key unexpectedly unusable for signing"
assert key.can_certify, "Key unexpectedly unusable for certifications"
assert not key.secret, "Key unexpectedly secret"
assert not key.protocol != constants.PROTOCOL_OpenPGP, \
assert not key.protocol != gpg.constants.PROTOCOL_OpenPGP, \
"Key has unexpected protocol: {}".format(key.protocol)
assert not key.issuer_serial, \
"Key unexpectedly carries issuer serial: {}".format(key.issuer_serial)
@ -120,10 +119,10 @@ def check_global(key, uids, n_subkeys):
# Only key Alfa is trusted
assert key.uids[0].name == 'Alfa Test' \
or key.owner_trust == constants.VALIDITY_UNKNOWN, \
or key.owner_trust == gpg.constants.VALIDITY_UNKNOWN, \
"Key has unexpected owner trust: {}".format(key.owner_trust)
assert key.uids[0].name != 'Alfa Test' \
or key.owner_trust == constants.VALIDITY_ULTIMATE, \
or key.owner_trust == gpg.constants.VALIDITY_ULTIMATE, \
"Key has unexpected owner trust: {}".format(key.owner_trust)
assert len(key.subkeys) - 1 == n_subkeys, \
@ -154,8 +153,8 @@ def check_subkey(fpr, which, subkey):
assert not subkey.secret, which + " key unexpectedly secret"
assert not subkey.is_cardkey, "Public key marked as card key"
assert not subkey.card_number, "Public key with card number set"
assert not subkey.pubkey_algo != (constants.PK_DSA if which == "Primary"
else constants.PK_ELG_E), \
assert not subkey.pubkey_algo != (gpg.constants.PK_DSA if which == "Primary"
else gpg.constants.PK_ELG_E), \
which + " key has unexpected public key algo: {}".\
format(subkey.pubkey_algo)
assert subkey.length == 1024, \
@ -170,10 +169,10 @@ def check_subkey(fpr, which, subkey):
def check_uid(which, ref, uid):
assert not uid.revoked, which + " user ID unexpectedly revoked"
assert not uid.invalid, which + " user ID unexpectedly invalid"
assert uid.validity == (constants.VALIDITY_UNKNOWN
assert uid.validity == (gpg.constants.VALIDITY_UNKNOWN
if uid.name.split()[0]
not in {'Alfa', 'Alpha', 'Alice'} else
constants.VALIDITY_ULTIMATE), \
gpg.constants.VALIDITY_ULTIMATE), \
which + " user ID has unexpectedly validity: {}".format(uid.validity)
assert not uid.signatures, which + " user ID unexpectedly signed"
assert uid.name == ref[0], \

View File

@ -21,19 +21,19 @@ from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import os
from gpg import core, constants
import gpg
import support
expected_notations = {
"laughing@me": ("Just Squeeze Me", constants.SIG_NOTATION_HUMAN_READABLE),
"laughing@me": ("Just Squeeze Me", gpg.constants.SIG_NOTATION_HUMAN_READABLE),
"preferred-email-encoding@pgp.com": ("pgpmime",
constants.SIG_NOTATION_HUMAN_READABLE
| constants.SIG_NOTATION_CRITICAL),
gpg.constants.SIG_NOTATION_HUMAN_READABLE
| gpg.constants.SIG_NOTATION_CRITICAL),
None: ("http://www.gnu.org/policy/", 0),
}
# GnuPG prior to 2.1.13 did not report the critical flag correctly.
with core.Context() as c:
with gpg.Context() as c:
version = c.engine_info.version
have_correct_sig_data = not (version.startswith("1.")
or version.startswith("2.0.")
@ -55,26 +55,26 @@ def check_result(result):
assert r.value == value, \
"Expected {!r}, got {!r}".format(value, r.value)
assert r.human_readable \
== bool(flags&constants.SIG_NOTATION_HUMAN_READABLE)
== bool(flags & gpg.constants.SIG_NOTATION_HUMAN_READABLE)
assert r.critical \
== (bool(flags&constants.SIG_NOTATION_CRITICAL)
== (bool(flags & gpg.constants.SIG_NOTATION_CRITICAL)
if have_correct_sig_data else False)
assert len(expected_notations) == 0
support.init_gpgme(constants.PROTOCOL_OpenPGP)
support.init_gpgme(gpg.constants.PROTOCOL_OpenPGP)
source = core.Data("Hallo Leute\n")
signed = core.Data()
source = gpg.Data("Hallo Leute\n")
signed = gpg.Data()
c = core.Context()
c = gpg.Context()
for name, (value, flags) in expected_notations.items():
c.sig_notation_add(name, value, flags)
c.op_sign(source, signed, constants.SIG_MODE_NORMAL)
c.op_sign(source, signed, gpg.constants.SIG_MODE_NORMAL)
signed.seek(0, os.SEEK_SET)
sink = core.Data()
sink = gpg.Data()
c.op_verify(signed, None, sink)
result = c.op_verify_result()
check_result(result)

View File

@ -22,7 +22,6 @@ del absolute_import, print_function, unicode_literals
import os
import gpg
from gpg import core, constants
import support
def fail(msg):
@ -39,11 +38,11 @@ def check_result(r, typ):
if signature.type != typ:
fail("Wrong type of signature created")
if signature.pubkey_algo != constants.PK_DSA:
if signature.pubkey_algo != gpg.constants.PK_DSA:
fail("Wrong pubkey algorithm reported: {}".format(
signature.pubkey_algo))
if signature.hash_algo != constants.MD_SHA1:
if signature.hash_algo != gpg.constants.MD_SHA1:
fail("Wrong hash algorithm reported: {}".format(
signature.hash_algo))
@ -55,38 +54,38 @@ def check_result(r, typ):
fail("Wrong fingerprint reported: {}".format(signature.fpr))
support.init_gpgme(constants.PROTOCOL_OpenPGP)
c = core.Context()
support.init_gpgme(gpg.constants.PROTOCOL_OpenPGP)
c = gpg.Context()
c.set_textmode(True)
c.set_armor(True)
source = core.Data("Hallo Leute\n")
sink = core.Data()
source = gpg.Data("Hallo Leute\n")
sink = gpg.Data()
c.op_sign(source, sink, constants.SIG_MODE_NORMAL)
c.op_sign(source, sink, gpg.constants.SIG_MODE_NORMAL)
result = c.op_sign_result()
check_result(result, constants.SIG_MODE_NORMAL)
check_result(result, gpg.constants.SIG_MODE_NORMAL)
support.print_data(sink)
# Now a detached signature.
source.seek(0, os.SEEK_SET)
sink = core.Data()
sink = gpg.Data()
c.op_sign(source, sink, constants.SIG_MODE_DETACH)
c.op_sign(source, sink, gpg.constants.SIG_MODE_DETACH)
result = c.op_sign_result()
check_result(result, constants.SIG_MODE_DETACH)
check_result(result, gpg.constants.SIG_MODE_DETACH)
support.print_data(sink)
# And finally a cleartext signature. */
source.seek(0, os.SEEK_SET)
sink = core.Data()
sink = gpg.Data()
c.op_sign(source, sink, constants.SIG_MODE_CLEAR)
c.op_sign(source, sink, gpg.constants.SIG_MODE_CLEAR)
result = c.op_sign_result()
check_result(result, constants.SIG_MODE_CLEAR)
check_result(result, gpg.constants.SIG_MODE_CLEAR)
support.print_data(sink)
# Idiomatic interface.

View File

@ -21,7 +21,6 @@ from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import gpg
from gpg import core, constants
import support
def fail(msg):
@ -38,11 +37,11 @@ def check_result(r, typ):
if signature.type != typ:
fail("Wrong type of signature created")
if signature.pubkey_algo != constants.PK_DSA:
if signature.pubkey_algo != gpg.constants.PK_DSA:
fail("Wrong pubkey algorithm reported: {}".format(
signature.pubkey_algo))
if signature.hash_algo != constants.MD_SHA1:
if signature.hash_algo != gpg.constants.MD_SHA1:
fail("Wrong hash algorithm reported: {}".format(
signature.hash_algo))
@ -55,8 +54,8 @@ def check_result(r, typ):
fail("Wrong fingerprint reported: {}".format(signature.fpr))
support.init_gpgme(constants.PROTOCOL_OpenPGP)
c = core.Context()
support.init_gpgme(gpg.constants.PROTOCOL_OpenPGP)
c = gpg.Context()
c.set_textmode(True)
c.set_armor(True)
@ -69,10 +68,10 @@ c.op_keylist_end()
c.signers_add(keys[0])
c.signers_add(keys[1])
for mode in (constants.SIG_MODE_NORMAL, constants.SIG_MODE_DETACH,
constants.SIG_MODE_CLEAR):
source = core.Data("Hallo Leute\n")
sink = core.Data()
for mode in (gpg.constants.SIG_MODE_NORMAL, gpg.constants.SIG_MODE_DETACH,
gpg.constants.SIG_MODE_CLEAR):
source = gpg.Data("Hallo Leute\n")
sink = gpg.Data()
c.op_sign(source, sink, mode)
@ -84,15 +83,15 @@ for mode in (constants.SIG_MODE_NORMAL, constants.SIG_MODE_DETACH,
with gpg.Context(armor=True, textmode=True, signers=keys) as c:
message = "Hallo Leute\n".encode()
signed, result = c.sign(message)
check_result(result, constants.SIG_MODE_NORMAL)
check_result(result, gpg.constants.SIG_MODE_NORMAL)
assert signed.find(b'BEGIN PGP MESSAGE') > 0, 'Message not found'
signed, result = c.sign(message, mode=constants.SIG_MODE_DETACH)
check_result(result, constants.SIG_MODE_DETACH)
signed, result = c.sign(message, mode=gpg.constants.SIG_MODE_DETACH)
check_result(result, gpg.constants.SIG_MODE_DETACH)
assert signed.find(b'BEGIN PGP SIGNATURE') > 0, 'Signature not found'
signed, result = c.sign(message, mode=constants.SIG_MODE_CLEAR)
check_result(result, constants.SIG_MODE_CLEAR)
signed, result = c.sign(message, mode=gpg.constants.SIG_MODE_CLEAR)
check_result(result, gpg.constants.SIG_MODE_CLEAR)
assert signed.find(b'BEGIN PGP SIGNED MESSAGE') > 0, 'Message not found'
assert signed.find(message) > 0, 'Message content not found'
assert signed.find(b'BEGIN PGP SIGNATURE') > 0, 'Signature not found'

View File

@ -20,11 +20,11 @@
from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
from gpg import core, constants
import gpg
import support
support.init_gpgme(constants.PROTOCOL_OpenPGP)
c = core.Context()
support.init_gpgme(gpg.constants.PROTOCOL_OpenPGP)
c = gpg.Context()
def dump_item(item):
print("l={} k={} t={} o={} v={} u={}".format(

View File

@ -23,7 +23,6 @@ del absolute_import, print_function, unicode_literals
import sys
import os
import gpg
from gpg import core, constants, errors
import support
test_text1 = b"Just GNU it!\n"
@ -67,7 +66,7 @@ def check_result(result, summary, validity, fpr, status, notation):
"Unexpected signature summary: {}, want: {}".format(sig.summary,
summary)
assert sig.fpr == fpr
assert errors.GPGMEError(sig.status).getcode() == status
assert gpg.errors.GPGMEError(sig.status).getcode() == status
if notation:
expected_notations = {
@ -96,50 +95,50 @@ def check_result(result, summary, validity, fpr, status, notation):
assert sig.validity == validity, \
"Unexpected signature validity: {}, want: {}".format(
sig.validity, validity)
assert errors.GPGMEError(sig.validity_reason).getcode() == errors.NO_ERROR
assert gpg.errors.GPGMEError(sig.validity_reason).getcode() == gpg.errors.NO_ERROR
support.init_gpgme(constants.PROTOCOL_OpenPGP)
c = core.Context()
support.init_gpgme(gpg.constants.PROTOCOL_OpenPGP)
c = gpg.Context()
c.set_armor(True)
# Checking a valid message.
text = core.Data(test_text1)
sig = core.Data(test_sig1)
text = gpg.Data(test_text1)
sig = gpg.Data(test_sig1)
c.op_verify(sig, text, None)
result = c.op_verify_result()
check_result(result, constants.SIGSUM_VALID | constants.SIGSUM_GREEN,
constants.VALIDITY_FULL,
check_result(result, gpg.constants.SIGSUM_VALID | gpg.constants.SIGSUM_GREEN,
gpg.constants.VALIDITY_FULL,
"A0FF4590BB6122EDEF6E3C542D727CC768697734",
errors.NO_ERROR, True)
gpg.errors.NO_ERROR, True)
# Checking a manipulated message.
text = core.Data(test_text1f)
text = gpg.Data(test_text1f)
sig.seek(0, os.SEEK_SET)
c.op_verify(sig, text, None)
result = c.op_verify_result()
check_result(result, constants.SIGSUM_RED, constants.VALIDITY_UNKNOWN,
"2D727CC768697734", errors.BAD_SIGNATURE, False)
check_result(result, gpg.constants.SIGSUM_RED, gpg.constants.VALIDITY_UNKNOWN,
"2D727CC768697734", gpg.errors.BAD_SIGNATURE, False)
# Checking a normal signature.
text = core.Data()
sig = core.Data(test_sig2)
text = gpg.Data()
sig = gpg.Data(test_sig2)
c.op_verify(sig, None, text)
result = c.op_verify_result()
check_result(result, constants.SIGSUM_VALID | constants.SIGSUM_GREEN,
constants.VALIDITY_FULL,
check_result(result, gpg.constants.SIGSUM_VALID | gpg.constants.SIGSUM_GREEN,
gpg.constants.VALIDITY_FULL,
"A0FF4590BB6122EDEF6E3C542D727CC768697734",
errors.NO_ERROR, False)
gpg.errors.NO_ERROR, False)
# Checking an invalid message.
text = core.Data()
sig = core.Data(double_plaintext_sig)
text = gpg.Data()
sig = gpg.Data(double_plaintext_sig)
try:
c.op_verify(sig, None, text)
except Exception as e:
assert type(e) == errors.GPGMEError
assert e.getcode() == errors.BAD_DATA
assert type(e) == gpg.errors.GPGMEError
assert e.getcode() == gpg.errors.BAD_DATA
else:
assert False, "Expected an error but got none."
@ -148,35 +147,35 @@ else:
with gpg.Context(armor=True) as c:
# Checking a valid message.
_, result = c.verify(test_text1, test_sig1)
check_result(result, constants.SIGSUM_VALID | constants.SIGSUM_GREEN,
constants.VALIDITY_FULL,
check_result(result, gpg.constants.SIGSUM_VALID | gpg.constants.SIGSUM_GREEN,
gpg.constants.VALIDITY_FULL,
"A0FF4590BB6122EDEF6E3C542D727CC768697734",
errors.NO_ERROR, True)
gpg.errors.NO_ERROR, True)
# Checking a manipulated message.
try:
c.verify(test_text1f, test_sig1)
except errors.BadSignatures as e:
check_result(e.result, constants.SIGSUM_RED,
constants.VALIDITY_UNKNOWN,
"2D727CC768697734", errors.BAD_SIGNATURE, False)
except gpg.errors.BadSignatures as e:
check_result(e.result, gpg.constants.SIGSUM_RED,
gpg.constants.VALIDITY_UNKNOWN,
"2D727CC768697734", gpg.errors.BAD_SIGNATURE, False)
else:
assert False, "Expected an error but got none."
# Checking a normal signature.
sig = core.Data(test_sig2)
sig = gpg.Data(test_sig2)
data, result = c.verify(test_sig2)
check_result(result, constants.SIGSUM_VALID | constants.SIGSUM_GREEN,
constants.VALIDITY_FULL,
check_result(result, gpg.constants.SIGSUM_VALID | gpg.constants.SIGSUM_GREEN,
gpg.constants.VALIDITY_FULL,
"A0FF4590BB6122EDEF6E3C542D727CC768697734",
errors.NO_ERROR, False)
gpg.errors.NO_ERROR, False)
assert data == test_text1
# Checking an invalid message.
try:
c.verify(double_plaintext_sig)
except errors.GPGMEError as e:
assert e.getcode() == errors.BAD_DATA
except gpg.errors.GPGMEError as e:
assert e.getcode() == gpg.errors.BAD_DATA
else:
assert False, "Expected an error but got none."
@ -188,7 +187,7 @@ with gpg.Context(armor=True) as c:
try:
c.verify(test_text1, test_sig1, verify=[alpha, bob])
except errors.MissingSignatures as e:
except gpg.errors.MissingSignatures as e:
assert len(e.missing) == 1
assert e.missing[0] == bob
else:

View File

@ -21,16 +21,16 @@ from __future__ import absolute_import, print_function, unicode_literals
del absolute_import, print_function, unicode_literals
import time
from gpg import core, constants, errors
import gpg
import support
support.init_gpgme(constants.PROTOCOL_OpenPGP)
c = core.Context()
support.init_gpgme(gpg.constants.PROTOCOL_OpenPGP)
c = gpg.Context()
c.set_armor(True)
# Checking a message without a signature.
sig = core.Data("foo\n")
text = core.Data()
sig = gpg.Data("foo\n")
text = gpg.Data()
c.op_verify_start(sig, None, text)
try:
@ -40,6 +40,6 @@ try:
break
time.sleep(0.1)
except Exception as e:
assert e.getcode() == errors.NO_DATA
assert e.getcode() == gpg.errors.NO_DATA
else:
assert False, "Expected an error, got none"

View File

@ -17,9 +17,9 @@
# 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/>.
from gpg import core
import gpg
d0 = core.Data()
d0 = gpg.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"
assert hasattr(gpg.Data, 'seek'), "Generated wrapper functions are not shared"