Relocate memcmp test.
[openssl.git] / test / bio_callback_test.c
1 /*
2  * Copyright 2018 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 #include <stdio.h>
10 #include <string.h>
11 #include <openssl/bio.h>
12
13 #include "testutil.h"
14
15 #define MAXCOUNT 5
16 static int         my_param_count;
17 static BIO        *my_param_b[MAXCOUNT];
18 static int         my_param_oper[MAXCOUNT];
19 static const char *my_param_argp[MAXCOUNT];
20 static int         my_param_argi[MAXCOUNT];
21 static long        my_param_argl[MAXCOUNT];
22 static long        my_param_ret[MAXCOUNT];
23
24 static long my_bio_callback(BIO *b, int oper, const char *argp, int argi,
25                             long argl, long ret)
26 {
27     if (my_param_count >= MAXCOUNT)
28         return -1;
29     my_param_b[my_param_count]    = b;
30     my_param_oper[my_param_count] = oper;
31     my_param_argp[my_param_count] = argp;
32     my_param_argi[my_param_count] = argi;
33     my_param_argl[my_param_count] = argl;
34     my_param_ret[my_param_count]  = ret;
35     my_param_count++;
36     return ret;
37 }
38
39 static int test_bio_callback(void)
40 {
41     int ok = 0;
42     BIO *bio;
43     int i;
44     char *test1 = "test";
45     char *test2 = "hello";
46
47     my_param_count = 0;
48
49     bio = BIO_new(BIO_s_mem());
50     if (bio == NULL)
51         goto err;
52
53     BIO_set_callback(bio, my_bio_callback);
54     i = BIO_write(bio, test1, 4);
55     if (!TEST_int_eq(i, 4)
56             || !TEST_int_eq(my_param_count, 2)
57             || !TEST_ptr_eq(my_param_b[0], bio)
58             || !TEST_int_eq(my_param_oper[0], BIO_CB_WRITE)
59             || !TEST_ptr_eq(my_param_argp[0], test1)
60             || !TEST_int_eq(my_param_argi[0], 4)
61             || !TEST_long_eq(my_param_argl[0], 0L)
62             || !TEST_long_eq(my_param_ret[0], 1L)
63             || !TEST_ptr_eq(my_param_b[1], bio)
64             || !TEST_int_eq(my_param_oper[1], BIO_CB_WRITE | BIO_CB_RETURN)
65             || !TEST_ptr_eq(my_param_argp[1], test1)
66             || !TEST_int_eq(my_param_argi[1], 4)
67             || !TEST_long_eq(my_param_argl[1], 0L)
68             || !TEST_long_eq(my_param_ret[1], 4L))
69         goto err;
70
71     i = BIO_puts(bio, test2);
72     if (!TEST_int_eq(i, 5)
73             || !TEST_int_eq(my_param_count, 4)
74             || !TEST_ptr_eq(my_param_b[2], bio)
75             || !TEST_int_eq(my_param_oper[2], BIO_CB_PUTS)
76             || !TEST_ptr_eq(my_param_argp[2], test2)
77             || !TEST_int_eq(my_param_argi[2], 0)
78             || !TEST_long_eq(my_param_argl[2], 0L)
79             || !TEST_long_eq(my_param_ret[2], 1L)
80             || !TEST_ptr_eq(my_param_b[3], bio)
81             || !TEST_int_eq(my_param_oper[3], BIO_CB_PUTS | BIO_CB_RETURN)
82             || !TEST_ptr_eq(my_param_argp[3], test2)
83             || !TEST_int_eq(my_param_argi[3], 0)
84             || !TEST_long_eq(my_param_argl[3], 0L)
85             || !TEST_long_eq(my_param_ret[3], 5L))
86         goto err;
87
88     i = BIO_free(bio);
89
90     if (!TEST_int_eq(i, 1)
91             || !TEST_int_eq(my_param_count, 5)
92             || !TEST_ptr_eq(my_param_b[4], bio)
93             || !TEST_int_eq(my_param_oper[4], BIO_CB_FREE)
94             || !TEST_ptr_eq(my_param_argp[4], NULL)
95             || !TEST_int_eq(my_param_argi[4], 0)
96             || !TEST_long_eq(my_param_argl[4], 0L)
97             || !TEST_long_eq(my_param_ret[4], 1L))
98         goto finish;
99
100     ok = 1;
101     goto finish;
102
103 err:
104     BIO_free(bio);
105
106 finish:
107     /* This helps finding memory leaks with ASAN */
108     memset(my_param_b, 0, sizeof(my_param_b));
109     memset(my_param_argp, 0, sizeof(my_param_argp));
110     return ok;
111 }
112
113 int setup_tests(void)
114 {
115     ADD_TEST(test_bio_callback);
116     return 1;
117 }