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 <https://www.gnu.org/licenses/>.
22 As of now this is only a test program for manual tests.
34 #define pass() do { ; } while(0)
35 #define fail(a) do { fprintf (stderr, "%s:%d: test %d failed\n",\
36 __FILE__,__LINE__, (a)); \
44 test_b64enc_pgp (const char *string)
47 struct b64state state;
52 err = b64enc_start (&state, stdout, "PGP MESSAGE");
56 err = b64enc_write (&state, string, strlen (string));
60 err = b64enc_finish (&state);
69 test_b64enc_file (const char *fname)
72 struct b64state state;
77 fp = fname ? fopen (fname, "r") : stdin;
80 fprintf (stderr, "%s:%d: can't open '%s': %s\n",
81 __FILE__, __LINE__, fname? fname:"[stdin]", strerror (errno));
85 err = b64enc_start (&state, stdout, "DATA");
89 while ( (nread = fread (buffer, 1, sizeof buffer, fp)) )
91 err = b64enc_write (&state, buffer, nread);
96 err = b64enc_finish (&state);
105 test_b64dec_file (const char *fname)
108 struct b64state state;
111 size_t nread, nbytes;
113 fp = fname ? fopen (fname, "r") : stdin;
116 fprintf (stderr, "%s:%d: can't open '%s': %s\n",
117 __FILE__, __LINE__, fname? fname:"[stdin]", strerror (errno));
121 err = b64dec_start (&state, "");
125 while ( (nread = fread (buffer, 1, sizeof buffer, fp)) )
127 err = b64dec_proc (&state, buffer, nread, &nbytes);
130 if (gpg_err_code (err) == GPG_ERR_EOF)
135 fwrite (buffer, 1, nbytes, stdout);
138 err = b64dec_finish (&state);
149 main (int argc, char **argv)
156 if (argc && !strcmp (argv[0], "--verbose"))
162 if (argc && !strcmp (argv[0], "--encode"))
167 else if (argc && !strcmp (argv[0], "--decode"))
174 test_b64enc_file (argc? *argv: NULL);
176 test_b64dec_file (argc? *argv: NULL);
178 test_b64enc_pgp (argc? *argv: NULL);