summaryrefslogtreecommitdiff
path: root/src/regress/lib/libcrypto/base64
diff options
context:
space:
mode:
authorcvs2svn <admin@example.com>2019-11-19 19:57:05 +0000
committercvs2svn <admin@example.com>2019-11-19 19:57:05 +0000
commite9f9eb6198f1757b7c0dfef043fadf1fa8243022 (patch)
treeb5a648f6ccaf6c1cd9915ddb45503d1fccfeba0e /src/regress/lib/libcrypto/base64
parentab72e3a6f7e8d5c71bbba034410468781d5923b6 (diff)
downloadopenbsd-bluhm_20191119.tar.gz
openbsd-bluhm_20191119.tar.bz2
openbsd-bluhm_20191119.zip
This commit was manufactured by cvs2git to create tag 'bluhm_20191119'.bluhm_20191119
Diffstat (limited to 'src/regress/lib/libcrypto/base64')
-rw-r--r--src/regress/lib/libcrypto/base64/Makefile9
-rw-r--r--src/regress/lib/libcrypto/base64/base64test.c388
2 files changed, 0 insertions, 397 deletions
diff --git a/src/regress/lib/libcrypto/base64/Makefile b/src/regress/lib/libcrypto/base64/Makefile
deleted file mode 100644
index 2cc004c530..0000000000
--- a/src/regress/lib/libcrypto/base64/Makefile
+++ /dev/null
@@ -1,9 +0,0 @@
1# $OpenBSD: Makefile,v 1.3 2014/07/08 15:53:52 jsing Exp $
2
3PROG= base64test
4LDADD= -lcrypto
5DPADD= ${LIBCRYPTO}
6WARNINGS= Yes
7CFLAGS+= -DLIBRESSL_INTERNAL -Werror
8
9.include <bsd.regress.mk>
diff --git a/src/regress/lib/libcrypto/base64/base64test.c b/src/regress/lib/libcrypto/base64/base64test.c
deleted file mode 100644
index 1ff3b93002..0000000000
--- a/src/regress/lib/libcrypto/base64/base64test.c
+++ /dev/null
@@ -1,388 +0,0 @@
1/* $OpenBSD: base64test.c,v 1.6 2019/06/27 04:29:35 deraadt Exp $ */
2/*
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <openssl/bio.h>
19#include <openssl/evp.h>
20
21#include <err.h>
22#include <stdio.h>
23#include <string.h>
24#include <sys/types.h>
25
26#define BUF_SIZE 128
27
28struct base64_test {
29 const unsigned char in[BUF_SIZE];
30 const ssize_t in_len;
31 const unsigned char out[BUF_SIZE];
32 const ssize_t out_len;
33 const ssize_t valid_len;
34};
35
36/*
37 * Many of these tests are based on those found in Go's encoding/base64 tests.
38 */
39struct base64_test base64_tests[] = {
40
41 /* RFC3548 examples. */
42 { "\x14\xfb\x9c\x03\xd9\x7e", 6, "FPucA9l+", 8, 6, },
43 { "\x14\xfb\x9c\x03\xd9", 5, "FPucA9k=", 8, 5, },
44 { "\x14\xfb\x9c\x03", 4, "FPucAw==", 8, 4, },
45
46 /* RFC4648 examples. */
47 { "", 0, "", 0, 0, },
48 { "f", 1, "Zg==", 4, 1, },
49 { "fo", 2, "Zm8=", 4, 2, },
50 { "foo", 3, "Zm9v", 4, 3, },
51 { "foob", 4, "Zm9vYg==", 8, 4, },
52 { "fooba", 5, "Zm9vYmE=", 8, 5, },
53 { "foobar", 6, "Zm9vYmFy", 8, 6, },
54
55 /* Wikipedia examples. */
56 { "sure.", 5, "c3VyZS4=", 8, 5, },
57 { "sure", 4, "c3VyZQ==", 8, 4, },
58 { "sur", 3, "c3Vy", 4, 3, },
59 { "su", 2, "c3U=", 4, 2, },
60 { "leasure.", 8, "bGVhc3VyZS4=", 12, 8, },
61 { "easure.", 7, "ZWFzdXJlLg==", 12, 7, },
62 { "asure.", 6, "YXN1cmUu", 8, 6, },
63
64 { "abcd", 4, "YWJjZA==", 8, 4, },
65
66 {
67 "Twas brillig, and the slithy toves",
68 34,
69 "VHdhcyBicmlsbGlnLCBhbmQgdGhlIHNsaXRoeSB0b3Zlcw==",
70 48,
71 34,
72 },
73};
74
75#define N_TESTS (sizeof(base64_tests) / sizeof(*base64_tests))
76
77struct base64_test base64_nl_tests[] = {
78
79 /* Corrupt/invalid encodings. */
80 { "", -1, "", 0, 0, },
81 { "", -1, "!!!!", 4, 0, },
82 { "", -1, "====", 4, 0, },
83 { "", -1, "x===", 4, 0, },
84 { "", -1, "=AAA", 4, 0, },
85 { "", -1, "A=AA", 4, 0, },
86 { "", -1, "AA=A", 4, 0, },
87 { "", -1, "AA==A", 5, 0, },
88 { "", -1, "AAA=AAAA", 8, 0, },
89 { "", -1, "AAAAA", 5, 0, },
90 { "", -1, "AAAAAA", 6, 0, },
91 { "", -1, "A=", 2, 0, },
92 { "", -1, "A==", 3, 0, },
93 { "", -1, "AA=", 3, 0, },
94 { "", -1, "AA==", 4, 1, }, /* XXX - output ix 0x0. */
95 { "", -1, "AAA=", 4, 2, }, /* XXX - output ix 2x 0x0. */
96 { "", -1, "AAAA", 4, 3, }, /* XXX - output ix 3x 0x0. */
97 { "", -1, "AAAAAA=", 7, 0, },
98 { "", -1, "YWJjZA=====", 11, 0, },
99
100
101 /* Encodings with embedded CR/LF. */
102 { "sure", 4, "c3VyZQ==", 8, 4, },
103 { "sure", 4, "c3VyZQ==\r", 9, 4, },
104 { "sure", 4, "c3VyZQ==\n", 9, 4, },
105 { "sure", 4, "c3VyZQ==\r\n", 10, 4, },
106 { "sure", 4, "c3VyZ\r\nQ==", 10, 4, },
107 { "sure", 4, "c3V\ryZ\nQ==", 10, 4, },
108 { "sure", 4, "c3V\nyZ\rQ==", 10, 4, },
109 { "sure", 4, "c3VyZ\nQ==", 9, 4, },
110 { "sure", 4, "c3VyZQ\n==", 9, 4, },
111 { "sure", 4, "c3VyZQ=\n=", 9, 4, },
112 { "sure", 4, "c3VyZQ=\r\n\r\n=", 12, 4, },
113
114 {
115 "",
116 -1,
117 "YWJjZA======================================================"
118 "============",
119 74,
120 0,
121 },
122};
123
124#define N_NL_TESTS (sizeof(base64_nl_tests) / sizeof(*base64_nl_tests))
125
126struct base64_test base64_no_nl_tests[] = {
127
128 /*
129 * In non-newline mode, the output resulting from corrupt/invalid
130 * encodings is completely crazy. A number of zero bytes is returned
131 * rather than nothing.
132 */
133
134 /* Corrupt/invalid encodings. */
135 { "", -1, "", 0, 0, },
136 { "", -1, "!!!!", 4, 0, },
137 { "", -1, "====", 4, 1, },
138 { "", -1, "x===", 4, 1, },
139 { "", -1, "=AAA", 4, 3, },
140 { "", -1, "A=AA", 4, 3, },
141 { "", -1, "AA=A", 4, 3, },
142 { "", -1, "AA==A", 5, 1, },
143 { "", -1, "AAA=AAAA", 8, 6, },
144 { "", -1, "AAAAA", 5, 3, },
145 { "", -1, "AAAAAA", 6, 3, },
146 { "", -1, "A=", 2, 0, },
147 { "", -1, "A==", 3, 0, },
148 { "", -1, "AA=", 3, 0, },
149 { "", -1, "AA==", 4, 1, },
150 { "", -1, "AAA=", 4, 2, },
151 { "", -1, "AAAA", 4, 3, },
152 { "", -1, "AAAAAA=", 7, 3, },
153 { "", -1, "YWJjZA=====", 11, 4, },
154
155 /* Encodings with embedded CR/LF. */
156 { "sure", 4, "c3VyZQ==", 8, 4, },
157 { "sure", 4, "c3VyZQ==\r", 9, 4, },
158 { "sure", 4, "c3VyZQ==\n", 9, 4, },
159 { "sure", 4, "c3VyZQ==\r\n", 10, 4, },
160 { "sure", -1, "c3VyZ\r\nQ==", 10, 0, },
161 { "sure", -1, "c3V\ryZ\nQ==", 10, 0, },
162 { "sure", -1, "c3V\nyZ\rQ==", 10, 0, },
163 { "sure", -1, "c3VyZ\nQ==", 9, 0, },
164 { "sure", -1, "c3VyZQ\n==", 9, 0, },
165 { "sure", -1, "c3VyZQ=\n=", 9, 0, },
166 { "sure", -1, "c3VyZQ=\r\n\r\n=", 12, 0, },
167
168 /*
169 * This is invalid, yet results in 'abcd' followed by a stream of
170 * zero value bytes.
171 */
172 {
173 "",
174 -1,
175 "YWJjZA======================================================"
176 "============",
177 74,
178 52,
179 },
180};
181
182#define N_NO_NL_TESTS (sizeof(base64_no_nl_tests) / sizeof(*base64_no_nl_tests))
183
184static int
185base64_encoding_test(int test_no, struct base64_test *bt, int test_nl)
186{
187 BIO *bio_b64, *bio_mem;
188 unsigned char *buf, *out;
189 ssize_t i, len, b64len;
190 int failure = 0;
191
192 buf = malloc(BUF_SIZE);
193 if (buf == NULL)
194 errx(1, "malloc");
195
196 bio_b64 = BIO_new(BIO_f_base64());
197 if (bio_b64 == NULL)
198 errx(1, "BIO_new failed for BIO_f_base64");
199
200 bio_mem = BIO_new(BIO_s_mem());
201 if (bio_mem == NULL)
202 errx(1, "BIO_new failed for BIO_s_mem");
203
204 bio_mem = BIO_push(bio_b64, bio_mem);
205
206 if (!test_nl)
207 BIO_set_flags(bio_b64, BIO_FLAGS_BASE64_NO_NL);
208
209 len = BIO_write(bio_mem, bt->in, bt->in_len);
210 if (len != bt->in_len) {
211 fprintf(stderr, "FAIL: test %i - only wrote %zi out of %zi "
212 "characters\n", test_no, len, bt->in_len);
213 failure = 1;
214 goto done;
215 }
216 if (BIO_flush(bio_mem) < 0) {
217 fprintf(stderr, "FAIL: test %i - flush failed\n", test_no);
218 failure = 1;
219 goto done;
220 }
221
222 b64len = 0;
223 for (i = 0; i < bt->out_len; i++) {
224 if (bt->out[i] == '\r' || bt->out[i] == '\n')
225 continue;
226 buf[b64len++] = bt->out[i];
227 }
228 if (test_nl)
229 buf[b64len++] = '\n';
230
231 len = BIO_get_mem_data(bio_mem, &out);
232
233 /* An empty string with NL results in no output, rather than '\n'. */
234 if (test_nl && b64len == 1 && len == 0)
235 goto done;
236
237 if (len != b64len) {
238 fprintf(stderr, "FAIL: test %i - encoding resulted in %zi "
239 "characters instead of %zi\n", test_no, len, b64len);
240 failure = 1;
241 goto done;
242 }
243
244 if (memcmp(buf, out, b64len) != 0) {
245 fprintf(stderr, "FAIL: test %i - encoding differs:\n", test_no);
246 fprintf(stderr, " encoding: ");
247 for (i = 0; i < len; i++)
248 fprintf(stderr, "%c", out[i]);
249 fprintf(stderr, "\n");
250 fprintf(stderr, " test data: ");
251 for (i = 0; i < bt->out_len; i++)
252 fprintf(stderr, "%c", buf[i]);
253 fprintf(stderr, "\n");
254 failure = 1;
255 }
256
257done:
258 BIO_free_all(bio_mem);
259 free(buf);
260
261 return failure;
262}
263
264static int
265base64_decoding_test(int test_no, struct base64_test *bt, int test_nl)
266{
267 BIO *bio_b64, *bio_mem;
268 char *buf, *input;
269 ssize_t i, inlen, len;
270 int failure = 0;
271
272 buf = malloc(BUF_SIZE);
273 if (buf == NULL)
274 errx(1, "malloc");
275
276 input = (char *)bt->out;
277 inlen = bt->out_len;
278
279 if (test_nl)
280 inlen = asprintf(&input, "%s\r\n", bt->out);
281
282 if (inlen == -1)
283 errx(1, "asprintf");
284
285 bio_mem = BIO_new_mem_buf(input, inlen);
286 if (bio_mem == NULL)
287 errx(1, "BIO_new_mem_buf failed");
288
289 bio_b64 = BIO_new(BIO_f_base64());
290 if (bio_b64 == NULL)
291 errx(1, "BIO_new failed for BIO_f_base64");
292
293 if (!test_nl)
294 BIO_set_flags(bio_b64, BIO_FLAGS_BASE64_NO_NL);
295
296 bio_mem = BIO_push(bio_b64, bio_mem);
297
298 /*
299 * If we wrote zero characters then a BIO_read will result in a return
300 * value of -1, hence we need to handle this case.
301 */
302 len = BIO_read(bio_mem, buf, BUF_SIZE);
303 if (len != bt->valid_len && (bt->in_len != 0 || len != -1)) {
304 fprintf(stderr, "FAIL: test %i - decoding resulted in %zi "
305 "characters instead of %zi\n", test_no, len, bt->valid_len);
306 fprintf(stderr, " input: ");
307 for (i = 0; i < inlen; i++)
308 fprintf(stderr, "%c", input[i]);
309 fprintf(stderr, "\n");
310 fprintf(stderr, " decoding: ");
311 for (i = 0; i < len; i++)
312 fprintf(stderr, "0x%x ", buf[i]);
313 fprintf(stderr, "\n");
314 failure = 1;
315 goto done;
316 }
317
318 /* See if we expect this to fail decoding. */
319 if (bt->in_len == -1)
320 goto done;
321
322 if (memcmp(bt->in, buf, bt->in_len) != 0) {
323 fprintf(stderr, "FAIL: test %i - decoding differs:\n", test_no);
324 fprintf(stderr, " decoding: ");
325 for (i = 0; i < len; i++)
326 fprintf(stderr, "0x%x ", buf[i]);
327 fprintf(stderr, "\n");
328 fprintf(stderr, " test data: ");
329 for (i = 0; i < inlen; i++)
330 fprintf(stderr, "0x%x ", input[i]);
331 fprintf(stderr, "\n");
332 failure = 1;
333 }
334
335done:
336 BIO_free_all(bio_mem);
337 free(buf);
338 if (test_nl)
339 free(input);
340
341 return failure;
342}
343
344int
345main(int argc, char **argv)
346{
347 struct base64_test *bt;
348 int failed = 0;
349 size_t i;
350
351 fprintf(stderr, "Starting combined tests...\n");
352
353 for (i = 0; i < N_TESTS; i++) {
354 bt = &base64_tests[i];
355 if (bt->in_len != -1)
356 failed += base64_encoding_test(i, bt, 0);
357 if (bt->out_len != -1)
358 failed += base64_decoding_test(i, bt, 0);
359 if (bt->in_len != -1)
360 failed += base64_encoding_test(i, bt, 1);
361 if (bt->out_len != -1)
362 failed += base64_decoding_test(i, bt, 1);
363 }
364
365 fprintf(stderr, "Starting NL tests...\n");
366
367 for (i = 0; i < N_NL_TESTS; i++) {
368 bt = &base64_nl_tests[i];
369
370 if (bt->in_len != -1)
371 failed += base64_encoding_test(i, bt, 1);
372 if (bt->out_len != -1)
373 failed += base64_decoding_test(i, bt, 1);
374 }
375
376 fprintf(stderr, "Starting NO NL tests...\n");
377
378 for (i = 0; i < N_NO_NL_TESTS; i++) {
379 bt = &base64_no_nl_tests[i];
380
381 if (bt->in_len != -1)
382 failed += base64_encoding_test(i, bt, 0);
383 if (bt->out_len != -1)
384 failed += base64_decoding_test(i, bt, 0);
385 }
386
387 return failed;
388}