1 /* Self tests for base32.
2 Copyright (C) 2004, 2008-2020 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/>. */
34 const char *in
= "abcdefghijklmnop";
35 const char *b32in
= "MFRGGZDFMZTWQ2LKNNWG23TPOA======";
41 memset (out
, 0x42, sizeof (out
));
42 base32_encode (in
, 0, out
, 0);
43 ASSERT (out
[0] == '\x42');
45 memset (out
, 0x42, sizeof (out
));
46 base32_encode (in
, 1, out
, 10);
47 ASSERT (memcmp (out
, "ME======", 1) == 0);
49 memset (out
, 0x42, sizeof (out
));
50 base32_encode (in
, 1, out
, 2);
51 ASSERT (memcmp (out
, "ME======", 2) == 0);
53 memset (out
, 0x42, sizeof (out
));
54 base32_encode (in
, 1, out
, 3);
55 ASSERT (memcmp (out
, "ME======", 3) == 0);
57 memset (out
, 0x42, sizeof (out
));
58 base32_encode (in
, 1, out
, 4);
59 ASSERT (memcmp (out
, "ME======", 4) == 0);
61 memset (out
, 0x42, sizeof (out
));
62 base32_encode (in
, 1, out
, 8);
63 ASSERT (memcmp (out
, "ME======", 8) == 0);
65 memset (out
, 0x42, sizeof (out
));
66 base32_encode (in
, 2, out
, 8);
67 ASSERT (memcmp (out
, "MFRA====", 8) == 0);
69 memset (out
, 0x42, sizeof (out
));
70 base32_encode (in
, 3, out
, 8);
71 ASSERT (memcmp (out
, "MFRGG===", 8) == 0);
73 memset (out
, 0x42, sizeof (out
));
74 base32_encode (in
, 4, out
, 8);
75 ASSERT (memcmp (out
, "MFRGGZA=", 8) == 0);
77 memset (out
, 0x42, sizeof (out
));
78 base32_encode (in
, 5, out
, 8);
79 ASSERT (memcmp (out
, "MFRGGZDF", 8) == 0);
81 memset (out
, 0x42, sizeof (out
));
82 base32_encode (in
, 6, out
, 16);
83 ASSERT (memcmp (out
, "MFRGGZDFMY======", 16) == 0);
85 memset (out
, 0x42, sizeof (out
));
86 base32_encode (in
, 6, out
, 100);
87 ASSERT (memcmp (out
, "MFRGGZDFMY======", 16) == 0);
91 memset (out
, 0x42, sizeof (out
));
93 ok
= base32_decode (b32in
, 8, out
, &len
);
97 memset (out
, 0x42, sizeof (out
));
99 ok
= base32_decode (b32in
, 8, out
, &len
);
102 ASSERT (memcmp (out
, "abcdefghijklmnop", 1) == 0);
104 memset (out
, 0x42, sizeof (out
));
106 ok
= base32_decode (b32in
, 8, out
, &len
);
109 ASSERT (memcmp (out
, "abcdefghijklmnop", 2) == 0);
111 memset (out
, 0x42, sizeof (out
));
113 ok
= base32_decode (b32in
, 8, out
, &len
);
116 ASSERT (memcmp (out
, "abcdefghijklmnop", 3) == 0);
118 memset (out
, 0x42, sizeof (out
));
120 ok
= base32_decode (b32in
, 8, out
, &len
);
123 ASSERT (memcmp (out
, "abcdefghijklmnop", 4) == 0);
125 memset (out
, 0x42, sizeof (out
));
127 ok
= base32_decode (b32in
, 8, out
, &len
);
130 ASSERT (memcmp (out
, "abcdefghijklmnop", 5) == 0);
132 memset (out
, 0x42, sizeof (out
));
134 ok
= base32_decode (b32in
, 8, out
, &len
);
137 ASSERT (memcmp (out
, "abcdefghijklmnop", 5) == 0);
139 memset (out
, 0x42, sizeof (out
));
141 ok
= base32_decode (b32in
, strlen (b32in
), out
, &len
);
144 ASSERT (memcmp (out
, "abcdefghijklmnop", 16) == 0);
146 /* Allocating encode */
148 len
= base32_encode_alloc (in
, strlen (in
), &p
);
150 ASSERT (strcmp (p
, "MFRGGZDFMZTWQ2LKNNWG23TPOA======") == 0);
153 len
= base32_encode_alloc (in
, SIZE_MAX
- 5, &p
);
156 /* Decode context function */
158 struct base32_decode_context ctx
;
160 base32_decode_ctx_init (&ctx
);
163 ok
= base32_decode_ctx (&ctx
, b32in
, strlen (b32in
), out
, &len
);
166 ASSERT (memcmp (out
, "abcdefghijklmnop", len
) == 0);
169 /* Allocating decode context function */
171 ok
= base32_decode_alloc_ctx (NULL
, b32in
, strlen (b32in
), &p
, &len
);
174 ASSERT (memcmp (out
, "abcdefghijklmnop", len
) == 0);
178 struct base32_decode_context ctx
;
179 const char *newlineb32
= "MFRG\nGZDFMZTWQ2LKNNW\nG23TPOA======";
181 base32_decode_ctx_init (&ctx
);
183 ok
= base32_decode_alloc_ctx (&ctx
, newlineb32
, strlen (newlineb32
), &p
, &len
);
185 ASSERT (len
== strlen (in
));
186 ASSERT (memcmp (p
, in
, len
) == 0);
191 struct base32_decode_context ctx
;
192 base32_decode_ctx_init (&ctx
);
194 ok
= base32_decode_alloc_ctx (&ctx
, "MFRGGZDFM\nZTWQ2LK", 17, &p
, &len
);
197 ASSERT (memcmp (p
, "abcdefghij", len
) == 0);
200 base32_decode_ctx_init (&ctx
);
202 ok
= base32_decode_alloc_ctx (&ctx
, "MF\n", 3, &p
, &len
);
207 ok
= base32_decode_alloc_ctx (&ctx
, "RGGZDFMZ", 8, &p
, &len
);
210 ASSERT (memcmp (p
, "abcde", len
) == 0);
213 ok
= base32_decode_alloc_ctx (&ctx
, "TWQ2LK", 6, &p
, &len
);
216 ASSERT (memcmp (p
, "fghij", len
) == 0);
219 ok
= base32_decode_alloc_ctx (&ctx
, "", 0, &p
, &len
);
225 struct base32_decode_context ctx
;
226 const char *newlineb32
= "\n\n\n\n\n";
228 base32_decode_ctx_init (&ctx
);
230 ok
= base32_decode_alloc_ctx (&ctx
, newlineb32
, strlen (newlineb32
), &p
, &len
);
236 ok
= base32_decode_alloc_ctx (NULL
, " ! ", 3, &p
, &len
);
239 ok
= base32_decode_alloc_ctx (NULL
, "ABC\nDEF", 7, &p
, &len
);
242 ok
= base32_decode_alloc_ctx (NULL
, "AA", 2, &p
, &len
);
245 ok
= base32_decode_alloc_ctx (NULL
, "AA=", 3, &p
, &len
);
248 ok
= base32_decode_alloc_ctx (NULL
, "AABBAAxx", 8, &p
, &len
);
251 ok
= base32_decode_alloc_ctx (NULL
, "AABBAA=X", 8, &p
, &len
);
254 ok
= base32_decode_alloc_ctx (NULL
, "AABBAA=X", 8, &p
, &len
);
257 ok
= base32_decode_alloc_ctx (NULL
, "AABBAA=A", 8, &p
, &len
);