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