aa5f34ce962f31463b1889fa283c759b999bf8e7
[gpgme.git] / tests / opassuan / t-command.c
1 /* t-command.c - Regression test.
2    Copyright (C) 2009 g10 Code GmbH
3
4    This file is part of GPGME.
5
6    GPGME is free software; you can redistribute it and/or modify it
7    under the terms of the GNU Lesser General Public License as
8    published by the Free Software Foundation; either version 2.1 of
9    the License, or (at your option) any later version.
10    
11    GPGME is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15    
16    You should have received a copy of the GNU Lesser General Public
17    License along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <locale.h>
27 #include <assert.h>
28
29 #include <gpgme.h>
30
31 #define fail_if_err(err)                                        \
32   do                                                            \
33     {                                                           \
34       if (err)                                                  \
35         {                                                       \
36           fprintf (stderr, "%s:%d: %s: %s (%d.%d)\n",           \
37                    __FILE__, __LINE__, gpg_strsource (err),     \
38                    gpg_strerror (err),                          \
39                    gpg_err_source (err), gpg_err_code (err));   \
40           exit (1);                                             \
41         }                                                       \
42     }                                                           \
43   while (0)
44
45
46 static gpg_error_t
47 data_cb (void *opaque, const void *data, size_t datalen)
48 {
49   printf ("DATA_CB: datalen=%d\n", (int)datalen);
50   return 0;
51 }     
52
53
54 static gpg_error_t
55 inq_cb (void *opaque, const char *name, const char *args,
56         gpgme_data_t *r_data)
57 {
58   gpgme_data_t data;
59   gpg_error_t err;
60
61   if (name)
62     {
63       printf ("INQ_CB: name=`%s' args=`%s'\n", name, args);
64       /* There shall be no data object.  */
65       assert (!*r_data);
66       
67       err = gpgme_data_new (&data);
68       fail_if_err (err);
69       *r_data = data;
70       printf ("        sending data object %p\n", data);
71     }
72   else /* Finished using the formerly returned data object.  */
73     {
74       printf ("INQ_CB: data object %p finished\n", *r_data);
75       /* There shall be a data object so that it can be cleaned up. */
76       assert (r_data);
77
78       gpgme_data_release (*r_data);
79     }
80
81   /* Uncomment the next lines and send a "SCD LEARN" to test sending
82      cancel from in inquiry.  */
83   /* if (name && !strcmp (name, "KNOWNCARDP")) */
84   /*   return gpg_error (GPG_ERR_ASS_CANCELED); */
85
86
87   return 0;
88 }     
89
90
91 static gpg_error_t
92 status_cb (void *opaque, const char *status, const char *args)
93 {
94   printf ("STATUS_CB: status=`%s'  args=`%s'\n", status, args);
95   return 0;
96 }     
97
98
99
100 int 
101 main (int argc, char **argv)
102 {
103   gpgme_error_t err;
104   gpgme_error_t op_err;
105   gpgme_ctx_t ctx;
106   const char *command;
107
108   gpgme_check_version (NULL);
109 #ifndef HAVE_W32_SYSTEM
110   setlocale (LC_ALL, "");
111   gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL));
112   gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL));
113 #endif
114
115   if (argc)
116     {
117       argc--;
118       argv++;
119     }
120   command = argc? *argv : "NOP";
121   
122
123   err = gpgme_new (&ctx);
124   fail_if_err (err);
125
126   err = gpgme_set_protocol (ctx, GPGME_PROTOCOL_ASSUAN);
127   fail_if_err (err);
128
129   err = gpgme_op_assuan_transact_ext (ctx, command, data_cb, NULL,
130                                   inq_cb, NULL, status_cb, NULL, &op_err);
131   fail_if_err (err || op_err);
132
133   gpgme_release (ctx);
134
135   return 0;
136 }
137