Use BIO not FILE for test file
[openssl.git] / test / exdatatest.c
1 /*
2  * Copyright 2015-2017 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 <string.h>
12 #include <stdlib.h>
13 #include <openssl/crypto.h>
14
15 #include "testutil.h"
16
17 static long saved_argl;
18 static void *saved_argp;
19 static int saved_idx;
20 static int gbl_result;
21
22 static void exnew(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
23           int idx, long argl, void *argp)
24 {
25     if (!TEST_int_eq(idx, saved_idx)
26         || !TEST_long_eq(argl, saved_argl)
27         || !TEST_ptr_eq(argp, saved_argp)
28         || !TEST_ptr_null(ptr))
29         gbl_result = 0;
30 }
31
32 static int exdup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
33           void *from_d, int idx, long argl, void *argp)
34 {
35     if (!TEST_int_eq(idx, saved_idx)
36         || !TEST_long_eq(argl, saved_argl)
37         || !TEST_ptr_eq(argp, saved_argp)
38         || !TEST_ptr(from_d))
39         gbl_result = 0;
40     return 1;
41 }
42
43 static void exfree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
44             int idx, long argl, void *argp)
45 {
46     if (!TEST_int_eq(idx, saved_idx)
47         || !TEST_long_eq(argl, saved_argl)
48         || !TEST_ptr_eq(argp, saved_argp))
49         gbl_result = 0;
50 }
51
52 typedef struct myobj_st {
53     CRYPTO_EX_DATA ex_data;
54     int id;
55     int st;
56 } MYOBJ;
57
58 static MYOBJ *MYOBJ_new()
59 {
60     static int count = 0;
61     MYOBJ *obj = OPENSSL_malloc(sizeof(*obj));
62
63     obj->id = ++count;
64     obj->st = CRYPTO_new_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
65     return obj;
66 }
67
68 static void MYOBJ_sethello(MYOBJ *obj, char *cp)
69 {
70     obj->st = CRYPTO_set_ex_data(&obj->ex_data, saved_idx, cp);
71     if (!TEST_int_eq(obj->st, 1))
72         gbl_result = 0;
73 }
74
75 static char *MYOBJ_gethello(MYOBJ *obj)
76 {
77     return CRYPTO_get_ex_data(&obj->ex_data, saved_idx);
78 }
79
80 static void MYOBJ_free(MYOBJ *obj)
81 {
82     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
83     OPENSSL_free(obj);
84 }
85
86 static MYOBJ *MYOBJ_dup(MYOBJ *in)
87 {
88     MYOBJ *obj = MYOBJ_new();
89
90     obj->st |= CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_APP, &obj->ex_data,
91                                  &in->ex_data);
92     return obj;
93 }
94
95 static int test_exdata(void)
96 {
97     MYOBJ *t1, *t2, *t3;
98     const char *cp;
99     char *p;
100
101     gbl_result = 1;
102
103     p = strdup("hello world");
104     saved_argl = 21;
105     saved_argp = malloc(1);
106     saved_idx = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
107                                         saved_argl, saved_argp,
108                                         exnew, exdup, exfree);
109     t1 = MYOBJ_new();
110     t2 = MYOBJ_new();
111     if (!TEST_int_eq(t1->st, 1) || !TEST_int_eq(t2->st, 1))
112         return 0;
113
114     MYOBJ_sethello(t1, p);
115     cp = MYOBJ_gethello(t1);
116     if (!TEST_ptr_eq(cp, p))
117         return 0;
118
119     cp = MYOBJ_gethello(t2);
120     if (!TEST_ptr_null(cp))
121         return 0;
122
123     t3 = MYOBJ_dup(t1);
124     if (!TEST_int_eq(t3->st, 1))
125         return 0;
126
127     cp = MYOBJ_gethello(t3);
128     if (!TEST_ptr_eq(cp, p))
129         return 0;
130
131     MYOBJ_free(t1);
132     MYOBJ_free(t2);
133     MYOBJ_free(t3);
134     free(saved_argp);
135     free(p);
136
137     if (gbl_result)
138       return 1;
139     else
140       return 0;
141 }
142
143 void register_tests(void)
144 {
145     ADD_TEST(test_exdata);
146 }