113d40330Schristos /*
2b0d17251Schristos * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
313d40330Schristos *
4b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
513d40330Schristos * this file except in compliance with the License. You can obtain a copy
613d40330Schristos * in the file LICENSE in the source distribution or at
713d40330Schristos * https://www.openssl.org/source/license.html
813d40330Schristos */
913d40330Schristos
1013d40330Schristos #include "../testutil.h"
1113d40330Schristos #include "output.h"
1213d40330Schristos #include "tu_local.h"
1313d40330Schristos
1413d40330Schristos #include <openssl/crypto.h>
1513d40330Schristos #include <openssl/bio.h>
1613d40330Schristos
17b0d17251Schristos /* These are available for any test program */
1813d40330Schristos BIO *bio_out = NULL;
1913d40330Schristos BIO *bio_err = NULL;
2013d40330Schristos
21b0d17251Schristos /* These are available for TAP output only (internally) */
22b0d17251Schristos static BIO *tap_out = NULL;
23b0d17251Schristos static BIO *tap_err = NULL;
24b0d17251Schristos
test_open_streams(void)2513d40330Schristos void test_open_streams(void)
2613d40330Schristos {
27*8fbed61eSchristos char prefix[] = "# ";
28b0d17251Schristos tap_out = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT);
29b0d17251Schristos tap_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
3013d40330Schristos #ifdef __VMS
31b0d17251Schristos tap_out = BIO_push(BIO_new(BIO_f_linebuffer()), tap_out);
32b0d17251Schristos tap_err = BIO_push(BIO_new(BIO_f_linebuffer()), tap_err);
3313d40330Schristos #endif
34b0d17251Schristos tap_out = BIO_push(BIO_new(BIO_f_prefix()), tap_out);
35b0d17251Schristos tap_err = BIO_push(BIO_new(BIO_f_prefix()), tap_err);
36b0d17251Schristos
37b0d17251Schristos bio_out = BIO_push(BIO_new(BIO_f_prefix()), tap_out);
38b0d17251Schristos bio_err = BIO_push(BIO_new(BIO_f_prefix()), tap_err);
39*8fbed61eSchristos BIO_set_prefix(bio_out, prefix);
40*8fbed61eSchristos BIO_set_prefix(bio_err, prefix);
4113d40330Schristos
4213d40330Schristos OPENSSL_assert(bio_out != NULL);
4313d40330Schristos OPENSSL_assert(bio_err != NULL);
4413d40330Schristos }
4513d40330Schristos
test_adjust_streams_tap_level(int level)46b0d17251Schristos void test_adjust_streams_tap_level(int level)
47b0d17251Schristos {
48b0d17251Schristos BIO_set_indent(tap_out, level);
49b0d17251Schristos BIO_set_indent(tap_err, level);
50b0d17251Schristos }
51b0d17251Schristos
test_close_streams(void)5213d40330Schristos void test_close_streams(void)
5313d40330Schristos {
54b0d17251Schristos /*
55b0d17251Schristos * The rest of the chain is freed by the BIO_free_all() calls below, so
56b0d17251Schristos * we only need to free the last one in the bio_out and bio_err chains.
57b0d17251Schristos */
58b0d17251Schristos BIO_free(bio_out);
59b0d17251Schristos BIO_free(bio_err);
60b0d17251Schristos
61b0d17251Schristos BIO_free_all(tap_out);
62b0d17251Schristos BIO_free_all(tap_err);
6313d40330Schristos }
6413d40330Schristos
test_vprintf_stdout(const char * fmt,va_list ap)6513d40330Schristos int test_vprintf_stdout(const char *fmt, va_list ap)
6613d40330Schristos {
6713d40330Schristos return BIO_vprintf(bio_out, fmt, ap);
6813d40330Schristos }
6913d40330Schristos
test_vprintf_stderr(const char * fmt,va_list ap)7013d40330Schristos int test_vprintf_stderr(const char *fmt, va_list ap)
7113d40330Schristos {
7213d40330Schristos return BIO_vprintf(bio_err, fmt, ap);
7313d40330Schristos }
7413d40330Schristos
test_flush_stdout(void)7513d40330Schristos int test_flush_stdout(void)
7613d40330Schristos {
7713d40330Schristos return BIO_flush(bio_out);
7813d40330Schristos }
7913d40330Schristos
test_flush_stderr(void)8013d40330Schristos int test_flush_stderr(void)
8113d40330Schristos {
8213d40330Schristos return BIO_flush(bio_err);
8313d40330Schristos }
84b0d17251Schristos
test_vprintf_tapout(const char * fmt,va_list ap)85b0d17251Schristos int test_vprintf_tapout(const char *fmt, va_list ap)
86b0d17251Schristos {
87b0d17251Schristos return BIO_vprintf(tap_out, fmt, ap);
88b0d17251Schristos }
89b0d17251Schristos
test_vprintf_taperr(const char * fmt,va_list ap)90b0d17251Schristos int test_vprintf_taperr(const char *fmt, va_list ap)
91b0d17251Schristos {
92b0d17251Schristos return BIO_vprintf(tap_err, fmt, ap);
93b0d17251Schristos }
94b0d17251Schristos
test_flush_tapout(void)95b0d17251Schristos int test_flush_tapout(void)
96b0d17251Schristos {
97b0d17251Schristos return BIO_flush(tap_out);
98b0d17251Schristos }
99b0d17251Schristos
test_flush_taperr(void)100b0d17251Schristos int test_flush_taperr(void)
101b0d17251Schristos {
102b0d17251Schristos return BIO_flush(tap_err);
103b0d17251Schristos }
104