Blame lang/python/tests/t-quick-key-manipulation.py

Packit Service 672cf4
#!/usr/bin/env python
Packit Service 672cf4
Packit Service 672cf4
# Copyright (C) 2017 g10 Code GmbH
Packit Service 672cf4
#
Packit Service 672cf4
# This file is part of GPGME.
Packit Service 672cf4
#
Packit Service 672cf4
# GPGME is free software; you can redistribute it and/or modify it
Packit Service 672cf4
# under the terms of the GNU General Public License as published by
Packit Service 672cf4
# the Free Software Foundation; either version 2 of the License, or
Packit Service 672cf4
# (at your option) any later version.
Packit Service 672cf4
#
Packit Service 672cf4
# GPGME is distributed in the hope that it will be useful, but WITHOUT
Packit Service 672cf4
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit Service 672cf4
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
Packit Service 672cf4
# Public License for more details.
Packit Service 672cf4
#
Packit Service 672cf4
# You should have received a copy of the GNU Lesser General Public
Packit Service 6c01f9
# License along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit Service 672cf4
Packit Service 672cf4
from __future__ import absolute_import, print_function, unicode_literals
Packit Service 6c01f9
del absolute_import, print_function, unicode_literals
Packit Service 672cf4
Packit Service 672cf4
import os
Packit Service 672cf4
import gpg
Packit Service 672cf4
import sys
Packit Service 672cf4
Packit Service 672cf4
import support
Packit Service 672cf4
support.assert_gpg_version((2, 1, 14))
Packit Service 672cf4
Packit Service 672cf4
alpha = "Alpha <alpha@invalid.example.net>"
Packit Service 672cf4
bravo = "Bravo <bravo@invalid.example.net>"
Packit Service 672cf4
Packit Service 672cf4
with support.EphemeralContext() as ctx:
Packit Service 672cf4
    res = ctx.create_key(alpha, certify=True)
Packit Service 672cf4
    key = ctx.get_key(res.fpr)
Packit Service 672cf4
    assert len(key.subkeys) == 1, "Expected one primary key and no subkeys"
Packit Service 672cf4
    assert len(key.uids) == 1, "Expected exactly one UID"
Packit Service 672cf4
Packit Service 672cf4
    def get_uid(uid):
Packit Service 672cf4
        key = ctx.get_key(res.fpr)
Packit Service 672cf4
        for u in key.uids:
Packit Service 672cf4
            if u.uid == uid:
Packit Service 672cf4
                return u
Packit Service 672cf4
        return None
Packit Service 672cf4
Packit Service 672cf4
    # sanity check
Packit Service 672cf4
    uid = get_uid(alpha)
Packit Service 672cf4
    assert uid, "UID alpha not found"
Packit Service 672cf4
    assert uid.revoked == 0
Packit Service 672cf4
Packit Service 672cf4
    # add bravo
Packit Service 672cf4
    ctx.key_add_uid(key, bravo)
Packit Service 672cf4
    uid = get_uid(bravo)
Packit Service 672cf4
    assert uid, "UID bravo not found"
Packit Service 672cf4
    assert uid.revoked == 0
Packit Service 672cf4
Packit Service 672cf4
    # revoke alpha
Packit Service 672cf4
    ctx.key_revoke_uid(key, alpha)
Packit Service 672cf4
    uid = get_uid(alpha)
Packit Service 672cf4
    assert uid, "UID alpha not found"
Packit Service 672cf4
    assert uid.revoked == 1
Packit Service 672cf4
    uid = get_uid(bravo)
Packit Service 672cf4
    assert uid, "UID bravo not found"
Packit Service 672cf4
    assert uid.revoked == 0
Packit Service 672cf4
Packit Service 672cf4
    # try to revoke the last UID
Packit Service 672cf4
    try:
Packit Service 672cf4
        ctx.key_revoke_uid(key, alpha)
