Blame lang/python/tests/t-decrypt-verify.py

Packit Service 672cf4
#!/usr/bin/env python
Packit Service 672cf4
Packit Service 672cf4
# Copyright (C) 2016 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 gpg
Packit Service 672cf4
import support
Packit Service 672cf4
Packit Service 672cf4
def check_verify_result(result, summary, fpr, status):
Packit Service 672cf4
    assert len(result.signatures) == 1, "Unexpected number of signatures"
Packit Service 672cf4
    sig = result.signatures[0]
Packit Service 672cf4
    assert sig.summary == summary, "Unexpected signature summary"
Packit Service 672cf4
    assert sig.fpr == fpr
Packit Service 672cf4
    assert gpg.errors.GPGMEError(sig.status).getcode() == status
Packit Service 672cf4
    assert len(sig.notations) == 0
Packit Service 672cf4
    assert not sig.wrong_key_usage
Packit Service 672cf4
    assert sig.validity == gpg.constants.validity.FULL
Packit Service 6c01f9
    assert gpg.errors.GPGMEError(sig.validity_reason).getcode() == gpg.errors.NO_ERROR
Packit Service 672cf4
Packit Service 672cf4
c = gpg.Context()
Packit Service 672cf4
Packit Service 672cf4
source = gpg.Data(file=support.make_filename("cipher-2.asc"))
Packit Service 672cf4
sink = gpg.Data()
Packit Service 672cf4
Packit Service 672cf4
c.op_decrypt_verify(source, sink)
Packit Service 672cf4
result = c.op_decrypt_result()
Packit Service 672cf4
assert not result.unsupported_algorithm, \
Packit Service 672cf4
    "Unsupported algorithm: {}".format(result.unsupported_algorithm)
Packit Service 672cf4
Packit Service 672cf4
support.print_data(sink)
Packit Service 672cf4
Packit Service 672cf4
verify_result = c.op_verify_result()
Packit Service 6c01f9
check_verify_result(verify_result,
Packit Service 6c01f9
                    gpg.constants.sigsum.VALID | gpg.constants.sigsum.GREEN,
Packit Service 6c01f9
                    "A0FF4590BB6122EDEF6E3C542D727CC768697734",
Packit Service 6c01f9
                    gpg.errors.NO_ERROR)
Packit Service 672cf4
Packit Service 672cf4
# Idiomatic interface.
Packit Service 672cf4
with gpg.Context() as c:
Packit Service 672cf4
    alpha = c.get_key("A0FF4590BB6122EDEF6E3C542D727CC768697734", False)
Packit Service 672cf4
    bob = c.get_key("D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2", False)
Packit Service 672cf4
    plaintext, _, verify_result = \
Packit Service 672cf4
        c.decrypt(open(support.make_filename("cipher-2.asc")), verify=[alpha])
Packit Service 672cf4
    assert plaintext.find(b'Wenn Sie dies lesen k') >= 0, \
Packit Service 672cf4
        'Plaintext not found'
Packit Service 6c01f9
    check_verify_result(verify_result,
Packit Service 6c01f9
                        gpg.constants.sigsum.VALID | gpg.constants.sigsum.GREEN,
Packit Service 6c01f9
                        "A0FF4590BB6122EDEF6E3C542D727CC768697734",
Packit Service 6c01f9
                        gpg.errors.NO_ERROR)
Packit Service 672cf4
Packit Service 672cf4
    try:
Packit Service 6c01f9
        c.decrypt(open(support.make_filename("cipher-2.asc")),
Packit Service 6c01f9
                  verify=[alpha, bob])
Packit Service 6c01f9
    except gpg.errors.MissingSignatures as e:
Packit Service 672cf4
        assert len(e.missing) == 1
Packit Service 672cf4
        assert e.missing[0] == bob
Packit Service 672cf4
    else:
Packit Service 672cf4
        assert False, "Expected an error, got none"