1 /* Self tests for base64.
2 Copyright (C) 2004, 2008-2011 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/>. */
33 const char *in = "abcdefghijklmnop";
34 const char *b64in = "YWJjZGVmZw==";
40 memset (out, 0x42, sizeof (out));
41 base64_encode (in, 0, out, 0);
42 ASSERT (out[0] == '\x42');
44 memset (out, 0x42, sizeof (out));
45 base64_encode (in, 1, out, 1);
46 ASSERT (memcmp (out, "YQ==", 1) == 0);
48 memset (out, 0x42, sizeof (out));
49 base64_encode (in, 1, out, 2);
50 ASSERT (memcmp (out, "YQ==", 2) == 0);
52 memset (out, 0x42, sizeof (out));
53 base64_encode (in, 1, out, 3);
54 ASSERT (memcmp (out, "YQ==", 3) == 0);
56 memset (out, 0x42, sizeof (out));
57 base64_encode (in, 1, out, 4);
58 ASSERT (memcmp (out, "YQ==", 4) == 0);
60 memset (out, 0x42, sizeof (out));
61 base64_encode (in, 1, out, 8);
62 ASSERT (memcmp (out, "YQ==", 4) == 0);
64 memset (out, 0x42, sizeof (out));
65 base64_encode (in, 2, out, 4);
66 ASSERT (memcmp (out, "YWI=", 4) == 0);
68 memset (out, 0x42, sizeof (out));
69 base64_encode (in, 3, out, 4);
70 ASSERT (memcmp (out, "YWJj", 4) == 0);
72 memset (out, 0x42, sizeof (out));
73 base64_encode (in, 4, out, 5);
74 ASSERT (memcmp (out, "YWJjZA==", 5) == 0);
76 memset (out, 0x42, sizeof (out));
77 base64_encode (in, 4, out, 100);
78 ASSERT (memcmp (out, "YWJjZA==", 6) == 0);
82 memset (out, 0x42, sizeof (out));
84 ok = base64_decode (b64in, 4, out, &len);
88 memset (out, 0x42, sizeof (out));
90 ok = base64_decode (b64in, 4, out, &len);
93 ASSERT (memcmp (out, "abcdefg", 1) == 0);
95 memset (out, 0x42, sizeof (out));
97 ok = base64_decode (b64in, 4, out, &len);
100 ASSERT (memcmp (out, "abcdefg", 2) == 0);
102 memset (out, 0x42, sizeof (out));
104 ok = base64_decode (b64in, 4, out, &len);
107 ASSERT (memcmp (out, "abcdefg", 3) == 0);
109 memset (out, 0x42, sizeof (out));
111 ok = base64_decode (b64in, 4, out, &len);
114 ASSERT (memcmp (out, "abcdefg", 3) == 0);
116 memset (out, 0x42, sizeof (out));
118 ok = base64_decode (b64in, strlen (b64in), out, &len);
121 ASSERT (memcmp (out, "abcdefg", 7) == 0);
123 /* Allocating encode */
125 len = base64_encode_alloc (in, strlen (in), &p);
127 ASSERT (strcmp (p, "YWJjZGVmZ2hpamtsbW5vcA==") == 0);
130 len = base64_encode_alloc (in, SIZE_MAX - 5, &p);
133 /* Decode context function */
135 struct base64_decode_context ctx;
137 base64_decode_ctx_init (&ctx);
140 ok = base64_decode_ctx (&ctx, b64in, strlen (b64in), out, &len);
143 ASSERT (memcmp (out, "abcdefg", len) == 0);
146 /* Allocating decode context function */
148 ok = base64_decode_alloc_ctx (NULL, b64in, strlen (b64in), &p, &len);
151 ASSERT (memcmp (out, "abcdefg", len) == 0);
154 struct base64_decode_context ctx;
155 const char *newlineb64 = "YWJjZG\nVmZ2hp\namtsbW5vcA==";
157 base64_decode_ctx_init (&ctx);
159 ok = base64_decode_alloc_ctx (&ctx, newlineb64, strlen (newlineb64), &p, &len);
161 ASSERT (len == strlen (in));
162 ASSERT (memcmp (p, in, len) == 0);
166 struct base64_decode_context ctx;
167 base64_decode_ctx_init (&ctx);
169 ok = base64_decode_alloc_ctx (&ctx, "YW\nJjZGVmZ2hp", 13, &p, &len);
172 ASSERT (memcmp (p, "abcdefghi", len) == 0);
174 base64_decode_ctx_init (&ctx);
176 ok = base64_decode_alloc_ctx (&ctx, "YW\n", 3, &p, &len);
180 ok = base64_decode_alloc_ctx (&ctx, "JjZGVmZ2", 8, &p, &len);
183 ASSERT (memcmp (p, "abcdef", len) == 0);
185 ok = base64_decode_alloc_ctx (&ctx, "hp", 2, &p, &len);
188 /* Actually this looks buggy. Shouldn't output be 'ghi'? */
189 ASSERT (memcmp (p, "gh", len) == 0);
190 ok = base64_decode_alloc_ctx (&ctx, "", 0, &p, &len);
195 struct base64_decode_context ctx;
196 const char *newlineb64 = "\n\n\n\n\n";
198 base64_decode_ctx_init (&ctx);
200 ok = base64_decode_alloc_ctx (&ctx, newlineb64, strlen (newlineb64), &p, &len);
205 ok = base64_decode_alloc_ctx (NULL, " ! ", 3, &p, &len);
208 ok = base64_decode_alloc_ctx (NULL, "abc\ndef", 7, &p, &len);
211 ok = base64_decode_alloc_ctx (NULL, "aa", 2, &p, &len);
214 ok = base64_decode_alloc_ctx (NULL, "aa=", 3, &p, &len);
217 ok = base64_decode_alloc_ctx (NULL, "aax", 3, &p, &len);
220 ok = base64_decode_alloc_ctx (NULL, "aa=X", 4, &p, &len);
223 ok = base64_decode_alloc_ctx (NULL, "aa=X", 4, &p, &len);
226 ok = base64_decode_alloc_ctx (NULL, "aax=X", 5, &p, &len);