2 * Copyright 2017-2023 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "../testutil.h"
14 #include <openssl/crypto.h>
15 #include <openssl/bio.h>
17 /* These are available for any test program */
21 /* These are available for TAP output only (internally) */
22 static BIO *tap_out = NULL;
23 static BIO *tap_err = NULL;
25 #if defined(OPENSSL_THREADS)
26 static CRYPTO_RWLOCK *io_lock = NULL;
29 void test_open_streams(void)
31 tap_out = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT);
32 tap_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
34 tap_out = BIO_push(BIO_new(BIO_f_linebuffer()), tap_out);
35 tap_err = BIO_push(BIO_new(BIO_f_linebuffer()), tap_err);
37 tap_out = BIO_push(BIO_new(BIO_f_prefix()), tap_out);
38 tap_err = BIO_push(BIO_new(BIO_f_prefix()), tap_err);
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, "# ");
45 #if defined(OPENSSL_THREADS)
46 io_lock = CRYPTO_THREAD_lock_new();
49 OPENSSL_assert(bio_out != NULL);
50 OPENSSL_assert(bio_err != NULL);
51 #if defined(OPENSSL_THREADS)
52 OPENSSL_assert(io_lock != NULL);
56 void test_adjust_streams_tap_level(int level)
58 BIO_set_indent(tap_out, level);
59 BIO_set_indent(tap_err, level);
62 void test_close_streams(void)
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.
71 BIO_free_all(tap_out);
72 BIO_free_all(tap_err);
74 #if defined(OPENSSL_THREADS)
75 CRYPTO_THREAD_lock_free(io_lock);
79 static ossl_inline void test_io_lock(void)
81 #if defined(OPENSSL_THREADS)
82 OPENSSL_assert(CRYPTO_THREAD_write_lock(io_lock) > 0);
86 static ossl_inline void test_io_unlock(void)
88 #if defined(OPENSSL_THREADS)
89 CRYPTO_THREAD_unlock(io_lock);
93 int test_vprintf_stdout(const char *fmt, va_list ap)
98 r = BIO_vprintf(bio_out, fmt, ap);
104 int test_vprintf_stderr(const char *fmt, va_list ap)
109 r = BIO_vprintf(bio_err, fmt, ap);
115 int test_flush_stdout(void)
120 r = BIO_flush(bio_out);
126 int test_flush_stderr(void)
131 r = BIO_flush(bio_err);
137 int test_vprintf_tapout(const char *fmt, va_list ap)
142 r = BIO_vprintf(tap_out, fmt, ap);
148 int test_vprintf_taperr(const char *fmt, va_list ap)
153 r = BIO_vprintf(tap_err, fmt, ap);
159 int test_flush_tapout(void)
164 r = BIO_flush(tap_out);
170 int test_flush_taperr(void)
175 r = BIO_flush(tap_err);