1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include "../testutil.h"
11e1051a39Sopenharmony_ci#include "output.h"
12e1051a39Sopenharmony_ci#include "tu_local.h"
13e1051a39Sopenharmony_ci
14e1051a39Sopenharmony_ci#include <openssl/crypto.h>
15e1051a39Sopenharmony_ci#include <openssl/bio.h>
16e1051a39Sopenharmony_ci
17e1051a39Sopenharmony_ci/* These are available for any test program */
18e1051a39Sopenharmony_ciBIO *bio_out = NULL;
19e1051a39Sopenharmony_ciBIO *bio_err = NULL;
20e1051a39Sopenharmony_ci
21e1051a39Sopenharmony_ci/* These are available for TAP output only (internally) */
22e1051a39Sopenharmony_cistatic BIO *tap_out = NULL;
23e1051a39Sopenharmony_cistatic BIO *tap_err = NULL;
24e1051a39Sopenharmony_ci
25e1051a39Sopenharmony_civoid test_open_streams(void)
26e1051a39Sopenharmony_ci{
27e1051a39Sopenharmony_ci    tap_out = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT);
28e1051a39Sopenharmony_ci    tap_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
29e1051a39Sopenharmony_ci#ifdef __VMS
30e1051a39Sopenharmony_ci    tap_out = BIO_push(BIO_new(BIO_f_linebuffer()), tap_out);
31e1051a39Sopenharmony_ci    tap_err = BIO_push(BIO_new(BIO_f_linebuffer()), tap_err);
32e1051a39Sopenharmony_ci#endif
33e1051a39Sopenharmony_ci    tap_out = BIO_push(BIO_new(BIO_f_prefix()), tap_out);
34e1051a39Sopenharmony_ci    tap_err = BIO_push(BIO_new(BIO_f_prefix()), tap_err);
35e1051a39Sopenharmony_ci
36e1051a39Sopenharmony_ci    bio_out = BIO_push(BIO_new(BIO_f_prefix()), tap_out);
37e1051a39Sopenharmony_ci    bio_err = BIO_push(BIO_new(BIO_f_prefix()), tap_err);
38e1051a39Sopenharmony_ci    BIO_set_prefix(bio_out, "# ");
39e1051a39Sopenharmony_ci    BIO_set_prefix(bio_err, "# ");
40e1051a39Sopenharmony_ci
41e1051a39Sopenharmony_ci    OPENSSL_assert(bio_out != NULL);
42e1051a39Sopenharmony_ci    OPENSSL_assert(bio_err != NULL);
43e1051a39Sopenharmony_ci}
44e1051a39Sopenharmony_ci
45e1051a39Sopenharmony_civoid test_adjust_streams_tap_level(int level)
46e1051a39Sopenharmony_ci{
47e1051a39Sopenharmony_ci    BIO_set_indent(tap_out, level);
48e1051a39Sopenharmony_ci    BIO_set_indent(tap_err, level);
49e1051a39Sopenharmony_ci}
50e1051a39Sopenharmony_ci
51e1051a39Sopenharmony_civoid test_close_streams(void)
52e1051a39Sopenharmony_ci{
53e1051a39Sopenharmony_ci    /*
54e1051a39Sopenharmony_ci     * The rest of the chain is freed by the BIO_free_all() calls below, so
55e1051a39Sopenharmony_ci     * we only need to free the last one in the bio_out and bio_err chains.
56e1051a39Sopenharmony_ci     */
57e1051a39Sopenharmony_ci    BIO_free(bio_out);
58e1051a39Sopenharmony_ci    BIO_free(bio_err);
59e1051a39Sopenharmony_ci
60e1051a39Sopenharmony_ci    BIO_free_all(tap_out);
61e1051a39Sopenharmony_ci    BIO_free_all(tap_err);
62e1051a39Sopenharmony_ci}
63e1051a39Sopenharmony_ci
64e1051a39Sopenharmony_ciint test_vprintf_stdout(const char *fmt, va_list ap)
65e1051a39Sopenharmony_ci{
66e1051a39Sopenharmony_ci    return BIO_vprintf(bio_out, fmt, ap);
67e1051a39Sopenharmony_ci}
68e1051a39Sopenharmony_ci
69e1051a39Sopenharmony_ciint test_vprintf_stderr(const char *fmt, va_list ap)
70e1051a39Sopenharmony_ci{
71e1051a39Sopenharmony_ci    return BIO_vprintf(bio_err, fmt, ap);
72e1051a39Sopenharmony_ci}
73e1051a39Sopenharmony_ci
74e1051a39Sopenharmony_ciint test_flush_stdout(void)
75e1051a39Sopenharmony_ci{
76e1051a39Sopenharmony_ci    return BIO_flush(bio_out);
77e1051a39Sopenharmony_ci}
78e1051a39Sopenharmony_ci
79e1051a39Sopenharmony_ciint test_flush_stderr(void)
80e1051a39Sopenharmony_ci{
81e1051a39Sopenharmony_ci    return BIO_flush(bio_err);
82e1051a39Sopenharmony_ci}
83e1051a39Sopenharmony_ci
84e1051a39Sopenharmony_ciint test_vprintf_tapout(const char *fmt, va_list ap)
85e1051a39Sopenharmony_ci{
86e1051a39Sopenharmony_ci    return BIO_vprintf(tap_out, fmt, ap);
87e1051a39Sopenharmony_ci}
88e1051a39Sopenharmony_ci
89e1051a39Sopenharmony_ciint test_vprintf_taperr(const char *fmt, va_list ap)
90e1051a39Sopenharmony_ci{
91e1051a39Sopenharmony_ci    return BIO_vprintf(tap_err, fmt, ap);
92e1051a39Sopenharmony_ci}
93e1051a39Sopenharmony_ci
94e1051a39Sopenharmony_ciint test_flush_tapout(void)
95e1051a39Sopenharmony_ci{
96e1051a39Sopenharmony_ci    return BIO_flush(tap_out);
97e1051a39Sopenharmony_ci}
98e1051a39Sopenharmony_ci
99e1051a39Sopenharmony_ciint test_flush_taperr(void)
100e1051a39Sopenharmony_ci{
101e1051a39Sopenharmony_ci    return BIO_flush(tap_err);
102e1051a39Sopenharmony_ci}
103