2003-05-18 Marcus Brinkmann <marcus@g10code.de>
[gpgme.git] / tests / t-data.c
1 /* t-data - Regression tests for the gpgme_data_t abstraction.
2  *      Copyright (C) 2001 g10 Code GmbH
3  *
4  * This file is part of GPGME.
5  *
6  * GPGME is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * GPGME is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19  */
20
21 #include <stddef.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #include <gpgme.h>
28
29 #define fail_if_err(a) do { if(a) {                                          \
30                                fprintf (stderr, "%s:%d: (%i) gpgme_error_t " \
31                                 "%s\n", __FILE__, __LINE__, round,           \
32                                 gpgme_strerror(a));                          \
33                                 exit (1); }                                  \
34                              } while(0)
35
36 static char *
37 make_filename (const char *fname)
38 {
39   const char *srcdir = getenv ("srcdir");
40   char *buf;
41
42   if (!srcdir)
43     srcdir = ".";
44   buf = malloc (strlen(srcdir) + strlen(fname) + 2 );
45   if (!buf)
46     {
47       fprintf (stderr, "%s:%d: could not allocate string: %s\n",
48                __FILE__, __LINE__, strerror (errno));
49       exit (1);
50     }
51   strcpy (buf, srcdir);
52   strcat (buf, "/");
53   strcat (buf, fname);
54   return buf;
55 }
56
57 typedef enum
58   {
59     TEST_INITIALIZER,
60     TEST_INVALID_ARGUMENT,
61     TEST_INOUT_NONE,
62     TEST_INOUT_MEM_NO_COPY,
63     TEST_INOUT_MEM_COPY,
64     TEST_INOUT_MEM_FROM_FILE_COPY,
65     TEST_INOUT_MEM_FROM_INEXISTANT_FILE,
66     TEST_INOUT_MEM_FROM_FILE_NO_COPY,
67     TEST_INOUT_MEM_FROM_FILE_PART_BY_NAME,
68     TEST_INOUT_MEM_FROM_INEXISTANT_FILE_PART,
69     TEST_INOUT_MEM_FROM_FILE_PART_BY_FP,
70     TEST_OUT_CB,
71     TEST_END
72   } round_t;
73
74 const char *text = "Just GNU it!\n";
75 const char *text2 = "Just GNU it!\nJust GNU it!\n";
76
77 int
78 read_cb (void *cb_value, char *buffer, size_t count, size_t *nread)
79 {
80   static int off = 0;
81   int amount = strlen (text) - off;
82   /*  round_t round = *((round_t *) cb_value);  */
83
84   if (!buffer && !count && !nread)
85     {
86       /* Rewind requested.  */
87       off = 0;
88       return 0;
89     }
90   if (! buffer || !nread)
91     return -1;
92   if (amount <= 0)
93     {
94       /* End of file.  */
95       *nread = 0;
96       return -1;
97     }
98   if (amount > count)
99     amount = count;
100   memcpy (buffer, text, amount);
101   off += amount;
102   *nread = amount;
103   return 0;
104 }
105
106 void
107 read_once_test (round_t round, gpgme_data_t data)
108 {
109   char buffer[1024];
110   size_t read;
111
112   read = gpgme_data_read (data, buffer, sizeof (buffer));
113
114   if (read != strlen (text) || strncmp (buffer, text, strlen (text)))
115     {
116       fprintf (stderr, "%s:%d: (%i) gpgme_data_read returned wrong data\n",
117                __FILE__, __LINE__, round);
118       exit (1);
119     }
120
121   read = gpgme_data_read (data, buffer, sizeof (buffer));
122   if (read)
123     {
124       fprintf (stderr, "%s:%d: (%i) gpgme_data_read did not signal EOF\n",
125                __FILE__, __LINE__, round);
126       exit (1);
127     }
128 }
129
130 void
131 read_test (round_t round, gpgme_data_t data)
132 {
133   char buffer[1024];
134   size_t read;
135
136   if (round == TEST_INOUT_NONE)
137     {
138       read = gpgme_data_read (data, buffer, sizeof (buffer));
139       if (read > 0)
140         {
141           fprintf (stderr, "%s:%d: (%i) gpgme_data_read succeded unexpectedly\n",
142                    __FILE__, __LINE__, round);
143           exit (1);
144         }
145       return;
146     }
147
148   read_once_test (round, data);
149   gpgme_data_seek (data, 0, SEEK_SET);
150   read_once_test (round, data);
151 }
152
153 void
154 write_test (round_t round, gpgme_data_t data)
155 {
156   char buffer[1024];
157   size_t amt;
158
159   amt = gpgme_data_write (data, text, strlen (text));
160   if (amt != strlen (text))
161     fail_if_err (GPGME_File_Error);
162
163   gpgme_data_seek (data, 0, SEEK_SET);
164
165   if (round == TEST_INOUT_NONE)
166     read_once_test (round, data);
167   else
168     {
169       amt = gpgme_data_read (data, buffer, sizeof (buffer));
170
171       if (amt != strlen (text2) || strncmp (buffer, text2, strlen (text2)))
172         {
173           fprintf (stderr, "%s:%d: (%i) gpgme_data_read returned wrong data\n",
174                    __FILE__, __LINE__, round);
175           exit (1);
176         }
177
178       amt = gpgme_data_read (data, buffer, sizeof (buffer));
179       if (amt)
180         {
181           fprintf (stderr, "%s:%d: (%i) gpgme_data_read did not signal EOF\n",
182                    __FILE__, __LINE__, round);
183           exit (1);
184         }
185     }
186 }
187
188 int 
189 main (int argc, char **argv)
190 {
191   round_t round = TEST_INITIALIZER;
192   const char *text_filename = make_filename ("t-data-1.txt");
193   const char *longer_text_filename = make_filename ("t-data-2.txt");
194   const char *missing_filename = "this-file-surely-does-not-exist";
195   gpgme_error_t err = GPGME_No_Error;
196   gpgme_data_t data;
197
198   while (++round)
199     {
200       switch (round)
201         {
202         case TEST_INVALID_ARGUMENT:
203           err = gpgme_data_new (NULL);
204           if (!err)
205             {
206               fprintf (stderr, "%s:%d: gpgme_data_new on NULL pointer succeeded "
207                        "unexpectedly\n", __FILE__, __LINE__);
208               exit (1);
209             }
210           continue;
211         case TEST_INOUT_NONE:
212           err = gpgme_data_new (&data);
213           break;
214         case TEST_INOUT_MEM_NO_COPY:
215           err = gpgme_data_new_from_mem (&data, text, strlen (text), 0);
216           break;
217         case TEST_INOUT_MEM_COPY:
218           err = gpgme_data_new_from_mem (&data, text, strlen (text), 1);
219           break;
220         case TEST_INOUT_MEM_FROM_FILE_COPY:
221           err = gpgme_data_new_from_file (&data, text_filename, 1);
222           break;
223         case TEST_INOUT_MEM_FROM_INEXISTANT_FILE:
224           err = gpgme_data_new_from_file (&data, missing_filename, 1);
225           if (!err)
226             {
227               fprintf (stderr, "%s:%d: gpgme_data_new_from_file on inexistant "
228                        "file succeeded unexpectedly\n", __FILE__, __LINE__);
229               exit (1);
230             }
231           continue;
232         case TEST_INOUT_MEM_FROM_FILE_NO_COPY:
233           err = gpgme_data_new_from_file (&data, text_filename, 0);
234           /* This is not implemented yet.  */
235           if (err == GPGME_Not_Implemented || err == GPGME_Invalid_Value)
236             continue;
237           break;
238         case TEST_INOUT_MEM_FROM_FILE_PART_BY_NAME:
239           err = gpgme_data_new_from_filepart (&data, longer_text_filename, 0,
240                                               strlen (text), strlen (text));
241           break;
242         case TEST_INOUT_MEM_FROM_INEXISTANT_FILE_PART:
243           err = gpgme_data_new_from_filepart (&data, missing_filename, 0,
244                                               strlen (text), strlen (text));
245           if (!err)
246             {
247               fprintf (stderr, "%s:%d: gpgme_data_new_from_file on inexistant "
248                        "file succeeded unexpectedly\n", __FILE__, __LINE__);
249               exit (1);
250             }
251           continue;
252         case TEST_INOUT_MEM_FROM_FILE_PART_BY_FP:
253           {
254             FILE *fp = fopen (longer_text_filename, "rb");
255             if (! fp)
256               {
257                 fprintf (stderr, "%s:%d: fopen: %s\n", __FILE__, __LINE__,
258                          strerror (errno));
259                 exit (1);
260               }
261             err = gpgme_data_new_from_filepart (&data, 0, fp,
262                                                 strlen (text), strlen (text));
263           }
264           break;
265         case TEST_OUT_CB:
266           err = gpgme_data_new_with_read_cb (&data, read_cb, &round);
267           break;
268         case TEST_END:
269           return 0;
270         case TEST_INITIALIZER:
271           /* Shouldn't happen.  */
272           fprintf (stderr, "%s:%d: impossible condition\n", __FILE__, __LINE__);
273           exit (1);
274         }
275       fail_if_err (err);
276
277       read_test (round, data);
278       if (round != TEST_OUT_CB)
279         write_test (round, data);
280       gpgme_data_release (data);
281     }
282   return 0;
283 }