Support compiling without -loldnames on native Windows.
[gnulib.git] / tests / test-base32.c
blob906f411d8ce6487ce37ec9803f3ff9089437461b
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/>. */
19 #include <config.h>
21 #include "base32.h"
23 #include <stddef.h>
24 #include <stdbool.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdint.h>
29 #include "macros.h"
31 int
32 main (void)
34 const char *in = "abcdefghijklmnop";
35 const char *b32in = "MFRGGZDFMZTWQ2LKNNWG23TPOA======";
36 char out[255];
37 size_t len;
38 bool ok;
39 char *p;
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);
89 /* Decode. */
91 memset (out, 0x42, sizeof (out));
92 len = 0;
93 ok = base32_decode (b32in, 8, out, &len);
94 ASSERT (ok);
95 ASSERT (len == 0);
97 memset (out, 0x42, sizeof (out));
98 len = 1;
99 ok = base32_decode (b32in, 8, out, &len);
100 ASSERT (ok);
101 ASSERT (len == 1);
102 ASSERT (memcmp (out, "abcdefghijklmnop", 1) == 0);
104 memset (out, 0x42, sizeof (out));
105 len = 2;
106 ok = base32_decode (b32in, 8, out, &len);
107 ASSERT (ok);
108 ASSERT (len == 2);
109 ASSERT (memcmp (out, "abcdefghijklmnop", 2) == 0);
111 memset (out, 0x42, sizeof (out));
112 len = 3;
113 ok = base32_decode (b32in, 8, out, &len);
114 ASSERT (ok);
115 ASSERT (len == 3);
116 ASSERT (memcmp (out, "abcdefghijklmnop", 3) == 0);
118 memset (out, 0x42, sizeof (out));
119 len = 4;
120 ok = base32_decode (b32in, 8, out, &len);
121 ASSERT (ok);
122 ASSERT (len == 4);
123 ASSERT (memcmp (out, "abcdefghijklmnop", 4) == 0);
125 memset (out, 0x42, sizeof (out));
126 len = 5;
127 ok = base32_decode (b32in, 8, out, &len);
128 ASSERT (ok);
129 ASSERT (len == 5);
130 ASSERT (memcmp (out, "abcdefghijklmnop", 5) == 0);
132 memset (out, 0x42, sizeof (out));
133 len = 6;
134 ok = base32_decode (b32in, 8, out, &len);
135 ASSERT (ok);
136 ASSERT (len == 5);
137 ASSERT (memcmp (out, "abcdefghijklmnop", 5) == 0);
139 memset (out, 0x42, sizeof (out));
140 len = 100;
141 ok = base32_decode (b32in, strlen (b32in), out, &len);
142 ASSERT (ok);
143 ASSERT (len == 16);
144 ASSERT (memcmp (out, "abcdefghijklmnop", 16) == 0);
146 /* Allocating encode */
148 len = base32_encode_alloc (in, strlen (in), &p);
149 ASSERT (len == 32);
150 ASSERT (strcmp (p, "MFRGGZDFMZTWQ2LKNNWG23TPOA======") == 0);
151 free (p);
153 len = base32_encode_alloc (in, SIZE_MAX - 5, &p);
154 ASSERT (len == 0);
156 /* Decode context function */
158 struct base32_decode_context ctx;
160 base32_decode_ctx_init (&ctx);
162 len = sizeof (out);
163 ok = base32_decode_ctx (&ctx, b32in, strlen (b32in), out, &len);
164 ASSERT (ok);
165 ASSERT (len == 16);
166 ASSERT (memcmp (out, "abcdefghijklmnop", len) == 0);
169 /* Allocating decode context function */
171 ok = base32_decode_alloc_ctx (NULL, b32in, strlen (b32in), &p, &len);
172 ASSERT (ok);
173 ASSERT (len == 16);
174 ASSERT (memcmp (out, "abcdefghijklmnop", len) == 0);
175 free (p);
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);
184 ASSERT (ok);
185 ASSERT (len == strlen (in));
186 ASSERT (memcmp (p, in, len) == 0);
187 free (p);
191 struct base32_decode_context ctx;
192 base32_decode_ctx_init (&ctx);
194 ok = base32_decode_alloc_ctx (&ctx, "MFRGGZDFM\nZTWQ2LK", 17, &p, &len);
195 ASSERT (ok);
196 ASSERT (len == 10);
197 ASSERT (memcmp (p, "abcdefghij", len) == 0);
198 free (p);
200 base32_decode_ctx_init (&ctx);
202 ok = base32_decode_alloc_ctx (&ctx, "MF\n", 3, &p, &len);
203 ASSERT (ok);
204 ASSERT (len == 0);
205 free (p);
207 ok = base32_decode_alloc_ctx (&ctx, "RGGZDFMZ", 8, &p, &len);
208 ASSERT (ok);
209 ASSERT (len == 5);
210 ASSERT (memcmp (p, "abcde", len) == 0);
211 free (p);
213 ok = base32_decode_alloc_ctx (&ctx, "TWQ2LK", 6, &p, &len);
214 ASSERT (ok);
215 ASSERT (len == 5);
216 ASSERT (memcmp (p, "fghij", len) == 0);
217 free (p);
219 ok = base32_decode_alloc_ctx (&ctx, "", 0, &p, &len);
220 ASSERT (ok);
221 free (p);
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);
231 ASSERT (ok);
232 ASSERT (len == 0);
233 free (p);
236 ok = base32_decode_alloc_ctx (NULL, " ! ", 3, &p, &len);
237 ASSERT (!ok);
239 ok = base32_decode_alloc_ctx (NULL, "ABC\nDEF", 7, &p, &len);
240 ASSERT (!ok);
242 ok = base32_decode_alloc_ctx (NULL, "AA", 2, &p, &len);
243 ASSERT (!ok);
245 ok = base32_decode_alloc_ctx (NULL, "AA=", 3, &p, &len);
246 ASSERT (!ok);
248 ok = base32_decode_alloc_ctx (NULL, "AABBAAxx", 8, &p, &len);
249 ASSERT (!ok);
251 ok = base32_decode_alloc_ctx (NULL, "AABBAA=X", 8, &p, &len);
252 ASSERT (!ok);
254 ok = base32_decode_alloc_ctx (NULL, "AABBAA=X", 8, &p, &len);
255 ASSERT (!ok);
257 ok = base32_decode_alloc_ctx (NULL, "AABBAA=A", 8, &p, &len);
258 ASSERT (!ok);
260 return 0;