qt: Handle if gpg does not support tofu in test
[gpgme.git] / lang / python / tests / t-sig-notation.py
1 #!/usr/bin/env python
2
3 # Copyright (C) 2016 g10 Code GmbH
4 #
5 # This file is part of GPGME.
6 #
7 # GPGME is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # GPGME is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
15 # Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this program; if not, see <http://www.gnu.org/licenses/>.
19
20 from __future__ import absolute_import, print_function, unicode_literals
21 del absolute_import, print_function, unicode_literals
22
23 import os
24 import gpg
25 import support
26
27 expected_notations = {
28     "laughing@me": ("Just Squeeze Me", gpg.constants.sig.notation.HUMAN_READABLE),
29     "preferred-email-encoding@pgp.com": ("pgpmime",
30                                          gpg.constants.sig.notation.HUMAN_READABLE
31                                          | gpg.constants.sig.notation.CRITICAL),
32     None: ("http://www.gnu.org/policy/", 0),
33 }
34
35 # GnuPG prior to 2.1.13 did not report the critical flag correctly.
36 with gpg.Context() as c:
37     version = c.engine_info.version
38     have_correct_sig_data = not (version.startswith("1.")
39                                  or version.startswith("2.0.")
40                                  or version == "2.1.1"
41                                  or (version.startswith("2.1.1")
42                                      and version[5] < '3'))
43
44 def check_result(result):
45     assert len(result.signatures) == 1, "Unexpected number of signatures"
46     sig = result.signatures[0]
47     assert len(sig.notations) == len(expected_notations)
48
49     for r in sig.notations:
50         assert not 'name_len' in dir(r)
51         assert not 'value_len' in dir(r)
52         assert r.name in expected_notations
53         value, flags = expected_notations.pop(r.name)
54
55         assert r.value == value, \
56             "Expected {!r}, got {!r}".format(value, r.value)
57         assert r.human_readable \
58             == bool(flags & gpg.constants.sig.notation.HUMAN_READABLE)
59         assert r.critical \
60             == (bool(flags & gpg.constants.sig.notation.CRITICAL)
61                 if have_correct_sig_data else False)
62
63     assert len(expected_notations) == 0
64
65 source = gpg.Data("Hallo Leute\n")
66 signed = gpg.Data()
67
68 c = gpg.Context()
69 for name, (value, flags) in expected_notations.items():
70     c.sig_notation_add(name, value, flags)
71
72 c.op_sign(source, signed, gpg.constants.sig.mode.NORMAL)
73
74 signed.seek(0, os.SEEK_SET)
75 sink = gpg.Data()
76 c.op_verify(signed, None, sink)
77 result = c.op_verify_result()
78 check_result(result)