3 # Copyright (C) 2016 g10 Code GmbH
5 # This file is part of GPGME.
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.
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.
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/>.
20 from __future__ import absolute_import, print_function, unicode_literals
21 del absolute_import, print_function, unicode_literals
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"
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
39 source = gpg.Data(file=support.make_filename("cipher-2.asc"))
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)
47 support.print_data(sink)
49 verify_result = c.op_verify_result()
50 check_verify_result(verify_result,
51 gpg.constants.sigsum.VALID | gpg.constants.sigsum.GREEN,
52 "A0FF4590BB6122EDEF6E3C542D727CC768697734",
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, \
63 check_verify_result(verify_result,
64 gpg.constants.sigsum.VALID | gpg.constants.sigsum.GREEN,
65 "A0FF4590BB6122EDEF6E3C542D727CC768697734",
69 c.decrypt(open(support.make_filename("cipher-2.asc")),
71 except gpg.errors.MissingSignatures as e:
72 assert len(e.missing) == 1
73 assert e.missing[0] == bob
75 assert False, "Expected an error, got none"