]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/exdatatest.c
Update copyright year
[thirdparty/openssl.git] / test / exdatatest.c
CommitLineData
440e5d80 1/*
00c405b3 2 * Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
e6390aca 3 *
909f1a2e 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
440e5d80
RS
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
e6390aca 8 */
440e5d80 9
e6390aca 10#include <stdio.h>
e6390aca
RS
11#include <string.h>
12#include <stdlib.h>
13#include <openssl/crypto.h>
14
eb16fc8f
JS
15#include "testutil.h"
16
629192c1
RS
17static long saved_argl;
18static void *saved_argp;
19static int saved_idx;
1ee21259 20static int saved_idx2;
e17f5b6a 21static int saved_idx3;
eb16fc8f 22static int gbl_result;
e6390aca 23
1ee21259
TS
24/*
25 * SIMPLE EX_DATA IMPLEMENTATION
26 * Apps explicitly set/get ex_data as needed
27 */
28
e6390aca
RS
29static void exnew(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
30 int idx, long argl, void *argp)
31{
eb16fc8f
JS
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))
36 gbl_result = 0;
e6390aca
RS
37}
38
629192c1 39static int exdup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
712e8deb 40 void **from_d, int idx, long argl, void *argp)
e6390aca 41{
eb16fc8f
JS
42 if (!TEST_int_eq(idx, saved_idx)
43 || !TEST_long_eq(argl, saved_argl)
44 || !TEST_ptr_eq(argp, saved_argp)
45 || !TEST_ptr(from_d))
46 gbl_result = 0;
b3c31a65 47 return 1;
e6390aca
RS
48}
49
50static void exfree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
51 int idx, long argl, void *argp)
52{
eb16fc8f
JS
53 if (!TEST_int_eq(idx, saved_idx)
54 || !TEST_long_eq(argl, saved_argl)
55 || !TEST_ptr_eq(argp, saved_argp))
56 gbl_result = 0;
e6390aca
RS
57}
58
1ee21259
TS
59/*
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
63 */
64
65typedef struct myobj_ex_data_st {
66 char *hello;
67 int new;
68 int dup;
69} MYOBJ_EX_DATA;
70
71static void exnew2(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
72 int idx, long argl, void *argp)
73{
74 MYOBJ_EX_DATA *ex_data = OPENSSL_zalloc(sizeof(*ex_data));
e17f5b6a
RL
75
76 if (!TEST_true(idx == saved_idx2 || idx == saved_idx3)
1ee21259
TS
77 || !TEST_long_eq(argl, saved_argl)
78 || !TEST_ptr_eq(argp, saved_argp)
79 || !TEST_ptr_null(ptr)
80 || !TEST_ptr(ex_data)
e17f5b6a 81 || !TEST_true(CRYPTO_set_ex_data(ad, idx, ex_data))) {
1ee21259
TS
82 gbl_result = 0;
83 OPENSSL_free(ex_data);
84 } else {
85 ex_data->new = 1;
86 }
87}
88
89static int exdup2(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
712e8deb 90 void **from_d, int idx, long argl, void *argp)
1ee21259
TS
91{
92 MYOBJ_EX_DATA **update_ex_data = (MYOBJ_EX_DATA**)from_d;
e17f5b6a
RL
93 MYOBJ_EX_DATA *ex_data = NULL;
94
95 if (!TEST_true(idx == saved_idx2 || idx == saved_idx3)
1ee21259
TS
96 || !TEST_long_eq(argl, saved_argl)
97 || !TEST_ptr_eq(argp, saved_argp)
98 || !TEST_ptr(from_d)
99 || !TEST_ptr(*update_ex_data)
e17f5b6a 100 || !TEST_ptr(ex_data = CRYPTO_get_ex_data(to, idx))
1ee21259
TS
101 || !TEST_true(ex_data->new)) {
102 gbl_result = 0;
103 } else {
104 /* Copy hello over */
105 ex_data->hello = (*update_ex_data)->hello;
106 /* indicate this is a dup */
107 ex_data->dup = 1;
108 /* Keep my original ex_data */
109 *update_ex_data = ex_data;
110 }
111 return 1;
112}
113
114static void exfree2(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
115 int idx, long argl, void *argp)
116{
e17f5b6a
RL
117 MYOBJ_EX_DATA *ex_data = CRYPTO_get_ex_data(ad, idx);
118
119 if (!TEST_true(idx == saved_idx2 || idx == saved_idx3)
1ee21259
TS
120 || !TEST_long_eq(argl, saved_argl)
121 || !TEST_ptr_eq(argp, saved_argp)
e17f5b6a 122 || !TEST_true(CRYPTO_set_ex_data(ad, idx, NULL)))
1ee21259 123 gbl_result = 0;
e17f5b6a 124 OPENSSL_free(ex_data);
1ee21259
TS
125}
126
e6390aca
RS
127typedef struct myobj_st {
128 CRYPTO_EX_DATA ex_data;
129 int id;
629192c1 130 int st;
e6390aca
RS
131} MYOBJ;
132
3cb7c5cf 133static MYOBJ *MYOBJ_new(void)
e6390aca
RS
134{
135 static int count = 0;
136 MYOBJ *obj = OPENSSL_malloc(sizeof(*obj));
e6390aca
RS
137
138 obj->id = ++count;
629192c1 139 obj->st = CRYPTO_new_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
e6390aca
RS
140 return obj;
141}
142
143static void MYOBJ_sethello(MYOBJ *obj, char *cp)
144{
629192c1 145 obj->st = CRYPTO_set_ex_data(&obj->ex_data, saved_idx, cp);
eb16fc8f
JS
146 if (!TEST_int_eq(obj->st, 1))
147 gbl_result = 0;
e6390aca
RS
148}
149
150static char *MYOBJ_gethello(MYOBJ *obj)
151{
629192c1 152 return CRYPTO_get_ex_data(&obj->ex_data, saved_idx);
e6390aca
RS
153}
154
1ee21259
TS
155static void MYOBJ_sethello2(MYOBJ *obj, char *cp)
156{
157 MYOBJ_EX_DATA* ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx2);
e17f5b6a 158
1ee21259
TS
159 if (TEST_ptr(ex_data))
160 ex_data->hello = cp;
161 else
162 obj->st = gbl_result = 0;
163}
164
165static char *MYOBJ_gethello2(MYOBJ *obj)
166{
167 MYOBJ_EX_DATA* ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx2);
e17f5b6a
RL
168
169 if (TEST_ptr(ex_data))
170 return ex_data->hello;
171
172 obj->st = gbl_result = 0;
173 return NULL;
174}
175
176static void MYOBJ_allochello3(MYOBJ *obj, char *cp)
177{
178 MYOBJ_EX_DATA* ex_data = NULL;
179
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)))
184 ex_data->hello = cp;
185 else
186 obj->st = gbl_result = 0;
187}
188
189static char *MYOBJ_gethello3(MYOBJ *obj)
190{
191 MYOBJ_EX_DATA* ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx3);
192
1ee21259
TS
193 if (TEST_ptr(ex_data))
194 return ex_data->hello;
195
196 obj->st = gbl_result = 0;
197 return NULL;
198}
199
e6390aca
RS
200static void MYOBJ_free(MYOBJ *obj)
201{
202 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
203 OPENSSL_free(obj);
204}
205
b3c31a65
BE
206static MYOBJ *MYOBJ_dup(MYOBJ *in)
207{
208 MYOBJ *obj = MYOBJ_new();
209
eb16fc8f 210 obj->st |= CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_APP, &obj->ex_data,
b3c31a65 211 &in->ex_data);
b3c31a65
BE
212 return obj;
213}
214
eb16fc8f 215static int test_exdata(void)
e6390aca 216{
b3c31a65 217 MYOBJ *t1, *t2, *t3;
1ee21259 218 MYOBJ_EX_DATA *ex_data;
e6390aca
RS
219 const char *cp;
220 char *p;
221
eb16fc8f
JS
222 gbl_result = 1;
223
1ee21259 224 p = OPENSSL_strdup("hello world");
629192c1 225 saved_argl = 21;
1ee21259 226 saved_argp = OPENSSL_malloc(1);
629192c1
RS
227 saved_idx = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
228 saved_argl, saved_argp,
229 exnew, exdup, exfree);
1ee21259
TS
230 saved_idx2 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
231 saved_argl, saved_argp,
232 exnew2, exdup2, exfree2);
e6390aca
RS
233 t1 = MYOBJ_new();
234 t2 = MYOBJ_new();
eb16fc8f
JS
235 if (!TEST_int_eq(t1->st, 1) || !TEST_int_eq(t2->st, 1))
236 return 0;
1ee21259
TS
237 if (!TEST_ptr(CRYPTO_get_ex_data(&t1->ex_data, saved_idx2)))
238 return 0;
e17f5b6a
RL
239
240 /*
241 * saved_idx3 differs from other indexes by being created after the exdata
242 * was initialized.
243 */
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)))
1ee21259 248 return 0;
eb16fc8f 249
e6390aca
RS
250 MYOBJ_sethello(t1, p);
251 cp = MYOBJ_gethello(t1);
eb16fc8f
JS
252 if (!TEST_ptr_eq(cp, p))
253 return 0;
254
1ee21259
TS
255 MYOBJ_sethello2(t1, p);
256 cp = MYOBJ_gethello2(t1);
257 if (!TEST_ptr_eq(cp, p))
258 return 0;
259
e17f5b6a
RL
260 MYOBJ_allochello3(t1, p);
261 cp = MYOBJ_gethello3(t1);
262 if (!TEST_ptr_eq(cp, p))
263 return 0;
264
e6390aca 265 cp = MYOBJ_gethello(t2);
eb16fc8f
JS
266 if (!TEST_ptr_null(cp))
267 return 0;
268
1ee21259
TS
269 cp = MYOBJ_gethello2(t2);
270 if (!TEST_ptr_null(cp))
271 return 0;
272
b3c31a65 273 t3 = MYOBJ_dup(t1);
eb16fc8f
JS
274 if (!TEST_int_eq(t3->st, 1))
275 return 0;
276
1ee21259
TS
277 ex_data = CRYPTO_get_ex_data(&t3->ex_data, saved_idx2);
278 if (!TEST_ptr(ex_data))
279 return 0;
280 if (!TEST_int_eq(ex_data->dup, 1))
281 return 0;
282
b3c31a65 283 cp = MYOBJ_gethello(t3);
eb16fc8f
JS
284 if (!TEST_ptr_eq(cp, p))
285 return 0;
286
1ee21259
TS
287 cp = MYOBJ_gethello2(t3);
288 if (!TEST_ptr_eq(cp, p))
289 return 0;
290
e17f5b6a
RL
291 cp = MYOBJ_gethello3(t3);
292 if (!TEST_ptr_eq(cp, p))
293 return 0;
294
e6390aca
RS
295 MYOBJ_free(t1);
296 MYOBJ_free(t2);
b3c31a65 297 MYOBJ_free(t3);
1ee21259
TS
298 OPENSSL_free(saved_argp);
299 OPENSSL_free(p);
eb16fc8f
JS
300
301 if (gbl_result)
302 return 1;
303 else
304 return 0;
305}
306
ad887416 307int setup_tests(void)
eb16fc8f
JS
308{
309 ADD_TEST(test_exdata);
ad887416 310 return 1;
e6390aca 311}