1 /* Self tests for base64.
2 Copyright (C) 2004, 2008-2025 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 <https://www.gnu.org/licenses/>. */
32 const char *in
= "abcdefghijklmnop";
33 const char *b64in
= "YWJjZGVmZw==";
39 memset (out
, 0x42, sizeof (out
));
40 base64_encode (in
, 0, out
, 0);
41 ASSERT (out
[0] == '\x42');
43 memset (out
, 0x42, sizeof (out
));
44 base64_encode (in
, 1, out
, 1);
45 ASSERT (memcmp (out
, "YQ==", 1) == 0);
47 memset (out
, 0x42, sizeof (out
));
48 base64_encode (in
, 1, out
, 2);
49 ASSERT (memcmp (out
, "YQ==", 2) == 0);
51 memset (out
, 0x42, sizeof (out
));
52 base64_encode (in
, 1, out
, 3);
53 ASSERT (memcmp (out
, "YQ==", 3) == 0);
55 memset (out
, 0x42, sizeof (out
));
56 base64_encode (in
, 1, out
, 4);
57 ASSERT (memcmp (out
, "YQ==", 4) == 0);
59 memset (out
, 0x42, sizeof (out
));
60 base64_encode (in
, 1, out
, 8);
61 ASSERT (memcmp (out
, "YQ==", 4) == 0);
63 memset (out
, 0x42, sizeof (out
));
64 base64_encode (in
, 2, out
, 4);
65 ASSERT (memcmp (out
, "YWI=", 4) == 0);
67 memset (out
, 0x42, sizeof (out
));
68 base64_encode (in
, 3, out
, 4);
69 ASSERT (memcmp (out
, "YWJj", 4) == 0);
71 memset (out
, 0x42, sizeof (out
));
72 base64_encode (in
, 4, out
, 5);
73 ASSERT (memcmp (out
, "YWJjZA==", 5) == 0);
75 memset (out
, 0x42, sizeof (out
));
76 base64_encode (in
, 4, out
, 100);
77 ASSERT (memcmp (out
, "YWJjZA==", 6) == 0);
81 memset (out
, 0x42, sizeof (out
));
83 ok
= base64_decode (b64in
, 4, out
, &len
);
87 memset (out
, 0x42, sizeof (out
));
89 ok
= base64_decode (b64in
, 4, out
, &len
);
92 ASSERT (memcmp (out
, "abcdefg", 1) == 0);
94 memset (out
, 0x42, sizeof (out
));
96 ok
= base64_decode (b64in
, 4, out
, &len
);
99 ASSERT (memcmp (out
, "abcdefg", 2) == 0);
101 memset (out
, 0x42, sizeof (out
));
103 ok
= base64_decode (b64in
, 4, out
, &len
);
106 ASSERT (memcmp (out
, "abcdefg", 3) == 0);
108 memset (out
, 0x42, sizeof (out
));
110 ok
= base64_decode (b64in
, 4, out
, &len
);
113 ASSERT (memcmp (out
, "abcdefg", 3) == 0);
115 memset (out
, 0x42, sizeof (out
));
117 ok
= base64_decode (b64in
, strlen (b64in
), out
, &len
);
120 ASSERT (memcmp (out
, "abcdefg", 7) == 0);
122 /* Allocating encode */
124 len
= base64_encode_alloc (in
, strlen (in
), &p
);
126 ASSERT (strcmp (p
, "YWJjZGVmZ2hpamtsbW5vcA==") == 0);
129 len
= base64_encode_alloc (in
, IDX_MAX
- 5, &p
);
132 /* Decode context function */
134 struct base64_decode_context ctx
;
136 base64_decode_ctx_init (&ctx
);
139 ok
= base64_decode_ctx (&ctx
, b64in
, strlen (b64in
), out
, &len
);
142 ASSERT (memcmp (out
, "abcdefg", len
) == 0);
145 /* Allocating decode context function */
147 ok
= base64_decode_alloc_ctx (NULL
, b64in
, strlen (b64in
), &p
, &len
);
150 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);
167 struct base64_decode_context ctx
;
168 base64_decode_ctx_init (&ctx
);
170 ok
= base64_decode_alloc_ctx (&ctx
, "YW\nJjZGVmZ2hp", 13, &p
, &len
);
173 ASSERT (memcmp (p
, "abcdefghi", len
) == 0);
176 base64_decode_ctx_init (&ctx
);
178 ok
= base64_decode_alloc_ctx (&ctx
, "YW\n", 3, &p
, &len
);
183 ok
= base64_decode_alloc_ctx (&ctx
, "JjZGVmZ2", 8, &p
, &len
);
186 ASSERT (memcmp (p
, "abcdef", len
) == 0);
189 ok
= base64_decode_alloc_ctx (&ctx
, "hp", 2, &p
, &len
);
192 ASSERT (memcmp (p
, "ghi", len
) == 0);
195 ok
= base64_decode_alloc_ctx (&ctx
, "", 0, &p
, &len
);
201 struct base64_decode_context ctx
;
202 const char *newlineb64
= "\n\n\n\n\n";
204 base64_decode_ctx_init (&ctx
);
206 ok
= base64_decode_alloc_ctx (&ctx
, newlineb64
, strlen (newlineb64
), &p
, &len
);
212 ok
= base64_decode_alloc_ctx (NULL
, " ! ", 3, &p
, &len
);
215 ok
= base64_decode_alloc_ctx (NULL
, "abc\ndef", 7, &p
, &len
);
218 ok
= base64_decode_alloc_ctx (NULL
, "aa", 2, &p
, &len
);
221 ok
= base64_decode_alloc_ctx (NULL
, "aa=", 3, &p
, &len
);
224 ok
= base64_decode_alloc_ctx (NULL
, "aax", 3, &p
, &len
);
227 ok
= base64_decode_alloc_ctx (NULL
, "aa=X", 4, &p
, &len
);
230 ok
= base64_decode_alloc_ctx (NULL
, "aa=X", 4, &p
, &len
);
233 ok
= base64_decode_alloc_ctx (NULL
, "aax=X", 5, &p
, &len
);
236 ok
= base64_decode_alloc_ctx (NULL
, "SGVsbG9=", 8, &p
, &len
);
239 ok
= base64_decode_alloc_ctx (NULL
, "TR==", 4, &p
, &len
);
242 ok
= base64_decode_alloc_ctx (NULL
, "TWF=TWE=", 8, &p
, &len
);
245 return test_exit_status
;