error: Avoid "function declaration isn't a prototype" warning.
[gnulib.git] / tests / test-base64.c
blobc7fd80191dfe16884a4ccfde43130ad51679f044
1 /* Self tests for base64.
2 Copyright (C) 2004, 2008-2017 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/>. */
18 #include <config.h>
20 #include "base64.h"
22 #include <stddef.h>
23 #include <stdbool.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdint.h>
28 #include "macros.h"
30 int
31 main (void)
33 const char *in = "abcdefghijklmnop";
34 const char *b64in = "YWJjZGVmZw==";
35 char out[255];
36 size_t len;
37 bool ok;
38 char *p;
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);
80 /* Decode. */
82 memset (out, 0x42, sizeof (out));
83 len = 0;
84 ok = base64_decode (b64in, 4, out, &len);
85 ASSERT (ok);
86 ASSERT (len == 0);
88 memset (out, 0x42, sizeof (out));
89 len = 1;
90 ok = base64_decode (b64in, 4, out, &len);
91 ASSERT (ok);
92 ASSERT (len == 1);
93 ASSERT (memcmp (out, "abcdefg", 1) == 0);
95 memset (out, 0x42, sizeof (out));
96 len = 2;
97 ok = base64_decode (b64in, 4, out, &len);
98 ASSERT (ok);
99 ASSERT (len == 2);
100 ASSERT (memcmp (out, "abcdefg", 2) == 0);
102 memset (out, 0x42, sizeof (out));
103 len = 3;
104 ok = base64_decode (b64in, 4, out, &len);
105 ASSERT (ok);
106 ASSERT (len == 3);
107 ASSERT (memcmp (out, "abcdefg", 3) == 0);
109 memset (out, 0x42, sizeof (out));
110 len = 4;
111 ok = base64_decode (b64in, 4, out, &len);
112 ASSERT (ok);
113 ASSERT (len == 3);
114 ASSERT (memcmp (out, "abcdefg", 3) == 0);
116 memset (out, 0x42, sizeof (out));
117 len = 100;
118 ok = base64_decode (b64in, strlen (b64in), out, &len);
119 ASSERT (ok);
120 ASSERT (len == 7);
121 ASSERT (memcmp (out, "abcdefg", 7) == 0);
123 /* Allocating encode */
125 len = base64_encode_alloc (in, strlen (in), &p);
126 ASSERT (len == 24);
127 ASSERT (strcmp (p, "YWJjZGVmZ2hpamtsbW5vcA==") == 0);
128 free (p);
130 len = base64_encode_alloc (in, SIZE_MAX - 5, &p);
131 ASSERT (len == 0);
133 /* Decode context function */
135 struct base64_decode_context ctx;
137 base64_decode_ctx_init (&ctx);
139 len = sizeof (out);
140 ok = base64_decode_ctx (&ctx, b64in, strlen (b64in), out, &len);
141 ASSERT (ok);
142 ASSERT (len == 7);
143 ASSERT (memcmp (out, "abcdefg", len) == 0);
146 /* Allocating decode context function */
148 ok = base64_decode_alloc_ctx (NULL, b64in, strlen (b64in), &p, &len);
149 ASSERT (ok);
150 ASSERT (len == 7);
151 ASSERT (memcmp (out, "abcdefg", len) == 0);
152 free (p);
155 struct base64_decode_context ctx;
156 const char *newlineb64 = "YWJjZG\nVmZ2hp\namtsbW5vcA==";
158 base64_decode_ctx_init (&ctx);
160 ok = base64_decode_alloc_ctx (&ctx, newlineb64, strlen (newlineb64), &p, &len);
161 ASSERT (ok);
162 ASSERT (len == strlen (in));
163 ASSERT (memcmp (p, in, len) == 0);
164 free (p);
168 struct base64_decode_context ctx;
169 base64_decode_ctx_init (&ctx);
171 ok = base64_decode_alloc_ctx (&ctx, "YW\nJjZGVmZ2hp", 13, &p, &len);
172 ASSERT (ok);
173 ASSERT (len == 9);
174 ASSERT (memcmp (p, "abcdefghi", len) == 0);
175 free (p);
177 base64_decode_ctx_init (&ctx);
179 ok = base64_decode_alloc_ctx (&ctx, "YW\n", 3, &p, &len);
180 ASSERT (ok);
181 ASSERT (len == 0);
182 free (p);
184 ok = base64_decode_alloc_ctx (&ctx, "JjZGVmZ2", 8, &p, &len);
185 ASSERT (ok);
186 ASSERT (len == 6);
187 ASSERT (memcmp (p, "abcdef", len) == 0);
188 free (p);
190 ok = base64_decode_alloc_ctx (&ctx, "hp", 2, &p, &len);
191 ASSERT (ok);
192 ASSERT (len == 3);
193 ASSERT (memcmp (p, "ghi", len) == 0);
194 free (p);
196 ok = base64_decode_alloc_ctx (&ctx, "", 0, &p, &len);
197 ASSERT (ok);
198 free (p);
202 struct base64_decode_context ctx;
203 const char *newlineb64 = "\n\n\n\n\n";
205 base64_decode_ctx_init (&ctx);
207 ok = base64_decode_alloc_ctx (&ctx, newlineb64, strlen (newlineb64), &p, &len);
208 ASSERT (ok);
209 ASSERT (len == 0);
210 free (p);
213 ok = base64_decode_alloc_ctx (NULL, " ! ", 3, &p, &len);
214 ASSERT (!ok);
216 ok = base64_decode_alloc_ctx (NULL, "abc\ndef", 7, &p, &len);
217 ASSERT (!ok);
219 ok = base64_decode_alloc_ctx (NULL, "aa", 2, &p, &len);
220 ASSERT (!ok);
222 ok = base64_decode_alloc_ctx (NULL, "aa=", 3, &p, &len);
223 ASSERT (!ok);
225 ok = base64_decode_alloc_ctx (NULL, "aax", 3, &p, &len);
226 ASSERT (!ok);
228 ok = base64_decode_alloc_ctx (NULL, "aa=X", 4, &p, &len);
229 ASSERT (!ok);
231 ok = base64_decode_alloc_ctx (NULL, "aa=X", 4, &p, &len);
232 ASSERT (!ok);
234 ok = base64_decode_alloc_ctx (NULL, "aax=X", 5, &p, &len);
235 ASSERT (!ok);
237 return 0;