exdatatest.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <openssl/crypto.h>
  13. #include "testutil.h"
  14. static long saved_argl;
  15. static void *saved_argp;
  16. static int saved_idx;
  17. static int saved_idx2;
  18. static int saved_idx3;
  19. static int gbl_result;
  20. /*
  21. * SIMPLE EX_DATA IMPLEMENTATION
  22. * Apps explicitly set/get ex_data as needed
  23. */
  24. static void exnew(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
  25. int idx, long argl, void *argp)
  26. {
  27. if (!TEST_int_eq(idx, saved_idx)
  28. || !TEST_long_eq(argl, saved_argl)
  29. || !TEST_ptr_eq(argp, saved_argp)
  30. || !TEST_ptr_null(ptr))
  31. gbl_result = 0;
  32. }
  33. static int exdup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
  34. void *from_d, int idx, long argl, void *argp)
  35. {
  36. if (!TEST_int_eq(idx, saved_idx)
  37. || !TEST_long_eq(argl, saved_argl)
  38. || !TEST_ptr_eq(argp, saved_argp)
  39. || !TEST_ptr(from_d))
  40. gbl_result = 0;
  41. return 1;
  42. }
  43. static void exfree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
  44. int idx, long argl, void *argp)
  45. {
  46. if (!TEST_int_eq(idx, saved_idx)
  47. || !TEST_long_eq(argl, saved_argl)
  48. || !TEST_ptr_eq(argp, saved_argp))
  49. gbl_result = 0;
  50. }
  51. /*
  52. * PRE-ALLOCATED EX_DATA IMPLEMENTATION
  53. * Extended data structure is allocated in exnew2/freed in exfree2
  54. * Data is stored inside extended data structure
  55. */
  56. typedef struct myobj_ex_data_st {
  57. char *hello;
  58. int new;
  59. int dup;
  60. } MYOBJ_EX_DATA;
  61. static void exnew2(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
  62. int idx, long argl, void *argp)
  63. {
  64. MYOBJ_EX_DATA *ex_data = OPENSSL_zalloc(sizeof(*ex_data));
  65. if (!TEST_true(idx == saved_idx2 || idx == saved_idx3)
  66. || !TEST_long_eq(argl, saved_argl)
  67. || !TEST_ptr_eq(argp, saved_argp)
  68. || !TEST_ptr_null(ptr)
  69. || !TEST_ptr(ex_data)
  70. || !TEST_true(CRYPTO_set_ex_data(ad, idx, ex_data))) {
  71. gbl_result = 0;
  72. OPENSSL_free(ex_data);
  73. } else {
  74. ex_data->new = 1;
  75. }
  76. }
  77. static int exdup2(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
  78. void *from_d, int idx, long argl, void *argp)
  79. {
  80. MYOBJ_EX_DATA **update_ex_data = (MYOBJ_EX_DATA**)from_d;
  81. MYOBJ_EX_DATA *ex_data = NULL;
  82. if (!TEST_true(idx == saved_idx2 || idx == saved_idx3)
  83. || !TEST_long_eq(argl, saved_argl)
  84. || !TEST_ptr_eq(argp, saved_argp)
  85. || !TEST_ptr(from_d)
  86. || !TEST_ptr(*update_ex_data)
  87. || !TEST_ptr(ex_data = CRYPTO_get_ex_data(to, idx))
  88. || !TEST_true(ex_data->new)) {
  89. gbl_result = 0;
  90. } else {
  91. /* Copy hello over */
  92. ex_data->hello = (*update_ex_data)->hello;
  93. /* indicate this is a dup */
  94. ex_data->dup = 1;
  95. /* Keep my original ex_data */
  96. *update_ex_data = ex_data;
  97. }
  98. return 1;
  99. }
  100. static void exfree2(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
  101. int idx, long argl, void *argp)
  102. {
  103. MYOBJ_EX_DATA *ex_data = CRYPTO_get_ex_data(ad, idx);
  104. if (!TEST_true(idx == saved_idx2 || idx == saved_idx3)
  105. || !TEST_long_eq(argl, saved_argl)
  106. || !TEST_ptr_eq(argp, saved_argp)
  107. || !TEST_true(CRYPTO_set_ex_data(ad, idx, NULL)))
  108. gbl_result = 0;
  109. OPENSSL_free(ex_data);
  110. }
  111. typedef struct myobj_st {
  112. CRYPTO_EX_DATA ex_data;
  113. int id;
  114. int st;
  115. } MYOBJ;
  116. static MYOBJ *MYOBJ_new(void)
  117. {
  118. static int count = 0;
  119. MYOBJ *obj = OPENSSL_malloc(sizeof(*obj));
  120. obj->id = ++count;
  121. obj->st = CRYPTO_new_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
  122. return obj;
  123. }
  124. static void MYOBJ_sethello(MYOBJ *obj, char *cp)
  125. {
  126. obj->st = CRYPTO_set_ex_data(&obj->ex_data, saved_idx, cp);
  127. if (!TEST_int_eq(obj->st, 1))
  128. gbl_result = 0;
  129. }
  130. static char *MYOBJ_gethello(MYOBJ *obj)
  131. {
  132. return CRYPTO_get_ex_data(&obj->ex_data, saved_idx);
  133. }
  134. static void MYOBJ_sethello2(MYOBJ *obj, char *cp)
  135. {
  136. MYOBJ_EX_DATA* ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx2);
  137. if (TEST_ptr(ex_data))
  138. ex_data->hello = cp;
  139. else
  140. obj->st = gbl_result = 0;
  141. }
  142. static char *MYOBJ_gethello2(MYOBJ *obj)
  143. {
  144. MYOBJ_EX_DATA* ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx2);
  145. if (TEST_ptr(ex_data))
  146. return ex_data->hello;
  147. obj->st = gbl_result = 0;
  148. return NULL;
  149. }
  150. static void MYOBJ_allochello3(MYOBJ *obj, char *cp)
  151. {
  152. MYOBJ_EX_DATA* ex_data = NULL;
  153. if (TEST_ptr_null(ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx3))
  154. && TEST_true(CRYPTO_alloc_ex_data(CRYPTO_EX_INDEX_APP, obj,
  155. &obj->ex_data, saved_idx3))
  156. && TEST_ptr(ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx3)))
  157. ex_data->hello = cp;
  158. else
  159. obj->st = gbl_result = 0;
  160. }
  161. static char *MYOBJ_gethello3(MYOBJ *obj)
  162. {
  163. MYOBJ_EX_DATA* ex_data = CRYPTO_get_ex_data(&obj->ex_data, saved_idx3);
  164. if (TEST_ptr(ex_data))
  165. return ex_data->hello;
  166. obj->st = gbl_result = 0;
  167. return NULL;
  168. }
  169. static void MYOBJ_free(MYOBJ *obj)
  170. {
  171. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
  172. OPENSSL_free(obj);
  173. }
  174. static MYOBJ *MYOBJ_dup(MYOBJ *in)
  175. {
  176. MYOBJ *obj = MYOBJ_new();
  177. obj->st |= CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_APP, &obj->ex_data,
  178. &in->ex_data);
  179. return obj;
  180. }
  181. static int test_exdata(void)
  182. {
  183. MYOBJ *t1, *t2, *t3;
  184. MYOBJ_EX_DATA *ex_data;
  185. const char *cp;
  186. char *p;
  187. gbl_result = 1;
  188. p = OPENSSL_strdup("hello world");
  189. saved_argl = 21;
  190. saved_argp = OPENSSL_malloc(1);
  191. saved_idx = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
  192. saved_argl, saved_argp,
  193. exnew, exdup, exfree);
  194. saved_idx2 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
  195. saved_argl, saved_argp,
  196. exnew2, exdup2, exfree2);
  197. t1 = MYOBJ_new();
  198. t2 = MYOBJ_new();
  199. if (!TEST_int_eq(t1->st, 1) || !TEST_int_eq(t2->st, 1))
  200. return 0;
  201. if (!TEST_ptr(CRYPTO_get_ex_data(&t1->ex_data, saved_idx2)))
  202. return 0;
  203. /*
  204. * saved_idx3 differs from other indexes by being created after the exdata
  205. * was initialized.
  206. */
  207. saved_idx3 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
  208. saved_argl, saved_argp,
  209. exnew2, exdup2, exfree2);
  210. if (!TEST_ptr_null(CRYPTO_get_ex_data(&t1->ex_data, saved_idx3)))
  211. return 0;
  212. MYOBJ_sethello(t1, p);
  213. cp = MYOBJ_gethello(t1);
  214. if (!TEST_ptr_eq(cp, p))
  215. return 0;
  216. MYOBJ_sethello2(t1, p);
  217. cp = MYOBJ_gethello2(t1);
  218. if (!TEST_ptr_eq(cp, p))
  219. return 0;
  220. MYOBJ_allochello3(t1, p);
  221. cp = MYOBJ_gethello3(t1);
  222. if (!TEST_ptr_eq(cp, p))
  223. return 0;
  224. cp = MYOBJ_gethello(t2);
  225. if (!TEST_ptr_null(cp))
  226. return 0;
  227. cp = MYOBJ_gethello2(t2);
  228. if (!TEST_ptr_null(cp))
  229. return 0;
  230. t3 = MYOBJ_dup(t1);
  231. if (!TEST_int_eq(t3->st, 1))
  232. return 0;
  233. ex_data = CRYPTO_get_ex_data(&t3->ex_data, saved_idx2);
  234. if (!TEST_ptr(ex_data))
  235. return 0;
  236. if (!TEST_int_eq(ex_data->dup, 1))
  237. return 0;
  238. cp = MYOBJ_gethello(t3);
  239. if (!TEST_ptr_eq(cp, p))
  240. return 0;
  241. cp = MYOBJ_gethello2(t3);
  242. if (!TEST_ptr_eq(cp, p))
  243. return 0;
  244. cp = MYOBJ_gethello3(t3);
  245. if (!TEST_ptr_eq(cp, p))
  246. return 0;
  247. MYOBJ_free(t1);
  248. MYOBJ_free(t2);
  249. MYOBJ_free(t3);
  250. OPENSSL_free(saved_argp);
  251. OPENSSL_free(p);
  252. if (gbl_result)
  253. return 1;
  254. else
  255. return 0;
  256. }
  257. int setup_tests(void)
  258. {
  259. ADD_TEST(test_exdata);
  260. return 1;
  261. }