1 /* Self tests for base64.
2 Copyright (C) 2004 Free Software Foundation, Inc.
3 Written by Simon Josefsson.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
29 main (int argc, char *argv[])
31 const char *in = "abcdefghijklmnop";
32 const char *b64in = "YWJjZGVmZw==";
37 memset (out, 0x42, sizeof (out));
38 base64_encode (in, 0, out, 0);
40 fprintf (stderr, "failure\n");
42 memset (out, 0x42, sizeof (out));
43 base64_encode (in, 1, out, 1);
44 if (memcmp (out, "YQ==", 1) != 0)
47 fprintf (stderr, "failure (%s)\n", out);
50 memset (out, 0x42, sizeof (out));
51 base64_encode (in, 1, out, 2);
52 if (memcmp (out, "YQ==", 2) != 0)
55 fprintf (stderr, "failure (%s)\n", out);
58 memset (out, 0x42, sizeof (out));
59 base64_encode (in, 1, out, 3);
60 if (memcmp (out, "YQ==", 3) != 0)
63 fprintf (stderr, "failure (%s)\n", out);
66 memset (out, 0x42, sizeof (out));
67 base64_encode (in, 1, out, 4);
68 if (memcmp (out, "YQ==", 4) != 0)
71 fprintf (stderr, "failure (%s)\n", out);
74 memset (out, 0x42, sizeof (out));
75 base64_encode (in, 1, out, 8);
76 if (memcmp (out, "YQ==", 4) != 0)
79 fprintf (stderr, "failure (%s)\n", out);
82 memset (out, 0x42, sizeof (out));
83 base64_encode (in, 2, out, 4);
84 if (memcmp (out, "YWI=", 4) != 0)
87 fprintf (stderr, "failure (%s)\n", out);
90 memset (out, 0x42, sizeof (out));
91 base64_encode (in, 3, out, 4);
92 if (memcmp (out, "YWJj", 4) != 0)
95 fprintf (stderr, "failure (%s)\n", out);
98 memset (out, 0x42, sizeof (out));
99 base64_encode (in, 4, out, 5);
100 if (memcmp (out, "YWJjZA==", 5) != 0)
103 fprintf (stderr, "failure (%s)\n", out);
106 memset (out, 0x42, sizeof (out));
107 base64_encode (in, 4, out, 100);
108 if (memcmp (out, "YWJjZA==", 6) != 0)
111 fprintf (stderr, "failure (%s)\n", out);
116 memset (out, 0x42, sizeof (out));
118 ok = base64_decode (b64in, 4, out, &len);
120 fprintf (stderr, "decode failed\n");
122 fprintf (stderr, "failure (%d)\n", len);
124 memset (out, 0x42, sizeof (out));
126 ok = base64_decode (b64in, 4, out, &len);
128 fprintf (stderr, "decode failed\n");
129 if (len != 1 || memcmp (out, "abcdefg", 1) != 0)
132 fprintf (stderr, "failure (%d: %s)\n", len, out);
135 memset (out, 0x42, sizeof (out));
137 ok = base64_decode (b64in, 4, out, &len);
139 fprintf (stderr, "decode failed\n");
140 if (len != 2 || memcmp (out, "abcdefg", 2) != 0)
143 fprintf (stderr, "failure (%d: %s)\n", len, out);
146 memset (out, 0x42, sizeof (out));
148 ok = base64_decode (b64in, 4, out, &len);
150 fprintf (stderr, "decode failed\n");
151 if (len != 3 || memcmp (out, "abcdefg", 3) != 0)
154 fprintf (stderr, "failure (%d: %s)\n", len, out);
157 memset (out, 0x42, sizeof (out));
159 ok = base64_decode (b64in, 4, out, &len);
161 fprintf (stderr, "decode failed\n");
162 if (len != 3 || memcmp (out, "abcdefg", 3) != 0)
165 fprintf (stderr, "failure (%d: %s)\n", len, out);
168 memset (out, 0x42, sizeof (out));
170 ok = base64_decode (b64in, strlen (b64in), out, &len);
172 fprintf (stderr, "decode failed\n");
173 if (len != 7 || memcmp (out, "abcdefg", 7) != 0)
176 fprintf (stderr, "failure (%d: %s)\n", len, out);