Refactor the test framework testutil
[openssl.git] / test / testutil / basic_output.c
1 /*
2  * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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
12 #include <openssl/crypto.h>
13 #include <openssl/bio.h>
14
15 BIO *bio_out = NULL;
16 BIO *bio_err = NULL;
17
18 #ifdef OPENSSL_USE_APPLINK
19 /*
20  * Using BIO_new_fd() obligates the use of applinks on platforms where it's
21  * relevant.  Because it becomes a module of the libtestutil library and would
22  * be disregarded if not actively referred to, we have this dummy that does
23  * exactly this.  For any module that uses the rest of the routines here,
24  * OPENSSL_Applink should tag along for sure.
25  */
26 void Applink_dummy(void);
27 void Applink_dummy(void)
28 {
29     OPENSSL_EXTERN void OPENSSL_Applink(void);
30
31     OPENSSL_Applink();
32 }
33 /* Generate an error for anyone who tries to actually use this dummy */
34 # define Applink_dummy "DON'T USE THIS"
35 #endif
36
37 void test_open_streams(void)
38 {
39     bio_out = BIO_new_fd(1, 0);
40     bio_err = BIO_new_fd(2, 0);
41
42     OPENSSL_assert(bio_out != NULL);
43     OPENSSL_assert(bio_err != NULL);
44 }
45
46 void test_close_streams(void)
47 {
48     BIO_free(bio_out);
49     BIO_free(bio_err);
50 }
51
52 int test_puts_stdout(const char *str)
53 {
54     return BIO_puts(bio_out, str);
55 }
56
57 int test_puts_stderr(const char *str)
58 {
59     return BIO_puts(bio_err, str);
60 }
61
62 int test_vprintf_stdout(const char *fmt, va_list ap)
63 {
64     return BIO_vprintf(bio_out, fmt, ap);
65 }
66
67 int test_vprintf_stderr(const char *fmt, va_list ap)
68 {
69     return BIO_vprintf(bio_err, fmt, ap);
70 }
71
72 int test_flush_stdout(void)
73 {
74     return BIO_flush(bio_out);
75 }
76
77 int test_flush_stderr(void)
78 {
79     return BIO_flush(bio_err);
80 }