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