1 /* Self tests for base64.
2 Copyright (C) 2004, 2008, 2009 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 3 of the License, or
8 (at your option) any later version.
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
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 #define ASSERT(expr) \
35 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
45 const char *in = "abcdefghijklmnop";
46 const char *b64in = "YWJjZGVmZw==";
52 memset (out, 0x42, sizeof (out));
53 base64_encode (in, 0, out, 0);
54 ASSERT(out[0] == '\x42');
56 memset (out, 0x42, sizeof (out));
57 base64_encode (in, 1, out, 1);
58 ASSERT (memcmp (out, "YQ==", 1) == 0);
60 memset (out, 0x42, sizeof (out));
61 base64_encode (in, 1, out, 2);
62 ASSERT (memcmp (out, "YQ==", 2) == 0);
64 memset (out, 0x42, sizeof (out));
65 base64_encode (in, 1, out, 3);
66 ASSERT (memcmp (out, "YQ==", 3) == 0);
68 memset (out, 0x42, sizeof (out));
69 base64_encode (in, 1, out, 4);
70 ASSERT (memcmp (out, "YQ==", 4) == 0);
72 memset (out, 0x42, sizeof (out));
73 base64_encode (in, 1, out, 8);
74 ASSERT (memcmp (out, "YQ==", 4) == 0);
76 memset (out, 0x42, sizeof (out));
77 base64_encode (in, 2, out, 4);
78 ASSERT (memcmp (out, "YWI=", 4) == 0);
80 memset (out, 0x42, sizeof (out));
81 base64_encode (in, 3, out, 4);
82 ASSERT (memcmp (out, "YWJj", 4) == 0);
84 memset (out, 0x42, sizeof (out));
85 base64_encode (in, 4, out, 5);
86 ASSERT (memcmp (out, "YWJjZA==", 5) == 0);
88 memset (out, 0x42, sizeof (out));
89 base64_encode (in, 4, out, 100);
90 ASSERT (memcmp (out, "YWJjZA==", 6) == 0);
94 memset (out, 0x42, sizeof (out));
96 ok = base64_decode (b64in, 4, out, &len);
100 memset (out, 0x42, sizeof (out));
102 ok = base64_decode (b64in, 4, out, &len);
105 ASSERT (memcmp (out, "abcdefg", 1) == 0);
107 memset (out, 0x42, sizeof (out));
109 ok = base64_decode (b64in, 4, out, &len);
112 ASSERT (memcmp (out, "abcdefg", 2) == 0);
114 memset (out, 0x42, sizeof (out));
116 ok = base64_decode (b64in, 4, out, &len);
119 ASSERT (memcmp (out, "abcdefg", 3) == 0);
121 memset (out, 0x42, sizeof (out));
123 ok = base64_decode (b64in, 4, out, &len);
126 ASSERT (memcmp (out, "abcdefg", 3) == 0);
128 memset (out, 0x42, sizeof (out));
130 ok = base64_decode (b64in, strlen (b64in), out, &len);
133 ASSERT (memcmp (out, "abcdefg", 7) == 0);
135 /* Allocating encode */
137 len = base64_encode_alloc (in, strlen (in), &p);
139 ASSERT (strcmp (p, "YWJjZGVmZ2hpamtsbW5vcA==") == 0);
142 len = base64_encode_alloc (in, SIZE_MAX - 5, &p);
145 /* Decode context function */
147 struct base64_decode_context ctx;
149 base64_decode_ctx_init (&ctx);
152 ok = base64_decode_ctx (&ctx, b64in, strlen (b64in), out, &len);
155 ASSERT (memcmp (out, "abcdefg", len) == 0);
158 /* Allocating decode context function */
160 ok = base64_decode_alloc_ctx (NULL, b64in, strlen (b64in), &p, &len);
163 ASSERT (memcmp (out, "abcdefg", len) == 0);
166 struct base64_decode_context ctx;
167 const char *newlineb64 = "YWJjZG\nVmZ2hp\namtsbW5vcA==";
169 base64_decode_ctx_init (&ctx);
171 ok = base64_decode_alloc_ctx (&ctx, newlineb64, strlen (newlineb64), &p, &len);
173 ASSERT (len == strlen (in));
174 ASSERT (memcmp (p, in, len) == 0);
178 struct base64_decode_context ctx;
179 base64_decode_ctx_init (&ctx);
181 ok = base64_decode_alloc_ctx (&ctx, "YW\nJjZGVmZ2hp", 13, &p, &len);
184 ASSERT (memcmp (p, "abcdefghi", len) == 0);
186 base64_decode_ctx_init (&ctx);
188 ok = base64_decode_alloc_ctx (&ctx, "YW\n", 3, &p, &len);
192 ok = base64_decode_alloc_ctx (&ctx, "JjZGVmZ2", 8, &p, &len);
195 ASSERT (memcmp (p, "abcdef", len) == 0);
197 ok = base64_decode_alloc_ctx (&ctx, "hp", 2, &p, &len);
200 /* Actually this looks buggy. Shouldn't output be 'ghi'? */
201 ASSERT (memcmp (p, "gh", len) == 0);
202 ok = base64_decode_alloc_ctx (&ctx, "", 0, &p, &len);
207 struct base64_decode_context ctx;
208 const char *newlineb64 = "\n\n\n\n\n";
210 base64_decode_ctx_init (&ctx);
212 ok = base64_decode_alloc_ctx (&ctx, newlineb64, strlen (newlineb64), &p, &len);
217 ok = base64_decode_alloc_ctx (NULL, " ! ", 3, &p, &len);
220 ok = base64_decode_alloc_ctx (NULL, "abc\ndef", 7, &p, &len);
223 ok = base64_decode_alloc_ctx (NULL, "aa", 2, &p, &len);
226 ok = base64_decode_alloc_ctx (NULL, "aa=", 3, &p, &len);
229 ok = base64_decode_alloc_ctx (NULL, "aax", 3, &p, &len);
232 ok = base64_decode_alloc_ctx (NULL, "aa=X", 4, &p, &len);
235 ok = base64_decode_alloc_ctx (NULL, "aa=X", 4, &p, &len);
238 ok = base64_decode_alloc_ctx (NULL, "aax=X", 5, &p, &len);