]>
Commit | Line | Data |
---|---|---|
440e5d80 | 1 | /* |
629192c1 | 2 | * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved. |
e6390aca | 3 | * |
440e5d80 RS |
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 | |
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 |
17 | static long saved_argl; |
18 | static void *saved_argp; | |
19 | static int saved_idx; | |
1ee21259 | 20 | static int saved_idx2; |
eb16fc8f | 21 | static int gbl_result; |
e6390aca | 22 | |
1ee21259 TS |
23 | /* |
24 | * SIMPLE EX_DATA IMPLEMENTATION | |
25 | * Apps explicitly set/get ex_data as needed | |
26 | */ | |
27 | ||
e6390aca RS |
28 | static void exnew(void *parent, void *ptr, CRYPTO_EX_DATA *ad, |
29 | int idx, long argl, void *argp) | |
30 | { | |
eb16fc8f JS |
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)) | |
35 | gbl_result = 0; | |
e6390aca RS |
36 | } |
37 | ||
629192c1 | 38 | static int exdup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from, |
e6390aca RS |
39 | void *from_d, int idx, long argl, void *argp) |
40 | { | |
eb16fc8f JS |
41 | if (!TEST_int_eq(idx, saved_idx) |
42 | || !TEST_long_eq(argl, saved_argl) | |
43 | || !TEST_ptr_eq(argp, saved_argp) | |
44 | || !TEST_ptr(from_d)) | |
45 | gbl_result = 0; | |
b3c31a65 | 46 | return 1; |
e6390aca RS |
47 | } |
48 | ||
49 | static void exfree(void *parent, void *ptr, CRYPTO_EX_DATA *ad, | |
50 | int idx, long argl, void *argp) | |
51 | { | |
eb16fc8f JS |
52 | if (!TEST_int_eq(idx, saved_idx) |
53 | || !TEST_long_eq(argl, saved_argl) | |
54 | || !TEST_ptr_eq(argp, saved_argp)) | |
55 | gbl_result = 0; | |
e6390aca RS |
56 | } |
57 | ||
1ee21259 TS |
58 | /* |
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 | |
62 | */ | |
63 | ||
64 | typedef struct myobj_ex_data_st { | |
65 | char *hello; | |
66 | int new; | |
67 | int dup; | |
68 | } MYOBJ_EX_DATA; | |
69 | ||
70 | static void exnew2(void *parent, void *ptr, CRYPTO_EX_DATA *ad, | |
71 | int idx, long argl, void *argp) | |
72 | { | |
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) | |
78 | || !TEST_ptr(ex_data) | |
79 | || !TEST_true(CRYPTO_set_ex_data(ad, saved_idx2, ex_data))) { | |
80 | gbl_result = 0; | |
81 | OPENSSL_free(ex_data); | |
82 | } else { | |
83 | ex_data->new = 1; | |
84 | } | |
85 | } | |
86 | ||
87 | static int exdup2(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from, | |
88 | void *from_d, int idx, long argl, void *argp) | |
89 | { | |
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) | |
95 | || !TEST_ptr(from_d) | |
96 | || !TEST_ptr(*update_ex_data) | |
97 | || !TEST_ptr(ex_data) | |
98 | || !TEST_true(ex_data->new)) { | |
99 | gbl_result = 0; | |
100 | } else { | |
101 | /* Copy hello over */ | |
102 | ex_data->hello = (*update_ex_data)->hello; | |
103 | /* indicate this is a dup */ | |
104 | ex_data->dup = 1; | |
105 | /* Keep my original ex_data */ | |
106 | *update_ex_data = ex_data; | |
107 | } | |
108 | return 1; | |
109 | } | |
110 | ||
111 | static void exfree2(void *parent, void *ptr, CRYPTO_EX_DATA *ad, | |
112 | int idx, long argl, void *argp) | |
113 | { | |
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))) | |
121 | gbl_result = 0; | |
122 | } | |
123 | ||
e6390aca RS |
124 | typedef struct myobj_st { |
125 | CRYPTO_EX_DATA ex_data; | |
126 | int id; | |
629192c1 | 127 | int st; |
e6390aca RS |
128 | } MYOBJ; |
129 | ||
3cb7c5cf | 130 | static MYOBJ *MYOBJ_new(void) |
e6390aca RS |
131 | { |
132 | static int count = 0; | |
133 | MYOBJ *obj = OPENSSL_malloc(sizeof(*obj)); | |
e6390aca RS |
134 | |
135 | obj->id = ++count; | |
629192c1 | 136 | obj->st = CRYPTO_new_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data); |
e6390aca RS |
137 | return obj; |
138 | } | |
139 | ||
140 | static void MYOBJ_sethello(MYOBJ *obj, char *cp) | |
141 | { | |
629192c1 | 142 | obj->st = CRYPTO_set_ex_data(&obj->ex_data, saved_idx, cp); |
eb16fc8f JS |
143 | if (!TEST_int_eq(obj->st, 1)) |
144 | gbl_result = 0; | |
e6390aca RS |
145 | } |
146 | ||
147 | static char *MYOBJ_gethello(MYOBJ *obj) | |
148 | { | |
629192c1 | 149 | return CRYPTO_get_ex_data(&obj->ex_data, saved_idx); |
e6390aca RS |
150 | } |
151 | ||
1ee21259 TS |
152 | static void MYOBJ_sethello2(MYOBJ *obj, char *cp) |
153 | { | |
154 | MYOBJ_EX_DATA* ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx2); | |
155 | if (TEST_ptr(ex_data)) | |
156 | ex_data->hello = cp; | |
157 | else | |
158 | obj->st = gbl_result = 0; | |
159 | } | |
160 | ||
161 | static char *MYOBJ_gethello2(MYOBJ *obj) | |
162 | { | |
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; | |
166 | ||
167 | obj->st = gbl_result = 0; | |
168 | return NULL; | |
169 | } | |
170 | ||
e6390aca RS |
171 | static void MYOBJ_free(MYOBJ *obj) |
172 | { | |
173 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data); | |
174 | OPENSSL_free(obj); | |
175 | } | |
176 | ||
b3c31a65 BE |
177 | static MYOBJ *MYOBJ_dup(MYOBJ *in) |
178 | { | |
179 | MYOBJ *obj = MYOBJ_new(); | |
180 | ||
eb16fc8f | 181 | obj->st |= CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_APP, &obj->ex_data, |
b3c31a65 | 182 | &in->ex_data); |
b3c31a65 BE |
183 | return obj; |
184 | } | |
185 | ||
eb16fc8f | 186 | static int test_exdata(void) |
e6390aca | 187 | { |
b3c31a65 | 188 | MYOBJ *t1, *t2, *t3; |
1ee21259 | 189 | MYOBJ_EX_DATA *ex_data; |
e6390aca RS |
190 | const char *cp; |
191 | char *p; | |
192 | ||
eb16fc8f JS |
193 | gbl_result = 1; |
194 | ||
1ee21259 | 195 | p = OPENSSL_strdup("hello world"); |
629192c1 | 196 | saved_argl = 21; |
1ee21259 | 197 | saved_argp = OPENSSL_malloc(1); |
629192c1 RS |
198 | saved_idx = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP, |
199 | saved_argl, saved_argp, | |
200 | exnew, exdup, exfree); | |
1ee21259 TS |
201 | saved_idx2 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP, |
202 | saved_argl, saved_argp, | |
203 | exnew2, exdup2, exfree2); | |
e6390aca RS |
204 | t1 = MYOBJ_new(); |
205 | t2 = MYOBJ_new(); | |
eb16fc8f JS |
206 | if (!TEST_int_eq(t1->st, 1) || !TEST_int_eq(t2->st, 1)) |
207 | return 0; | |
1ee21259 TS |
208 | if (!TEST_ptr(CRYPTO_get_ex_data(&t1->ex_data, saved_idx2))) |
209 | return 0; | |
210 | if (!TEST_ptr(CRYPTO_get_ex_data(&t2->ex_data, saved_idx2))) | |
211 | return 0; | |
eb16fc8f | 212 | |
e6390aca RS |
213 | MYOBJ_sethello(t1, p); |
214 | cp = MYOBJ_gethello(t1); | |
eb16fc8f JS |
215 | if (!TEST_ptr_eq(cp, p)) |
216 | return 0; | |
217 | ||
1ee21259 TS |
218 | MYOBJ_sethello2(t1, p); |
219 | cp = MYOBJ_gethello2(t1); | |
220 | if (!TEST_ptr_eq(cp, p)) | |
221 | return 0; | |
222 | ||
e6390aca | 223 | cp = MYOBJ_gethello(t2); |
eb16fc8f JS |
224 | if (!TEST_ptr_null(cp)) |
225 | return 0; | |
226 | ||
1ee21259 TS |
227 | cp = MYOBJ_gethello2(t2); |
228 | if (!TEST_ptr_null(cp)) | |
229 | return 0; | |
230 | ||
b3c31a65 | 231 | t3 = MYOBJ_dup(t1); |
eb16fc8f JS |
232 | if (!TEST_int_eq(t3->st, 1)) |
233 | return 0; | |
234 | ||
1ee21259 TS |
235 | ex_data = CRYPTO_get_ex_data(&t3->ex_data, saved_idx2); |
236 | if (!TEST_ptr(ex_data)) | |
237 | return 0; | |
238 | if (!TEST_int_eq(ex_data->dup, 1)) | |
239 | return 0; | |
240 | ||
b3c31a65 | 241 | cp = MYOBJ_gethello(t3); |
eb16fc8f JS |
242 | if (!TEST_ptr_eq(cp, p)) |
243 | return 0; | |
244 | ||
1ee21259 TS |
245 | cp = MYOBJ_gethello2(t3); |
246 | if (!TEST_ptr_eq(cp, p)) | |
247 | return 0; | |
248 | ||
e6390aca RS |
249 | MYOBJ_free(t1); |
250 | MYOBJ_free(t2); | |
b3c31a65 | 251 | MYOBJ_free(t3); |
1ee21259 TS |
252 | OPENSSL_free(saved_argp); |
253 | OPENSSL_free(p); | |
eb16fc8f JS |
254 | |
255 | if (gbl_result) | |
256 | return 1; | |
257 | else | |
258 | return 0; | |
259 | } | |
260 | ||
ad887416 | 261 | int setup_tests(void) |
eb16fc8f JS |
262 | { |
263 | ADD_TEST(test_exdata); | |
ad887416 | 264 | return 1; |
e6390aca | 265 | } |