~ubuntu-branches/ubuntu/karmic/gnupg2/karmic-updates

« back to all changes in this revision

Viewing changes to common/t-b64.c

  • Committer: Bazaar Package Importer
  • Author(s): Eric Dorland
  • Date: 2009-03-08 22:46:47 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090308224647-gq17gatcl71lrc2k
Tags: 2.0.11-1
* New upstream release. (Closes: #496663)
* debian/control: Make the description a little more distinctive than
  gnupg v1's. Thanks Jari Aalto. (Closes: #496323)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
test_b64enc_pgp (const char *string)
 
45
{
 
46
  gpg_error_t err;
 
47
  struct b64state state;
 
48
 
 
49
  if (!string)
 
50
    string = "a";
 
51
 
 
52
  err = b64enc_start (&state, stdout, "PGP MESSAGE");
 
53
  if (err)
 
54
    fail (1);
 
55
 
 
56
  err = b64enc_write (&state, string, strlen (string));
 
57
  if (err)
 
58
    fail (2);
 
59
 
 
60
  err = b64enc_finish (&state);
 
61
  if (err)
 
62
    fail (3);
 
63
 
 
64
  pass ();
 
65
}
 
66
 
 
67
 
 
68
static void
 
69
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
  fp = fname ? fopen (fname, "r") : stdin;
 
78
  if (!fp)
 
79
    {
 
80
      fprintf (stderr, "%s:%d: can't open `%s': %s\n",
 
81
               __FILE__, __LINE__, fname? fname:"[stdin]", strerror (errno));
 
82
      fail (0);
 
83
    }
 
84
 
 
85
  err = b64enc_start (&state, stdout, "DATA");
 
86
  if (err)
 
87
    fail (1);
 
88
 
 
89
  while ( (nread = fread (buffer, 1, sizeof buffer, fp)) )
 
90
    {
 
91
      err = b64enc_write (&state, buffer, nread);
 
92
      if (err)
 
93
        fail (2);
 
94
    }
 
95
 
 
96
  err = b64enc_finish (&state);
 
97
  if (err)
 
98
    fail (3);
 
99
 
 
100
  fclose (fp);
 
101
  pass ();
 
102
}
 
103
 
 
104
static void
 
105
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
  fp = fname ? fopen (fname, "r") : stdin;
 
114
  if (!fp)
 
115
    {
 
116
      fprintf (stderr, "%s:%d: can't open `%s': %s\n",
 
117
               __FILE__, __LINE__, fname? fname:"[stdin]", strerror (errno));
 
118
      fail (0);
 
119
    }
 
120
 
 
121
  err = b64dec_start (&state, "");
 
122
  if (err)
 
123
    fail (1);
 
124
 
 
125
  while ( (nread = fread (buffer, 1, sizeof buffer, fp)) )
 
126
    {
 
127
      err = b64dec_proc (&state, buffer, nread, &nbytes);
 
128
      if (err)
 
129
        {
 
130
          if (gpg_err_code (err) == GPG_ERR_EOF)
 
131
            break;
 
132
          fail (2);
 
133
        }
 
134
      else if (nbytes)
 
135
        fwrite (buffer, 1, nbytes, stdout);
 
136
    }
 
137
 
 
138
  err = b64dec_finish (&state);
 
139
  if (err)
 
140
    fail (3);
 
141
 
 
142
  fclose (fp);
 
143
  pass ();
 
144
}
 
145
 
 
146
 
 
147
 
 
148
int
 
149
main (int argc, char **argv)
 
150
{
 
151
  int do_encode = 0;
 
152
  int do_decode = 0;
 
153
 
 
154
  if (argc)
 
155
    { argc--; argv++; }
 
156
  if (argc && !strcmp (argv[0], "--verbose"))
 
157
    {
 
158
      verbose = 1;
 
159
      argc--; argv++;
 
160
    }
 
161
 
 
162
  if (argc && !strcmp (argv[0], "--encode"))
 
163
    {
 
164
      do_encode = 1;
 
165
      argc--; argv++;
 
166
    }
 
167
  else if (argc && !strcmp (argv[0], "--decode"))
 
168
    {
 
169
      do_decode = 1;
 
170
      argc--; argv++;
 
171
    }
 
172
 
 
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);
 
179
 
 
180
  return !!errcount;
 
181
}
 
182