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 gbl_result;
22 static void exnew(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
23 int idx, long argl, void *argp)
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))
32 static int exdup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
33 void *from_d, int idx, long argl, void *argp)
35 if (!TEST_int_eq(idx, saved_idx)
36 || !TEST_long_eq(argl, saved_argl)
37 || !TEST_ptr_eq(argp, saved_argp)
43 static void exfree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
44 int idx, long argl, void *argp)
46 if (!TEST_int_eq(idx, saved_idx)
47 || !TEST_long_eq(argl, saved_argl)
48 || !TEST_ptr_eq(argp, saved_argp))
52 typedef struct myobj_st {
53 CRYPTO_EX_DATA ex_data;
58 static MYOBJ *MYOBJ_new()
61 MYOBJ *obj = OPENSSL_malloc(sizeof(*obj));
64 obj->st = CRYPTO_new_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
68 static void MYOBJ_sethello(MYOBJ *obj, char *cp)
70 obj->st = CRYPTO_set_ex_data(&obj->ex_data, saved_idx, cp);
71 if (!TEST_int_eq(obj->st, 1))
75 static char *MYOBJ_gethello(MYOBJ *obj)
77 return CRYPTO_get_ex_data(&obj->ex_data, saved_idx);
80 static void MYOBJ_free(MYOBJ *obj)
82 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
86 static MYOBJ *MYOBJ_dup(MYOBJ *in)
88 MYOBJ *obj = MYOBJ_new();
90 obj->st |= CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_APP, &obj->ex_data,
95 static int test_exdata(void)
103 p = strdup("hello world");
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);
111 if (!TEST_int_eq(t1->st, 1) || !TEST_int_eq(t2->st, 1))
114 MYOBJ_sethello(t1, p);
115 cp = MYOBJ_gethello(t1);
116 if (!TEST_ptr_eq(cp, p))
119 cp = MYOBJ_gethello(t2);
120 if (!TEST_ptr_null(cp))
124 if (!TEST_int_eq(t3->st, 1))
127 cp = MYOBJ_gethello(t3);
128 if (!TEST_ptr_eq(cp, p))
143 void register_tests(void)
145 ADD_TEST(test_exdata);