Imported Upstream version 1.8.0
[platform/upstream/gpgme.git] / lang / python / tests / support.py
1 # Copyright (C) 2016 g10 Code GmbH
2 #
3 # This file is part of GPGME.
4 #
5 # GPGME is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # GPGME is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
13 # Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this program; if not, see <http://www.gnu.org/licenses/>.
17
18 from __future__ import absolute_import, print_function, unicode_literals
19 del absolute_import, print_function, unicode_literals
20
21 import sys
22 import os
23 import gpg
24
25 # known keys
26 alpha = "A0FF4590BB6122EDEF6E3C542D727CC768697734"
27 bob = "D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2"
28 encrypt_only = "F52770D5C4DB41408D918C9F920572769B9FE19C"
29 sign_only = "7CCA20CCDE5394CEE71C9F0BFED153F12F18F45D"
30 no_such_key = "A" * 40
31
32 def make_filename(name):
33     return os.path.join(os.environ['top_srcdir'], 'tests', 'gpg', name)
34
35 def in_srcdir(name):
36     return os.path.join(os.environ['srcdir'], name)
37
38 def init_gpgme(proto):
39     gpg.core.engine_check_version(proto)
40
41 verbose = int(os.environ.get('verbose', 0)) > 1
42 def print_data(data):
43     if verbose:
44         try:
45             # See if it is a file-like object.
46             data.seek(0, os.SEEK_SET)
47             data = data.read()
48         except:
49             # Hope for the best.
50             pass
51         sys.stdout.buffer.write(data)
52
53 def mark_key_trusted(ctx, key):
54     class Editor(object):
55         def __init__(self):
56             self.steps = ["trust", "save"]
57         def edit(self, status, args, out):
58             if args == "keyedit.prompt":
59                 result = self.steps.pop(0)
60             elif args == "edit_ownertrust.value":
61                 result = "5"
62             elif args == "edit_ownertrust.set_ultimate.okay":
63                 result = "Y"
64             elif args == "keyedit.save.okay":
65                 result = "Y"
66             else:
67                 result = None
68             return result
69     with gpg.Data() as sink:
70         ctx.op_edit(key, Editor().edit, sink, sink)