enginetest.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright 2000-2016 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 <openssl/e_os2.h>
  12. #ifdef OPENSSL_NO_ENGINE
  13. int main(int argc, char *argv[])
  14. {
  15. printf("No ENGINE support\n");
  16. return (0);
  17. }
  18. #else
  19. # include <openssl/buffer.h>
  20. # include <openssl/crypto.h>
  21. # include <openssl/engine.h>
  22. # include <openssl/err.h>
  23. # include "testutil.h"
  24. static void display_engine_list(void)
  25. {
  26. ENGINE *h;
  27. int loop;
  28. loop = 0;
  29. for (h = ENGINE_get_first(); h != NULL; h = ENGINE_get_next(h)) {
  30. TEST_info("#%d: id = \"%s\", name = \"%s\"",
  31. loop++, ENGINE_get_id(h), ENGINE_get_name(h));
  32. }
  33. /*
  34. * ENGINE_get_first() increases the struct_ref counter, so we must call
  35. * ENGINE_free() to decrease it again
  36. */
  37. ENGINE_free(h);
  38. }
  39. #define NUMTOADD 512
  40. static int test_engines(void)
  41. {
  42. ENGINE *block[NUMTOADD];
  43. char buf[256];
  44. const char *id, *name;
  45. ENGINE *ptr;
  46. int loop;
  47. int to_return = 0;
  48. ENGINE *new_h1 = NULL;
  49. ENGINE *new_h2 = NULL;
  50. ENGINE *new_h3 = NULL;
  51. ENGINE *new_h4 = NULL;
  52. memset(block, 0, sizeof(block));
  53. if (!TEST_ptr(new_h1 = ENGINE_new())
  54. || !TEST_true(ENGINE_set_id(new_h1, "test_id0"))
  55. || !TEST_true(ENGINE_set_name(new_h1, "First test item"))
  56. || !TEST_ptr(new_h2 = ENGINE_new())
  57. || !TEST_true(ENGINE_set_id(new_h2, "test_id1"))
  58. || !TEST_true(ENGINE_set_name(new_h2, "Second test item"))
  59. || !TEST_ptr(new_h3 = ENGINE_new())
  60. || !TEST_true(ENGINE_set_id(new_h3, "test_id2"))
  61. || !TEST_true(ENGINE_set_name(new_h3, "Third test item"))
  62. || !TEST_ptr(new_h4 = ENGINE_new())
  63. || !TEST_true(ENGINE_set_id(new_h4, "test_id3"))
  64. || !TEST_true(ENGINE_set_name(new_h4, "Fourth test item")))
  65. goto end;
  66. TEST_info("Engines:");
  67. display_engine_list();
  68. if (!TEST_true(ENGINE_add(new_h1)))
  69. goto end;
  70. TEST_info("Engines:");
  71. display_engine_list();
  72. ptr = ENGINE_get_first();
  73. if (!TEST_true(ENGINE_remove(ptr)))
  74. goto end;
  75. ENGINE_free(ptr);
  76. TEST_info("Engines:");
  77. display_engine_list();
  78. if (!TEST_true(ENGINE_add(new_h3))
  79. || !TEST_true(ENGINE_add(new_h2)))
  80. goto end;
  81. TEST_info("Engines:");
  82. display_engine_list();
  83. if (!TEST_true(ENGINE_remove(new_h2)))
  84. goto end;
  85. TEST_info("Engines:");
  86. display_engine_list();
  87. if (!TEST_true(ENGINE_add(new_h4)))
  88. goto end;
  89. TEST_info("Engines:");
  90. display_engine_list();
  91. /* Should fail. */
  92. if (!TEST_false(ENGINE_add(new_h3)))
  93. goto end;
  94. ERR_clear_error();
  95. /* Should fail. */
  96. if (!TEST_false(ENGINE_remove(new_h2)))
  97. goto end;
  98. ERR_clear_error();
  99. if (!TEST_true(ENGINE_remove(new_h3)))
  100. goto end;
  101. TEST_info("Engines:");
  102. display_engine_list();
  103. if (!TEST_true(ENGINE_remove(new_h4)))
  104. goto end;
  105. TEST_info("Engines:");
  106. display_engine_list();
  107. /*
  108. * Depending on whether there's any hardware support compiled in, this
  109. * remove may be destined to fail.
  110. */
  111. if ((ptr = ENGINE_get_first()) != NULL) {
  112. if (!ENGINE_remove(ptr))
  113. TEST_info("Remove failed - probably no hardware support present");
  114. }
  115. ENGINE_free(ptr);
  116. TEST_info("Engines:");
  117. display_engine_list();
  118. if (!TEST_true(ENGINE_add(new_h1))
  119. || !TEST_true(ENGINE_remove(new_h1)))
  120. goto end;
  121. TEST_info("About to beef up the engine-type list");
  122. for (loop = 0; loop < NUMTOADD; loop++) {
  123. sprintf(buf, "id%d", loop);
  124. id = OPENSSL_strdup(buf);
  125. sprintf(buf, "Fake engine type %d", loop);
  126. name = OPENSSL_strdup(buf);
  127. if (!TEST_ptr(block[loop] = ENGINE_new())
  128. || !TEST_true(ENGINE_set_id(block[loop], id))
  129. || !TEST_true(ENGINE_set_name(block[loop], name)))
  130. goto end;
  131. }
  132. for (loop = 0; loop < NUMTOADD; loop++) {
  133. if (!TEST_true(ENGINE_add(block[loop]))) {
  134. printf("Adding stopped at %d, (%s,%s)",
  135. loop, ENGINE_get_id(block[loop]),
  136. ENGINE_get_name(block[loop]));
  137. goto cleanup_loop;
  138. }
  139. }
  140. cleanup_loop:
  141. TEST_info("About to empty the engine-type list");
  142. while ((ptr = ENGINE_get_first()) != NULL) {
  143. if (!TEST_true(ENGINE_remove(ptr)))
  144. goto end;
  145. ENGINE_free(ptr);
  146. }
  147. for (loop = 0; loop < NUMTOADD; loop++) {
  148. OPENSSL_free((void *)ENGINE_get_id(block[loop]));
  149. OPENSSL_free((void *)ENGINE_get_name(block[loop]));
  150. }
  151. to_return = 1;
  152. end:
  153. ENGINE_free(new_h1);
  154. ENGINE_free(new_h2);
  155. ENGINE_free(new_h3);
  156. ENGINE_free(new_h4);
  157. for (loop = 0; loop < NUMTOADD; loop++)
  158. ENGINE_free(block[loop]);
  159. return to_return;
  160. }
  161. void register_tests(void)
  162. {
  163. ADD_TEST(test_engines);
  164. }
  165. #endif