]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/exdatatest.c
2 * Copyright 2015-2020 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
));
140 obj
->st
= CRYPTO_new_ex_data(CRYPTO_EX_INDEX_APP
, obj
, &obj
->ex_data
);
145 static void MYOBJ_sethello(MYOBJ
*obj
, char *cp
)
147 obj
->st
= CRYPTO_set_ex_data(&obj
->ex_data
, saved_idx
, cp
);
148 if (!TEST_int_eq(obj
->st
, 1))
152 static char *MYOBJ_gethello(MYOBJ
*obj
)
154 return CRYPTO_get_ex_data(&obj
->ex_data
, saved_idx
);
157 static void MYOBJ_sethello2(MYOBJ
*obj
, char *cp
)
159 MYOBJ_EX_DATA
* ex_data
= CRYPTO_get_ex_data(&obj
->ex_data
, saved_idx2
);
161 if (TEST_ptr(ex_data
))
164 obj
->st
= gbl_result
= 0;
167 static char *MYOBJ_gethello2(MYOBJ
*obj
)
169 MYOBJ_EX_DATA
* ex_data
= CRYPTO_get_ex_data(&obj
->ex_data
, saved_idx2
);
171 if (TEST_ptr(ex_data
))
172 return ex_data
->hello
;
174 obj
->st
= gbl_result
= 0;
178 static void MYOBJ_allochello3(MYOBJ
*obj
, char *cp
)
180 MYOBJ_EX_DATA
* ex_data
= NULL
;
182 if (TEST_ptr_null(ex_data
= CRYPTO_get_ex_data(&obj
->ex_data
, saved_idx3
))
183 && TEST_true(CRYPTO_alloc_ex_data(CRYPTO_EX_INDEX_APP
, obj
,
184 &obj
->ex_data
, saved_idx3
))
185 && TEST_ptr(ex_data
= CRYPTO_get_ex_data(&obj
->ex_data
, saved_idx3
)))
188 obj
->st
= gbl_result
= 0;
191 static char *MYOBJ_gethello3(MYOBJ
*obj
)
193 MYOBJ_EX_DATA
* ex_data
= CRYPTO_get_ex_data(&obj
->ex_data
, saved_idx3
);
195 if (TEST_ptr(ex_data
))
196 return ex_data
->hello
;
198 obj
->st
= gbl_result
= 0;
202 static void MYOBJ_free(MYOBJ
*obj
)
205 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_APP
, obj
, &obj
->ex_data
);
210 static MYOBJ
*MYOBJ_dup(MYOBJ
*in
)
212 MYOBJ
*obj
= MYOBJ_new();
215 obj
->st
|= CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_APP
, &obj
->ex_data
,
220 static int test_exdata(void)
222 MYOBJ
*t1
= NULL
, *t2
= NULL
, *t3
= NULL
;
223 MYOBJ_EX_DATA
*ex_data
= NULL
;
230 if (!TEST_ptr(p
= OPENSSL_strdup("hello world")))
233 if (!TEST_ptr(saved_argp
= OPENSSL_malloc(1)))
235 saved_idx
= CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP
,
236 saved_argl
, saved_argp
,
237 exnew
, exdup
, exfree
);
238 saved_idx2
= CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP
,
239 saved_argl
, saved_argp
,
240 exnew2
, exdup2
, exfree2
);
243 if (!TEST_int_eq(t1
->st
, 1) || !TEST_int_eq(t2
->st
, 1))
245 if (!TEST_ptr(CRYPTO_get_ex_data(&t1
->ex_data
, saved_idx2
)))
249 * saved_idx3 differs from other indexes by being created after the exdata
252 saved_idx3
= CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP
,
253 saved_argl
, saved_argp
,
254 exnew2
, exdup2
, exfree2
);
255 if (!TEST_ptr_null(CRYPTO_get_ex_data(&t1
->ex_data
, saved_idx3
)))
258 MYOBJ_sethello(t1
, p
);
259 cp
= MYOBJ_gethello(t1
);
260 if (!TEST_ptr_eq(cp
, p
))
263 MYOBJ_sethello2(t1
, p
);
264 cp
= MYOBJ_gethello2(t1
);
265 if (!TEST_ptr_eq(cp
, p
))
268 MYOBJ_allochello3(t1
, p
);
269 cp
= MYOBJ_gethello3(t1
);
270 if (!TEST_ptr_eq(cp
, p
))
273 cp
= MYOBJ_gethello(t2
);
274 if (!TEST_ptr_null(cp
))
277 cp
= MYOBJ_gethello2(t2
);
278 if (!TEST_ptr_null(cp
))
282 if (!TEST_int_eq(t3
->st
, 1))
285 ex_data
= CRYPTO_get_ex_data(&t3
->ex_data
, saved_idx2
);
286 if (!TEST_ptr(ex_data
))
288 if (!TEST_int_eq(ex_data
->dup
, 1))
291 cp
= MYOBJ_gethello(t3
);
292 if (!TEST_ptr_eq(cp
, p
))
295 cp
= MYOBJ_gethello2(t3
);
296 if (!TEST_ptr_eq(cp
, p
))
299 cp
= MYOBJ_gethello3(t3
);
300 if (!TEST_ptr_eq(cp
, p
))
309 OPENSSL_free(saved_argp
);
315 int setup_tests(void)
317 ADD_TEST(test_exdata
);