python: Port more tests.

* lang/python/pyme/core.py (Context.op_keylist_all): Add missing
'op_keylist_end'.
(Context.op_trustlist_all): Fix function. Add missing
'op_trustlist_end'.
* lang/python/tests/Makefile.am (pytests): Add new files.
* lang/python/tests/t-import.py: New file.
* lang/python/tests/t-keylist.py: Likewise.
* lang/python/tests/t-trustlist.py: Check alternate interface.

Signed-off-by: Justus Winter <justus@g10code.com>
This commit is contained in:
Justus Winter 2016-06-06 12:49:11 +02:00
parent 57b5168552
commit 1bff47ee58
5 changed files with 335 additions and 6 deletions

View File

@ -255,6 +255,7 @@ class Context(GpgmeWrapper):
while key:
yield key
key = self.op_keylist_next()
self.op_keylist_end()
def op_keylist_next(self):
"""Returns the next key in the list created
@ -285,10 +286,11 @@ class Context(GpgmeWrapper):
def op_trustlist_all(self, *args, **kwargs):
self.op_trustlist_start(*args, **kwargs)
trust = self.ctx.op_trustlist_next()
trust = self.op_trustlist_next()
while trust:
yield trust
trust = self.ctx.op_trustlist_next()
trust = self.op_trustlist_next()
self.op_trustlist_end()
def op_trustlist_next(self):
"""Returns the next trust item in the list created

View File

@ -42,8 +42,10 @@ py_tests = t-wrapper.py \
t-decrypt-verify.py \
t-sig-notation.py \
t-export.py \
t-import.py \
t-trustlist.py \
t-edit.py \
t-keylist.py \
t-wait.py \
t-encrypt-large.py \
t-file-name.py \

76
lang/python/tests/t-import.py Executable file
View File

