1 /* t-decrypt-verify.c - Regression test.
2 Copyright (C) 2000 Werner Koch (dd9jn)
3 Copyright (C) 2001, 2002, 2003 g10 Code GmbH
5 This file is part of GPGME.
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.
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.
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. */
29 #define fail_if_err(err) \
34 fprintf (stderr, "%s:%d: GpgmeError %s\n", \
35 __FILE__, __LINE__, gpgme_strerror (err)); \
43 print_data (GpgmeData dh)
46 char buf[BUF_SIZE + 1];
49 ret = gpgme_data_seek (dh, 0, SEEK_SET);
51 fail_if_err (GPGME_File_Error);
52 while ((ret = gpgme_data_read (dh, buf, BUF_SIZE)) > 0)
53 fwrite (buf, ret, 1, stdout);
55 fail_if_err (GPGME_File_Error);
60 passphrase_cb (void *opaque, const char *desc, void **hd, const char **result)
62 /* Cleanup by looking at *hd. */
72 make_filename (const char *fname)
74 const char *srcdir = getenv ("srcdir");
79 buf = malloc (strlen(srcdir) + strlen(fname) + 2);
82 fprintf (stderr, "%s:%d: could not allocate string: %s\n",
83 __FILE__, __LINE__, strerror (errno));
94 check_verify_result (GpgmeVerifyResult result, int summary, char *fpr,
99 sig = result->signatures;
100 if (!sig || sig->next)
102 fprintf (stderr, "%s:%i: Unexpected number of signatures\n",
106 if (sig->summary != summary)
108 fprintf (stderr, "%s:%i: Unexpected signature summary: 0x%x\n",
109 __FILE__, __LINE__, sig->summary);
112 if (strcmp (sig->fpr, fpr))
114 fprintf (stderr, "%s:%i: Unexpected fingerprint: %s\n",
115 __FILE__, __LINE__, sig->fpr);
118 if (sig->status != status)
120 fprintf (stderr, "%s:%i: Unexpected signature status: %s\n",
121 __FILE__, __LINE__, gpgme_strerror (sig->status));
126 fprintf (stderr, "%s:%i: Unexpected notation data\n",
130 if (sig->wrong_key_usage)
132 fprintf (stderr, "%s:%i: Unexpectedly wrong key usage\n",
136 if (sig->validity != GPGME_VALIDITY_UNKNOWN)
138 fprintf (stderr, "%s:%i: Unexpected validity: %i\n",
139 __FILE__, __LINE__, sig->validity);
142 if (sig->validity_reason != GPGME_No_Error)
144 fprintf (stderr, "%s:%i: Unexpected validity reason: %s\n",
145 __FILE__, __LINE__, gpgme_strerror (sig->validity_reason));
152 main (int argc, char *argv[])
157 GpgmeDecryptResult decrypt_result;
158 GpgmeVerifyResult verify_result;
159 const char *cipher_2_asc = make_filename ("cipher-2.asc");
162 err = gpgme_new (&ctx);
165 agent_info = getenv("GPG_AGENT_INFO");
166 if (!(agent_info && strchr (agent_info, ':')))
167 gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL);
169 err = gpgme_data_new_from_file (&in, cipher_2_asc, 1);
171 err = gpgme_data_new (&out);
174 err = gpgme_op_decrypt_verify (ctx, in, out);
176 decrypt_result = gpgme_op_decrypt_result (ctx);
177 if (decrypt_result->unsupported_algorithm)
179 fprintf (stderr, "%s:%i: unsupported algorithm: %s\n",
180 __FILE__, __LINE__, decrypt_result->unsupported_algorithm);
184 verify_result = gpgme_op_verify_result (ctx);
185 check_verify_result (verify_result, 0,
186 "A0FF4590BB6122EDEF6E3C542D727CC768697734",
189 gpgme_data_release (in);
190 gpgme_data_release (out);