Blame lang/python/tests/t-sig-notation.py

Packit d7e8d0
#!/usr/bin/env python
Packit d7e8d0
Packit d7e8d0
# Copyright (C) 2016 g10 Code GmbH
Packit d7e8d0
#
Packit d7e8d0
# This file is part of GPGME.
Packit d7e8d0
#
Packit d7e8d0
# GPGME is free software; you can redistribute it and/or modify it
Packit d7e8d0
# under the terms of the GNU General Public License as published by
Packit d7e8d0
# the Free Software Foundation; either version 2 of the License, or
Packit d7e8d0
# (at your option) any later version.
Packit d7e8d0
#
Packit d7e8d0
# GPGME is distributed in the hope that it will be useful, but WITHOUT
Packit d7e8d0
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit d7e8d0
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
Packit d7e8d0
# Public License for more details.
Packit d7e8d0
#
Packit d7e8d0
# You should have received a copy of the GNU Lesser General Public
Packit d7e8d0
# License along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit d7e8d0
Packit d7e8d0
from __future__ import absolute_import, print_function, unicode_literals
Packit d7e8d0
del absolute_import, print_function, unicode_literals
Packit d7e8d0
Packit d7e8d0
import os
Packit d7e8d0
import gpg
Packit d7e8d0
import support
Packit d7e8d0
_ = support # to appease pyflakes.
Packit d7e8d0
Packit d7e8d0
expected_notations = {
Packit d7e8d0
    "laughing@me": ("Just Squeeze Me", gpg.constants.sig.notation.HUMAN_READABLE),
Packit d7e8d0
    "preferred-email-encoding@pgp.com": ("pgpmime",
Packit d7e8d0
                                         gpg.constants.sig.notation.HUMAN_READABLE
Packit d7e8d0
                                         | gpg.constants.sig.notation.CRITICAL),
Packit d7e8d0
    None: ("http://www.gnu.org/policy/", 0),
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
# GnuPG prior to 2.1.13 did not report the critical flag correctly.
Packit d7e8d0
with gpg.Context() as c:
Packit d7e8d0
    version = c.engine_info.version
Packit d7e8d0
    have_correct_sig_data = not (version.startswith("1.")
Packit d7e8d0
                                 or version.startswith("2.0.")
Packit d7e8d0
                                 or version == "2.1.1"
Packit d7e8d0
                                 or (version.startswith("2.1.1")
Packit d7e8d0
                                     and version[5] < '3'))
Packit d7e8d0
Packit d7e8d0
def check_result(result):
Packit d7e8d0
    assert len(result.signatures) == 1, "Unexpected number of signatures"
Packit d7e8d0
    sig = result.signatures[0]
Packit d7e8d0
    assert len(sig.notations) == len(expected_notations)
Packit d7e8d0
Packit d7e8d0
    for r in sig.notations:
Packit d7e8d0
        assert not 'name_len' in dir(r)
Packit d7e8d0
        assert not 'value_len' in dir(r)
Packit d7e8d0
        assert r.name in expected_notations
Packit d7e8d0
        value, flags = expected_notations.pop(r.name)
Packit d7e8d0
Packit d7e8d0
        assert r.value == value, \
Packit d7e8d0
            "Expected {!r}, got {!r}".format(value, r.value)
Packit d7e8d0
        assert r.human_readable \
Packit d7e8d0
            == bool(flags & gpg.constants.sig.notation.HUMAN_READABLE)
Packit d7e8d0
        assert r.critical \
Packit d7e8d0
            == (bool(flags & gpg.constants.sig.notation.CRITICAL)
Packit d7e8d0
                if have_correct_sig_data else False)
Packit d7e8d0
Packit d7e8d0
    assert len(expected_notations) == 0
Packit d7e8d0
Packit d7e8d0
source = gpg.Data("Hallo Leute\n")
Packit d7e8d0
signed = gpg.Data()
Packit d7e8d0
Packit d7e8d0
c = gpg.Context()
Packit d7e8d0
for name, (value, flags) in expected_notations.items():
Packit d7e8d0
    c.sig_notation_add(name, value, flags)
Packit d7e8d0
Packit d7e8d0
c.op_sign(source, signed, gpg.constants.sig.mode.NORMAL)
Packit d7e8d0
Packit d7e8d0
signed.seek(0, os.SEEK_SET)
Packit d7e8d0
sink = gpg.Data()
Packit d7e8d0
c.op_verify(signed, None, sink)
Packit d7e8d0
result = c.op_verify_result()
Packit d7e8d0
check_result(result)