Use gpgme interface for error handling to avoid linking with gpg-error.
[gpgme.git] / tests / gpg / t-edit.c
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 \f
41 static void
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
58 edit_fnc (void *opaque, gpgme_status_code_t status, const char *args, int fd)
59 {
60   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: %i, %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_write (fd, result, strlen (result));
107       gpgme_io_write (fd, "\n", 1);
108     }
109   return 0;
110 }
111
112
113 int 
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   init_gpgme (GPGME_PROTOCOL_OpenPGP);
124
125   err = gpgme_new (&ctx);
126   fail_if_err (err);
127   err = gpgme_data_new (&out);
128   fail_if_err (err);
129
130   agent_info = getenv("GPG_AGENT_INFO");
131   if (!(agent_info && strchr (agent_info, ':')))
132     gpgme_set_passphrase_cb (ctx, passphrase_cb, 0);
133
134   err = gpgme_op_keylist_start (ctx, pattern, 0);
135   fail_if_err (err);
136   err = gpgme_op_keylist_next (ctx, &key);
137   fail_if_err (err);
138   err = gpgme_op_keylist_end (ctx);
139   fail_if_err (err);
140
141   err = gpgme_op_edit (ctx, key, edit_fnc, out, out);
142   fail_if_err (err);
143
144   fputs ("[-- Last response --]\n", stdout);
145   flush_data (out);
146
147   gpgme_data_release (out);
148   gpgme_key_unref (key);
149   gpgme_release (ctx);
150
151   return 0;
152 }