exdatatest.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright 2015-2017 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 gbl_result;
  18. static void exnew(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
  19. int idx, long argl, void *argp)
  20. {
  21. if (!TEST_int_eq(idx, saved_idx)
  22. || !TEST_long_eq(argl, saved_argl)
  23. || !TEST_ptr_eq(argp, saved_argp)
  24. || !TEST_ptr_null(ptr))
  25. gbl_result = 0;
  26. }
  27. static int exdup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
  28. void *from_d, int idx, long argl, void *argp)
  29. {
  30. if (!TEST_int_eq(idx, saved_idx)
  31. || !TEST_long_eq(argl, saved_argl)
  32. || !TEST_ptr_eq(argp, saved_argp)
  33. || !TEST_ptr(from_d))
  34. gbl_result = 0;
  35. return 1;
  36. }
  37. static void exfree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
  38. int idx, long argl, void *argp)
  39. {
  40. if (!TEST_int_eq(idx, saved_idx)
  41. || !TEST_long_eq(argl, saved_argl)
  42. || !TEST_ptr_eq(argp, saved_argp))
  43. gbl_result = 0;
  44. }
  45. typedef struct myobj_st {
  46. CRYPTO_EX_DATA ex_data;
  47. int id;
  48. int st;
  49. } MYOBJ;
  50. static MYOBJ *MYOBJ_new()
  51. {
  52. static int count = 0;
  53. MYOBJ *obj = OPENSSL_malloc(sizeof(*obj));
  54. obj->id = ++count;
  55. obj->st = CRYPTO_new_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
  56. return obj;
  57. }
  58. static void MYOBJ_sethello(MYOBJ *obj, char *cp)
  59. {
  60. obj->st = CRYPTO_set_ex_data(&obj->ex_data, saved_idx, cp);
  61. if (!TEST_int_eq(obj->st, 1))
  62. gbl_result = 0;
  63. }
  64. static char *MYOBJ_gethello(MYOBJ *obj)
  65. {
  66. return CRYPTO_get_ex_data(&obj->ex_data, saved_idx);
  67. }
  68. static void MYOBJ_free(MYOBJ *obj)
  69. {
  70. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
  71. OPENSSL_free(obj);
  72. }
  73. static MYOBJ *MYOBJ_dup(MYOBJ *in)
  74. {
  75. MYOBJ *obj = MYOBJ_new();
  76. obj->st |= CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_APP, &obj->ex_data,
  77. &in->ex_data);
  78. return obj;
  79. }
  80. static int test_exdata(void)
  81. {
  82. MYOBJ *t1, *t2, *t3;
  83. const char *cp;
  84. char *p;
  85. gbl_result = 1;
  86. p = strdup("hello world");
  87. saved_argl = 21;
  88. saved_argp = malloc(1);
  89. saved_idx = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
  90. saved_argl, saved_argp,
  91. exnew, exdup, exfree);
  92. t1 = MYOBJ_new();
  93. t2 = MYOBJ_new();
  94. if (!TEST_int_eq(t1->st, 1) || !TEST_int_eq(t2->st, 1))
  95. return 0;
  96. MYOBJ_sethello(t1, p);
  97. cp = MYOBJ_gethello(t1);
  98. if (!TEST_ptr_eq(cp, p))
  99. return 0;
  100. cp = MYOBJ_gethello(t2);
  101. if (!TEST_ptr_null(cp))
  102. return 0;
  103. t3 = MYOBJ_dup(t1);
  104. if (!TEST_int_eq(t3->st, 1))
  105. return 0;
  106. cp = MYOBJ_gethello(t3);
  107. if (!TEST_ptr_eq(cp, p))
  108. return 0;
  109. MYOBJ_free(t1);
  110. MYOBJ_free(t2);
  111. MYOBJ_free(t3);
  112. free(saved_argp);
  113. free(p);
  114. if (gbl_result)
  115. return 1;
  116. else
  117. return 0;
  118. }
  119. void register_tests(void)
  120. {
  121. ADD_TEST(test_exdata);
  122. }