2003-04-29 Marcus Brinkmann <marcus@g10code.de>
[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 <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <errno.h>
25
26 #include <gpgme.h>
27
28 \f
29 #define fail_if_err(err)                                        \
30   do                                                            \
31     {                                                           \
32       if (err)                                                  \
33         {                                                       \
34           fprintf (stderr, "%s:%d: GpgmeError %s\n",            \
35                    __FILE__, __LINE__, gpgme_strerror (err));   \
36           exit (1);                                             \
37         }                                                       \
38     }                                                           \
39   while (0)
40
41
42 static void
43 print_data (GpgmeData dh)
44 {
45 #define BUF_SIZE 512
46   char buf[BUF_SIZE + 1];
47   int ret;
48   
49   ret = gpgme_data_seek (dh, 0, SEEK_SET);
50   if (ret)
51     fail_if_err (GPGME_File_Error);
52   while ((ret = gpgme_data_read (dh, buf, BUF_SIZE)) > 0)
53     fwrite (buf, ret, 1, stdout);
54   if (ret < 0)
55     fail_if_err (GPGME_File_Error);
56 }
57
58
59 static GpgmeError
60 passphrase_cb (void *opaque, const char *desc, void **hd, const char **result)
61 {
62   /* Cleanup by looking at *hd.  */
63   if (!desc)
64     return 0;
65
66   *result = "abc";
67   return 0;
68 }
69
70
71 static char *
72 make_filename (const char *fname)
73 {
74   const char *srcdir = getenv ("srcdir");
75   char *buf;
76
77   if (!srcdir)
78     srcdir = ".";
79   buf = malloc (strlen(srcdir) + strlen(fname) + 2);
80   if (!buf)
81     {
82       fprintf (stderr, "%s:%d: could not allocate string: %s\n",
83                __FILE__, __LINE__, strerror (errno));
84       exit (1);
85     }
86   strcpy (buf, srcdir);
87   strcat (buf, "/");
88   strcat (buf, fname);
89   return buf;
90 }
91
92
93 static void
94 check_verify_result (GpgmeVerifyResult result, int summary, char *fpr,
95                      GpgmeError status)
96 {
97   GpgmeSignature sig;
98
99   sig = result->signatures;
100   if (!sig || sig->next)
101     {
102       fprintf (stderr, "%s:%i: Unexpected number of signatures\n",
103                __FILE__, __LINE__);
104       exit (1);
105     }
106   if (sig->summary != summary)
107     {
108       fprintf (stderr, "%s:%i: Unexpected signature summary: 0x%x\n",
109                __FILE__, __LINE__, sig->summary);
110       exit (1);
111     }
112   if (strcmp (sig->fpr, fpr))
113     {
114       fprintf (stderr, "%s:%i: Unexpected fingerprint: %s\n",
115                __FILE__, __LINE__, sig->fpr);
116       exit (1);
117     }
118   if (sig->status != status)
119     {
120       fprintf (stderr, "%s:%i: Unexpected signature status: %s\n",
121                __FILE__, __LINE__, gpgme_strerror (sig->status));
122       exit (1);
123     }
124   if (sig->notations)
125     {
126       fprintf (stderr, "%s:%i: Unexpected notation data\n",
127                __FILE__, __LINE__);
128       exit (1);
129     }
130   if (sig->wrong_key_usage)
131     {
132       fprintf (stderr, "%s:%i: Unexpectedly wrong key usage\n",
133                __FILE__, __LINE__);
134       exit (1);
135     }
136   if (sig->validity != GPGME_VALIDITY_UNKNOWN)
137     {
138       fprintf (stderr, "%s:%i: Unexpected validity: %i\n",
139                __FILE__, __LINE__, sig->validity);
140       exit (1);
141     }
142   if (sig->validity_reason != GPGME_No_Error)
143     {
144       fprintf (stderr, "%s:%i: Unexpected validity reason: %s\n",
145                __FILE__, __LINE__, gpgme_strerror (sig->validity_reason));
146       exit (1);
147     }
148 }
149
150
151 int 
152 main (int argc, char *argv[])
153 {
154   GpgmeCtx ctx;
155   GpgmeError err;
156   GpgmeData in, out;
157   GpgmeDecryptResult decrypt_result;
158   GpgmeVerifyResult verify_result;
159   const char *cipher_2_asc = make_filename ("cipher-2.asc");
160   char *agent_info;
161
162   err = gpgme_new (&ctx);
163   fail_if_err (err);
164
165   agent_info = getenv("GPG_AGENT_INFO");
166   if (!(agent_info && strchr (agent_info, ':')))
167     gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL);
168
169   err = gpgme_data_new_from_file (&in, cipher_2_asc, 1);
170   fail_if_err (err);
171   err = gpgme_data_new (&out);
172   fail_if_err (err);
173
174   err = gpgme_op_decrypt_verify (ctx, in, out);
175   fail_if_err (err);
176   decrypt_result = gpgme_op_decrypt_result (ctx);
177   if (decrypt_result->unsupported_algorithm)
178     {
179       fprintf (stderr, "%s:%i: unsupported algorithm: %s\n",
180                __FILE__, __LINE__, decrypt_result->unsupported_algorithm);
181       exit (1);
182     }    
183   print_data (out);
184   verify_result = gpgme_op_verify_result (ctx);
185   check_verify_result (verify_result, 0,
186                        "A0FF4590BB6122EDEF6E3C542D727CC768697734",
187                        GPGME_No_Error);
188
189   gpgme_data_release (in);
190   gpgme_data_release (out);
191   gpgme_release (ctx);
192   return 0;
193 }
194
195