LCOV - code coverage report
Current view: top level - common - t-b64.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 73 0.0 %
Date: 2016-09-12 13:01:59 Functions: 0 4 0.0 %

          Line data    Source code
       1             : /* t-b64.c - Module tests for b64enc.c and b64dec.c
       2             :  *      Copyright (C) 2008 Free Software Foundation, Inc.
       3             :  *
       4             :  * This file is part of GnuPG.
       5             :  *
       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.
      10             :  *
      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.
      15             :  *
      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/>.
      18             :  */
      19             : 
      20             : /*
      21             : 
      22             :    As of now this is only a test program for manual tests.
      23             : 
      24             :  */
      25             : 
      26             : 
      27             : 
      28             : #include <config.h>
      29             : #include <stdio.h>
      30             : #include <stdlib.h>
      31             : 
      32             : #include "util.h"
      33             : 
      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)
      39             : 
      40             : static int verbose;
      41             : static int errcount;
      42             : 
      43             : static void
      44           0 : test_b64enc_pgp (const char *string)
      45             : {
      46             :   gpg_error_t err;
      47             :   struct b64state state;
      48             : 
      49           0 :   if (!string)
      50           0 :     string = "a";
      51             : 
      52           0 :   err = b64enc_start (&state, stdout, "PGP MESSAGE");
      53           0 :   if (err)
      54           0 :     fail (1);
      55             : 
      56           0 :   err = b64enc_write (&state, string, strlen (string));
      57           0 :   if (err)
      58           0 :     fail (2);
      59             : 
      60           0 :   err = b64enc_finish (&state);
      61           0 :   if (err)
      62           0 :     fail (3);
      63             : 
      64             :   pass ();
      65           0 : }
      66             : 
      67             : 
      68             : static void
      69           0 : test_b64enc_file (const char *fname)
      70             : {
      71             :   gpg_error_t err;
      72             :   struct b64state state;
      73             :   FILE *fp;
      74             :   char buffer[50];
      75             :   size_t nread;
      76             : 
      77           0 :   fp = fname ? fopen (fname, "r") : stdin;
      78           0 :   if (!fp)
      79             :     {
      80           0 :       fprintf (stderr, "%s:%d: can't open '%s': %s\n",
      81           0 :                __FILE__, __LINE__, fname? fname:"[stdin]", strerror (errno));
      82           0 :       fail (0);
      83             :     }
      84             : 
      85           0 :   err = b64enc_start (&state, stdout, "DATA");
      86           0 :   if (err)
      87           0 :     fail (1);
      88             : 
      89           0 :   while ( (nread = fread (buffer, 1, sizeof buffer, fp)) )
      90             :     {
      91           0 :       err = b64enc_write (&state, buffer, nread);
      92           0 :       if (err)
      93           0 :         fail (2);
      94             :     }
      95             : 
      96           0 :   err = b64enc_finish (&state);
      97           0 :   if (err)
      98           0 :     fail (3);
      99             : 
     100           0 :   fclose (fp);
     101             :   pass ();
     102           0 : }
     103             : 
     104             : static void
     105           0 : test_b64dec_file (const char *fname)
     106             : {
     107             :   gpg_error_t err;
     108             :   struct b64state state;
     109             :   FILE *fp;
     110             :   char buffer[50];
     111             :   size_t nread, nbytes;
     112             : 
     113           0 :   fp = fname ? fopen (fname, "r") : stdin;
     114           0 :   if (!fp)
     115             :     {
     116           0 :       fprintf (stderr, "%s:%d: can't open '%s': %s\n",
     117           0 :                __FILE__, __LINE__, fname? fname:"[stdin]", strerror (errno));
     118           0 :       fail (0);
     119             :     }
     120             : 
     121           0 :   err = b64dec_start (&state, "");
     122           0 :   if (err)
     123           0 :     fail (1);
     124             : 
     125           0 :   while ( (nread = fread (buffer, 1, sizeof buffer, fp)) )
     126             :     {
     127           0 :       err = b64dec_proc (&state, buffer, nread, &nbytes);
     128           0 :       if (err)
     129             :         {
     130           0 :           if (gpg_err_code (err) == GPG_ERR_EOF)
     131           0 :             break;
     132           0 :           fail (2);
     133             :         }
     134           0 :       else if (nbytes)
     135           0 :         fwrite (buffer, 1, nbytes, stdout);
     136             :     }
     137             : 
     138           0 :   err = b64dec_finish (&state);
     139           0 :   if (err)
     140           0 :     fail (3);
     141             : 
     142           0 :   fclose (fp);
     143             :   pass ();
     144           0 : }
     145             : 
     146             : 
     147             : 
     148             : int
     149           0 : main (int argc, char **argv)
     150             : {
     151           0 :   int do_encode = 0;
     152           0 :   int do_decode = 0;
     153             : 
     154           0 :   if (argc)
     155           0 :     { argc--; argv++; }
     156           0 :   if (argc && !strcmp (argv[0], "--verbose"))
     157             :     {
     158           0 :       verbose = 1;
     159           0 :       argc--; argv++;
     160             :     }
     161             : 
     162           0 :   if (argc && !strcmp (argv[0], "--encode"))
     163             :     {
     164           0 :       do_encode = 1;
     165           0 :       argc--; argv++;
     166             :     }
     167           0 :   else if (argc && !strcmp (argv[0], "--decode"))
     168             :     {
     169           0 :       do_decode = 1;
     170           0 :       argc--; argv++;
     171             :     }
     172             : 
     173           0 :   if (do_encode)
     174           0 :     test_b64enc_file (argc? *argv: NULL);
     175           0 :   else if (do_decode)
     176           0 :     test_b64dec_file (argc? *argv: NULL);
     177             :   else
     178           0 :     test_b64enc_pgp (argc? *argv: NULL);
     179             : 
     180           0 :   return !!errcount;
     181             : }

Generated by: LCOV version 1.11