1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <assert.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 
21 #include "apr_encode.h"
22 #include "apr_strings.h"
23 
24 #include "abts.h"
25 #include "testutil.h"
26 
27 static const unsigned char ufoobar[] = { 'f', 'o', 'o', 'b', 'a', 'r' };
28 
test_encode_base64(abts_case * tc,void * data)29 static void test_encode_base64(abts_case * tc, void *data)
30 {
31     apr_pool_t *pool;
32     const char *src, *target;
33     const char *dest;
34     apr_size_t len;
35 
36     apr_pool_create(&pool, NULL);
37 
38     /*
39      * Test vectors from https://tools.ietf.org/html/rfc4648#section-10
40      */
41     src = "";
42     target = "";
43     dest = apr_pencode_base64(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
44     ABTS_STR_EQUAL(tc, target, dest);
45 
46     src = "f";
47     target = "Zg==";
48     dest = apr_pencode_base64(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
49     ABTS_STR_EQUAL(tc, target, dest);
50 
51     src = "f";
52     target = "Zg";
53     dest = apr_pencode_base64(pool, src, APR_ENCODE_STRING, APR_ENCODE_NOPADDING, &len);
54     ABTS_STR_EQUAL(tc, target, dest);
55 
56     src = "fo";
57     target = "Zm8=";
58     dest = apr_pencode_base64(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
59     ABTS_STR_EQUAL(tc, target, dest);
60 
61     src = "fo";
62     target = "Zm8";
63     dest = apr_pencode_base64(pool, src, APR_ENCODE_STRING, APR_ENCODE_NOPADDING, &len);
64     ABTS_STR_EQUAL(tc, target, dest);
65 
66     src = "foo";
67     target = "Zm9v";
68     dest = apr_pencode_base64(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
69     ABTS_STR_EQUAL(tc, target, dest);
70 
71     src = "foo";
72     target = "Zm9v";
73     dest = apr_pencode_base64(pool, src, APR_ENCODE_STRING, APR_ENCODE_NOPADDING, &len);
74     ABTS_STR_EQUAL(tc, target, dest);
75 
76     apr_pool_destroy(pool);
77 }
78 
test_encode_base64_binary(abts_case * tc,void * data)79 static void test_encode_base64_binary(abts_case * tc, void *data)
80 {
81     apr_pool_t *pool;
82     const char *target;
83     const char *dest;
84     apr_size_t len;
85 
86     apr_pool_create(&pool, NULL);
87 
88     /*
89      * Test vectors from https://tools.ietf.org/html/rfc4648#section-10
90      */
91     target = "";
92     dest = apr_pencode_base64_binary(pool, ufoobar, 0, APR_ENCODE_NONE, &len);
93     ABTS_STR_EQUAL(tc, target, dest);
94 
95     target = "Zg==";
96     dest = apr_pencode_base64_binary(pool, ufoobar, 1, APR_ENCODE_NONE, &len);
97     ABTS_STR_EQUAL(tc, target, dest);
98 
99     target = "Zg";
100     dest = apr_pencode_base64_binary(pool, ufoobar, 1, APR_ENCODE_NOPADDING, &len);
101     ABTS_STR_EQUAL(tc, target, dest);
102 
103     target = "Zm8=";
104     dest = apr_pencode_base64_binary(pool, ufoobar, 2, APR_ENCODE_NONE, &len);
105     ABTS_STR_EQUAL(tc, target, dest);
106 
107     target = "Zm8";
108     dest = apr_pencode_base64_binary(pool, ufoobar, 2, APR_ENCODE_NOPADDING, &len);
109     ABTS_STR_EQUAL(tc, target, dest);
110 
111     target = "Zm9v";
112     dest = apr_pencode_base64_binary(pool, ufoobar, 3, APR_ENCODE_NONE, &len);
113     ABTS_STR_EQUAL(tc, target, dest);
114 
115     target = "Zm9v";
116     dest = apr_pencode_base64_binary(pool, ufoobar, 3, APR_ENCODE_NOPADDING, &len);
117     ABTS_STR_EQUAL(tc, target, dest);
118 
119     apr_pool_destroy(pool);
120 }
121 
test_decode_base64(abts_case * tc,void * data)122 static void test_decode_base64(abts_case * tc, void *data)
123 {
124     apr_pool_t *pool;
125     const char *target, *src;
126     const char *dest;
127     apr_size_t len;
128 
129     apr_pool_create(&pool, NULL);
130 
131     /*
132      * Test vectors from https://tools.ietf.org/html/rfc4648#section-10
133      */
134     src = "";
135     target = "";
136     dest = apr_pdecode_base64(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
137     ABTS_STR_EQUAL(tc, dest, target);
138 
139     src = "Zg==";
140     target = "f";
141     dest = apr_pdecode_base64(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
142     ABTS_STR_EQUAL(tc, dest, target);
143 
144     src = "Zg";
145     target = "f";
146     dest = apr_pdecode_base64(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
147     ABTS_STR_EQUAL(tc, dest, target);
148 
149     src = "Zm8=";
150     target = "fo";
151     dest = apr_pdecode_base64(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
152     ABTS_STR_EQUAL(tc, dest, target);
153 
154     src = "Zm8";
155     target = "fo";
156     dest = apr_pdecode_base64(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
157     ABTS_STR_EQUAL(tc, dest, target);
158 
159     src = "Zm9v";
160     target = "foo";
161     dest = apr_pdecode_base64(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
162     ABTS_STR_EQUAL(tc, dest, target);
163 
164     src = "Zm9v";
165     target = "foo";
166     dest = apr_pdecode_base64(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
167     ABTS_STR_EQUAL(tc, dest, target);
168 
169     apr_pool_destroy(pool);
170 }
171 
test_decode_base64_binary(abts_case * tc,void * data)172 static void test_decode_base64_binary(abts_case * tc, void *data)
173 {
174     apr_pool_t *pool;
175     const char *src;
176     const unsigned char *udest;
177     apr_size_t len;
178 
179     apr_pool_create(&pool, NULL);
180 
181     /*
182      * Test vectors from https://tools.ietf.org/html/rfc4648#section-10
183      */
184     src = "";
185     udest = apr_pdecode_base64_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
186     ABTS_ASSERT(tc, "apr_pdecode_base64_binary target!=dest", memcmp(ufoobar, udest, 0) == 0);
187     ABTS_INT_EQUAL(tc, len, 0);
188 
189     src = "Zg==";
190     udest = apr_pdecode_base64_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
191     ABTS_ASSERT(tc, "apr_pdecode_base64_binary target!=dest", memcmp(ufoobar, udest, 1) == 0);
192     ABTS_INT_EQUAL(tc, len, 1);
193 
194     src = "Zg";
195     udest = apr_pdecode_base64_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
196     ABTS_ASSERT(tc, "apr_pdecode_base64_binary target!=dest", memcmp(ufoobar, udest, 1) == 0);
197     ABTS_INT_EQUAL(tc, len, 1);
198 
199     src = "Zm8=";
200     udest = apr_pdecode_base64_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
201     ABTS_ASSERT(tc, "apr_pdecode_base64_binary target!=dest", memcmp(ufoobar, udest, 2) == 0);
202     ABTS_INT_EQUAL(tc, len, 2);
203 
204     src = "Zm8";
205     udest = apr_pdecode_base64_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
206     ABTS_ASSERT(tc, "apr_pdecode_base64_binary target!=dest", memcmp(ufoobar, udest, 2) == 0);
207     ABTS_INT_EQUAL(tc, len, 2);
208 
209     src = "Zm9v";
210     udest = apr_pdecode_base64_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
211     ABTS_ASSERT(tc, "apr_pdecode_base64_binary target!=dest", memcmp(ufoobar, udest, 3) == 0);
212     ABTS_INT_EQUAL(tc, len, 3);
213 
214     src = "Zm9v";
215     udest = apr_pdecode_base64_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
216     ABTS_ASSERT(tc, "apr_pdecode_base64_binary target!=dest", memcmp(ufoobar, udest, 3) == 0);
217     ABTS_INT_EQUAL(tc, len, 3);
218 
219     apr_pool_destroy(pool);
220 }
221 
test_encode_base32(abts_case * tc,void * data)222 static void test_encode_base32(abts_case * tc, void *data)
223 {
224     apr_pool_t *pool;
225     const char *src, *target;
226     const char *dest;
227     apr_size_t len;
228 
229     apr_pool_create(&pool, NULL);
230 
231     /*
232      * Test vectors from https://tools.ietf.org/html/rfc4648#section-10
233      */
234     src = "";
235     target = "";
236     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
237     ABTS_STR_EQUAL(tc, target, dest);
238 
239     src = "f";
240     target = "MY======";
241     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
242     ABTS_STR_EQUAL(tc, target, dest);
243 
244     src = "f";
245     target = "MY";
246     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NOPADDING, &len);
247     ABTS_STR_EQUAL(tc, target, dest);
248 
249     src = "f";
250     target = "CO======";
251     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
252     ABTS_STR_EQUAL(tc, target, dest);
253 
254     src = "f";
255     target = "CO";
256     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX | APR_ENCODE_NOPADDING, &len);
257     ABTS_STR_EQUAL(tc, target, dest);
258 
259     src = "fo";
260     target = "MZXQ====";
261     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
262     ABTS_STR_EQUAL(tc, target, dest);
263 
264     src = "fo";
265     target = "MZXQ";
266     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NOPADDING, &len);
267     ABTS_STR_EQUAL(tc, target, dest);
268 
269     src = "fo";
270     target = "CPNG====";
271     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
272     ABTS_STR_EQUAL(tc, target, dest);
273 
274     src = "fo";
275     target = "CPNG";
276     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX | APR_ENCODE_NOPADDING, &len);
277     ABTS_STR_EQUAL(tc, target, dest);
278 
279     src = "foo";
280     target = "MZXW6===";
281     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
282     ABTS_STR_EQUAL(tc, target, dest);
283 
284     src = "foo";
285     target = "MZXW6";
286     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NOPADDING, &len);
287     ABTS_STR_EQUAL(tc, target, dest);
288 
289     src = "foo";
290     target = "CPNMU===";
291     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
292     ABTS_STR_EQUAL(tc, target, dest);
293 
294     src = "foo";
295     target = "CPNMU";
296     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX | APR_ENCODE_NOPADDING, &len);
297     ABTS_STR_EQUAL(tc, target, dest);
298 
299     src = "foob";
300     target = "MZXW6YQ=";
301     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
302     ABTS_STR_EQUAL(tc, target, dest);
303 
304     src = "foob";
305     target = "MZXW6YQ";
306     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NOPADDING, &len);
307     ABTS_STR_EQUAL(tc, target, dest);
308 
309     src = "foob";
310     target = "CPNMUOG=";
311     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
312     ABTS_STR_EQUAL(tc, target, dest);
313 
314     src = "foob";
315     target = "CPNMUOG";
316     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX | APR_ENCODE_NOPADDING, &len);
317     ABTS_STR_EQUAL(tc, target, dest);
318 
319     src = "fooba";
320     target = "MZXW6YTB";
321     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
322     ABTS_STR_EQUAL(tc, target, dest);
323 
324     src = "fooba";
325     target = "MZXW6YTB";
326     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NOPADDING, &len);
327     ABTS_STR_EQUAL(tc, target, dest);
328 
329     src = "fooba";
330     target = "CPNMUOJ1";
331     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
332     ABTS_STR_EQUAL(tc, target, dest);
333 
334     src = "fooba";
335     target = "CPNMUOJ1";
336     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX | APR_ENCODE_NOPADDING, &len);
337     ABTS_STR_EQUAL(tc, target, dest);
338 
339     src = "foobar";
340     target = "MZXW6YTBOI======";
341     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
342     ABTS_STR_EQUAL(tc, target, dest);
343 
344     src = "foobar";
345     target = "MZXW6YTBOI";
346     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NOPADDING, &len);
347     ABTS_STR_EQUAL(tc, target, dest);
348 
349     src = "foobar";
350     target = "CPNMUOJ1E8======";
351     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
352     ABTS_STR_EQUAL(tc, target, dest);
353 
354     src = "foobar";
355     target = "CPNMUOJ1E8";
356     dest = apr_pencode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX | APR_ENCODE_NOPADDING, &len);
357     ABTS_STR_EQUAL(tc, target, dest);
358 
359     apr_pool_destroy(pool);
360 }
361 
test_encode_base32_binary(abts_case * tc,void * data)362 static void test_encode_base32_binary(abts_case * tc, void *data)
363 {
364     apr_pool_t *pool;
365     const char *target;
366     const char *dest;
367     apr_size_t len;
368 
369     apr_pool_create(&pool, NULL);
370 
371     /*
372      * Test vectors from https://tools.ietf.org/html/rfc4648#section-10
373      */
374     target = "";
375     dest = apr_pencode_base32_binary(pool, ufoobar, 0, APR_ENCODE_NONE, &len);
376     ABTS_STR_EQUAL(tc, target, dest);
377 
378     target = "MY======";
379     dest = apr_pencode_base32_binary(pool, ufoobar, 1, APR_ENCODE_NONE, &len);
380     ABTS_STR_EQUAL(tc, target, dest);
381 
382     target = "MY";
383     dest = apr_pencode_base32_binary(pool, ufoobar, 1, APR_ENCODE_NOPADDING, &len);
384     ABTS_STR_EQUAL(tc, target, dest);
385 
386     target = "CO======";
387     dest = apr_pencode_base32_binary(pool, ufoobar, 1, APR_ENCODE_BASE32HEX, &len);
388     ABTS_STR_EQUAL(tc, target, dest);
389 
390     target = "CO";
391     dest = apr_pencode_base32_binary(pool, ufoobar, 1, APR_ENCODE_BASE32HEX | APR_ENCODE_NOPADDING, &len);
392     ABTS_STR_EQUAL(tc, target, dest);
393 
394     target = "MZXQ====";
395     dest = apr_pencode_base32_binary(pool, ufoobar, 2, APR_ENCODE_NONE, &len);
396     ABTS_STR_EQUAL(tc, target, dest);
397 
398     target = "MZXQ";
399     dest = apr_pencode_base32_binary(pool, ufoobar, 2, APR_ENCODE_NOPADDING, &len);
400     ABTS_STR_EQUAL(tc, target, dest);
401 
402     target = "CPNG====";
403     dest = apr_pencode_base32_binary(pool, ufoobar, 2, APR_ENCODE_BASE32HEX, &len);
404     ABTS_STR_EQUAL(tc, target, dest);
405 
406     target = "CPNG";
407     dest = apr_pencode_base32_binary(pool, ufoobar, 2, APR_ENCODE_BASE32HEX | APR_ENCODE_NOPADDING, &len);
408     ABTS_STR_EQUAL(tc, target, dest);
409 
410     target = "MZXW6===";
411     dest = apr_pencode_base32_binary(pool, ufoobar, 3, APR_ENCODE_NONE, &len);
412     ABTS_STR_EQUAL(tc, target, dest);
413 
414     target = "MZXW6";
415     dest = apr_pencode_base32_binary(pool, ufoobar, 3, APR_ENCODE_NOPADDING, &len);
416     ABTS_STR_EQUAL(tc, target, dest);
417 
418     target = "CPNMU===";
419     dest = apr_pencode_base32_binary(pool, ufoobar, 3, APR_ENCODE_BASE32HEX, &len);
420     ABTS_STR_EQUAL(tc, target, dest);
421 
422     target = "CPNMU";
423     dest = apr_pencode_base32_binary(pool, ufoobar, 3, APR_ENCODE_BASE32HEX | APR_ENCODE_NOPADDING, &len);
424     ABTS_STR_EQUAL(tc, target, dest);
425 
426     target = "MZXW6YQ=";
427     dest = apr_pencode_base32_binary(pool, ufoobar, 4, APR_ENCODE_NONE, &len);
428     ABTS_STR_EQUAL(tc, target, dest);
429 
430     target = "MZXW6YQ";
431     dest = apr_pencode_base32_binary(pool, ufoobar, 4, APR_ENCODE_NOPADDING, &len);
432     ABTS_STR_EQUAL(tc, target, dest);
433 
434     target = "CPNMUOG=";
435     dest = apr_pencode_base32_binary(pool, ufoobar, 4, APR_ENCODE_BASE32HEX, &len);
436     ABTS_STR_EQUAL(tc, target, dest);
437 
438     target = "CPNMUOG";
439     dest = apr_pencode_base32_binary(pool, ufoobar, 4, APR_ENCODE_BASE32HEX | APR_ENCODE_NOPADDING, &len);
440     ABTS_STR_EQUAL(tc, target, dest);
441 
442     target = "MZXW6YTB";
443     dest = apr_pencode_base32_binary(pool, ufoobar, 5, APR_ENCODE_NONE, &len);
444     ABTS_STR_EQUAL(tc, target, dest);
445 
446     target = "MZXW6YTB";
447     dest = apr_pencode_base32_binary(pool, ufoobar, 5, APR_ENCODE_NOPADDING, &len);
448     ABTS_STR_EQUAL(tc, target, dest);
449 
450     target = "CPNMUOJ1";
451     dest = apr_pencode_base32_binary(pool, ufoobar, 5, APR_ENCODE_BASE32HEX, &len);
452     ABTS_STR_EQUAL(tc, target, dest);
453 
454     target = "CPNMUOJ1";
455     dest = apr_pencode_base32_binary(pool, ufoobar, 5, APR_ENCODE_BASE32HEX | APR_ENCODE_NOPADDING, &len);
456     ABTS_STR_EQUAL(tc, target, dest);
457 
458     target = "MZXW6YTBOI======";
459     dest = apr_pencode_base32_binary(pool, ufoobar, 6, APR_ENCODE_NONE, &len);
460     ABTS_STR_EQUAL(tc, target, dest);
461 
462     target = "MZXW6YTBOI";
463     dest = apr_pencode_base32_binary(pool, ufoobar, 6, APR_ENCODE_NOPADDING, &len);
464     ABTS_STR_EQUAL(tc, target, dest);
465 
466     target = "CPNMUOJ1E8======";
467     dest = apr_pencode_base32_binary(pool, ufoobar, 6, APR_ENCODE_BASE32HEX, &len);
468     ABTS_STR_EQUAL(tc, target, dest);
469 
470     target = "CPNMUOJ1E8";
471     dest = apr_pencode_base32_binary(pool, ufoobar, 6, APR_ENCODE_BASE32HEX | APR_ENCODE_NOPADDING, &len);
472     ABTS_STR_EQUAL(tc, target, dest);
473 
474     apr_pool_destroy(pool);
475 }
476 
test_decode_base32(abts_case * tc,void * data)477 static void test_decode_base32(abts_case * tc, void *data)
478 {
479     apr_pool_t *pool;
480     const char *target, *src;
481     const char *dest;
482     apr_size_t len;
483 
484     apr_pool_create(&pool, NULL);
485 
486     /*
487      * Test vectors from https://tools.ietf.org/html/rfc4648#section-10
488      */
489     src = "";
490     target = "";
491     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
492     ABTS_STR_EQUAL(tc, target, dest);
493 
494     src = "MY======";
495     target = "f";
496     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
497     ABTS_STR_EQUAL(tc, target, dest);
498 
499     src = "MY";
500     target = "f";
501     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
502     ABTS_STR_EQUAL(tc, target, dest);
503 
504     src = "CO======";
505     target = "f";
506     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
507     ABTS_STR_EQUAL(tc, target, dest);
508 
509     src = "CO";
510     target = "f";
511     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
512     ABTS_STR_EQUAL(tc, target, dest);
513 
514     src = "MZXQ====";
515     target = "fo";
516     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
517     ABTS_STR_EQUAL(tc, target, dest);
518 
519     src = "MZXQ";
520     target = "fo";
521     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
522     ABTS_STR_EQUAL(tc, target, dest);
523 
524     src = "CPNG====";
525     target = "fo";
526     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
527     ABTS_STR_EQUAL(tc, target, dest);
528 
529     src = "CPNG";
530     target = "fo";
531     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
532     ABTS_STR_EQUAL(tc, target, dest);
533 
534     src = "MZXW6===";
535     target = "foo";
536     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
537     ABTS_STR_EQUAL(tc, target, dest);
538 
539     src = "MZXW6";
540     target = "foo";
541     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
542     ABTS_STR_EQUAL(tc, target, dest);
543 
544     src = "CPNMU===";
545     target = "foo";
546     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
547     ABTS_STR_EQUAL(tc, target, dest);
548 
549     src = "CPNMU";
550     target = "foo";
551     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
552     ABTS_STR_EQUAL(tc, target, dest);
553 
554     src = "MZXW6YQ=";
555     target = "foob";
556     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
557     ABTS_STR_EQUAL(tc, target, dest);
558 
559     src = "MZXW6YQ=";
560     target = "foob";
561     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
562     ABTS_STR_EQUAL(tc, target, dest);
563 
564     src = "CPNMUOG=";
565     target = "foob";
566     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
567     ABTS_STR_EQUAL(tc, target, dest);
568 
569     src = "CPNMUOG";
570     target = "foob";
571     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
572     ABTS_STR_EQUAL(tc, target, dest);
573 
574     src = "MZXW6YTB";
575     target = "fooba";
576     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
577     ABTS_STR_EQUAL(tc, target, dest);
578 
579     src = "MZXW6YTB";
580     target = "fooba";
581     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
582     ABTS_STR_EQUAL(tc, target, dest);
583 
584     src = "CPNMUOJ1";
585     target = "fooba";
586     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
587     ABTS_STR_EQUAL(tc, target, dest);
588 
589     src = "CPNMUOJ1";
590     target = "fooba";
591     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
592     ABTS_STR_EQUAL(tc, target, dest);
593 
594     src = "MZXW6YTBOI======";
595     target = "foobar";
596     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
597     ABTS_STR_EQUAL(tc, target, dest);
598 
599     src = "MZXW6YTBOI";
600     target = "foobar";
601     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
602     ABTS_STR_EQUAL(tc, target, dest);
603 
604     src = "CPNMUOJ1E8======";
605     target = "foobar";
606     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
607     ABTS_STR_EQUAL(tc, target, dest);
608 
609     src = "CPNMUOJ1E8";
610     target = "foobar";
611     dest = apr_pdecode_base32(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
612     ABTS_STR_EQUAL(tc, target, dest);
613 
614     apr_pool_destroy(pool);
615 }
616 
test_decode_base32_binary(abts_case * tc,void * data)617 static void test_decode_base32_binary(abts_case * tc, void *data)
618 {
619     apr_pool_t *pool;
620     const char *src;
621     const unsigned char *udest;
622     apr_size_t len;
623 
624     apr_pool_create(&pool, NULL);
625 
626     /*
627      * Test vectors from https://tools.ietf.org/html/rfc4648#section-10
628      */
629     src = "";
630     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
631     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 0) == 0);
632     ABTS_INT_EQUAL(tc, 0, len);
633 
634     src = "MY======";
635     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
636     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 1) == 0);
637     ABTS_INT_EQUAL(tc, 1, len);
638 
639     src = "MY";
640     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
641     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 1) == 0);
642     ABTS_INT_EQUAL(tc, 1, len);
643 
644     src = "CO======";
645     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
646     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 1) == 0);
647     ABTS_INT_EQUAL(tc, 1, len);
648 
649     src = "CO";
650     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
651     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 1) == 0);
652     ABTS_INT_EQUAL(tc, 1, len);
653 
654     src = "MZXQ====";
655     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
656     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 2) == 0);
657     ABTS_INT_EQUAL(tc, 2, len);
658 
659     src = "MZXQ";
660     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
661     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 2) == 0);
662     ABTS_INT_EQUAL(tc, 2, len);
663 
664     src = "CPNG====";
665     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
666     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 2) == 0);
667     ABTS_INT_EQUAL(tc, 2, len);
668 
669     src = "CPNG";
670     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
671     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 2) == 0);
672     ABTS_INT_EQUAL(tc, 2, len);
673 
674     src = "MZXW6===";
675     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
676     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 3) == 0);
677     ABTS_INT_EQUAL(tc, 3, len);
678 
679     src = "MZXW6";
680     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
681     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 3) == 0);
682     ABTS_INT_EQUAL(tc, 3, len);
683 
684     src = "CPNMU===";
685     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
686     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 3) == 0);
687     ABTS_INT_EQUAL(tc, 3, len);
688 
689     src = "CPNMU";
690     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
691     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 3) == 0);
692     ABTS_INT_EQUAL(tc, 3, len);
693 
694     src = "MZXW6YQ=";
695     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
696     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 4) == 0);
697     ABTS_INT_EQUAL(tc, 4, len);
698 
699     src = "MZXW6YQ=";
700     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
701     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 4) == 0);
702     ABTS_INT_EQUAL(tc, 4, len);
703 
704     src = "CPNMUOG=";
705     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
706     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 4) == 0);
707     ABTS_INT_EQUAL(tc, 4, len);
708 
709     src = "CPNMUOG";
710     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
711     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 4) == 0);
712     ABTS_INT_EQUAL(tc, 4, len);
713 
714     src = "MZXW6YTB";
715     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
716     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 5) == 0);
717     ABTS_INT_EQUAL(tc, 5, len);
718 
719     src = "MZXW6YTB";
720     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
721     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 5) == 0);
722     ABTS_INT_EQUAL(tc, 5, len);
723 
724     src = "CPNMUOJ1";
725     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
726     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 5) == 0);
727     ABTS_INT_EQUAL(tc, 5, len);
728 
729     src = "CPNMUOJ1";
730     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
731     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 5) == 0);
732     ABTS_INT_EQUAL(tc, 5, len);
733 
734     src = "MZXW6YTBOI======";
735     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
736     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 6) == 0);
737     ABTS_INT_EQUAL(tc, 6, len);
738 
739     src = "MZXW6YTBOI";
740     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
741     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 6) == 0);
742     ABTS_INT_EQUAL(tc, 6, len);
743 
744     src = "CPNMUOJ1E8======";
745     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
746     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 6) == 0);
747     ABTS_INT_EQUAL(tc, 6, len);
748 
749     src = "CPNMUOJ1E8";
750     udest = apr_pdecode_base32_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_BASE32HEX, &len);
751     ABTS_ASSERT(tc, "apr_pdecode_base32_binary target!=dest", memcmp(ufoobar, udest, 6) == 0);
752     ABTS_INT_EQUAL(tc, 6, len);
753 
754     apr_pool_destroy(pool);
755 }
756 
test_encode_base16(abts_case * tc,void * data)757 static void test_encode_base16(abts_case * tc, void *data)
758 {
759     apr_pool_t *pool;
760     const char *src, *target;
761     const char *dest;
762     apr_size_t len;
763 
764     apr_pool_create(&pool, NULL);
765 
766     src = "foobar";
767     target = "666f6f626172";
768     dest = apr_pencode_base16(pool, src, APR_ENCODE_STRING, APR_ENCODE_LOWER, &len);
769     ABTS_STR_EQUAL(tc, target, dest);
770     apr_encode_base16(NULL, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
771     ABTS_ASSERT(tc,
772                 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
773                 (len == strlen(dest) + 1));
774 
775     src = "foobar";
776     target = "666F6F626172";
777     dest = apr_pencode_base16(pool, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
778     ABTS_STR_EQUAL(tc, target, dest);
779     apr_encode_base16(NULL, src, APR_ENCODE_STRING, APR_ENCODE_NONE, &len);
780     ABTS_ASSERT(tc,
781                 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
782                 (len == strlen(dest) + 1));
783 
784     src = "foobar";
785     target = "66:6f:6f:62:61:72";
786     dest = apr_pencode_base16(pool, src, APR_ENCODE_STRING, APR_ENCODE_COLON | APR_ENCODE_LOWER, &len);
787     ABTS_STR_EQUAL(tc, target, dest);
788     apr_encode_base16(NULL, src, APR_ENCODE_STRING, APR_ENCODE_COLON, &len);
789     ABTS_ASSERT(tc,
790                 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
791                 (len == strlen(dest) + 1));
792 
793     src = "foobar";
794     target = "66:6F:6F:62:61:72";
795     dest = apr_pencode_base16(pool, src, APR_ENCODE_STRING, APR_ENCODE_COLON, &len);
796     ABTS_STR_EQUAL(tc, target, dest);
797     apr_encode_base16(NULL, src, APR_ENCODE_STRING, APR_ENCODE_COLON, &len);
798     ABTS_ASSERT(tc,
799                 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
800                 (len == strlen(dest) + 1));
801 
802     apr_pool_destroy(pool);
803 }
804 
test_encode_base16_binary(abts_case * tc,void * data)805 static void test_encode_base16_binary(abts_case * tc, void *data)
806 {
807     apr_pool_t *pool;
808     const char *target;
809     const unsigned char usrc[] = {
810         0xFF, 0x00, 0xFF, 0x00
811     };
812     const char *dest;
813     apr_size_t len;
814 
815     apr_pool_create(&pool, NULL);
816 
817     target = "ff00ff00";
818     dest = apr_pencode_base16_binary(pool, usrc, 4, APR_ENCODE_LOWER, &len);
819     ABTS_STR_EQUAL(tc, target, dest);
820     apr_encode_base16_binary(NULL, usrc, 4, APR_ENCODE_NONE, &len);
821     ABTS_ASSERT(tc,
822                 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
823                 (len == strlen(dest) + 1));
824 
825     target = "FF00FF00";
826     dest = apr_pencode_base16_binary(pool, usrc, 4, APR_ENCODE_NONE, &len);
827     ABTS_STR_EQUAL(tc, target, dest);
828     apr_encode_base16_binary(NULL, usrc, 4, APR_ENCODE_NONE, &len);
829     ABTS_ASSERT(tc,
830                 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
831                 (len == strlen(dest) + 1));
832 
833     target = "ff:00:ff:00";
834     dest = apr_pencode_base16_binary(pool, usrc, 4, APR_ENCODE_COLON | APR_ENCODE_LOWER, &len);
835     ABTS_STR_EQUAL(tc, target, dest);
836     apr_encode_base16_binary(NULL, usrc, 4, APR_ENCODE_COLON, &len);
837     ABTS_ASSERT(tc,
838                 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
839                 (len == strlen(dest) + 1));
840 
841     target = "FF:00:FF:00";
842     dest = apr_pencode_base16_binary(pool, usrc, 4, APR_ENCODE_COLON, &len);
843     ABTS_STR_EQUAL(tc, target, dest);
844     apr_encode_base16_binary(NULL, usrc, 4, APR_ENCODE_COLON, &len);
845     ABTS_ASSERT(tc,
846                 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
847                 (len == strlen(dest) + 1));
848 
849     apr_pool_destroy(pool);
850 }
851 
test_decode_base16(abts_case * tc,void * data)852 static void test_decode_base16(abts_case * tc, void *data)
853 {
854     apr_pool_t *pool;
855     const char *src, *target;
856     const char *dest;
857     apr_size_t len;
858 
859     apr_pool_create(&pool, NULL);
860 
861     src = "3A:3B:3C:3D";
862     target = ":;<=";
863     dest = apr_pdecode_base16(pool, src, APR_ENCODE_STRING, APR_ENCODE_COLON, &len);
864     ABTS_STR_EQUAL(tc, target, dest);
865     ABTS_INT_EQUAL(tc, 4, (int)len);
866     apr_decode_base16(NULL, src, APR_ENCODE_STRING, APR_ENCODE_COLON, &len);
867     ABTS_ASSERT(tc,
868                 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, (apr_size_t) 5),
869                 (len == 5));
870 
871     apr_pool_destroy(pool);
872 }
873 
test_decode_base16_binary(abts_case * tc,void * data)874 static void test_decode_base16_binary(abts_case * tc, void *data)
875 {
876     apr_pool_t *pool;
877     const char *src;
878     const unsigned char utarget[] = {
879         0xFF, 0x00, 0xFF, 0x00
880     };
881     const unsigned char *udest;
882     apr_size_t len, vlen;
883 
884     apr_pool_create(&pool, NULL);
885 
886     src = "ff:00:ff:00";
887     udest = apr_pdecode_base16_binary(pool, src, APR_ENCODE_STRING, APR_ENCODE_COLON, &vlen);
888     ABTS_ASSERT(tc, "apr_pdecode_base16_binary target!=dest", memcmp(utarget, udest, 4) == 0);
889     ABTS_INT_EQUAL(tc, (int)vlen, 4);
890     apr_decode_base16_binary(NULL, src, APR_ENCODE_STRING, APR_ENCODE_COLON, &len);
891     ABTS_ASSERT(tc,
892                 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, (apr_size_t) 4),
893                 (len == 4));
894 
895     apr_pool_destroy(pool);
896 }
897 
testencode(abts_suite * suite)898 abts_suite *testencode(abts_suite * suite)
899 {
900     suite = ADD_SUITE(suite);
901 
902     abts_run_test(suite, test_encode_base64, NULL);
903     abts_run_test(suite, test_encode_base64_binary, NULL);
904     abts_run_test(suite, test_decode_base64, NULL);
905     abts_run_test(suite, test_decode_base64_binary, NULL);
906     abts_run_test(suite, test_encode_base32, NULL);
907     abts_run_test(suite, test_encode_base32_binary, NULL);
908     abts_run_test(suite, test_decode_base32, NULL);
909     abts_run_test(suite, test_decode_base32_binary, NULL);
910     abts_run_test(suite, test_encode_base16, NULL);
911     abts_run_test(suite, test_encode_base16_binary, NULL);
912     abts_run_test(suite, test_decode_base16, NULL);
913     abts_run_test(suite, test_decode_base16_binary, NULL);
914 
915     return suite;
916 }
917