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>
15 #include "test_main.h"
18 static long saved_argl;
19 static void *saved_argp;
21 static int gbl_result;
23 static void exnew(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
24 int idx, long argl, void *argp)
26 if (!TEST_int_eq(idx, saved_idx)
27 || !TEST_long_eq(argl, saved_argl)
28 || !TEST_ptr_eq(argp, saved_argp)
29 || !TEST_ptr_null(ptr))
33 static int exdup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
34 void *from_d, int idx, long argl, void *argp)
36 if (!TEST_int_eq(idx, saved_idx)
37 || !TEST_long_eq(argl, saved_argl)
38 || !TEST_ptr_eq(argp, saved_argp)
44 static void exfree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
45 int idx, long argl, void *argp)
47 if (!TEST_int_eq(idx, saved_idx)
48 || !TEST_long_eq(argl, saved_argl)
49 || !TEST_ptr_eq(argp, saved_argp))
53 typedef struct myobj_st {
54 CRYPTO_EX_DATA ex_data;
59 static MYOBJ *MYOBJ_new()
62 MYOBJ *obj = OPENSSL_malloc(sizeof(*obj));
65 obj->st = CRYPTO_new_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
69 static void MYOBJ_sethello(MYOBJ *obj, char *cp)
71 obj->st = CRYPTO_set_ex_data(&obj->ex_data, saved_idx, cp);
72 if (!TEST_int_eq(obj->st, 1))
76 static char *MYOBJ_gethello(MYOBJ *obj)
78 return CRYPTO_get_ex_data(&obj->ex_data, saved_idx);
81 static void MYOBJ_free(MYOBJ *obj)
83 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
87 static MYOBJ *MYOBJ_dup(MYOBJ *in)
89 MYOBJ *obj = MYOBJ_new();
91 obj->st |= CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_APP, &obj->ex_data,
96 static int test_exdata(void)
104 p = strdup("hello world");
106 saved_argp = malloc(1);
107 saved_idx = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
108 saved_argl, saved_argp,
109 exnew, exdup, exfree);
112 if (!TEST_int_eq(t1->st, 1) || !TEST_int_eq(t2->st, 1))
115 MYOBJ_sethello(t1, p);
116 cp = MYOBJ_gethello(t1);
117 if (!TEST_ptr_eq(cp, p))
120 cp = MYOBJ_gethello(t2);
121 if (!TEST_ptr_null(cp))
125 if (!TEST_int_eq(t3->st, 1))
128 cp = MYOBJ_gethello(t3);
129 if (!TEST_ptr_eq(cp, p))
144 void register_tests(void)
146 ADD_TEST(test_exdata);