exdatatest.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  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 gbl_result;
  19. /*
  20. * SIMPLE EX_DATA IMPLEMENTATION
  21. * Apps explicitly set/get ex_data as needed
  22. */
  23. static void exnew(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
  24. int idx, long argl, void *argp)
  25. {
  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))
  30. gbl_result = 0;
  31. }
  32. static int exdup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
  33. void *from_d, int idx, long argl, void *argp)
  34. {
  35. if (!TEST_int_eq(idx, saved_idx)
  36. || !TEST_long_eq(argl, saved_argl)
  37. || !TEST_ptr_eq(argp, saved_argp)
  38. || !TEST_ptr(from_d))
  39. gbl_result = 0;
  40. return 1;
  41. }
  42. static void exfree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
  43. int idx, long argl, void *argp)
  44. {
  45. if (!TEST_int_eq(idx, saved_idx)
  46. || !TEST_long_eq(argl, saved_argl)
  47. || !TEST_ptr_eq(argp, saved_argp))
  48. gbl_result = 0;
  49. }
  50. /*
  51. * PRE-ALLOCATED EX_DATA IMPLEMENTATION
  52. * Extended data structure is allocated in exnew2/freed in exfree2
  53. * Data is stored inside extended data structure
  54. */
  55. typedef struct myobj_ex_data_st {
  56. char *hello;
  57. int new;
  58. int dup;
  59. } MYOBJ_EX_DATA;
  60. static void exnew2(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
  61. int idx, long argl, void *argp)
  62. {
  63. MYOBJ_EX_DATA *ex_data = OPENSSL_zalloc(sizeof(*ex_data));
  64. if (!TEST_int_eq(idx, saved_idx2)
  65. || !TEST_long_eq(argl, saved_argl)
  66. || !TEST_ptr_eq(argp, saved_argp)
  67. || !TEST_ptr_null(ptr)
  68. || !TEST_ptr(ex_data)
  69. || !TEST_true(CRYPTO_set_ex_data(ad, saved_idx2, ex_data))) {
  70. gbl_result = 0;
  71. OPENSSL_free(ex_data);
  72. } else {
  73. ex_data->new = 1;
  74. }
  75. }
  76. static int exdup2(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
  77. void *from_d, int idx, long argl, void *argp)
  78. {
  79. MYOBJ_EX_DATA **update_ex_data = (MYOBJ_EX_DATA**)from_d;
  80. MYOBJ_EX_DATA *ex_data = CRYPTO_get_ex_data(to, saved_idx2);
  81. if (!TEST_int_eq(idx, saved_idx2)
  82. || !TEST_long_eq(argl, saved_argl)
  83. || !TEST_ptr_eq(argp, saved_argp)
  84. || !TEST_ptr(from_d)
  85. || !TEST_ptr(*update_ex_data)
  86. || !TEST_ptr(ex_data)
  87. || !TEST_true(ex_data->new)) {
  88. gbl_result = 0;
  89. } else {
  90. /* Copy hello over */
  91. ex_data->hello = (*update_ex_data)->hello;
  92. /* indicate this is a dup */
  93. ex_data->dup = 1;
  94. /* Keep my original ex_data */
  95. *update_ex_data = ex_data;
  96. }
  97. return 1;
  98. }
  99. static void exfree2(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
  100. int idx, long argl, void *argp)
  101. {
  102. MYOBJ_EX_DATA *ex_data = CRYPTO_get_ex_data(ad, saved_idx2);
  103. OPENSSL_free(ex_data);
  104. if (!TEST_int_eq(idx, saved_idx2)
  105. || !TEST_long_eq(argl, saved_argl)
  106. || !TEST_ptr_eq(argp, saved_argp)
  107. || !TEST_ptr(ex_data)
  108. || !TEST_true(CRYPTO_set_ex_data(ad, saved_idx2, NULL)))
  109. gbl_result = 0;
  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_free(MYOBJ *obj)
  151. {
  152. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
  153. OPENSSL_free(obj);
  154. }
  155. static MYOBJ *MYOBJ_dup(MYOBJ *in)
  156. {
  157. MYOBJ *obj = MYOBJ_new();
  158. obj->st |= CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_APP, &obj->ex_data,
  159. &in->ex_data);
  160. return obj;
  161. }
  162. static int test_exdata(void)
  163. {
  164. MYOBJ *t1, *t2, *t3;
  165. MYOBJ_EX_DATA *ex_data;
  166. const char *cp;
  167. char *p;
  168. gbl_result = 1;
  169. p = OPENSSL_strdup("hello world");
  170. saved_argl = 21;
  171. saved_argp = OPENSSL_malloc(1);
  172. saved_idx = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
  173. saved_argl, saved_argp,
  174. exnew, exdup, exfree);
  175. saved_idx2 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
  176. saved_argl, saved_argp,
  177. exnew2, exdup2, exfree2);
  178. t1 = MYOBJ_new();
  179. t2 = MYOBJ_new();
  180. if (!TEST_int_eq(t1->st, 1) || !TEST_int_eq(t2->st, 1))
  181. return 0;
  182. if (!TEST_ptr(CRYPTO_get_ex_data(&t1->ex_data, saved_idx2)))
  183. return 0;
  184. if (!TEST_ptr(CRYPTO_get_ex_data(&t2->ex_data, saved_idx2)))
  185. return 0;
  186. MYOBJ_sethello(t1, p);
  187. cp = MYOBJ_gethello(t1);
  188. if (!TEST_ptr_eq(cp, p))
  189. return 0;
  190. MYOBJ_sethello2(t1, p);
  191. cp = MYOBJ_gethello2(t1);
  192. if (!TEST_ptr_eq(cp, p))
  193. return 0;
  194. cp = MYOBJ_gethello(t2);
  195. if (!TEST_ptr_null(cp))
  196. return 0;
  197. cp = MYOBJ_gethello2(t2);
  198. if (!TEST_ptr_null(cp))
  199. return 0;
  200. t3 = MYOBJ_dup(t1);
  201. if (!TEST_int_eq(t3->st, 1))
  202. return 0;
  203. ex_data = CRYPTO_get_ex_data(&t3->ex_data, saved_idx2);
  204. if (!TEST_ptr(ex_data))
  205. return 0;
  206. if (!TEST_int_eq(ex_data->dup, 1))
  207. return 0;
  208. cp = MYOBJ_gethello(t3);
  209. if (!TEST_ptr_eq(cp, p))
  210. return 0;
  211. cp = MYOBJ_gethello2(t3);
  212. if (!TEST_ptr_eq(cp, p))
  213. return 0;
  214. MYOBJ_free(t1);
  215. MYOBJ_free(t2);
  216. MYOBJ_free(t3);
  217. OPENSSL_free(saved_argp);
  218. OPENSSL_free(p);
  219. if (gbl_result)
  220. return 1;
  221. else
  222. return 0;
  223. }
  224. int setup_tests(void)
  225. {
  226. ADD_TEST(test_exdata);
  227. return 1;
  228. }