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