1 /*
2 * Copyright 2017-2023 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 #if defined(OPENSSL_THREADS)
26 static CRYPTO_RWLOCK *io_lock = NULL;
27 #endif
28
test_open_streams(void)29 void test_open_streams(void)
30 {
31 tap_out = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT);
32 tap_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
33 #ifdef __VMS
34 tap_out = BIO_push(BIO_new(BIO_f_linebuffer()), tap_out);
35 tap_err = BIO_push(BIO_new(BIO_f_linebuffer()), tap_err);
36 #endif
37 tap_out = BIO_push(BIO_new(BIO_f_prefix()), tap_out);
38 tap_err = BIO_push(BIO_new(BIO_f_prefix()), tap_err);
39
40 bio_out = BIO_push(BIO_new(BIO_f_prefix()), tap_out);
41 bio_err = BIO_push(BIO_new(BIO_f_prefix()), tap_err);
42 BIO_set_prefix(bio_out, "# ");
43 BIO_set_prefix(bio_err, "# ");
44
45 #if defined(OPENSSL_THREADS)
46 io_lock = CRYPTO_THREAD_lock_new();
47 #endif
48
49 OPENSSL_assert(bio_out != NULL);
50 OPENSSL_assert(bio_err != NULL);
51 #if defined(OPENSSL_THREADS)
52 OPENSSL_assert(io_lock != NULL);
53 #endif
54 }
55
test_adjust_streams_tap_level(int level)56 void test_adjust_streams_tap_level(int level)
57 {
58 BIO_set_indent(tap_out, level);
59 BIO_set_indent(tap_err, level);
60 }
61
test_close_streams(void)62 void test_close_streams(void)
63 {
64 /*
65 * The rest of the chain is freed by the BIO_free_all() calls below, so
66 * we only need to free the last one in the bio_out and bio_err chains.
67 */
68 BIO_free(bio_out);
69 BIO_free(bio_err);
70
71 BIO_free_all(tap_out);
72 BIO_free_all(tap_err);
73
74 #if defined(OPENSSL_THREADS)
75 CRYPTO_THREAD_lock_free(io_lock);
76 #endif
77 }
78
test_io_lock(void)79 static ossl_inline void test_io_lock(void)
80 {
81 #if defined(OPENSSL_THREADS)
82 OPENSSL_assert(CRYPTO_THREAD_write_lock(io_lock) > 0);
83 #endif
84 }
85
test_io_unlock(void)86 static ossl_inline void test_io_unlock(void)
87 {
88 #if defined(OPENSSL_THREADS)
89 CRYPTO_THREAD_unlock(io_lock);
90 #endif
91 }
92
test_vprintf_stdout(const char * fmt,va_list ap)93 int test_vprintf_stdout(const char *fmt, va_list ap)
94 {
95 int r;
96
97 test_io_lock();
98 r = BIO_vprintf(bio_out, fmt, ap);
99 test_io_unlock();
100
101 return r;
102 }
103
test_vprintf_stderr(const char * fmt,va_list ap)104 int test_vprintf_stderr(const char *fmt, va_list ap)
105 {
106 int r;
107
108 test_io_lock();
109 r = BIO_vprintf(bio_err, fmt, ap);
110 test_io_unlock();
111
112 return r;
113 }
114
test_flush_stdout(void)115 int test_flush_stdout(void)
116 {
117 int r;
118
119 test_io_lock();
120 r = BIO_flush(bio_out);
121 test_io_unlock();
122
123 return r;
124 }
125
test_flush_stderr(void)126 int test_flush_stderr(void)
127 {
128 int r;
129
130 test_io_lock();
131 r = BIO_flush(bio_err);
132 test_io_unlock();
133
134 return r;
135 }
136
test_vprintf_tapout(const char * fmt,va_list ap)137 int test_vprintf_tapout(const char *fmt, va_list ap)
138 {
139 int r;
140
141 test_io_lock();
142 r = BIO_vprintf(tap_out, fmt, ap);
143 test_io_unlock();
144
145 return r;
146 }
147
test_vprintf_taperr(const char * fmt,va_list ap)148 int test_vprintf_taperr(const char *fmt, va_list ap)
149 {
150 int r;
151
152 test_io_lock();
153 r = BIO_vprintf(tap_err, fmt, ap);
154 test_io_unlock();
155
156 return r;
157 }
158
test_flush_tapout(void)159 int test_flush_tapout(void)
160 {
161 int r;
162
163 test_io_lock();
164 r = BIO_flush(tap_out);
165 test_io_unlock();
166
167 return r;
168 }
169
test_flush_taperr(void)170 int test_flush_taperr(void)
171 {
172 int r;
173
174 test_io_lock();
175 r = BIO_flush(tap_err);
176 test_io_unlock();
177
178 return r;
179 }
180