doc/
[gpgme.git] / tests / gpg / t-decrypt-verify.c
1 /* t-decrypt-verify.c  - regression test
2    Copyright (C) 2000 Werner Koch (dd9jn)
3    Copyright (C) 2001, 2002, 2003 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 General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (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    General Public License for more details.
16  
17    You should have received a copy of the GNU General Public License
18    along with GPGME; if not, write to the Free Software Foundation,
19    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
25 #include <errno.h>
26
27 #include <gpgme.h>
28
29 struct passphrase_cb_info_s
30 {
31   GpgmeCtx c;
32   int did_it;
33 };
34
35
36 #define fail_if_err(a) do { if(a) { int my_errno = errno;       \
37             fprintf (stderr, "%s:%d: GpgmeError %s\n",          \
38                  __FILE__, __LINE__, gpgme_strerror(a));        \
39             if ((a) == GPGME_File_Error)                        \
40                    fprintf (stderr, "\terrno=`%s'\n", strerror (my_errno)); \
41                    exit (1); }                                  \
42                              } while(0)
43
44
45 static void
46 print_data (GpgmeData dh)
47 {
48   char buf[100];
49   int ret;
50   
51   ret = gpgme_data_seek (dh, 0, SEEK_SET);
52   if (ret)
53     fail_if_err (GPGME_File_Error);
54   while ((ret = gpgme_data_read (dh, buf, 100)) > 0)
55     fwrite (buf, ret, 1, stdout);
56   if (ret < 0)
57     fail_if_err (GPGME_File_Error);
58 }
59
60
61 static GpgmeError
62 passphrase_cb (void *opaque, const char *desc,
63                void **r_hd, const char **result)
64 {
65   if (!desc)
66     /* Cleanup by looking at *r_hd.  */
67     return 0;
68
69   *result = "abc";
70   fprintf (stderr, "%% requesting passphrase for `%s': ", desc);
71   fprintf (stderr, "sending `%s'\n", *result);
72   
73   return 0;
74 }
75
76
77 static char *
78 mk_fname (const char *fname)
79 {
80   const char *srcdir = getenv ("srcdir");
81   char *buf;
82
83   if (!srcdir)
84     srcdir = ".";
85   buf = malloc (strlen(srcdir) + strlen(fname) + 2);
86   if (!buf)
87     exit (8);
88   strcpy (buf, srcdir);
89   strcat (buf, "/");
90   strcat (buf, fname);
91   return buf;
92 }
93
94 int 
95 main (int argc, char **argv)
96 {
97   GpgmeCtx ctx;
98   GpgmeError err;
99   GpgmeData in, out, pwdata = NULL;
100   struct passphrase_cb_info_s info;
101   const char *cipher_2_asc = mk_fname ("cipher-2.asc");
102   GpgmeSigStat status;
103   char *p;
104
105   do
106     {
107       err = gpgme_new (&ctx);
108       fail_if_err (err);
109
110       p = getenv("GPG_AGENT_INFO");
111       if (!(p && strchr (p, ':')))
112         {
113           memset (&info, 0, sizeof info);
114           info.c = ctx;
115           gpgme_set_passphrase_cb (ctx, passphrase_cb, &info);
116         } 
117
118       err = gpgme_data_new_from_file (&in, cipher_2_asc, 1);
119       fail_if_err (err);
120
121       err = gpgme_data_new (&out);
122       fail_if_err (err);
123
124       err = gpgme_op_decrypt_verify (ctx, in, out);
125       fail_if_err (err);
126     
127       fflush (NULL);
128       fputs ("Begin Result:\n", stdout);
129       print_data (out);
130       fputs ("End Result.\n", stdout);
131
132       if (!gpgme_get_sig_status (ctx, 0, &status, NULL))
133         {
134           fprintf (stderr, "Signature check failed unexpectedly.\n");
135           exit (1);
136         }
137       if (status != GPGME_SIG_STAT_GOOD)
138         {
139           fprintf (stderr, "Signature check failed unexpectedly.\n");
140           exit (1);
141         }
142
143       gpgme_data_release (in);
144       gpgme_data_release (out);
145       gpgme_data_release (pwdata);
146       gpgme_release (ctx);
147     }
148   while (argc > 1 && !strcmp (argv[1], "--loop"));
149
150   return 0;
151 }
152
153