2 * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
13 #include <openssl/crypto.h>
17 static long saved_argl;
18 static void *saved_argp;
20 static int saved_idx2;
21 static int saved_idx3;
22 static int gbl_result;
25 * SIMPLE EX_DATA IMPLEMENTATION
26 * Apps explicitly set/get ex_data as needed
29 static void exnew(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
30 int idx, long argl, void *argp)
32 if (!TEST_int_eq(idx, saved_idx)
33 || !TEST_long_eq(argl, saved_argl)
34 || !TEST_ptr_eq(argp, saved_argp)
35 || !TEST_ptr_null(ptr))
39 static int exdup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
40 void *from_d, int idx, long argl, void *argp)
42 if (!TEST_int_eq(idx, saved_idx)
43 || !TEST_long_eq(argl, saved_argl)
44 || !TEST_ptr_eq(argp, saved_argp)
50 static void exfree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
51 int idx, long argl, void *argp)
53 if (!TEST_int_eq(idx, saved_idx)
54 || !TEST_long_eq(argl, saved_argl)
55 || !TEST_ptr_eq(argp, saved_argp))
60 * PRE-ALLOCATED EX_DATA IMPLEMENTATION
61 * Extended data structure is allocated in exnew2/freed in exfree2
62 * Data is stored inside extended data structure
65 typedef struct myobj_ex_data_st {
71 static void exnew2(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
72 int idx, long argl, void *argp)
74 MYOBJ_EX_DATA *ex_data = OPENSSL_zalloc(sizeof(*ex_data));
76 if (!TEST_true(idx == saved_idx2 || idx == saved_idx3)
77 || !TEST_long_eq(argl, saved_argl)
78 || !TEST_ptr_eq(argp, saved_argp)
79 || !TEST_ptr_null(ptr)
81 || !TEST_true(CRYPTO_set_ex_data(ad, idx, ex_data))) {
83 OPENSSL_free(ex_data);
89 static int exdup2(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
90 void *from_d, int idx, long argl, void *argp)
92 MYOBJ_EX_DATA **update_ex_data = (MYOBJ_EX_DATA**)from_d;
93 MYOBJ_EX_DATA *ex_data = NULL;
95 if (!TEST_true(idx == saved_idx2 || idx == saved_idx3)
96 || !TEST_long_eq(argl, saved_argl)
97 || !TEST_ptr_eq(argp, saved_argp)
99 || !TEST_ptr(*update_ex_data)
100 || !TEST_ptr(ex_data = CRYPTO_get_ex_data(to, idx))
101 || !TEST_true(ex_data->new)) {
104 /* Copy hello over */
105 ex_data->hello = (*update_ex_data)->hello;
106 /* indicate this is a dup */
108 /* Keep my original ex_data */
109 *update_ex_data = ex_data;
114 static void exfree2(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
115 int idx, long argl, void *argp)
117 MYOBJ_EX_DATA *ex_data = CRYPTO_get_ex_data(ad, idx);
119 if (!TEST_true(idx == saved_idx2 || idx == saved_idx3)
120 || !TEST_long_eq(argl, saved_argl)
121 || !TEST_ptr_eq(argp, saved_argp)
122 || !TEST_true(CRYPTO_set_ex_data(ad, idx, NULL)))
124 OPENSSL_free(ex_data);
127 typedef struct myobj_st {
128 CRYPTO_EX_DATA ex_data;
133 static MYOBJ *MYOBJ_new(void)
135 static int count = 0;
136 MYOBJ *obj = OPENSSL_malloc(sizeof(*obj));
139 obj->st = CRYPTO_new_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
143 static void MYOBJ_sethello(MYOBJ *obj, char *cp)
145 obj->st = CRYPTO_set_ex_data(&obj->ex_data, saved_idx, cp);
146 if (!TEST_int_eq(obj->st, 1))
150 static char *MYOBJ_gethello(MYOBJ *obj)
152 return CRYPTO_get_ex_data(&obj->ex_data, saved_idx);
155 static void MYOBJ_sethello2(MYOBJ *obj, char *cp)
157 MYOBJ_EX_DATA* ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx2);
159 if (TEST_ptr(ex_data))
162 obj->st = gbl_result = 0;
165 static char *MYOBJ_gethello2(MYOBJ *obj)
167 MYOBJ_EX_DATA* ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx2);
169 if (TEST_ptr(ex_data))
170 return ex_data->hello;
172 obj->st = gbl_result = 0;
176 static void MYOBJ_allochello3(MYOBJ *obj, char *cp)
178 MYOBJ_EX_DATA* ex_data = NULL;
180 if (TEST_ptr_null(ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx3))
181 && TEST_true(CRYPTO_alloc_ex_data(CRYPTO_EX_INDEX_APP, obj,
182 &obj->ex_data, saved_idx3))
183 && TEST_ptr(ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx3)))
186 obj->st = gbl_result = 0;
189 static char *MYOBJ_gethello3(MYOBJ *obj)
191 MYOBJ_EX_DATA* ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx3);
193 if (TEST_ptr(ex_data))
194 return ex_data->hello;
196 obj->st = gbl_result = 0;
200 static void MYOBJ_free(MYOBJ *obj)
202 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
206 static MYOBJ *MYOBJ_dup(MYOBJ *in)
208 MYOBJ *obj = MYOBJ_new();
210 obj->st |= CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_APP, &obj->ex_data,
215 static int test_exdata(void)
218 MYOBJ_EX_DATA *ex_data;
224 p = OPENSSL_strdup("hello world");
226 saved_argp = OPENSSL_malloc(1);
227 saved_idx = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
228 saved_argl, saved_argp,
229 exnew, exdup, exfree);
230 saved_idx2 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
231 saved_argl, saved_argp,
232 exnew2, exdup2, exfree2);
235 if (!TEST_int_eq(t1->st, 1) || !TEST_int_eq(t2->st, 1))
237 if (!TEST_ptr(CRYPTO_get_ex_data(&t1->ex_data, saved_idx2)))
241 * saved_idx3 differs from other indexes by being created after the exdata
244 saved_idx3 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
245 saved_argl, saved_argp,
246 exnew2, exdup2, exfree2);
247 if (!TEST_ptr_null(CRYPTO_get_ex_data(&t1->ex_data, saved_idx3)))
250 MYOBJ_sethello(t1, p);
251 cp = MYOBJ_gethello(t1);
252 if (!TEST_ptr_eq(cp, p))
255 MYOBJ_sethello2(t1, p);
256 cp = MYOBJ_gethello2(t1);
257 if (!TEST_ptr_eq(cp, p))
260 MYOBJ_allochello3(t1, p);
261 cp = MYOBJ_gethello3(t1);
262 if (!TEST_ptr_eq(cp, p))
265 cp = MYOBJ_gethello(t2);
266 if (!TEST_ptr_null(cp))
269 cp = MYOBJ_gethello2(t2);
270 if (!TEST_ptr_null(cp))
274 if (!TEST_int_eq(t3->st, 1))
277 ex_data = CRYPTO_get_ex_data(&t3->ex_data, saved_idx2);
278 if (!TEST_ptr(ex_data))
280 if (!TEST_int_eq(ex_data->dup, 1))
283 cp = MYOBJ_gethello(t3);
284 if (!TEST_ptr_eq(cp, p))
287 cp = MYOBJ_gethello2(t3);
288 if (!TEST_ptr_eq(cp, p))
291 cp = MYOBJ_gethello3(t3);
292 if (!TEST_ptr_eq(cp, p))
298 OPENSSL_free(saved_argp);
307 int setup_tests(void)
309 ADD_TEST(test_exdata);