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, see <https://gnu.org/licenses/>.
19  * SPDX-License-Identifier: LGPL-2.1-or-later
20  */
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
flush_data(gpgme_data_t dh)42 flush_data (gpgme_data_t dh)
43 {
44   char buf[100];
45   int ret;
46 
47   ret = gpgme_data_seek (dh, 0, SEEK_SET);
48   if (ret)
49     fail_if_err (gpgme_error_from_errno (errno));
50   while ((ret = gpgme_data_read (dh, buf, 100)) > 0)
51     fwrite (buf, ret, 1, stdout);
52   if (ret < 0)
53     fail_if_err (gpgme_error_from_errno (errno));
54 }
55 
56 
57 gpgme_error_t
interact_fnc(void * opaque,const char * status,const char * args,int fd)58 interact_fnc (void *opaque, const char *status, const char *args, int fd)
59 {
60   const char *result = NULL;
61   gpgme_data_t out = (gpgme_data_t) opaque;
62 
63   fputs ("[-- Response --]\n", stdout);
64   flush_data (out);
65 
66   fprintf (stdout, "[-- Code: %s, %s --]\n", status, args);
67 
68   if (fd >= 0)
69     {
70       if (!strcmp (args, "keyedit.prompt"))
71 	{
72 	  static int step = 0;
73 
74 	  switch (step)
75 	    {
76 	    case 0:
77 	      result = "fpr";
78 	      break;
79 	    case 1:
80 	      result = "expire";
81 	      break;
82 
83 	      /* This fixes the primary user ID so the keylisting
84 		 tests will have predictable output.  */
85 	    case 2:
86 	      result = "1";
87 	      break;
88 	    case 3:
89 	      result = "primary";
90 	      break;
91 
92 	    default:
93 	      result = "quit";
94 	      break;
95 	    }
96 	  step++;
97 	}
98       else if (!strcmp (args, "keyedit.save.okay"))
99 	result = "Y";
100       else if (!strcmp (args, "keygen.valid"))
101 	result = "0";
102     }
103 
104   if (result)
105     {
106       gpgme_io_writen (fd, result, strlen (result));
107       gpgme_io_writen (fd, "\n", 1);
108     }
109   return 0;
110 }
111 
112 
113 int
main(int argc,char ** argv)114 main (int argc, char **argv)
115 {
116   gpgme_ctx_t ctx;
117   gpgme_error_t err;
118   gpgme_data_t out = NULL;
119   gpgme_key_t key = NULL;
120   const char *pattern = "Alpha";
121   char *agent_info;
122 
123   (void)argc;
124   (void)argv;
125 
126   init_gpgme (GPGME_PROTOCOL_OpenPGP);
127 
128   err = gpgme_new (&ctx);
129   fail_if_err (err);
130   err = gpgme_data_new (&out);
131   fail_if_err (err);
132 
133   agent_info = getenv("GPG_AGENT_INFO");
134   if (!(agent_info && strchr (agent_info, ':')))
135     gpgme_set_passphrase_cb (ctx, passphrase_cb, 0);
136 
137   err = gpgme_op_keylist_start (ctx, pattern, 0);
138   fail_if_err (err);
139   err = gpgme_op_keylist_next (ctx, &key);
140   fail_if_err (err);
141   err = gpgme_op_keylist_end (ctx);
142   fail_if_err (err);
143 
144   err = gpgme_op_interact (ctx, key, 0, interact_fnc, out, out);
145   fail_if_err (err);
146 
147   fputs ("[-- Last response --]\n", stdout);
148   flush_data (out);
149 
150   gpgme_data_release (out);
151   gpgme_key_unref (key);
152   gpgme_release (ctx);
153 
154   return 0;
155 }
156