Imported Upstream version 1.9.0
[platform/upstream/gpgme.git] / lang / python / tests / t-decrypt-verify.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 gpg
24 import support
25
26 def check_verify_result(result, summary, fpr, status):
27     assert len(result.signatures) == 1, "Unexpected number of signatures"
28     sig = result.signatures[0]
29     assert sig.summary == summary, "Unexpected signature summary"
30     assert sig.fpr == fpr
31     assert gpg.errors.GPGMEError(sig.status).getcode() == status
32     assert len(sig.notations) == 0
33     assert not sig.wrong_key_usage
34     assert sig.validity == gpg.constants.validity.FULL
35     assert gpg.errors.GPGMEError(sig.validity_reason).getcode() == gpg.errors.NO_ERROR
36
37 c = gpg.Context()
38
39 source = gpg.Data(file=support.make_filename("cipher-2.asc"))
40 sink = gpg.Data()
41
42 c.op_decrypt_verify(source, sink)
43 result = c.op_decrypt_result()
44 assert not result.unsupported_algorithm, \
45     "Unsupported algorithm: {}".format(result.unsupported_algorithm)
46
47 support.print_data(sink)
48
49 verify_result = c.op_verify_result()
50 check_verify_result(verify_result,
51                     gpg.constants.sigsum.VALID | gpg.constants.sigsum.GREEN,
52                     "A0FF4590BB6122EDEF6E3C542D727CC768697734",
53                     gpg.errors.NO_ERROR)
54
55 # Idiomatic interface.
56 with gpg.Context() as c:
57     alpha = c.get_key("A0FF4590BB6122EDEF6E3C542D727CC768697734", False)
58     bob = c.get_key("D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2", False)
59     plaintext, _, verify_result = \
60         c.decrypt(open(support.make_filename("cipher-2.asc")), verify=[alpha])
61     assert plaintext.find(b'Wenn Sie dies lesen k') >= 0, \
62         'Plaintext not found'
63     check_verify_result(verify_result,
64                         gpg.constants.sigsum.VALID | gpg.constants.sigsum.GREEN,
65                         "A0FF4590BB6122EDEF6E3C542D727CC768697734",
66                         gpg.errors.NO_ERROR)
67
68     try:
69         c.decrypt(open(support.make_filename("cipher-2.asc")),
70                   verify=[alpha, bob])
71     except gpg.errors.MissingSignatures as e:
72         assert len(e.missing) == 1
73         assert e.missing[0] == bob
74     else:
75         assert False, "Expected an error, got none"