Home | History | Annotate | Line # | Download | only in testutil
      1 /*
      2  * Copyright 2017-2020 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     char prefix[] = "# ";
     28     tap_out = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT);
     29     tap_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
     30 #ifdef __VMS
     31     tap_out = BIO_push(BIO_new(BIO_f_linebuffer()), tap_out);
     32     tap_err = BIO_push(BIO_new(BIO_f_linebuffer()), tap_err);
     33 #endif
     34     tap_out = BIO_push(BIO_new(BIO_f_prefix()), tap_out);
     35     tap_err = BIO_push(BIO_new(BIO_f_prefix()), tap_err);
     36 
     37     bio_out = BIO_push(BIO_new(BIO_f_prefix()), tap_out);
     38     bio_err = BIO_push(BIO_new(BIO_f_prefix()), tap_err);
     39     BIO_set_prefix(bio_out, prefix);
     40     BIO_set_prefix(bio_err, prefix);
     41 
     42     OPENSSL_assert(bio_out != NULL);
     43     OPENSSL_assert(bio_err != NULL);
     44 }
     45 
     46 void test_adjust_streams_tap_level(int level)
     47 {
     48     BIO_set_indent(tap_out, level);
     49     BIO_set_indent(tap_err, level);
     50 }
     51 
     52 void test_close_streams(void)
     53 {
     54     /*
     55      * The rest of the chain is freed by the BIO_free_all() calls below, so
     56      * we only need to free the last one in the bio_out and bio_err chains.
     57      */
     58     BIO_free(bio_out);
     59     BIO_free(bio_err);
     60 
     61     BIO_free_all(tap_out);
     62     BIO_free_all(tap_err);
     63 }
     64 
     65 int test_vprintf_stdout(const char *fmt, va_list ap)
     66 {
     67     return BIO_vprintf(bio_out, fmt, ap);
     68 }
     69 
     70 int test_vprintf_stderr(const char *fmt, va_list ap)
     71 {
     72     return BIO_vprintf(bio_err, fmt, ap);
     73 }
     74 
     75 int test_flush_stdout(void)
     76 {
     77     return BIO_flush(bio_out);
     78 }
     79 
     80 int test_flush_stderr(void)
     81 {
     82     return BIO_flush(bio_err);
     83 }
     84 
     85 int test_vprintf_tapout(const char *fmt, va_list ap)
     86 {
     87     return BIO_vprintf(tap_out, fmt, ap);
     88 }
     89 
     90 int test_vprintf_taperr(const char *fmt, va_list ap)
     91 {
     92     return BIO_vprintf(tap_err, fmt, ap);
     93 }
     94 
     95 int test_flush_tapout(void)
     96 {
     97     return BIO_flush(tap_out);
     98 }
     99 
    100 int test_flush_taperr(void)
    101 {
    102     return BIO_flush(tap_err);
    103 }
    104