diff --git a/lang/python/tests/t-keylist-from-data.py b/lang/python/tests/t-keylist-from-data.py index f82ca842..4fd9ba08 100755 --- a/lang/python/tests/t-keylist-from-data.py +++ b/lang/python/tests/t-keylist-from-data.py @@ -1,267 +1,269 @@ #!/usr/bin/env python # 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 __future__ import absolute_import, print_function, unicode_literals import gpg import support del absolute_import, print_function, unicode_literals support.assert_gpg_version((2, 1, 14)) # 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(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 != 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) - 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) - assert key.owner_trust == gpg.constants.validity.UNKNOWN, - "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]) + 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) + 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) + assert key.owner_trust == gpg.constants.validity.UNKNOWN, \ + "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" + 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 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 != - (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, - 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) + 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, \ + 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 == gpg.constants.validity.UNKNOWN, - which + " user ID has unexpected validity: {}".format(uid.validity) + assert uid.validity == gpg.constants.validity.UNKNOWN, \ + which + " user ID has unexpected 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) + 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) # Export all the data from our keyring... key_data = gpg.Data() with gpg.Context() as c: c.op_export_keys([c.get_key(k[0]) for k in keys], 0, key_data) # ... rewind the tape... key_data.rewind() # ... and feed it into a keylist in an empty context. with support.EphemeralContext() as c: for i, key in enumerate(c.keylist(source=key_data)): 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(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) assert len(list(c.keylist())) == 0, "Keys were imported" diff --git a/lang/python/tests/t-keylist.py b/lang/python/tests/t-keylist.py index b725fc36..9cbada5e 100755 --- a/lang/python/tests/t-keylist.py +++ b/lang/python/tests/t-keylist.py @@ -1,336 +1,337 @@ #!/usr/bin/env python # 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 __future__ import absolute_import, print_function, unicode_literals import gpg import support del absolute_import, print_function, unicode_literals 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 # 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(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 != 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) - 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) + 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) + 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 key.uids[0].name == 'Alfa Test' 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 == gpg.constants. - validity.ULTIMATE, "Key has unexpected owner trust: {}". - format(key.owner_trust) + assert (key.uids[0].name == 'Alfa Test' + 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 == gpg.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]) + 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 != - (gpg.constants.pk.DSA if which == "Primary" else gpg.constants.pk.ELG_E), - which + " key has unexpected public key algo: {}".format(subkey. + 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, - 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) + 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 == (gpg.constants.validity.UNKNOWN if uid.name.split()[0] not in {'Alfa', 'Alpha', 'Alice'} else - gpg.constants.validity.ULTIMATE), - which + " user ID has unexpectedly validity: {}".format(uid.validity) + 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], - "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) + 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(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" # We test for a parameter-less keylist keyring_length = len(list(c.op_keylist_all())) assert keyring_length > 1,\ "Expected to find some keys, but got %r" % keyring_length # Then we do want to call with a pattern, only # i.e. without giving secret=0 alpha_keys = list(c.op_keylist_all(b"Alpha")) assert len(alpha_keys) == 1, "Expected only one key for 'Alpha', got %r" % len( alpha_keys) # Check negative result. assert len(list(c.keylist("no such key in sight"))) == 0 for i, key in enumerate(c.keylist()): 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(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) # check get_key() with gpg.Context() as c: c.get_key(support.alpha) c.get_key(support.alpha, secret=True) c.get_key(support.bob) try: c.get_key(support.bob, secret=True) except KeyError: pass else: assert False, "Expected KeyError" # Legacy error try: c.get_key(support.no_such_key) except gpg.errors.GPGMEError: pass else: assert False, "Expected GPGMEError" diff --git a/lang/python/tests/t-quick-key-creation.py b/lang/python/tests/t-quick-key-creation.py index 47209288..b3303fff 100755 --- a/lang/python/tests/t-quick-key-creation.py +++ b/lang/python/tests/t-quick-key-creation.py @@ -1,148 +1,151 @@ #!/usr/bin/env python # Copyright (C) 2017 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 __future__ import absolute_import, print_function, unicode_literals import gpg import itertools import time import support support.assert_gpg_version((2, 1, 2)) del absolute_import, print_function, unicode_literals alpha = "Alpha " with support.EphemeralContext() as ctx: res = ctx.create_key(alpha) keys = list(ctx.keylist()) assert len(keys) == 1, "Weird number of keys created" key = keys[0] assert key.fpr == res.fpr assert len(key.subkeys) == 2, "Expected one primary key and one subkey" assert key.subkeys[0].expires > 0, "Expected primary key to expire" # Try to create a key with the same UID try: ctx.create_key(alpha) assert False, "Expected an error but got none" except gpg.errors.GpgError as e: pass # Try to create a key with the same UID, now with force! res2 = ctx.create_key(alpha, force=True) assert res.fpr != res2.fpr # From here on, we use one context, and create unique UIDs uid_counter = 0 def make_uid(): global uid_counter uid_counter += 1 return "user{0}@invalid.example.org".format(uid_counter) with support.EphemeralContext() as ctx: # Check gpg.constants.create.NOEXPIRE... res = ctx.create_key(make_uid(), expires=False) key = ctx.get_key(res.fpr, secret=True) assert key.fpr == res.fpr assert len(key.subkeys) == 2, "Expected one primary key and one subkey" assert key.subkeys[0].expires == 0, "Expected primary key not to expire" t = 2 * 24 * 60 * 60 slack = 5 * 60 res = ctx.create_key(make_uid(), expires_in=t) key = ctx.get_key(res.fpr, secret=True) assert key.fpr == res.fpr assert len(key.subkeys) == 2, "Expected one primary key and one subkey" assert abs(time.time() + t - key.subkeys[0].expires) < slack, \ "Primary keys expiration time is off" # Check capabilities - for sign, encrypt, certify, authenticate in itertools. - product([False, True], [False, True], [False, True], [False, True]): + for sign, encrypt, certify, authenticate \ + in itertools.product([False, True], + [False, True], + [False, True], + [False, True]): # Filter some out if not (sign or encrypt or certify or authenticate): # This triggers the default capabilities tested before. continue if (sign or encrypt or authenticate) and not certify: # The primary key always certifies. continue res = ctx.create_key( make_uid(), algorithm="rsa", sign=sign, encrypt=encrypt, certify=certify, authenticate=authenticate) key = ctx.get_key(res.fpr, secret=True) assert key.fpr == res.fpr assert len(key.subkeys) == 1, \ "Expected no subkey for non-default capabilities" p = key.subkeys[0] assert sign == p.can_sign assert encrypt == p.can_encrypt assert certify == p.can_certify assert authenticate == p.can_authenticate # Check algorithm res = ctx.create_key(make_uid(), algorithm="rsa") key = ctx.get_key(res.fpr, secret=True) assert key.fpr == res.fpr for k in key.subkeys: assert k.pubkey_algo == 1 # Check algorithm with size res = ctx.create_key(make_uid(), algorithm="rsa1024") key = ctx.get_key(res.fpr, secret=True) assert key.fpr == res.fpr for k in key.subkeys: assert k.pubkey_algo == 1 assert k.length == 1024 # Check algorithm future-default ctx.create_key(make_uid(), algorithm="future-default") # Check passphrase protection recipient = make_uid() passphrase = "streng geheim" res = ctx.create_key(recipient, passphrase=passphrase) ciphertext, _, _ = ctx.encrypt( b"hello there", recipients=[ctx.get_key(res.fpr)]) cb_called = False def cb(*args): global cb_called cb_called = True return passphrase ctx.pinentry_mode = gpg.constants.PINENTRY_MODE_LOOPBACK ctx.set_passphrase_cb(cb) plaintext, _, _ = ctx.decrypt(ciphertext) assert plaintext == b"hello there" assert cb_called diff --git a/lang/python/tests/t-quick-subkey-creation.py b/lang/python/tests/t-quick-subkey-creation.py index 30424c19..cdbb71be 100755 --- a/lang/python/tests/t-quick-subkey-creation.py +++ b/lang/python/tests/t-quick-subkey-creation.py @@ -1,115 +1,117 @@ #!/usr/bin/env python # Copyright (C) 2017 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 __future__ import absolute_import, print_function, unicode_literals import gpg import itertools import time import support del absolute_import, print_function, unicode_literals alpha = "Alpha " bravo = "Bravo " with support.EphemeralContext() as ctx: res = ctx.create_key(alpha, certify=True) keys = list(ctx.keylist()) assert len(keys) == 1, "Weird number of keys created" key = keys[0] assert key.fpr == res.fpr assert len(key.subkeys) == 1, "Expected one primary key and no subkeys" def get_subkey(fpr): k = ctx.get_key(fpr) for sk in k.subkeys: if sk.fpr == fpr: return sk return None # Check gpg.constants.create.NOEXPIRE... res = ctx.create_subkey(key, expires=False) subkey = get_subkey(res.fpr) assert subkey.expires == 0, "Expected subkey not to expire" assert subkey.can_encrypt, \ "Default subkey capabilities do not include encryption" t = 2 * 24 * 60 * 60 slack = 5 * 60 res = ctx.create_subkey(key, expires_in=t) subkey = get_subkey(res.fpr) assert abs(time.time() + t - subkey.expires) < slack, \ "subkeys expiration time is off" # Check capabilities - for sign, encrypt, authenticate in itertools. - product([False, True], [False, True], [False, True]): + for sign, encrypt, authenticate \ + in itertools.product([False, True], + [False, True], + [False, True]): # Filter some out if not (sign or encrypt or authenticate): # This triggers the default capabilities tested before. continue res = ctx.create_subkey( key, sign=sign, encrypt=encrypt, authenticate=authenticate) subkey = get_subkey(res.fpr) assert sign == subkey.can_sign assert encrypt == subkey.can_encrypt assert authenticate == subkey.can_authenticate # Check algorithm res = ctx.create_subkey(key, algorithm="rsa") subkey = get_subkey(res.fpr) assert subkey.pubkey_algo == 1 # Check algorithm with size res = ctx.create_subkey(key, algorithm="rsa1024") subkey = get_subkey(res.fpr) assert subkey.pubkey_algo == 1 assert subkey.length == 1024 # Check algorithm future-default ctx.create_subkey(key, algorithm="future-default") # Check passphrase protection. For this we create a new key # so that we have a key with just one encryption subkey. bravo_res = ctx.create_key(bravo, certify=True) bravo_key = ctx.get_key(bravo_res.fpr) assert len( bravo_key.subkeys) == 1, "Expected one primary key and no subkeys" passphrase = "streng geheim" res = ctx.create_subkey(bravo_key, passphrase=passphrase) ciphertext, _, _ = ctx.encrypt( b"hello there", recipients=[ctx.get_key(bravo_res.fpr)]) cb_called = False def cb(*args): global cb_called cb_called = True return passphrase ctx.pinentry_mode = gpg.constants.PINENTRY_MODE_LOOPBACK ctx.set_passphrase_cb(cb) plaintext, _, _ = ctx.decrypt(ciphertext) assert plaintext == b"hello there" assert cb_called