Imported Upstream version 1.12.0
[platform/upstream/gpgme.git] / lang / python / tests / t-signers.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
22 import gpg
23 import support
24
25 del absolute_import, print_function, unicode_literals
26
27
28 def fail(msg):
29     raise RuntimeError(msg)
30
31
32 def check_result(r, typ):
33     if r.invalid_signers:
34         fail("Invalid signer found: {}".format(r.invalid_signers.fpr))
35
36     if len(r.signatures) != 2:
37         fail("Unexpected number of signatures created")
38
39     for signature in r.signatures:
40         if signature.type != typ:
41             fail("Wrong type of signature created")
42
43         if signature.pubkey_algo != gpg.constants.pk.DSA:
44             fail("Wrong pubkey algorithm reported: {}".format(
45                 signature.pubkey_algo))
46
47         if signature.hash_algo != gpg.constants.md.SHA1:
48             fail("Wrong hash algorithm reported: {}".format(
49                 signature.hash_algo))
50
51         if signature.sig_class != 1:
52             fail("Wrong signature class reported: got {}, want {}".format(
53                 signature.sig_class, 1))
54
55         if signature.fpr not in ("A0FF4590BB6122EDEF6E3C542D727CC768697734",
56                                  "23FD347A419429BACCD5E72D6BC4778054ACD246"):
57             fail("Wrong fingerprint reported: {}".format(signature.fpr))
58
59
60 c = gpg.Context()
61 c.set_textmode(True)
62 c.set_armor(True)
63
64 keys = []
65 c.op_keylist_start('', True)
66 keys.append(c.op_keylist_next())
67 keys.append(c.op_keylist_next())
68 c.op_keylist_end()
69
70 c.signers_add(keys[0])
71 c.signers_add(keys[1])
72
73 for mode in (gpg.constants.sig.mode.NORMAL, gpg.constants.sig.mode.DETACH,
74              gpg.constants.sig.mode.CLEAR):
75     source = gpg.Data("Hallo Leute\n")
76     sink = gpg.Data()
77
78     c.op_sign(source, sink, mode)
79
80     result = c.op_sign_result()
81     check_result(result, mode)
82     support.print_data(sink)
83
84 # Idiomatic interface.
85 with gpg.Context(armor=True, textmode=True, signers=keys) as c:
86     message = "Hallo Leute\n".encode()
87     signed, result = c.sign(message)
88     check_result(result, gpg.constants.sig.mode.NORMAL)
89     assert signed.find(b'BEGIN PGP MESSAGE') > 0, 'Message not found'
90
91     signed, result = c.sign(message, mode=gpg.constants.sig.mode.DETACH)
92     check_result(result, gpg.constants.sig.mode.DETACH)
93     assert signed.find(b'BEGIN PGP SIGNATURE') > 0, 'Signature not found'
94
95     signed, result = c.sign(message, mode=gpg.constants.sig.mode.CLEAR)
96     check_result(result, gpg.constants.sig.mode.CLEAR)
97     assert signed.find(b'BEGIN PGP SIGNED MESSAGE') > 0, 'Message not found'
98     assert signed.find(message) > 0, 'Message content not found'
99     assert signed.find(b'BEGIN PGP SIGNATURE') > 0, 'Signature not found'