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 <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <unistd.h>
26
27 #include <gpgme.h>
28
29 #include "t-support.h"
30
31 \f
32 static void
33 check_verify_result (gpgme_verify_result_t result, int summary, char *fpr,
34                      gpgme_error_t status)
35 {
36   gpgme_signature_t sig;
37
38   sig = result->signatures;
39   if (!sig || sig->next)
40     {
41       fprintf (stderr, "%s:%i: Unexpected number of signatures\n",
42                __FILE__, __LINE__);
43       exit (1);
44     }
45   if (sig->summary != summary)
46     {
47       fprintf (stderr, "%s:%i: Unexpected signature summary: 0x%x\n",
48                __FILE__, __LINE__, sig->summary);
49       exit (1);
50     }
51   if (strcmp (sig->fpr, fpr))
52     {
53       fprintf (stderr, "%s:%i: Unexpected fingerprint: %s\n",
54                __FILE__, __LINE__, sig->fpr);
55       exit (1);
56     }
57   if (gpg_err_code (sig->status) != status)
58     {
59       fprintf (stderr, "%s:%i: Unexpected signature status: %s\n",
60                __FILE__, __LINE__, gpgme_strerror (sig->status));
61       exit (1);
62     }
63   if (sig->notations)
64     {
65       fprintf (stderr, "%s:%i: Unexpected notation data\n",
66                __FILE__, __LINE__);
67       exit (1);
68     }
69   if (sig->wrong_key_usage)
70     {
71       fprintf (stderr, "%s:%i: Unexpectedly wrong key usage\n",
72                __FILE__, __LINE__);
73       exit (1);
74     }
75   if (sig->validity != GPGME_VALIDITY_UNKNOWN)
76     {
77       fprintf (stderr, "%s:%i: Unexpected validity: %i\n",
78                __FILE__, __LINE__, sig->validity);
79       exit (1);
80     }
81   if (gpg_err_code (sig->validity_reason) != GPG_ERR_NO_ERROR)
82     {
83       fprintf (stderr, "%s:%i: Unexpected validity reason: %s\n",
84                __FILE__, __LINE__, gpgme_strerror (sig->validity_reason));
85       exit (1);
86     }
87 }
88
89
90 int 
91 main (int argc, char *argv[])
92 {
93   gpgme_ctx_t ctx;
94   gpgme_error_t err;
95   gpgme_data_t in, out;
96   gpgme_decrypt_result_t decrypt_result;
97   gpgme_verify_result_t verify_result;
98   const char *cipher_2_asc = make_filename ("cipher-2.asc");
99   char *agent_info;
100
101   err = gpgme_new (&ctx);
102   fail_if_err (err);
103
104   agent_info = getenv("GPG_AGENT_INFO");
105   if (!(agent_info && strchr (agent_info, ':')))
106     gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL);
107
108   err = gpgme_data_new_from_file (&in, cipher_2_asc, 1);
109   fail_if_err (err);
110   err = gpgme_data_new (&out);
111   fail_if_err (err);
112
113   err = gpgme_op_decrypt_verify (ctx, in, out);
114   fail_if_err (err);
115   decrypt_result = gpgme_op_decrypt_result (ctx);
116   if (decrypt_result->unsupported_algorithm)
117     {
118       fprintf (stderr, "%s:%i: unsupported algorithm: %s\n",
119                __FILE__, __LINE__, decrypt_result->unsupported_algorithm);
120       exit (1);
121     }    
122   print_data (out);
123   verify_result = gpgme_op_verify_result (ctx);
124   check_verify_result (verify_result, 0,
125                        "A0FF4590BB6122EDEF6E3C542D727CC768697734",
126                        GPG_ERR_NO_ERROR);
127
128   gpgme_data_release (in);
129   gpgme_data_release (out);
130   gpgme_release (ctx);
131   return 0;
132 }
133
134