xref: /freebsd/crypto/openssl/test/bio_callback_test.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert #define OPENSSL_SUPPRESS_DEPRECATED
10*e0c4386eSCy Schubert #include <stdio.h>
11*e0c4386eSCy Schubert #include <string.h>
12*e0c4386eSCy Schubert #include <openssl/bio.h>
13*e0c4386eSCy Schubert 
14*e0c4386eSCy Schubert #include "testutil.h"
15*e0c4386eSCy Schubert 
16*e0c4386eSCy Schubert #define MAXCOUNT 5
17*e0c4386eSCy Schubert static int         my_param_count;
18*e0c4386eSCy Schubert static BIO        *my_param_b[MAXCOUNT];
19*e0c4386eSCy Schubert static int         my_param_oper[MAXCOUNT];
20*e0c4386eSCy Schubert static const char *my_param_argp[MAXCOUNT];
21*e0c4386eSCy Schubert static int         my_param_argi[MAXCOUNT];
22*e0c4386eSCy Schubert static long        my_param_argl[MAXCOUNT];
23*e0c4386eSCy Schubert static long        my_param_ret[MAXCOUNT];
24*e0c4386eSCy Schubert static size_t      my_param_len[MAXCOUNT];
25*e0c4386eSCy Schubert static size_t      my_param_processed[MAXCOUNT];
26*e0c4386eSCy Schubert 
my_bio_cb_ex(BIO * b,int oper,const char * argp,size_t len,int argi,long argl,int ret,size_t * processed)27*e0c4386eSCy Schubert static long my_bio_cb_ex(BIO *b, int oper, const char *argp, size_t len,
28*e0c4386eSCy Schubert                          int argi, long argl, int ret, size_t *processed)
29*e0c4386eSCy Schubert {
30*e0c4386eSCy Schubert     if (my_param_count >= MAXCOUNT)
31*e0c4386eSCy Schubert         return -1;
32*e0c4386eSCy Schubert     my_param_b[my_param_count]    = b;
33*e0c4386eSCy Schubert     my_param_oper[my_param_count] = oper;
34*e0c4386eSCy Schubert     my_param_argp[my_param_count] = argp;
35*e0c4386eSCy Schubert     my_param_argi[my_param_count] = argi;
36*e0c4386eSCy Schubert     my_param_argl[my_param_count] = argl;
37*e0c4386eSCy Schubert     my_param_ret[my_param_count]  = ret;
38*e0c4386eSCy Schubert     my_param_len[my_param_count]  = len;
39*e0c4386eSCy Schubert     my_param_processed[my_param_count] = processed != NULL ? *processed : 0;
40*e0c4386eSCy Schubert 
41*e0c4386eSCy Schubert     my_param_count++;
42*e0c4386eSCy Schubert     return ret;
43*e0c4386eSCy Schubert }
44*e0c4386eSCy Schubert 
test_bio_callback_ex(void)45*e0c4386eSCy Schubert static int test_bio_callback_ex(void)
46*e0c4386eSCy Schubert {
47*e0c4386eSCy Schubert     int ok = 0;
48*e0c4386eSCy Schubert     BIO *bio;
49*e0c4386eSCy Schubert     int i;
50*e0c4386eSCy Schubert     char test1[] = "test";
51*e0c4386eSCy Schubert     const size_t test1len = sizeof(test1) - 1;
52*e0c4386eSCy Schubert     char test2[] = "hello";
53*e0c4386eSCy Schubert     const size_t test2len = sizeof(test2) - 1;
54*e0c4386eSCy Schubert     char buf[16];
55*e0c4386eSCy Schubert 
56*e0c4386eSCy Schubert     my_param_count = 0;
57*e0c4386eSCy Schubert 
58*e0c4386eSCy Schubert     bio = BIO_new(BIO_s_mem());
59*e0c4386eSCy Schubert     if (bio == NULL)
60*e0c4386eSCy Schubert         goto err;
61*e0c4386eSCy Schubert 
62*e0c4386eSCy Schubert     BIO_set_callback_ex(bio, my_bio_cb_ex);
63*e0c4386eSCy Schubert     i = BIO_write(bio, test1, test1len);
64*e0c4386eSCy Schubert     if (!TEST_int_eq(i, test1len)
65*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_count, 2)
66*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_b[0], bio)
67*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_oper[0], BIO_CB_WRITE)
68*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_argp[0], test1)
69*e0c4386eSCy Schubert             || !TEST_size_t_eq(my_param_len[0], test1len)
70*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_argl[0], 0L)
71*e0c4386eSCy Schubert             || !TEST_int_eq((int)my_param_ret[0], 1)
72*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_b[1], bio)
73*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_oper[1], BIO_CB_WRITE | BIO_CB_RETURN)
74*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_argp[1], test1)
75*e0c4386eSCy Schubert             || !TEST_size_t_eq(my_param_len[1], test1len)
76*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_argl[1], 0L)
77*e0c4386eSCy Schubert             || !TEST_size_t_eq(my_param_processed[1], test1len)
78*e0c4386eSCy Schubert             || !TEST_int_eq((int)my_param_ret[1], 1))
79*e0c4386eSCy Schubert         goto err;
80*e0c4386eSCy Schubert 
81*e0c4386eSCy Schubert     my_param_count = 0;
82*e0c4386eSCy Schubert     i = BIO_read(bio, buf, sizeof(buf));
83*e0c4386eSCy Schubert     if (!TEST_mem_eq(buf, i, test1, test1len)
84*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_count, 2)
85*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_b[0], bio)
86*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_oper[0], BIO_CB_READ)
87*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_argp[0], buf)
88*e0c4386eSCy Schubert             || !TEST_size_t_eq(my_param_len[0], sizeof(buf))
89*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_argl[0], 0L)
90*e0c4386eSCy Schubert             || !TEST_int_eq((int)my_param_ret[0], 1)
91*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_b[1], bio)
92*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN)
93*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_argp[1], buf)
94*e0c4386eSCy Schubert             || !TEST_size_t_eq(my_param_len[1], sizeof(buf))
95*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_argl[1], 0L)
96*e0c4386eSCy Schubert             || !TEST_size_t_eq(my_param_processed[1], test1len)
97*e0c4386eSCy Schubert             || !TEST_int_eq((int)my_param_ret[1], 1))
98*e0c4386eSCy Schubert         goto err;
99*e0c4386eSCy Schubert 
100*e0c4386eSCy Schubert     /* By default a mem bio returns -1 if it has run out of data */
101*e0c4386eSCy Schubert     my_param_count = 0;
102*e0c4386eSCy Schubert     i = BIO_read(bio, buf, sizeof(buf));
103*e0c4386eSCy Schubert     if (!TEST_int_eq(i, -1)
104*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_count, 2)
105*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_b[0], bio)
106*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_oper[0], BIO_CB_READ)
107*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_argp[0], buf)
108*e0c4386eSCy Schubert             || !TEST_size_t_eq(my_param_len[0], sizeof(buf))
109*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_argl[0], 0L)
110*e0c4386eSCy Schubert             || !TEST_int_eq((int)my_param_ret[0], 1)
111*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_b[1], bio)
112*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN)
113*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_argp[1], buf)
114*e0c4386eSCy Schubert             || !TEST_size_t_eq(my_param_len[1], sizeof(buf))
115*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_argl[1], 0L)
116*e0c4386eSCy Schubert             || !TEST_size_t_eq(my_param_processed[1], 0)
117*e0c4386eSCy Schubert             || !TEST_int_eq((int)my_param_ret[1], -1))
118*e0c4386eSCy Schubert         goto err;
119*e0c4386eSCy Schubert 
120*e0c4386eSCy Schubert     /* Force the mem bio to return 0 if it has run out of data */
121*e0c4386eSCy Schubert     my_param_count = 0;
122*e0c4386eSCy Schubert     i = BIO_set_mem_eof_return(bio, 0);
123*e0c4386eSCy Schubert     if (!TEST_int_eq(i, 1)
124*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_count, 2)
125*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_b[0], bio)
126*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_oper[0], BIO_CB_CTRL)
127*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_argp[0], NULL)
128*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_argi[0], BIO_C_SET_BUF_MEM_EOF_RETURN)
129*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_argl[0], 0L)
130*e0c4386eSCy Schubert             || !TEST_int_eq((int)my_param_ret[0], 1)
131*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_b[1], bio)
132*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_oper[1], BIO_CB_CTRL | BIO_CB_RETURN)
133*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_argp[1], NULL)
134*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_argi[1], BIO_C_SET_BUF_MEM_EOF_RETURN)
135*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_argl[1], 0L)
136*e0c4386eSCy Schubert             || !TEST_int_eq((int)my_param_ret[1], 1))
137*e0c4386eSCy Schubert         goto err;
138*e0c4386eSCy Schubert     my_param_count = 0;
139*e0c4386eSCy Schubert     i = BIO_read(bio, buf, sizeof(buf));
140*e0c4386eSCy Schubert     if (!TEST_int_eq(i, 0)
141*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_count, 2)
142*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_b[0], bio)
143*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_oper[0], BIO_CB_READ)
144*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_argp[0], buf)
145*e0c4386eSCy Schubert             || !TEST_size_t_eq(my_param_len[0], sizeof(buf))
146*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_argl[0], 0L)
147*e0c4386eSCy Schubert             || !TEST_int_eq((int)my_param_ret[0], 1)
148*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_b[1], bio)
149*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN)
150*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_argp[1], buf)
151*e0c4386eSCy Schubert             || !TEST_size_t_eq(my_param_len[1], sizeof(buf))
152*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_argl[1], 0L)
153*e0c4386eSCy Schubert             || !TEST_size_t_eq(my_param_processed[1], 0)
154*e0c4386eSCy Schubert             || !TEST_int_eq((int)my_param_ret[1], 0))
155*e0c4386eSCy Schubert         goto err;
156*e0c4386eSCy Schubert 
157*e0c4386eSCy Schubert     my_param_count = 0;
158*e0c4386eSCy Schubert     i = BIO_puts(bio, test2);
159*e0c4386eSCy Schubert     if (!TEST_int_eq(i, 5)
160*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_count, 2)
161*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_b[0], bio)
162*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_oper[0], BIO_CB_PUTS)
163*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_argp[0], test2)
164*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_argi[0], 0)
165*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_argl[0], 0L)
166*e0c4386eSCy Schubert             || !TEST_int_eq((int)my_param_ret[0], 1)
167*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_b[1], bio)
168*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_oper[1], BIO_CB_PUTS | BIO_CB_RETURN)
169*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_argp[1], test2)
170*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_argi[1], 0)
171*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_argl[1], 0L)
172*e0c4386eSCy Schubert             || !TEST_size_t_eq(my_param_processed[1], test2len)
173*e0c4386eSCy Schubert             || !TEST_int_eq((int)my_param_ret[1], 1))
174*e0c4386eSCy Schubert         goto err;
175*e0c4386eSCy Schubert 
176*e0c4386eSCy Schubert     my_param_count = 0;
177*e0c4386eSCy Schubert     i = BIO_free(bio);
178*e0c4386eSCy Schubert     if (!TEST_int_eq(i, 1)
179*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_count, 1)
180*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_b[0], bio)
181*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_oper[0], BIO_CB_FREE)
182*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_argp[0], NULL)
183*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_argi[0], 0)
184*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_argl[0], 0L)
185*e0c4386eSCy Schubert             || !TEST_int_eq((int)my_param_ret[0], 1))
186*e0c4386eSCy Schubert         goto finish;
187*e0c4386eSCy Schubert 
188*e0c4386eSCy Schubert     ok = 1;
189*e0c4386eSCy Schubert     goto finish;
190*e0c4386eSCy Schubert 
191*e0c4386eSCy Schubert err:
192*e0c4386eSCy Schubert     BIO_free(bio);
193*e0c4386eSCy Schubert 
194*e0c4386eSCy Schubert finish:
195*e0c4386eSCy Schubert     /* This helps finding memory leaks with ASAN */
196*e0c4386eSCy Schubert     memset(my_param_b, 0, sizeof(my_param_b));
197*e0c4386eSCy Schubert     memset(my_param_argp, 0, sizeof(my_param_argp));
198*e0c4386eSCy Schubert     return ok;
199*e0c4386eSCy Schubert }
200*e0c4386eSCy Schubert 
201*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DEPRECATED_3_0
my_bio_callback(BIO * b,int oper,const char * argp,int argi,long argl,long ret)202*e0c4386eSCy Schubert static long my_bio_callback(BIO *b, int oper, const char *argp, int argi,
203*e0c4386eSCy Schubert                             long argl, long ret)
204*e0c4386eSCy Schubert {
205*e0c4386eSCy Schubert     if (my_param_count >= MAXCOUNT)
206*e0c4386eSCy Schubert         return -1;
207*e0c4386eSCy Schubert     my_param_b[my_param_count]    = b;
208*e0c4386eSCy Schubert     my_param_oper[my_param_count] = oper;
209*e0c4386eSCy Schubert     my_param_argp[my_param_count] = argp;
210*e0c4386eSCy Schubert     my_param_argi[my_param_count] = argi;
211*e0c4386eSCy Schubert     my_param_argl[my_param_count] = argl;
212*e0c4386eSCy Schubert     my_param_ret[my_param_count]  = ret;
213*e0c4386eSCy Schubert     my_param_count++;
214*e0c4386eSCy Schubert     return ret;
215*e0c4386eSCy Schubert }
216*e0c4386eSCy Schubert 
test_bio_callback(void)217*e0c4386eSCy Schubert static int test_bio_callback(void)
218*e0c4386eSCy Schubert {
219*e0c4386eSCy Schubert     int ok = 0;
220*e0c4386eSCy Schubert     BIO *bio;
221*e0c4386eSCy Schubert     int i;
222*e0c4386eSCy Schubert     char test1[] = "test";
223*e0c4386eSCy Schubert     const int test1len = sizeof(test1) - 1;
224*e0c4386eSCy Schubert     char test2[] = "hello";
225*e0c4386eSCy Schubert     const int test2len = sizeof(test2) - 1;
226*e0c4386eSCy Schubert     char buf[16];
227*e0c4386eSCy Schubert 
228*e0c4386eSCy Schubert     my_param_count = 0;
229*e0c4386eSCy Schubert 
230*e0c4386eSCy Schubert     bio = BIO_new(BIO_s_mem());
231*e0c4386eSCy Schubert     if (bio == NULL)
232*e0c4386eSCy Schubert         goto err;
233*e0c4386eSCy Schubert 
234*e0c4386eSCy Schubert     BIO_set_callback(bio, my_bio_callback);
235*e0c4386eSCy Schubert     i = BIO_write(bio, test1, test1len);
236*e0c4386eSCy Schubert     if (!TEST_int_eq(i, test1len)
237*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_count, 2)
238*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_b[0], bio)
239*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_oper[0], BIO_CB_WRITE)
240*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_argp[0], test1)
241*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_argi[0], test1len)
242*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_argl[0], 0L)
243*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_ret[0], 1L)
244*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_b[1], bio)
245*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_oper[1], BIO_CB_WRITE | BIO_CB_RETURN)
246*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_argp[1], test1)
247*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_argi[1], test1len)
248*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_argl[1], 0L)
249*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_ret[1], (long)test1len))
250*e0c4386eSCy Schubert         goto err;
251*e0c4386eSCy Schubert 
252*e0c4386eSCy Schubert     my_param_count = 0;
253*e0c4386eSCy Schubert     i = BIO_read(bio, buf, sizeof(buf));
254*e0c4386eSCy Schubert     if (!TEST_mem_eq(buf, i, test1, test1len)
255*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_count, 2)
256*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_b[0], bio)
257*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_oper[0], BIO_CB_READ)
258*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_argp[0], buf)
259*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_argi[0], sizeof(buf))
260*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_argl[0], 0L)
261*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_ret[0], 1L)
262*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_b[1], bio)
263*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN)
264*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_argp[1], buf)
265*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_argi[1], sizeof(buf))
266*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_argl[1], 0L)
267*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_ret[1], (long)test1len))
268*e0c4386eSCy Schubert         goto err;
269*e0c4386eSCy Schubert 
270*e0c4386eSCy Schubert     /* By default a mem bio returns -1 if it has run out of data */
271*e0c4386eSCy Schubert     my_param_count = 0;
272*e0c4386eSCy Schubert     i = BIO_read(bio, buf, sizeof(buf));
273*e0c4386eSCy Schubert     if (!TEST_int_eq(i, -1)
274*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_count, 2)
275*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_b[0], bio)
276*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_oper[0], BIO_CB_READ)
277*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_argp[0], buf)
278*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_argi[0], sizeof(buf))
279*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_argl[0], 0L)
280*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_ret[0], 1L)
281*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_b[1], bio)
282*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN)
283*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_argp[1], buf)
284*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_argi[1], sizeof(buf))
285*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_argl[1], 0L)
286*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_ret[1], -1L))
287*e0c4386eSCy Schubert         goto err;
288*e0c4386eSCy Schubert 
289*e0c4386eSCy Schubert     /* Force the mem bio to return 0 if it has run out of data */
290*e0c4386eSCy Schubert     BIO_set_mem_eof_return(bio, 0);
291*e0c4386eSCy Schubert     my_param_count = 0;
292*e0c4386eSCy Schubert     i = BIO_read(bio, buf, sizeof(buf));
293*e0c4386eSCy Schubert     if (!TEST_int_eq(i, 0)
294*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_count, 2)
295*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_b[0], bio)
296*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_oper[0], BIO_CB_READ)
297*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_argp[0], buf)
298*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_argi[0], sizeof(buf))
299*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_argl[0], 0L)
300*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_ret[0], 1L)
301*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_b[1], bio)
302*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN)
303*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_argp[1], buf)
304*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_argi[1], sizeof(buf))
305*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_argl[1], 0L)
306*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_ret[1], 0L))
307*e0c4386eSCy Schubert         goto err;
308*e0c4386eSCy Schubert 
309*e0c4386eSCy Schubert     my_param_count = 0;
310*e0c4386eSCy Schubert     i = BIO_puts(bio, test2);
311*e0c4386eSCy Schubert     if (!TEST_int_eq(i, 5)
312*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_count, 2)
313*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_b[0], bio)
314*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_oper[0], BIO_CB_PUTS)
315*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_argp[0], test2)
316*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_argi[0], 0)
317*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_argl[0], 0L)
318*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_ret[0], 1L)
319*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_b[1], bio)
320*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_oper[1], BIO_CB_PUTS | BIO_CB_RETURN)
321*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_argp[1], test2)
322*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_argi[1], 0)
323*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_argl[1], 0L)
324*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_ret[1], (long)test2len))
325*e0c4386eSCy Schubert         goto err;
326*e0c4386eSCy Schubert 
327*e0c4386eSCy Schubert     my_param_count = 0;
328*e0c4386eSCy Schubert     i = BIO_free(bio);
329*e0c4386eSCy Schubert     if (!TEST_int_eq(i, 1)
330*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_count, 1)
331*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_b[0], bio)
332*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_oper[0], BIO_CB_FREE)
333*e0c4386eSCy Schubert             || !TEST_ptr_eq(my_param_argp[0], NULL)
334*e0c4386eSCy Schubert             || !TEST_int_eq(my_param_argi[0], 0)
335*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_argl[0], 0L)
336*e0c4386eSCy Schubert             || !TEST_long_eq(my_param_ret[0], 1L))
337*e0c4386eSCy Schubert         goto finish;
338*e0c4386eSCy Schubert 
339*e0c4386eSCy Schubert     ok = 1;
340*e0c4386eSCy Schubert     goto finish;
341*e0c4386eSCy Schubert 
342*e0c4386eSCy Schubert err:
343*e0c4386eSCy Schubert     BIO_free(bio);
344*e0c4386eSCy Schubert 
345*e0c4386eSCy Schubert finish:
346*e0c4386eSCy Schubert     /* This helps finding memory leaks with ASAN */
347*e0c4386eSCy Schubert     memset(my_param_b, 0, sizeof(my_param_b));
348*e0c4386eSCy Schubert     memset(my_param_argp, 0, sizeof(my_param_argp));
349*e0c4386eSCy Schubert     return ok;
350*e0c4386eSCy Schubert }
351*e0c4386eSCy Schubert #endif
352*e0c4386eSCy Schubert 
setup_tests(void)353*e0c4386eSCy Schubert int setup_tests(void)
354*e0c4386eSCy Schubert {
355*e0c4386eSCy Schubert     ADD_TEST(test_bio_callback_ex);
356*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DEPRECATED_3_0
357*e0c4386eSCy Schubert     ADD_TEST(test_bio_callback);
358*e0c4386eSCy Schubert #endif
359*e0c4386eSCy Schubert     return 1;
360*e0c4386eSCy Schubert }
361