]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/exdatatest.c
2 * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved.
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
13 #include <openssl/crypto.h>
17 static long saved_argl
;
18 static void *saved_argp
;
20 static int saved_idx2
;
21 static int gbl_result
;
24 * SIMPLE EX_DATA IMPLEMENTATION
25 * Apps explicitly set/get ex_data as needed
28 static void exnew(void *parent
, void *ptr
, CRYPTO_EX_DATA
*ad
,
29 int idx
, long argl
, void *argp
)
31 if (!TEST_int_eq(idx
, saved_idx
)
32 || !TEST_long_eq(argl
, saved_argl
)
33 || !TEST_ptr_eq(argp
, saved_argp
)
34 || !TEST_ptr_null(ptr
))
38 static int exdup(CRYPTO_EX_DATA
*to
, const CRYPTO_EX_DATA
*from
,
39 void *from_d
, int idx
, long argl
, void *argp
)
41 if (!TEST_int_eq(idx
, saved_idx
)
42 || !TEST_long_eq(argl
, saved_argl
)
43 || !TEST_ptr_eq(argp
, saved_argp
)
49 static void exfree(void *parent
, void *ptr
, CRYPTO_EX_DATA
*ad
,
50 int idx
, long argl
, void *argp
)
52 if (!TEST_int_eq(idx
, saved_idx
)
53 || !TEST_long_eq(argl
, saved_argl
)
54 || !TEST_ptr_eq(argp
, saved_argp
))
59 * PRE-ALLOCATED EX_DATA IMPLEMENTATION
60 * Extended data structure is allocated in exnew2/freed in exfree2
61 * Data is stored inside extended data structure
64 typedef struct myobj_ex_data_st
{
70 static void exnew2(void *parent
, void *ptr
, CRYPTO_EX_DATA
*ad
,
71 int idx
, long argl
, void *argp
)
73 MYOBJ_EX_DATA
*ex_data
= OPENSSL_zalloc(sizeof(*ex_data
));
74 if (!TEST_int_eq(idx
, saved_idx2
)
75 || !TEST_long_eq(argl
, saved_argl
)
76 || !TEST_ptr_eq(argp
, saved_argp
)
77 || !TEST_ptr_null(ptr
)
79 || !TEST_true(CRYPTO_set_ex_data(ad
, saved_idx2
, ex_data
))) {
81 OPENSSL_free(ex_data
);
87 static int exdup2(CRYPTO_EX_DATA
*to
, const CRYPTO_EX_DATA
*from
,
88 void *from_d
, int idx
, long argl
, void *argp
)
90 MYOBJ_EX_DATA
**update_ex_data
= (MYOBJ_EX_DATA
**)from_d
;
91 MYOBJ_EX_DATA
*ex_data
= CRYPTO_get_ex_data(to
, saved_idx2
);
92 if (!TEST_int_eq(idx
, saved_idx2
)
93 || !TEST_long_eq(argl
, saved_argl
)
94 || !TEST_ptr_eq(argp
, saved_argp
)
96 || !TEST_ptr(*update_ex_data
)
98 || !TEST_true(ex_data
->new)) {
101 /* Copy hello over */
102 ex_data
->hello
= (*update_ex_data
)->hello
;
103 /* indicate this is a dup */
105 /* Keep my original ex_data */
106 *update_ex_data
= ex_data
;
111 static void exfree2(void *parent
, void *ptr
, CRYPTO_EX_DATA
*ad
,
112 int idx
, long argl
, void *argp
)
114 MYOBJ_EX_DATA
*ex_data
= CRYPTO_get_ex_data(ad
, saved_idx2
);
115 OPENSSL_free(ex_data
);
116 if (!TEST_int_eq(idx
, saved_idx2
)
117 || !TEST_long_eq(argl
, saved_argl
)
118 || !TEST_ptr_eq(argp
, saved_argp
)
119 || !TEST_ptr(ex_data
)
120 || !TEST_true(CRYPTO_set_ex_data(ad
, saved_idx2
, NULL
)))
124 typedef struct myobj_st
{
125 CRYPTO_EX_DATA ex_data
;
130 static MYOBJ
*MYOBJ_new()
132 static int count
= 0;
133 MYOBJ
*obj
= OPENSSL_malloc(sizeof(*obj
));
136 obj
->st
= CRYPTO_new_ex_data(CRYPTO_EX_INDEX_APP
, obj
, &obj
->ex_data
);
140 static void MYOBJ_sethello(MYOBJ
*obj
, char *cp
)
142 obj
->st
= CRYPTO_set_ex_data(&obj
->ex_data
, saved_idx
, cp
);
143 if (!TEST_int_eq(obj
->st
, 1))
147 static char *MYOBJ_gethello(MYOBJ
*obj
)
149 return CRYPTO_get_ex_data(&obj
->ex_data
, saved_idx
);
152 static void MYOBJ_sethello2(MYOBJ
*obj
, char *cp
)
154 MYOBJ_EX_DATA
* ex_data
= CRYPTO_get_ex_data(&obj
->ex_data
, saved_idx2
);
155 if (TEST_ptr(ex_data
))
158 obj
->st
= gbl_result
= 0;
161 static char *MYOBJ_gethello2(MYOBJ
*obj
)
163 MYOBJ_EX_DATA
* ex_data
= CRYPTO_get_ex_data(&obj
->ex_data
, saved_idx2
);
164 if (TEST_ptr(ex_data
))
165 return ex_data
->hello
;
167 obj
->st
= gbl_result
= 0;
171 static void MYOBJ_free(MYOBJ
*obj
)
173 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_APP
, obj
, &obj
->ex_data
);
177 static MYOBJ
*MYOBJ_dup(MYOBJ
*in
)
179 MYOBJ
*obj
= MYOBJ_new();
181 obj
->st
|= CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_APP
, &obj
->ex_data
,
186 static int test_exdata(void)
189 MYOBJ_EX_DATA
*ex_data
;
195 p
= OPENSSL_strdup("hello world");
197 saved_argp
= OPENSSL_malloc(1);
198 saved_idx
= CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP
,
199 saved_argl
, saved_argp
,
200 exnew
, exdup
, exfree
);
201 saved_idx2
= CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP
,
202 saved_argl
, saved_argp
,
203 exnew2
, exdup2
, exfree2
);
206 if (!TEST_int_eq(t1
->st
, 1) || !TEST_int_eq(t2
->st
, 1))
208 if (!TEST_ptr(CRYPTO_get_ex_data(&t1
->ex_data
, saved_idx2
)))
210 if (!TEST_ptr(CRYPTO_get_ex_data(&t2
->ex_data
, saved_idx2
)))
213 MYOBJ_sethello(t1
, p
);
214 cp
= MYOBJ_gethello(t1
);
215 if (!TEST_ptr_eq(cp
, p
))
218 MYOBJ_sethello2(t1
, p
);
219 cp
= MYOBJ_gethello2(t1
);
220 if (!TEST_ptr_eq(cp
, p
))
223 cp
= MYOBJ_gethello(t2
);
224 if (!TEST_ptr_null(cp
))
227 cp
= MYOBJ_gethello2(t2
);
228 if (!TEST_ptr_null(cp
))
232 if (!TEST_int_eq(t3
->st
, 1))
235 ex_data
= CRYPTO_get_ex_data(&t3
->ex_data
, saved_idx2
);
236 if (!TEST_ptr(ex_data
))
238 if (!TEST_int_eq(ex_data
->dup
, 1))
241 cp
= MYOBJ_gethello(t3
);
242 if (!TEST_ptr_eq(cp
, p
))
245 cp
= MYOBJ_gethello2(t3
);
246 if (!TEST_ptr_eq(cp
, p
))
252 OPENSSL_free(saved_argp
);
261 void register_tests(void)
263 ADD_TEST(test_exdata
);