xref: /openbsd-src/regress/lib/libcrypto/bio/bio_mem.c (revision 720505cf7dda7a37320557697e38f275bbb49ae5)
1*720505cfStb /*	$OpenBSD: bio_mem.c,v 1.1 2022/12/08 17:49:02 tb Exp $	*/
2*720505cfStb /*
3*720505cfStb  * Copyright (c) 2022 Joel Sing <jsing@openbsd.org>
4*720505cfStb  *
5*720505cfStb  * Permission to use, copy, modify, and distribute this software for any
6*720505cfStb  * purpose with or without fee is hereby granted, provided that the above
7*720505cfStb  * copyright notice and this permission notice appear in all copies.
8*720505cfStb  *
9*720505cfStb  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*720505cfStb  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*720505cfStb  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*720505cfStb  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*720505cfStb  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*720505cfStb  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*720505cfStb  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*720505cfStb  */
17*720505cfStb 
18*720505cfStb #include <err.h>
19*720505cfStb #include <stdint.h>
20*720505cfStb #include <stdio.h>
21*720505cfStb #include <stdlib.h>
22*720505cfStb #include <string.h>
23*720505cfStb 
24*720505cfStb #include <openssl/bio.h>
25*720505cfStb #include <openssl/buffer.h>
26*720505cfStb 
27*720505cfStb static int
bio_mem_test(void)28*720505cfStb bio_mem_test(void)
29*720505cfStb {
30*720505cfStb 	uint8_t *data = NULL;
31*720505cfStb 	size_t data_len;
32*720505cfStb 	uint8_t *rodata;
33*720505cfStb 	long rodata_len;
34*720505cfStb 	BUF_MEM *pbuf;
35*720505cfStb 	BUF_MEM *buf = NULL;
36*720505cfStb 	BIO *bio = NULL;
37*720505cfStb 	int ret;
38*720505cfStb 	int failed = 1;
39*720505cfStb 
40*720505cfStb 	data_len = 4096;
41*720505cfStb 	if ((data = malloc(data_len)) == NULL)
42*720505cfStb 		err(1, "malloc");
43*720505cfStb 
44*720505cfStb 	memset(data, 0xdb, data_len);
45*720505cfStb 	data[0] = 0x01;
46*720505cfStb 	data[data_len - 1] = 0xff;
47*720505cfStb 
48*720505cfStb 	if ((bio = BIO_new(BIO_s_mem())) == NULL) {
49*720505cfStb 		fprintf(stderr, "FAIL: BIO_new() returned NULL\n");
50*720505cfStb 		goto failure;
51*720505cfStb 	}
52*720505cfStb 	if ((ret = BIO_write(bio, data, data_len)) != (int)data_len) {
53*720505cfStb 		fprintf(stderr, "FAIL: BIO_write() = %d, want %zu\n", ret,
54*720505cfStb 		    data_len);
55*720505cfStb 		goto failure;
56*720505cfStb 	}
57*720505cfStb 	if ((rodata_len = BIO_get_mem_data(bio, &rodata)) != (long)data_len) {
58*720505cfStb 		fprintf(stderr, "FAIL: BIO_get_mem_data() = %ld, want %zu\n",
59*720505cfStb 		    rodata_len, data_len);
60*720505cfStb 		goto failure;
61*720505cfStb 	}
62*720505cfStb 	if (rodata[0] != 0x01) {
63*720505cfStb 		fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", rodata[0], 0x01);
64*720505cfStb 		goto failure;
65*720505cfStb 	}
66*720505cfStb 	if (rodata[rodata_len - 1] != 0xff) {
67*720505cfStb 		fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n",
68*720505cfStb 		    rodata[rodata_len - 1], 0xff);
69*720505cfStb 		goto failure;
70*720505cfStb 	}
71*720505cfStb 
72*720505cfStb 	if (!BIO_get_mem_ptr(bio, &pbuf)) {
73*720505cfStb 		fprintf(stderr, "FAIL: BIO_get_mem_ptr() failed\n");
74*720505cfStb 		goto failure;
75*720505cfStb 	}
76*720505cfStb 	if (pbuf->length != data_len) {
77*720505cfStb 		fprintf(stderr, "FAIL: Got buffer with length %zu, want %zu\n",
78*720505cfStb 		    pbuf->length, data_len);
79*720505cfStb 		goto failure;
80*720505cfStb 	}
81*720505cfStb 	if (memcmp(pbuf->data, data, data_len) != 0) {
82*720505cfStb 		fprintf(stderr, "FAIL: Got buffer with differing data\n");
83*720505cfStb 		goto failure;
84*720505cfStb 	}
85*720505cfStb 	pbuf = NULL;
86*720505cfStb 
87*720505cfStb 	if ((buf = BUF_MEM_new()) == NULL) {
88*720505cfStb 		fprintf(stderr, "FAIL: BUF_MEM_new() returned NULL\n");
89*720505cfStb 		goto failure;
90*720505cfStb 	}
91*720505cfStb 	if (!BIO_set_mem_buf(bio, buf, BIO_NOCLOSE)) {
92*720505cfStb 		fprintf(stderr, "FAIL: BUF_set_mem_buf() failed\n");
93*720505cfStb 		goto failure;
94*720505cfStb 	}
95*720505cfStb 	if ((ret = BIO_puts(bio, "Hello\n")) != 6) {
96*720505cfStb 		fprintf(stderr, "FAIL: BUF_puts() = %d, want %d\n", ret, 6);
97*720505cfStb 		goto failure;
98*720505cfStb 	}
99*720505cfStb 	if ((ret = BIO_puts(bio, "World\n")) != 6) {
100*720505cfStb 		fprintf(stderr, "FAIL: BUF_puts() = %d, want %d\n", ret, 6);
101*720505cfStb 		goto failure;
102*720505cfStb 	}
103*720505cfStb 	if (buf->length != 12) {
104*720505cfStb 		fprintf(stderr, "FAIL: buffer has length %zu, want %d\n",
105*720505cfStb 		    buf->length, 12);
106*720505cfStb 		goto failure;
107*720505cfStb 	}
108*720505cfStb 	buf->length = 11;
109*720505cfStb 	if ((ret = BIO_gets(bio, data, data_len)) != 6) {
110*720505cfStb 		fprintf(stderr, "FAIL: BUF_gets() = %d, want %d\n", ret, 6);
111*720505cfStb 		goto failure;
112*720505cfStb 	}
113*720505cfStb 	if (strcmp(data, "Hello\n") != 0) {
114*720505cfStb 		fprintf(stderr, "FAIL: BUF_gets() returned '%s', want '%s'\n",
115*720505cfStb 		    data, "Hello\\n");
116*720505cfStb 		goto failure;
117*720505cfStb 	}
118*720505cfStb 	if ((ret = BIO_gets(bio, data, data_len)) != 5) {
119*720505cfStb 		fprintf(stderr, "FAIL: BUF_gets() = %d, want %d\n", ret, 5);
120*720505cfStb 		goto failure;
121*720505cfStb 	}
122*720505cfStb 	if (strcmp(data, "World") != 0) {
123*720505cfStb 		fprintf(stderr, "FAIL: BUF_gets() returned '%s', want '%s'\n",
124*720505cfStb 		    data, "World");
125*720505cfStb 		goto failure;
126*720505cfStb 	}
127*720505cfStb 
128*720505cfStb 	if (!BIO_eof(bio)) {
129*720505cfStb 		fprintf(stderr, "FAIL: BIO is not EOF\n");
130*720505cfStb 		goto failure;
131*720505cfStb 	}
132*720505cfStb 	if ((ret = BIO_read(bio, data, data_len)) != -1) {
133*720505cfStb 		fprintf(stderr, "FAIL: BIO_read() = %d, want -1\n", ret);
134*720505cfStb 		goto failure;
135*720505cfStb 	}
136*720505cfStb 	if (!BIO_set_mem_eof_return(bio, -2)) {
137*720505cfStb 		fprintf(stderr, "FAIL: BIO_set_mem_eof_return() failed\n");
138*720505cfStb 		goto failure;
139*720505cfStb 	}
140*720505cfStb 	if ((ret = BIO_read(bio, data, data_len)) != -2) {
141*720505cfStb 		fprintf(stderr, "FAIL: BIO_read() = %d, want -2\n", ret);
142*720505cfStb 		goto failure;
143*720505cfStb 	}
144*720505cfStb 
145*720505cfStb 	failed = 0;
146*720505cfStb 
147*720505cfStb  failure:
148*720505cfStb 	free(data);
149*720505cfStb 	BUF_MEM_free(buf);
150*720505cfStb 	BIO_free(bio);
151*720505cfStb 
152*720505cfStb 	return failed;
153*720505cfStb }
154*720505cfStb 
155*720505cfStb static int
bio_mem_small_io_test(void)156*720505cfStb bio_mem_small_io_test(void)
157*720505cfStb {
158*720505cfStb 	uint8_t buf[2];
159*720505cfStb 	int i, j, ret;
160*720505cfStb 	BIO *bio;
161*720505cfStb 	int failed = 1;
162*720505cfStb 
163*720505cfStb 	memset(buf, 0xdb, sizeof(buf));
164*720505cfStb 
165*720505cfStb 	if ((bio = BIO_new(BIO_s_mem())) == NULL) {
166*720505cfStb 		fprintf(stderr, "FAIL: BIO_new() returned NULL\n");
167*720505cfStb 		goto failure;
168*720505cfStb 	}
169*720505cfStb 
170*720505cfStb 	for (i = 0; i < 100; i++) {
171*720505cfStb 		if (!BIO_reset(bio)) {
172*720505cfStb 			fprintf(stderr, "FAIL: BIO_reset() failed\n");
173*720505cfStb 			goto failure;
174*720505cfStb 		}
175*720505cfStb 		for (j = 0; j < 25000; j++) {
176*720505cfStb 			ret = BIO_write(bio, buf, sizeof(buf));
177*720505cfStb 			if (ret != sizeof(buf)) {
178*720505cfStb 				fprintf(stderr, "FAIL: BIO_write() = %d, "
179*720505cfStb 				    "want %zu\n", ret, sizeof(buf));
180*720505cfStb 				goto failure;
181*720505cfStb 			}
182*720505cfStb 		}
183*720505cfStb 		for (j = 0; j < 25000; j++) {
184*720505cfStb 			ret = BIO_read(bio, buf, sizeof(buf));
185*720505cfStb 			if (ret != sizeof(buf)) {
186*720505cfStb 				fprintf(stderr, "FAIL: BIO_read() = %d, "
187*720505cfStb 				    "want %zu\n", ret, sizeof(buf));
188*720505cfStb 				goto failure;
189*720505cfStb 			}
190*720505cfStb 			ret = BIO_write(bio, buf, sizeof(buf));
191*720505cfStb 			if (ret != sizeof(buf)) {
192*720505cfStb 				fprintf(stderr, "FAIL: BIO_write() = %d, "
193*720505cfStb 				    "want %zu\n", ret, sizeof(buf));
194*720505cfStb 				goto failure;
195*720505cfStb 			}
196*720505cfStb 		}
197*720505cfStb 		for (j = 0; j < 25000; j++) {
198*720505cfStb 			ret = BIO_read(bio, buf, sizeof(buf));
199*720505cfStb 			if (ret != sizeof(buf)) {
200*720505cfStb 				fprintf(stderr, "FAIL: BIO_read() = %d, "
201*720505cfStb 				    "want %zu\n", ret, sizeof(buf));
202*720505cfStb 				goto failure;
203*720505cfStb 			}
204*720505cfStb 		}
205*720505cfStb 		if (!BIO_eof(bio)) {
206*720505cfStb 			fprintf(stderr, "FAIL: BIO not EOF\n");
207*720505cfStb 			goto failure;
208*720505cfStb 		}
209*720505cfStb 	}
210*720505cfStb 
211*720505cfStb 	if (buf[0] != 0xdb || buf[1] != 0xdb) {
212*720505cfStb 		fprintf(stderr, "FAIL: buf = {0x%x, 0x%x}, want {0xdb, 0xdb}\n",
213*720505cfStb 		    buf[0], buf[1]);
214*720505cfStb 		goto failure;
215*720505cfStb 	}
216*720505cfStb 
217*720505cfStb 	failed = 0;
218*720505cfStb 
219*720505cfStb  failure:
220*720505cfStb 	BIO_free(bio);
221*720505cfStb 
222*720505cfStb 	return failed;
223*720505cfStb }
224*720505cfStb 
225*720505cfStb static int
bio_mem_readonly_test(void)226*720505cfStb bio_mem_readonly_test(void)
227*720505cfStb {
228*720505cfStb 	uint8_t *data = NULL;
229*720505cfStb 	size_t data_len;
230*720505cfStb 	uint8_t buf[2048];
231*720505cfStb 	BIO *bio = NULL;
232*720505cfStb 	int ret;
233*720505cfStb 	int failed = 1;
234*720505cfStb 
235*720505cfStb 	data_len = 4096;
236*720505cfStb 	if ((data = malloc(data_len)) == NULL)
237*720505cfStb 		err(1, "malloc");
238*720505cfStb 
239*720505cfStb 	memset(data, 0xdb, data_len);
240*720505cfStb 	data[0] = 0x01;
241*720505cfStb 	data[data_len - 1] = 0xff;
242*720505cfStb 
243*720505cfStb 	if ((bio = BIO_new_mem_buf(data, data_len)) == NULL) {
244*720505cfStb 		fprintf(stderr, "FAIL: BIO_new_mem_buf failed\n");
245*720505cfStb 		goto failure;
246*720505cfStb 	}
247*720505cfStb 	if ((ret = BIO_read(bio, buf, 1)) != 1) {
248*720505cfStb 		fprintf(stderr, "FAIL: BIO_read() = %d, want %zu\n", ret,
249*720505cfStb 		    sizeof(buf));
250*720505cfStb 		goto failure;
251*720505cfStb 	}
252*720505cfStb 	if (buf[0] != 0x01) {
253*720505cfStb 		fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", buf[0], 0x01);
254*720505cfStb 		goto failure;
255*720505cfStb 	}
256*720505cfStb 	if ((ret = BIO_read(bio, buf, sizeof(buf))) != sizeof(buf)) {
257*720505cfStb 		fprintf(stderr, "FAIL: BIO_read() = %d, want %zu\n", ret,
258*720505cfStb 		    sizeof(buf));
259*720505cfStb 		goto failure;
260*720505cfStb 	}
261*720505cfStb 	if (buf[0] != 0xdb) {
262*720505cfStb 		fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", buf[0], 0xdb);
263*720505cfStb 		goto failure;
264*720505cfStb 	}
265*720505cfStb 	if ((ret = BIO_write(bio, buf, 1)) != -1) {
266*720505cfStb 		fprintf(stderr, "FAIL: BIO_write() = %d, want -1\n", ret);
267*720505cfStb 		goto failure;
268*720505cfStb 	}
269*720505cfStb 	if (BIO_eof(bio)) {
270*720505cfStb 		fprintf(stderr, "FAIL: BIO is EOF\n");
271*720505cfStb 		goto failure;
272*720505cfStb 	}
273*720505cfStb 	if (BIO_ctrl_pending(bio) != 2047) {
274*720505cfStb 		fprintf(stderr, "FAIL: BIO_ctrl_pending() = %zu, want 2047\n",
275*720505cfStb 		    BIO_ctrl_pending(bio));
276*720505cfStb 		goto failure;
277*720505cfStb 	}
278*720505cfStb 	if ((ret = BIO_read(bio, buf, sizeof(buf))) != 2047) {
279*720505cfStb 		fprintf(stderr, "FAIL: BIO_read() = %d, want 2047\n", ret);
280*720505cfStb 		goto failure;
281*720505cfStb 	}
282*720505cfStb 	if (buf[2045] != 0xdb) {
283*720505cfStb 		fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", buf[2045], 0xdb);
284*720505cfStb 		goto failure;
285*720505cfStb 	}
286*720505cfStb 	if (buf[2046] != 0xff) {
287*720505cfStb 		fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", buf[2046], 0xff);
288*720505cfStb 		goto failure;
289*720505cfStb 	}
290*720505cfStb 	if (!BIO_eof(bio)) {
291*720505cfStb 		fprintf(stderr, "FAIL: BIO is not EOF\n");
292*720505cfStb 		goto failure;
293*720505cfStb 	}
294*720505cfStb 	if (BIO_ctrl_pending(bio) != 0) {
295*720505cfStb 		fprintf(stderr, "FAIL: BIO_ctrl_pending() = %zu, want 0\n",
296*720505cfStb 		    BIO_ctrl_pending(bio));
297*720505cfStb 		goto failure;
298*720505cfStb 	}
299*720505cfStb 
300*720505cfStb 	if (!BIO_reset(bio)) {
301*720505cfStb 		fprintf(stderr, "FAIL: failed to reset bio\n");
302*720505cfStb 		goto failure;
303*720505cfStb 	}
304*720505cfStb 	if (BIO_eof(bio)) {
305*720505cfStb 		fprintf(stderr, "FAIL: BIO is EOF\n");
306*720505cfStb 		goto failure;
307*720505cfStb 	}
308*720505cfStb 	if (BIO_ctrl_pending(bio) != 4096) {
309*720505cfStb 		fprintf(stderr, "FAIL: BIO_ctrl_pending() = %zu, want 4096\n",
310*720505cfStb 		    BIO_ctrl_pending(bio));
311*720505cfStb 		goto failure;
312*720505cfStb 	}
313*720505cfStb 	if ((ret = BIO_read(bio, buf, 2)) != 2) {
314*720505cfStb 		fprintf(stderr, "FAIL: BIO_read() = %d, want 2\n", ret);
315*720505cfStb 		goto failure;
316*720505cfStb 	}
317*720505cfStb 	if (buf[0] != 0x01) {
318*720505cfStb 		fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", buf[0], 0x01);
319*720505cfStb 		goto failure;
320*720505cfStb 	}
321*720505cfStb 	if (buf[1] != 0xdb) {
322*720505cfStb 		fprintf(stderr, "FAIL: got 0x%x, want 0x%x\n", buf[1], 0xdb);
323*720505cfStb 		goto failure;
324*720505cfStb 	}
325*720505cfStb 
326*720505cfStb 	failed = 0;
327*720505cfStb 
328*720505cfStb  failure:
329*720505cfStb 	BIO_free(bio);
330*720505cfStb 	free(data);
331*720505cfStb 
332*720505cfStb 	return failed;
333*720505cfStb }
334*720505cfStb 
335*720505cfStb int
main(int argc,char ** argv)336*720505cfStb main(int argc, char **argv)
337*720505cfStb {
338*720505cfStb 	int failed = 0;
339*720505cfStb 
340*720505cfStb 	failed |= bio_mem_test();
341*720505cfStb 	failed |= bio_mem_small_io_test();
342*720505cfStb 	failed |= bio_mem_readonly_test();
343*720505cfStb 
344*720505cfStb 	return failed;
345*720505cfStb }
346