1*1dcdf01fSchristos /*
2*1dcdf01fSchristos * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
3*1dcdf01fSchristos *
4*1dcdf01fSchristos * Licensed under the OpenSSL license (the "License"). You may not use
5*1dcdf01fSchristos * this file except in compliance with the License. You can obtain a copy
6*1dcdf01fSchristos * in the file LICENSE in the source distribution or at
7*1dcdf01fSchristos * https://www.openssl.org/source/license.html
8*1dcdf01fSchristos */
9*1dcdf01fSchristos #include <stdio.h>
10*1dcdf01fSchristos #include <string.h>
11*1dcdf01fSchristos #include <openssl/bio.h>
12*1dcdf01fSchristos
13*1dcdf01fSchristos #include "testutil.h"
14*1dcdf01fSchristos
15*1dcdf01fSchristos #define MAXCOUNT 5
16*1dcdf01fSchristos static int my_param_count;
17*1dcdf01fSchristos static BIO *my_param_b[MAXCOUNT];
18*1dcdf01fSchristos static int my_param_oper[MAXCOUNT];
19*1dcdf01fSchristos static const char *my_param_argp[MAXCOUNT];
20*1dcdf01fSchristos static int my_param_argi[MAXCOUNT];
21*1dcdf01fSchristos static long my_param_argl[MAXCOUNT];
22*1dcdf01fSchristos static long my_param_ret[MAXCOUNT];
23*1dcdf01fSchristos
my_bio_callback(BIO * b,int oper,const char * argp,int argi,long argl,long ret)24*1dcdf01fSchristos static long my_bio_callback(BIO *b, int oper, const char *argp, int argi,
25*1dcdf01fSchristos long argl, long ret)
26*1dcdf01fSchristos {
27*1dcdf01fSchristos if (my_param_count >= MAXCOUNT)
28*1dcdf01fSchristos return -1;
29*1dcdf01fSchristos my_param_b[my_param_count] = b;
30*1dcdf01fSchristos my_param_oper[my_param_count] = oper;
31*1dcdf01fSchristos my_param_argp[my_param_count] = argp;
32*1dcdf01fSchristos my_param_argi[my_param_count] = argi;
33*1dcdf01fSchristos my_param_argl[my_param_count] = argl;
34*1dcdf01fSchristos my_param_ret[my_param_count] = ret;
35*1dcdf01fSchristos my_param_count++;
36*1dcdf01fSchristos return ret;
37*1dcdf01fSchristos }
38*1dcdf01fSchristos
test_bio_callback(void)39*1dcdf01fSchristos static int test_bio_callback(void)
40*1dcdf01fSchristos {
41*1dcdf01fSchristos int ok = 0;
42*1dcdf01fSchristos BIO *bio;
43*1dcdf01fSchristos int i;
44*1dcdf01fSchristos char test1[] = "test";
45*1dcdf01fSchristos const int test1len = sizeof(test1) - 1;
46*1dcdf01fSchristos char test2[] = "hello";
47*1dcdf01fSchristos const int test2len = sizeof(test2) - 1;
48*1dcdf01fSchristos char buf[16];
49*1dcdf01fSchristos
50*1dcdf01fSchristos my_param_count = 0;
51*1dcdf01fSchristos
52*1dcdf01fSchristos bio = BIO_new(BIO_s_mem());
53*1dcdf01fSchristos if (bio == NULL)
54*1dcdf01fSchristos goto err;
55*1dcdf01fSchristos
56*1dcdf01fSchristos BIO_set_callback(bio, my_bio_callback);
57*1dcdf01fSchristos i = BIO_write(bio, test1, test1len);
58*1dcdf01fSchristos if (!TEST_int_eq(i, test1len)
59*1dcdf01fSchristos || !TEST_int_eq(my_param_count, 2)
60*1dcdf01fSchristos || !TEST_ptr_eq(my_param_b[0], bio)
61*1dcdf01fSchristos || !TEST_int_eq(my_param_oper[0], BIO_CB_WRITE)
62*1dcdf01fSchristos || !TEST_ptr_eq(my_param_argp[0], test1)
63*1dcdf01fSchristos || !TEST_int_eq(my_param_argi[0], test1len)
64*1dcdf01fSchristos || !TEST_long_eq(my_param_argl[0], 0L)
65*1dcdf01fSchristos || !TEST_long_eq(my_param_ret[0], 1L)
66*1dcdf01fSchristos || !TEST_ptr_eq(my_param_b[1], bio)
67*1dcdf01fSchristos || !TEST_int_eq(my_param_oper[1], BIO_CB_WRITE | BIO_CB_RETURN)
68*1dcdf01fSchristos || !TEST_ptr_eq(my_param_argp[1], test1)
69*1dcdf01fSchristos || !TEST_int_eq(my_param_argi[1], test1len)
70*1dcdf01fSchristos || !TEST_long_eq(my_param_argl[1], 0L)
71*1dcdf01fSchristos || !TEST_long_eq(my_param_ret[1], (long)test1len))
72*1dcdf01fSchristos goto err;
73*1dcdf01fSchristos
74*1dcdf01fSchristos my_param_count = 0;
75*1dcdf01fSchristos i = BIO_read(bio, buf, sizeof(buf));
76*1dcdf01fSchristos if (!TEST_mem_eq(buf, i, test1, test1len)
77*1dcdf01fSchristos || !TEST_int_eq(my_param_count, 2)
78*1dcdf01fSchristos || !TEST_ptr_eq(my_param_b[0], bio)
79*1dcdf01fSchristos || !TEST_int_eq(my_param_oper[0], BIO_CB_READ)
80*1dcdf01fSchristos || !TEST_ptr_eq(my_param_argp[0], buf)
81*1dcdf01fSchristos || !TEST_int_eq(my_param_argi[0], sizeof(buf))
82*1dcdf01fSchristos || !TEST_long_eq(my_param_argl[0], 0L)
83*1dcdf01fSchristos || !TEST_long_eq(my_param_ret[0], 1L)
84*1dcdf01fSchristos || !TEST_ptr_eq(my_param_b[1], bio)
85*1dcdf01fSchristos || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN)
86*1dcdf01fSchristos || !TEST_ptr_eq(my_param_argp[1], buf)
87*1dcdf01fSchristos || !TEST_int_eq(my_param_argi[1], sizeof(buf))
88*1dcdf01fSchristos || !TEST_long_eq(my_param_argl[1], 0L)
89*1dcdf01fSchristos || !TEST_long_eq(my_param_ret[1], (long)test1len))
90*1dcdf01fSchristos goto err;
91*1dcdf01fSchristos
92*1dcdf01fSchristos /* By default a mem bio returns -1 if it has run out of data */
93*1dcdf01fSchristos my_param_count = 0;
94*1dcdf01fSchristos i = BIO_read(bio, buf, sizeof(buf));
95*1dcdf01fSchristos if (!TEST_int_eq(i, -1)
96*1dcdf01fSchristos || !TEST_int_eq(my_param_count, 2)
97*1dcdf01fSchristos || !TEST_ptr_eq(my_param_b[0], bio)
98*1dcdf01fSchristos || !TEST_int_eq(my_param_oper[0], BIO_CB_READ)
99*1dcdf01fSchristos || !TEST_ptr_eq(my_param_argp[0], buf)
100*1dcdf01fSchristos || !TEST_int_eq(my_param_argi[0], sizeof(buf))
101*1dcdf01fSchristos || !TEST_long_eq(my_param_argl[0], 0L)
102*1dcdf01fSchristos || !TEST_long_eq(my_param_ret[0], 1L)
103*1dcdf01fSchristos || !TEST_ptr_eq(my_param_b[1], bio)
104*1dcdf01fSchristos || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN)
105*1dcdf01fSchristos || !TEST_ptr_eq(my_param_argp[1], buf)
106*1dcdf01fSchristos || !TEST_int_eq(my_param_argi[1], sizeof(buf))
107*1dcdf01fSchristos || !TEST_long_eq(my_param_argl[1], 0L)
108*1dcdf01fSchristos || !TEST_long_eq(my_param_ret[1], -1L))
109*1dcdf01fSchristos goto err;
110*1dcdf01fSchristos
111*1dcdf01fSchristos /* Force the mem bio to return 0 if it has run out of data */
112*1dcdf01fSchristos BIO_set_mem_eof_return(bio, 0);
113*1dcdf01fSchristos my_param_count = 0;
114*1dcdf01fSchristos i = BIO_read(bio, buf, sizeof(buf));
115*1dcdf01fSchristos if (!TEST_int_eq(i, 0)
116*1dcdf01fSchristos || !TEST_int_eq(my_param_count, 2)
117*1dcdf01fSchristos || !TEST_ptr_eq(my_param_b[0], bio)
118*1dcdf01fSchristos || !TEST_int_eq(my_param_oper[0], BIO_CB_READ)
119*1dcdf01fSchristos || !TEST_ptr_eq(my_param_argp[0], buf)
120*1dcdf01fSchristos || !TEST_int_eq(my_param_argi[0], sizeof(buf))
121*1dcdf01fSchristos || !TEST_long_eq(my_param_argl[0], 0L)
122*1dcdf01fSchristos || !TEST_long_eq(my_param_ret[0], 1L)
123*1dcdf01fSchristos || !TEST_ptr_eq(my_param_b[1], bio)
124*1dcdf01fSchristos || !TEST_int_eq(my_param_oper[1], BIO_CB_READ | BIO_CB_RETURN)
125*1dcdf01fSchristos || !TEST_ptr_eq(my_param_argp[1], buf)
126*1dcdf01fSchristos || !TEST_int_eq(my_param_argi[1], sizeof(buf))
127*1dcdf01fSchristos || !TEST_long_eq(my_param_argl[1], 0L)
128*1dcdf01fSchristos || !TEST_long_eq(my_param_ret[1], 0L))
129*1dcdf01fSchristos goto err;
130*1dcdf01fSchristos
131*1dcdf01fSchristos my_param_count = 0;
132*1dcdf01fSchristos i = BIO_puts(bio, test2);
133*1dcdf01fSchristos if (!TEST_int_eq(i, 5)
134*1dcdf01fSchristos || !TEST_int_eq(my_param_count, 2)
135*1dcdf01fSchristos || !TEST_ptr_eq(my_param_b[0], bio)
136*1dcdf01fSchristos || !TEST_int_eq(my_param_oper[0], BIO_CB_PUTS)
137*1dcdf01fSchristos || !TEST_ptr_eq(my_param_argp[0], test2)
138*1dcdf01fSchristos || !TEST_int_eq(my_param_argi[0], 0)
139*1dcdf01fSchristos || !TEST_long_eq(my_param_argl[0], 0L)
140*1dcdf01fSchristos || !TEST_long_eq(my_param_ret[0], 1L)
141*1dcdf01fSchristos || !TEST_ptr_eq(my_param_b[1], bio)
142*1dcdf01fSchristos || !TEST_int_eq(my_param_oper[1], BIO_CB_PUTS | BIO_CB_RETURN)
143*1dcdf01fSchristos || !TEST_ptr_eq(my_param_argp[1], test2)
144*1dcdf01fSchristos || !TEST_int_eq(my_param_argi[1], 0)
145*1dcdf01fSchristos || !TEST_long_eq(my_param_argl[1], 0L)
146*1dcdf01fSchristos || !TEST_long_eq(my_param_ret[1], (long)test2len))
147*1dcdf01fSchristos goto err;
148*1dcdf01fSchristos
149*1dcdf01fSchristos my_param_count = 0;
150*1dcdf01fSchristos i = BIO_free(bio);
151*1dcdf01fSchristos if (!TEST_int_eq(i, 1)
152*1dcdf01fSchristos || !TEST_int_eq(my_param_count, 1)
153*1dcdf01fSchristos || !TEST_ptr_eq(my_param_b[0], bio)
154*1dcdf01fSchristos || !TEST_int_eq(my_param_oper[0], BIO_CB_FREE)
155*1dcdf01fSchristos || !TEST_ptr_eq(my_param_argp[0], NULL)
156*1dcdf01fSchristos || !TEST_int_eq(my_param_argi[0], 0)
157*1dcdf01fSchristos || !TEST_long_eq(my_param_argl[0], 0L)
158*1dcdf01fSchristos || !TEST_long_eq(my_param_ret[0], 1L))
159*1dcdf01fSchristos goto finish;
160*1dcdf01fSchristos
161*1dcdf01fSchristos ok = 1;
162*1dcdf01fSchristos goto finish;
163*1dcdf01fSchristos
164*1dcdf01fSchristos err:
165*1dcdf01fSchristos BIO_free(bio);
166*1dcdf01fSchristos
167*1dcdf01fSchristos finish:
168*1dcdf01fSchristos /* This helps finding memory leaks with ASAN */
169*1dcdf01fSchristos memset(my_param_b, 0, sizeof(my_param_b));
170*1dcdf01fSchristos memset(my_param_argp, 0, sizeof(my_param_argp));
171*1dcdf01fSchristos return ok;
172*1dcdf01fSchristos }
173*1dcdf01fSchristos
setup_tests(void)174*1dcdf01fSchristos int setup_tests(void)
175*1dcdf01fSchristos {
176*1dcdf01fSchristos ADD_TEST(test_bio_callback);
177*1dcdf01fSchristos return 1;
178*1dcdf01fSchristos }
179