1 /* Self tests for base32.
2 Copyright (C) 2004, 2008-2025 Free Software Foundation, Inc.
3 Based on the tests for base64 written by Simon Josefsson.
4 Adapted for base32 by Gijs van Tulder.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
33 const char *in
= "abcdefghijklmnop";
34 const char *b32in
= "MFRGGZDFMZTWQ2LKNNWG23TPOA======";
40 memset (out
, 0x42, sizeof (out
));
41 base32_encode (in
, 0, out
, 0);
42 ASSERT (out
[0] == '\x42');
44 memset (out
, 0x42, sizeof (out
));
45 base32_encode (in
, 1, out
, 10);
46 ASSERT (memcmp (out
, "ME======", 1) == 0);
48 memset (out
, 0x42, sizeof (out
));
49 base32_encode (in
, 1, out
, 2);
50 ASSERT (memcmp (out
, "ME======", 2) == 0);
52 memset (out
, 0x42, sizeof (out
));
53 base32_encode (in
, 1, out
, 3);
54 ASSERT (memcmp (out
, "ME======", 3) == 0);
56 memset (out
, 0x42, sizeof (out
));
57 base32_encode (in
, 1, out
, 4);
58 ASSERT (memcmp (out
, "ME======", 4) == 0);
60 memset (out
, 0x42, sizeof (out
));
61 base32_encode (in
, 1, out
, 8);
62 ASSERT (memcmp (out
, "ME======", 8) == 0);
64 memset (out
, 0x42, sizeof (out
));
65 base32_encode (in
, 2, out
, 8);
66 ASSERT (memcmp (out
, "MFRA====", 8) == 0);
68 memset (out
, 0x42, sizeof (out
));
69 base32_encode (in
, 3, out
, 8);
70 ASSERT (memcmp (out
, "MFRGG===", 8) == 0);
72 memset (out
, 0x42, sizeof (out
));
73 base32_encode (in
, 4, out
, 8);
74 ASSERT (memcmp (out
, "MFRGGZA=", 8) == 0);
76 memset (out
, 0x42, sizeof (out
));
77 base32_encode (in
, 5, out
, 8);
78 ASSERT (memcmp (out
, "MFRGGZDF", 8) == 0);
80 memset (out
, 0x42, sizeof (out
));
81 base32_encode (in
, 6, out
, 16);
82 ASSERT (memcmp (out
, "MFRGGZDFMY======", 16) == 0);
84 memset (out
, 0x42, sizeof (out
));
85 base32_encode (in
, 6, out
, 100);
86 ASSERT (memcmp (out
, "MFRGGZDFMY======", 16) == 0);
90 memset (out
, 0x42, sizeof (out
));
92 ok
= base32_decode (b32in
, 8, out
, &len
);
96 memset (out
, 0x42, sizeof (out
));
98 ok
= base32_decode (b32in
, 8, out
, &len
);
101 ASSERT (memcmp (out
, "abcdefghijklmnop", 1) == 0);
103 memset (out
, 0x42, sizeof (out
));
105 ok
= base32_decode (b32in
, 8, out
, &len
);
108 ASSERT (memcmp (out
, "abcdefghijklmnop", 2) == 0);
110 memset (out
, 0x42, sizeof (out
));
112 ok
= base32_decode (b32in
, 8, out
, &len
);
115 ASSERT (memcmp (out
, "abcdefghijklmnop", 3) == 0);
117 memset (out
, 0x42, sizeof (out
));
119 ok
= base32_decode (b32in
, 8, out
, &len
);
122 ASSERT (memcmp (out
, "abcdefghijklmnop", 4) == 0);
124 memset (out
, 0x42, sizeof (out
));
126 ok
= base32_decode (b32in
, 8, out
, &len
);
129 ASSERT (memcmp (out
, "abcdefghijklmnop", 5) == 0);
131 memset (out
, 0x42, sizeof (out
));
133 ok
= base32_decode (b32in
, 8, out
, &len
);
136 ASSERT (memcmp (out
, "abcdefghijklmnop", 5) == 0);
138 memset (out
, 0x42, sizeof (out
));
140 ok
= base32_decode (b32in
, strlen (b32in
), out
, &len
);
143 ASSERT (memcmp (out
, "abcdefghijklmnop", 16) == 0);
145 /* Allocating encode */
147 len
= base32_encode_alloc (in
, strlen (in
), &p
);
149 ASSERT (strcmp (p
, "MFRGGZDFMZTWQ2LKNNWG23TPOA======") == 0);
152 len
= base32_encode_alloc (in
, IDX_MAX
- 5, &p
);
155 /* Decode context function */
157 struct base32_decode_context ctx
;
159 base32_decode_ctx_init (&ctx
);
162 ok
= base32_decode_ctx (&ctx
, b32in
, strlen (b32in
), out
, &len
);
165 ASSERT (memcmp (out
, "abcdefghijklmnop", len
) == 0);
168 /* Allocating decode context function */
170 ok
= base32_decode_alloc_ctx (NULL
, b32in
, strlen (b32in
), &p
, &len
);
173 ASSERT (memcmp (out
, "abcdefghijklmnop", len
) == 0);
177 struct base32_decode_context ctx
;
178 const char *newlineb32
= "MFRG\nGZDFMZTWQ2LKNNW\nG23TPOA======";
180 base32_decode_ctx_init (&ctx
);
182 ok
= base32_decode_alloc_ctx (&ctx
, newlineb32
, strlen (newlineb32
), &p
, &len
);
184 ASSERT (len
== strlen (in
));
185 ASSERT (memcmp (p
, in
, len
) == 0);
190 struct base32_decode_context ctx
;
191 base32_decode_ctx_init (&ctx
);
193 ok
= base32_decode_alloc_ctx (&ctx
, "MFRGGZDFM\nZTWQ2LK", 17, &p
, &len
);
196 ASSERT (memcmp (p
, "abcdefghij", len
) == 0);
199 base32_decode_ctx_init (&ctx
);
201 ok
= base32_decode_alloc_ctx (&ctx
, "MF\n", 3, &p
, &len
);
206 ok
= base32_decode_alloc_ctx (&ctx
, "RGGZDFMZ", 8, &p
, &len
);
209 ASSERT (memcmp (p
, "abcde", len
) == 0);
212 ok
= base32_decode_alloc_ctx (&ctx
, "TWQ2LK", 6, &p
, &len
);
215 ASSERT (memcmp (p
, "fghij", len
) == 0);
218 ok
= base32_decode_alloc_ctx (&ctx
, "", 0, &p
, &len
);
224 struct base32_decode_context ctx
;
225 const char *newlineb32
= "\n\n\n\n\n";
227 base32_decode_ctx_init (&ctx
);
229 ok
= base32_decode_alloc_ctx (&ctx
, newlineb32
, strlen (newlineb32
), &p
, &len
);
235 ok
= base32_decode_alloc_ctx (NULL
, " ! ", 3, &p
, &len
);
238 ok
= base32_decode_alloc_ctx (NULL
, "ABC\nDEF", 7, &p
, &len
);
241 ok
= base32_decode_alloc_ctx (NULL
, "AA", 2, &p
, &len
);
244 ok
= base32_decode_alloc_ctx (NULL
, "AA=", 3, &p
, &len
);
247 ok
= base32_decode_alloc_ctx (NULL
, "AABBAAxx", 8, &p
, &len
);
250 ok
= base32_decode_alloc_ctx (NULL
, "AABBAA=X", 8, &p
, &len
);
253 ok
= base32_decode_alloc_ctx (NULL
, "AABBAA=X", 8, &p
, &len
);
256 ok
= base32_decode_alloc_ctx (NULL
, "AABBAA=A", 8, &p
, &len
);
259 ok
= base32_decode_alloc_ctx (NULL
, "FZ======", 8, &p
, &len
);
262 ok
= base32_decode_alloc_ctx (NULL
, "FYXB====", 8, &p
, &len
);
265 ok
= base32_decode_alloc_ctx (NULL
, "FYXC5===", 8, &p
, &len
);
268 ok
= base32_decode_alloc_ctx (NULL
, "FYXC4LR=", 8, &p
, &len
);
271 ok
= base32_decode_alloc_ctx (NULL
, "FZ======FY======", 16, &p
, &len
);
274 return test_exit_status
;