@ -0,0 +1,76 @@
#!/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/>.
from pyme import core, constants
import support
def check_result(result, fpr, secret):
assert result.considered == 1 or (secret and result.considered == 3)
assert result.no_user_id == 0
assert not ((secret and result.imported != 0)
or (not secret and (result.imported != 0
and result.imported != 1)))
assert result.imported_rsa == 0
assert not ((secret and (result.unchanged != 0 and result.unchanged != 1))
or (not secret and ((result.imported == 0
and result.unchanged != 1)
or (result.imported == 1
and result.unchanged != 0))))
assert result.new_user_ids == 0
assert result.new_sub_keys == 0
assert not ((secret
and ((result.secret_imported == 0
and result.new_signatures != 0)
or (result.secret_imported == 1
and result.new_signatures > 1)))
or (not secret and result.new_signatures != 0))
assert result.new_revocations == 0
assert not ((secret and result.secret_read != 1 and result.secret_read != 3)
or (not secret and result.secret_read != 0))
assert not ((secret and result.secret_imported != 0
and result.secret_imported != 1
and result.secret_imported != 2)
or (not secret and result.secret_imported != 0))
assert not ((secret
and ((result.secret_imported == 0
and result.secret_unchanged != 1
and result.secret_unchanged != 2)
or (result.secret_imported == 1
and result.secret_unchanged != 0)))
or (not secret and result.secret_unchanged != 0))
assert result.not_imported == 0
if secret:
assert not (len(result.imports) in (0, 3))
else:
assert not (len(result.imports) in (0, 2))
assert fpr == result.imports[0].fpr
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()
c.op_import(core.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")))
result = c.op_import_result()
check_result(result, "ADAB7FCC1F4DE2616ECFA402AF82244F9CD9FD55", True)

243
lang/python/tests/t-keylist.py Executable file
View File

@ -0,0 +1,243 @@
#!/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 pyme
from pyme import core, constants
import support
support.init_gpgme(constants.PROTOCOL_OpenPGP)
c = core.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
# already been checked that these 3 subkeys are available.
def check_whisky(name, key):
sub1 = key.subkeys[2]
sub2 = key.subkeys[3]
assert sub1.expired and sub2.expired, \
"Subkey of `{}' not flagged as expired".format(name)
assert sub1.expires == 1129636886 and sub2.expires == 1129636939, \
"Subkey of `{}' has wrong expiration date".format(name)
keys = [
[ "A0FF4590BB6122EDEF6E3C542D727CC768697734", "6AE6D7EE46A871F8",
[ [ "Alfa Test", "demo key", "alfa@example.net" ],
[ "Alpha Test", "demo key", "alpha@example.net" ],
[ "Alice", "demo key", "" ] ], 1 ],
[ "D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2", "5381EA4EE29BA37F",
[ [ "Bob", "demo key", "" ],
[ "Bravo Test", "demo key", "bravo@example.net" ] ], 1 ],
[ "61EE841A2A27EB983B3B3C26413F4AF31AFDAB6C", "E71E72ACBC43DA60",
[ [ "Charlie Test", "demo key", "charlie@example.net" ] ], 1 ],
[ "6560C59C43D031C54D7C588EEBA9F240EB9DC9E6", "06F22880B0C45424",
[ [ "Delta Test", "demo key", "delta@example.net" ] ], 1 ],
[ "3531152DE293E26A07F504BC318C1FAEFAEF6D1B", "B5C79E1A7272144D",
[ [ "Echelon", "demo key", "" ],
[ "Echo Test", "demo key", "echo@example.net" ],
[ "Eve", "demo key", "" ] ], 1 ],
[ "56D33268F7FE693FBB594762D4BF57F37372E243", "0A32EE79EE45198E",
[ [ "Foxtrot Test", "demo key", "foxtrot@example.net" ] ], 1 ],
[ "C9C07DCC6621B9FB8D071B1D168410A48FC282E6", "247491CC9DCAD354",
[ [ "Golf Test", "demo key", "golf@example.net" ] ], 1 ],
[ "9E91CBB11E4D4135583EF90513DB965534C6E3F1", "76E26537D622AD0A",
[ [ "Hotel Test", "demo key", "hotel@example.net" ] ], 1 ],
[ "CD538D6CC9FB3D745ECDA5201FE8FC6F04259677", "C1C8EFDE61F76C73",
[ [ "India Test", "demo key", "india@example.net" ] ], 1 ],
[ "F8F1EDC73995AB739AD54B380C820C71D2699313", "BD0B108735F8F136",
[ [ "Juliet Test", "demo key", "juliet@example.net" ] ], 1 ],
[ "3FD11083779196C2ECDD9594AD1B0FAD43C2D0C7", "86CBB34A9AF64D02",
[ [ "Kilo Test", "demo key", "kilo@example.net" ] ], 1 ],
[ "1DDD28CEF714F5B03B8C246937CAB51FB79103F8", "0363B449FE56350C",
[ [ "Lima Test", "demo key", "lima@example.net" ] ], 1 ],
[ "2686AA191A278013992C72EBBE794852BE5CF886", "5F600A834F31EAE8",
[ [ "Mallory", "demo key", "" ],
[ "Mike Test", "demo key", "mike@example.net" ] ], 1 ],
[ "5AB9D6D7BAA1C95B3BAA3D9425B00FD430CEC684", "4C1D63308B70E472",
[ [ "November Test", "demo key", "november@example.net" ] ], 1 ],
[ "43929E89F8F79381678CAE515F6356BA6D9732AC", "FF0785712681619F",
[ [ "Oscar Test", "demo key", "oscar@example.net" ] ], 1 ],
[ "6FAA9C201E5E26DCBAEC39FD5D15E01D3FF13206", "2764E18263330D9C",
[ [ "Papa test", "demo key", "papa@example.net" ] ], 1 ],
[ "A7969DA1C3297AA96D49843F1C67EC133C661C84", "6CDCFC44A029ACF4",
[ [ "Quebec Test", "demo key", "quebec@example.net" ] ], 1 ],
[ "38FBE1E4BF6A5E1242C8F6A13BDBEDB1777FBED3", "9FAB805A11D102EA",
[ [ "Romeo Test", "demo key", "romeo@example.net" ] ], 1 ],
[ "045B2334ADD69FC221076841A5E67F7FA3AE3EA1", "93B88B0F0F1B50B4",
[ [ "Sierra Test", "demo key", "sierra@example.net" ] ], 1 ],
[ "ECAC774F4EEEB0620767044A58CB9A4C85A81F38", "97B60E01101C0402",
[ [ "Tango Test", "demo key", "tango@example.net" ] ], 1 ],
[ "0DBCAD3F08843B9557C6C4D4A94C0F75653244D6", "93079B915522BDB9",
[ [ "Uniform Test", "demo key", "uniform@example.net" ] ], 1 ],
[ "E8143C489C8D41124DC40D0B47AF4B6961F04784", "04071FB807287134",
[ [ "Victor Test", "demo key", "victor@example.org" ] ], 1 ],
[ "E8D6C90B683B0982BD557A99DEF0F7B8EC67DBDE", "D7FBB421FD6E27F6",
[ [ "Whisky Test", "demo key", "whisky@example.net" ] ], 3,
check_whisky ],
[ "04C1DF62EFA0EBB00519B06A8979A6C5567FB34A", "5CC6F87F41E408BE",
[ [ "XRay Test", "demo key", "xray@example.net" ] ], 1 ],
[ "ED9B316F78644A58D042655A9EEF34CD4B11B25F", "5ADFD255F7B080AD",
[ [ "Yankee Test", "demo key", "yankee@example.net" ] ], 1 ],
[ "23FD347A419429BACCD5E72D6BC4778054ACD246", "EF9DC276A172C881",
[ [ "Zulu Test", "demo key", "zulu@example.net" ] ], 1 ],
]
def check_global(i, key, uids, n_subkeys):
assert not key.revoked, "Key unexpectedly revoked"
assert not key.expired, "Key unexpectedly expired"
assert not key.disabled, "Key unexpectedly disabled"
assert not key.invalid, "Key unexpectedly invalid"
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, \
"Key has unexpected protocol: {}".format(key.protocol)
assert not key.issuer_serial, \
"Key unexpectedly carries issuer serial: {}".format(key.issuer_serial)
assert not key.issuer_name, \
"Key unexpectedly carries issuer name: {}".format(key.issuer_name)
assert not key.chain_id, \
"Key unexpectedly carries chain ID: {}".format(key.chain_id)
# Only key Alfa is trusted
assert i == 0 or key.owner_trust == constants.VALIDITY_UNKNOWN, \
"Key has unexpected owner trust: {}".format(key.owner_trust)
assert i != 0 or key.owner_trust == constants.VALIDITY_ULTIMATE, \
"Key has unexpected owner trust: {}".format(key.owner_trust)
assert len(key.subkeys) - 1 == n_subkeys, \
"Key `{}' has unexpected number of subkeys".format(uids[0][0])
def check_subkey(fpr, which, subkey):
assert not subkey.revoked, which + " key unexpectedly revoked"
assert not subkey.expired, which + " key unexpectedly expired"
assert not subkey.disabled, which + " key unexpectedly disabled"
assert not subkey.invalid, which + " key unexpectedly invalid"
if which == "Primary":
assert not subkey.can_encrypt, \
which + " key unexpectedly usable for encryption"
assert subkey.can_sign, \
which + " key unexpectedly unusable for signing"
assert subkey.can_certify, \
which + " key unexpectedly unusable for certifications"
else:
assert subkey.can_encrypt, \
which + " key unexpectedly unusable for encryption"
assert not subkey.can_sign, \
which + " key unexpectedly usable for signing"
assert not subkey.can_certify, \
which + " key unexpectedly usable for certifications"
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), \
which + " key has unexpected public key algo: {}".\
format(subkey.pubkey_algo)
assert subkey.length == 1024, \
which + " key has unexpected length: {}".format(subkey.length)
assert fpr.endswith(subkey.keyid), \
which + " key has unexpected key ID: {}".format(subkey.keyid)
assert which == "Secondary" or subkey.fpr == fpr, \
which + " key has unexpected fingerprint: {}".format(subkey.fpr)
assert not subkey.expires, \
which + " key unexpectedly expires: {}".format(subkey.expires)
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
if uid.name.split()[0]
not in {'Alfa', 'Alpha', 'Alice'} else
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], \
"Unexpected name in {} user ID: {!r}".format(which.lower(), uid.name)
assert uid.comment == ref[1], \
"Unexpected comment in {} user ID: {!r}".format(which.lower(),
uid.comment)
assert uid.email == ref[2], \
"Unexpected email in {} user ID: {!r}".format(which.lower(), uid.email)
i = 0
c.op_keylist_start(None, False)
key = c.op_keylist_next ()
while key:
try:
if len(keys[i]) == 4:
fpr, sec_keyid, uids, n_subkeys = keys[i]
misc_check = None
else:
fpr, sec_keyid, uids, n_subkeys, misc_check = keys[i]
except IndexError:
# There are more keys. We don't check for that.
break
# Global key flags.
check_global(i, key, uids, n_subkeys)
check_subkey(fpr, "Primary", key.subkeys[0])
check_subkey(sec_keyid, "Secondary", key.subkeys[1])
assert len(key.uids) == len(uids)
check_uid("First", uids[0], key.uids[0])
if len(key.uids) > 1:
check_uid("Second", uids[1], key.uids[1])
if len(key.uids) > 2:
check_uid("Third", uids[2], key.uids[2])
if misc_check:
misc_check (uids[0][0], key)
key = c.op_keylist_next ()
i += 1
c.op_keylist_end()
result = c.op_keylist_result()
assert not result.truncated, "Key listing unexpectedly truncated"
for i, key in enumerate(c.op_keylist_all(None, False)):
try:
if len(keys[i]) == 4:
fpr, sec_keyid, uids, n_subkeys = keys[i]
misc_check = None
else:
fpr, sec_keyid, uids, n_subkeys, misc_check = keys[i]
except IndexError:
# There are more keys. We don't check for that.
break
# Global key flags.
check_global(i, key, uids, n_subkeys)
check_subkey(fpr, "Primary", key.subkeys[0])
check_subkey(sec_keyid, "Secondary", key.subkeys[1])
assert len(key.uids) == len(uids)
check_uid("First", uids[0], key.uids[0])
if len(key.uids) > 1:
check_uid("Second", uids[1], key.uids[1])
if len(key.uids) > 2:
check_uid("Third", uids[2], key.uids[2])
if misc_check:
misc_check (uids[0][0], key)

View File

@ -22,13 +22,19 @@ import support
support.init_gpgme(constants.PROTOCOL_OpenPGP)
c = core.Context()
c.op_trustlist_start("alice", 0)
def dump_item(item):
print("l={} k={} t={} o={} v={} u={}".format(
item.level, item.keyid, item.type, item.owner_trust,
item.validity, item.name))
c.op_trustlist_start("alice", 0)
while True:
item = c.op_trustlist_next()
if not item:
break
dump_item(item)
c.op_trustlist_end()
print("l={} k={} t={} o={} v={} u={}".format(
item.level, item.keyid, item.type, item.owner_trust,
item.validity, item.name))
for item in c.op_trustlist_all("alice", 0):
dump_item(item)