basic_output.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #include "../testutil.h"
  10. #include "output.h"
  11. #include "tu_local.h"
  12. #include <openssl/crypto.h>
  13. #include <openssl/bio.h>
  14. /* These are available for any test program */
  15. BIO *bio_out = NULL;
  16. BIO *bio_err = NULL;
  17. /* These are available for TAP output only (internally) */
  18. static BIO *tap_out = NULL;
  19. static BIO *tap_err = NULL;
  20. #if defined(OPENSSL_THREADS)
  21. static CRYPTO_RWLOCK *io_lock = NULL;
  22. #endif
  23. void test_open_streams(void)
  24. {
  25. tap_out = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT);
  26. tap_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  27. #ifdef __VMS
  28. tap_out = BIO_push(BIO_new(BIO_f_linebuffer()), tap_out);
  29. tap_err = BIO_push(BIO_new(BIO_f_linebuffer()), tap_err);
  30. #endif
  31. tap_out = BIO_push(BIO_new(BIO_f_prefix()), tap_out);
  32. tap_err = BIO_push(BIO_new(BIO_f_prefix()), tap_err);
  33. bio_out = BIO_push(BIO_new(BIO_f_prefix()), tap_out);
  34. bio_err = BIO_push(BIO_new(BIO_f_prefix()), tap_err);
  35. BIO_set_prefix(bio_out, "# ");
  36. BIO_set_prefix(bio_err, "# ");
  37. #if defined(OPENSSL_THREADS)
  38. io_lock = CRYPTO_THREAD_lock_new();
  39. #endif
  40. OPENSSL_assert(bio_out != NULL);
  41. OPENSSL_assert(bio_err != NULL);
  42. #if defined(OPENSSL_THREADS)
  43. OPENSSL_assert(io_lock != NULL);
  44. #endif
  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. 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. BIO_free_all(tap_out);
  60. BIO_free_all(tap_err);
  61. #if defined(OPENSSL_THREADS)
  62. CRYPTO_THREAD_lock_free(io_lock);
  63. #endif
  64. }
  65. static ossl_inline void test_io_lock(void)
  66. {
  67. #if defined(OPENSSL_THREADS)
  68. OPENSSL_assert(CRYPTO_THREAD_write_lock(io_lock) > 0);
  69. #endif
  70. }
  71. static ossl_inline void test_io_unlock(void)
  72. {
  73. #if defined(OPENSSL_THREADS)
  74. CRYPTO_THREAD_unlock(io_lock);
  75. #endif
  76. }
  77. int test_vprintf_stdout(const char *fmt, va_list ap)
  78. {
  79. int r;
  80. test_io_lock();
  81. r = BIO_vprintf(bio_out, fmt, ap);
  82. test_io_unlock();
  83. return r;
  84. }
  85. int test_vprintf_stderr(const char *fmt, va_list ap)
  86. {
  87. int r;
  88. test_io_lock();
  89. r = BIO_vprintf(bio_err, fmt, ap);
  90. test_io_unlock();
  91. return r;
  92. }
  93. int test_flush_stdout(void)
  94. {
  95. int r;
  96. test_io_lock();
  97. r = BIO_flush(bio_out);
  98. test_io_unlock();
  99. return r;
  100. }
  101. int test_flush_stderr(void)
  102. {
  103. int r;
  104. test_io_lock();
  105. r = BIO_flush(bio_err);
  106. test_io_unlock();
  107. return r;
  108. }
  109. int test_vprintf_tapout(const char *fmt, va_list ap)
  110. {
  111. int r;
  112. test_io_lock();
  113. r = BIO_vprintf(tap_out, fmt, ap);
  114. test_io_unlock();
  115. return r;
  116. }
  117. int test_vprintf_taperr(const char *fmt, va_list ap)
  118. {
  119. int r;
  120. test_io_lock();
  121. r = BIO_vprintf(tap_err, fmt, ap);
  122. test_io_unlock();
  123. return r;
  124. }
  125. int test_flush_tapout(void)
  126. {
  127. int r;
  128. test_io_lock();
  129. r = BIO_flush(tap_out);
  130. test_io_unlock();
  131. return r;
  132. }
  133. int test_flush_taperr(void)
  134. {
  135. int r;
  136. test_io_lock();
  137. r = BIO_flush(tap_err);
  138. test_io_unlock();
  139. return r;
  140. }