2009-05-15 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / common / t-b64.c
bloba230dc0331db27229a75d954529b538bc00e04fd
1 /* t-b64.c - Module tests for b64enc.c and b64dec.c
2 * Copyright (C) 2008 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG 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 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG 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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 /*
22 As of now this is only a test program for manual tests.
28 #include <config.h>
29 #include <stdio.h>
30 #include <stdlib.h>
32 #include "util.h"
34 #define pass() do { ; } while(0)
35 #define fail(a) do { fprintf (stderr, "%s:%d: test %d failed\n",\
36 __FILE__,__LINE__, (a)); \
37 errcount++; \
38 } while(0)
40 static int verbose;
41 static int errcount;
43 static void
44 test_b64enc_pgp (const char *string)
46 gpg_error_t err;
47 struct b64state state;
49 if (!string)
50 string = "a";
52 err = b64enc_start (&state, stdout, "PGP MESSAGE");
53 if (err)
54 fail (1);
56 err = b64enc_write (&state, string, strlen (string));
57 if (err)
58 fail (2);
60 err = b64enc_finish (&state);
61 if (err)
62 fail (3);
64 pass ();
68 static void
69 test_b64enc_file (const char *fname)
71 gpg_error_t err;
72 struct b64state state;
73 FILE *fp;
74 char buffer[50];
75 size_t nread;
77 fp = fname ? fopen (fname, "r") : stdin;
78 if (!fp)
80 fprintf (stderr, "%s:%d: can't open `%s': %s\n",
81 __FILE__, __LINE__, fname? fname:"[stdin]", strerror (errno));
82 fail (0);
85 err = b64enc_start (&state, stdout, "DATA");
86 if (err)
87 fail (1);
89 while ( (nread = fread (buffer, 1, sizeof buffer, fp)) )
91 err = b64enc_write (&state, buffer, nread);
92 if (err)
93 fail (2);
96 err = b64enc_finish (&state);
97 if (err)
98 fail (3);
100 fclose (fp);
101 pass ();
104 static void
105 test_b64dec_file (const char *fname)
107 gpg_error_t err;
108 struct b64state state;
109 FILE *fp;
110 char buffer[50];
111 size_t nread, nbytes;
113 fp = fname ? fopen (fname, "r") : stdin;
114 if (!fp)
116 fprintf (stderr, "%s:%d: can't open `%s': %s\n",
117 __FILE__, __LINE__, fname? fname:"[stdin]", strerror (errno));
118 fail (0);
121 err = b64dec_start (&state, "");
122 if (err)
123 fail (1);
125 while ( (nread = fread (buffer, 1, sizeof buffer, fp)) )
127 err = b64dec_proc (&state, buffer, nread, &nbytes);
128 if (err)
130 if (gpg_err_code (err) == GPG_ERR_EOF)
131 break;
132 fail (2);
134 else if (nbytes)
135 fwrite (buffer, 1, nbytes, stdout);
138 err = b64dec_finish (&state);
139 if (err)
140 fail (3);
142 fclose (fp);
143 pass ();
149 main (int argc, char **argv)
151 int do_encode = 0;
152 int do_decode = 0;
154 if (argc)
155 { argc--; argv++; }
156 if (argc && !strcmp (argv[0], "--verbose"))
158 verbose = 1;
159 argc--; argv++;
162 if (argc && !strcmp (argv[0], "--encode"))
164 do_encode = 1;
165 argc--; argv++;
167 else if (argc && !strcmp (argv[0], "--decode"))
169 do_decode = 1;
170 argc--; argv++;
173 if (do_encode)
174 test_b64enc_file (argc? *argv: NULL);
175 else if (do_decode)
176 test_b64dec_file (argc? *argv: NULL);
177 else
178 test_b64enc_pgp (argc? *argv: NULL);
180 return !!errcount;