Line data Source code
1 : /* t-edit.c - Regression test.
2 : Copyright (C) 2000 Werner Koch (dd9jn)
3 : Copyright (C) 2001, 2002, 2003, 2004 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 Lesser General Public License as
9 : published by the Free Software Foundation; either version 2.1 of
10 : the License, or (at your option) any later version.
11 :
12 : GPGME is distributed in the hope that it will be useful, but
13 : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : Lesser General 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, write to the Free Software
19 : Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 : 02111-1307, USA. */
21 :
22 : /* We need to include config.h so that we know whether we are building
23 : with large file system (LFS) support. */
24 : #ifdef HAVE_CONFIG_H
25 : #include <config.h>
26 : #endif
27 :
28 : #include <stdio.h>
29 : #include <stdlib.h>
30 : #include <string.h>
31 : #include <assert.h>
32 : #include <errno.h>
33 : #include <unistd.h>
34 : #include <errno.h>
35 :
36 : #include <gpgme.h>
37 :
38 : #include "t-support.h"
39 :
40 :
41 : static void
42 18 : flush_data (gpgme_data_t dh)
43 : {
44 : char buf[100];
45 : int ret;
46 :
47 18 : ret = gpgme_data_seek (dh, 0, SEEK_SET);
48 18 : if (ret)
49 0 : fail_if_err (gpgme_error_from_errno (errno));
50 228 : while ((ret = gpgme_data_read (dh, buf, 100)) > 0)
51 192 : fwrite (buf, ret, 1, stdout);
52 18 : if (ret < 0)
53 0 : fail_if_err (gpgme_error_from_errno (errno));
54 18 : }
55 :
56 :
57 : gpgme_error_t
58 17 : edit_fnc (void *opaque, gpgme_status_code_t status, const char *args, int fd)
59 : {
60 17 : char *result = NULL;
61 17 : gpgme_data_t out = (gpgme_data_t) opaque;
62 :
63 17 : fputs ("[-- Response --]\n", stdout);
64 17 : flush_data (out);
65 :
66 17 : fprintf (stdout, "[-- Code: %i, %s --]\n", status, args);
67 :
68 17 : if (fd >= 0)
69 : {
70 7 : if (!strcmp (args, "keyedit.prompt"))
71 : {
72 : static int step = 0;
73 :
74 5 : switch (step)
75 : {
76 : case 0:
77 1 : result = "fpr";
78 1 : break;
79 : case 1:
80 1 : result = "expire";
81 1 : break;
82 :
83 : /* This fixes the primary user ID so the keylisting
84 : tests will have predictable output. */
85 : case 2:
86 1 : result = "1";
87 1 : break;
88 : case 3:
89 1 : result = "primary";
90 1 : break;
91 :
92 : default:
93 1 : result = "quit";
94 1 : break;
95 : }
96 5 : step++;
97 : }
98 2 : else if (!strcmp (args, "keyedit.save.okay"))
99 1 : result = "Y";
100 1 : else if (!strcmp (args, "keygen.valid"))
101 1 : result = "0";
102 : }
103 :
104 17 : if (result)
105 : {
106 7 : gpgme_io_write (fd, result, strlen (result));
107 7 : gpgme_io_write (fd, "\n", 1);
108 : }
109 17 : return 0;
110 : }
111 :
112 :
113 : int
114 1 : main (int argc, char **argv)
115 : {
116 : gpgme_ctx_t ctx;
117 : gpgme_error_t err;
118 1 : gpgme_data_t out = NULL;
119 1 : gpgme_key_t key = NULL;
120 1 : const char *pattern = "Alpha";
121 : char *agent_info;
122 :
123 1 : init_gpgme (GPGME_PROTOCOL_OpenPGP);
124 :
125 1 : err = gpgme_new (&ctx);
126 1 : fail_if_err (err);
127 1 : err = gpgme_data_new (&out);
128 1 : fail_if_err (err);
129 :
130 1 : agent_info = getenv("GPG_AGENT_INFO");
131 1 : if (!(agent_info && strchr (agent_info, ':')))
132 1 : gpgme_set_passphrase_cb (ctx, passphrase_cb, 0);
133 :
134 1 : err = gpgme_op_keylist_start (ctx, pattern, 0);
135 1 : fail_if_err (err);
136 1 : err = gpgme_op_keylist_next (ctx, &key);
137 1 : fail_if_err (err);
138 1 : err = gpgme_op_keylist_end (ctx);
139 1 : fail_if_err (err);
140 :
141 1 : err = gpgme_op_edit (ctx, key, edit_fnc, out, out);
142 1 : fail_if_err (err);
143 :
144 1 : fputs ("[-- Last response --]\n", stdout);
145 1 : flush_data (out);
146 :
147 1 : gpgme_data_release (out);
148 1 : gpgme_key_unref (key);
149 1 : gpgme_release (ctx);
150 :
151 1 : return 0;
152 : }
|