Packit Service 672cf4
        # IMHO this should fail.  issue2961.
Packit Service 672cf4
        # assert False, "Expected an error but got none"
Packit Service 672cf4
    except gpg.errors.GpgError:
Packit Service 672cf4
        pass
Packit Service 672cf4
Packit Service 672cf4
    # Everything should be the same
Packit Service 672cf4
    uid = get_uid(alpha)
Packit Service 672cf4
    assert uid, "UID alpha not found"
Packit Service 672cf4
    assert uid.revoked == 1
Packit Service 672cf4
    uid = get_uid(bravo)
Packit Service 672cf4
    assert uid, "UID bravo not found"
Packit Service 672cf4
    assert uid.revoked == 0
Packit Service 672cf4
Packit Service 672cf4
    # try to revoke a non-existent UID
Packit Service 672cf4
    try:
Packit Service 6c01f9
        ctx.key_revoke_uid(key, "i dont exist")
Packit Service 672cf4
        # IMHO this should fail.  issue2963.
Packit Service 672cf4
        # assert False, "Expected an error but got none"
Packit Service 672cf4
    except gpg.errors.GpgError:
Packit Service 672cf4
        pass
Packit Service 672cf4
Packit Service 672cf4
    # try to add an pre-existent UID
Packit Service 672cf4
    try:
Packit Service 672cf4
        ctx.key_add_uid(key, bravo)
Packit Service 672cf4
        assert False, "Expected an error but got none"
Packit Service 672cf4
    except gpg.errors.GpgError:
Packit Service 672cf4
        pass
Packit Service 672cf4
Packit Service 672cf4
    # Check setting the TOFU policy.
Packit Service 672cf4
    with open(os.path.join(ctx.home_dir, "gpg.conf"), "a") as handle:
Packit Service 672cf4
        handle.write("trust-model tofu+pgp\n")
Packit Service 672cf4
Packit Service 672cf4
    if not support.have_tofu_support(ctx, bravo):
Packit Service 672cf4
        print("GnuPG does not support TOFU, skipping TOFU tests.")
Packit Service 672cf4
        sys.exit()
Packit Service 672cf4
Packit Service 672cf4
    for name, policy in [(name, getattr(gpg.constants.tofu.policy, name))
Packit Service 672cf4
                         for name in filter(lambda x: not x.startswith('__'),
Packit Service 672cf4
                                            dir(gpg.constants.tofu.policy))]:
Packit Service 672cf4
        if policy == gpg.constants.tofu.policy.NONE:
Packit Service 672cf4
            # We must not set the policy to NONE.
Packit Service 672cf4
            continue
Packit Service 672cf4
Packit Service 672cf4
        ctx.key_tofu_policy(key, policy)
Packit Service 672cf4
Packit Service 6c01f9
        keys = list(ctx.keylist(key.uids[0].uid,
Packit Service 6c01f9
                                mode=(gpg.constants.keylist.mode.LOCAL
Packit Service 6c01f9
                                      |gpg.constants.keylist.mode.WITH_TOFU)))
Packit Service 672cf4
        assert len(keys) == 1
Packit Service 672cf4
Packit Service 672cf4
        if policy == gpg.constants.tofu.policy.AUTO:
Packit Service 672cf4
            # We cannot check that it is set to AUTO.
Packit Service 672cf4
            continue
Packit Service 672cf4
Packit Service 672cf4
        for uid in keys[0].uids:
Packit Service 672cf4
            if uid.uid == alpha:
Packit Service 672cf4
                # TOFU information of revoked UIDs is not updated.
Packit Service 672cf4
                # XXX: Is that expected?
Packit Service 672cf4
                continue
Packit Service 672cf4
            assert uid.tofu[0].policy == policy, \
Packit Service 672cf4
                "Expected policy {0} ({1}), got {2}".format(policy, name,
Packit Service 672cf4
                                                            uid.tofu[0].policy)