2016-09-14 08:51:49 +00:00
|
|
|
#!/usr/bin/env python
|
2016-05-17 11:46:44 +00:00
|
|
|
|
|
|
|
# Copyright (C) 2016 g10 Code GmbH
|
|
|
|
#
|
|
|
|
# This file is part of GPGME.
|
|
|
|
#
|
|
|
|
# GPGME is free software; you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# GPGME is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
|
|
|
|
# Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-09-14 09:39:00 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
|
|
|
|
2016-10-28 20:45:49 +00:00
|
|
|
import gpg
|
2016-05-17 11:46:44 +00:00
|
|
|
import support
|
|
|
|
|
2018-08-18 10:29:14 +00:00
|
|
|
del absolute_import, print_function, unicode_literals
|
|
|
|
|
2016-10-31 13:42:26 +00:00
|
|
|
c = gpg.Context()
|
2016-05-17 11:46:44 +00:00
|
|
|
c.set_armor(True)
|
|
|
|
|
2016-10-31 13:42:26 +00:00
|
|
|
source = gpg.Data("Hallo Leute\n")
|
|
|
|
sink = gpg.Data()
|
2016-05-17 11:46:44 +00:00
|
|
|
|
|
|
|
keys = []
|
|
|
|
keys.append(c.get_key("A0FF4590BB6122EDEF6E3C542D727CC768697734", False))
|
|
|
|
keys.append(c.get_key("D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2", False))
|
|
|
|
|
2016-10-31 13:42:26 +00:00
|
|
|
c.op_encrypt(keys, gpg.constants.ENCRYPT_ALWAYS_TRUST, source, sink)
|
2016-05-17 11:46:44 +00:00
|
|
|
result = c.op_encrypt_result()
|
|
|
|
assert not result.invalid_recipients, \
|
2016-06-08 16:58:57 +00:00
|
|
|
"Invalid recipients: {}".format(", ".join(r.fpr for r in result.recipients))
|
2016-05-18 13:33:36 +00:00
|
|
|
support.print_data(sink)
|
2016-06-08 16:58:57 +00:00
|
|
|
|
|
|
|
# Idiomatic interface.
|
2016-10-28 20:45:49 +00:00
|
|
|
with gpg.Context(armor=True) as c:
|
2018-08-18 10:29:14 +00:00
|
|
|
ciphertext, _, _ = c.encrypt(
|
|
|
|
"Hallo Leute\n".encode(),
|
|
|
|
recipients=keys,
|
|
|
|
sign=False,
|
|
|
|
always_trust=True)
|
2016-06-08 16:58:57 +00:00
|
|
|
assert len(ciphertext) > 0
|
|
|
|
assert ciphertext.find(b'BEGIN PGP MESSAGE') > 0, 'Marker not found'
|
|
|
|
|
2018-08-18 10:29:14 +00:00
|
|
|
c.encrypt(
|
|
|
|
"Hallo Leute\n".encode(),
|
|
|
|
recipients=[c.get_key(support.encrypt_only, False)],
|
|
|
|
sign=False,
|
|
|
|
always_trust=True)
|
2016-06-08 16:58:57 +00:00
|
|
|
|
|
|
|
try:
|
2018-08-18 10:29:14 +00:00
|
|
|
c.encrypt(
|
|
|
|
"Hallo Leute\n".encode(),
|
|
|
|
recipients=[c.get_key(support.sign_only, False)],
|
|
|
|
sign=False,
|
|
|
|
always_trust=True)
|
2016-10-28 20:45:49 +00:00
|
|
|
except gpg.errors.InvalidRecipients as e:
|
2016-06-08 16:58:57 +00:00
|
|
|
assert len(e.recipients) == 1
|
|
|
|
assert support.sign_only.endswith(e.recipients[0].fpr)
|
|
|
|
else:
|
|
|
|
assert False, "Expected an InvalidRecipients error, got none"
|
2016-12-02 22:37:27 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
# People might be tempted to provide strings.
|
|
|
|
# We should raise something useful.
|
2018-08-18 10:29:14 +00:00
|
|
|
ciphertext, _, _ = c.encrypt(
|
|
|
|
"Hallo Leute\n", recipients=keys, sign=False, always_trust=True)
|
2016-12-02 22:37:27 +00:00
|
|
|
except TypeError as e:
|
|
|
|
# This test is a bit fragile, because the message
|
|
|
|
# may very well change. So if the behaviour will change
|
|
|
|
# this test can easily be deleted.
|
|
|
|
assert "encode" in str(e)
|