Add a test for SSL_set_bio()
[openssl.git] / test / exdatatest.c
1 /*
2  * Copyright 2015-2016 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 <stdio.h>
11 #include <assert.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <openssl/crypto.h>
15
16 static long sargl;
17 static void *sargp;
18 static int sidx;
19
20 static void exnew(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
21           int idx, long argl, void *argp)
22 {
23     assert(idx == sidx);
24     assert(argl == sargl);
25     assert(argp == sargp);
26 }
27
28 static int exdup(CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from,
29           void *from_d, int idx, long argl, void *argp)
30 {
31     assert(idx == sidx);
32     assert(argl == sargl);
33     assert(argp == sargp);
34     return 0;
35 }
36
37 static void exfree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
38             int idx, long argl, void *argp)
39 {
40     assert(idx == sidx);
41     assert(argl == sargl);
42     assert(argp == sargp);
43 }
44
45 typedef struct myobj_st {
46     CRYPTO_EX_DATA ex_data;
47     int id;
48 } MYOBJ;
49
50 static MYOBJ *MYOBJ_new()
51 {
52     static int count = 0;
53     MYOBJ *obj = OPENSSL_malloc(sizeof(*obj));
54     int st;
55
56     obj->id = ++count;
57     st = CRYPTO_new_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
58     assert(st != 0);
59     return obj;
60 }
61
62 static void MYOBJ_sethello(MYOBJ *obj, char *cp)
63 {
64     int st;
65
66     st = CRYPTO_set_ex_data(&obj->ex_data, sidx, cp);
67     assert(st != 0);
68 }
69
70 static char *MYOBJ_gethello(MYOBJ *obj)
71 {
72     return CRYPTO_get_ex_data(&obj->ex_data, sidx);
73 }
74
75 static void MYOBJ_free(MYOBJ *obj)
76 {
77     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
78     OPENSSL_free(obj);
79 }
80
81 int main()
82 {
83     MYOBJ *t1, *t2;
84     const char *cp;
85     char *p;
86
87     p = strdup("hello world");
88     sargl = 21;
89     sargp = malloc(1);
90     sidx = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP, sargl, sargp,
91                                    exnew, exdup, exfree);
92     t1 = MYOBJ_new();
93     t2 = MYOBJ_new();
94     MYOBJ_sethello(t1, p);
95     cp = MYOBJ_gethello(t1);
96     assert(cp == p);
97     cp = MYOBJ_gethello(t2);
98     assert(cp == NULL);
99     MYOBJ_free(t1);
100     MYOBJ_free(t2);
101     free(sargp);
102     free(p);
103     return 0;
104 }