asynctest.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright 2015-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. #ifdef _WIN32
  10. # include <windows.h>
  11. #endif
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <openssl/async.h>
  15. #include <openssl/crypto.h>
  16. static int ctr = 0;
  17. static ASYNC_JOB *currjob = NULL;
  18. static int only_pause(void *args)
  19. {
  20. ASYNC_pause_job();
  21. return 1;
  22. }
  23. static int add_two(void *args)
  24. {
  25. ctr++;
  26. ASYNC_pause_job();
  27. ctr++;
  28. return 2;
  29. }
  30. static int save_current(void *args)
  31. {
  32. currjob = ASYNC_get_current_job();
  33. ASYNC_pause_job();
  34. return 1;
  35. }
  36. #define MAGIC_WAIT_FD ((OSSL_ASYNC_FD)99)
  37. static int waitfd(void *args)
  38. {
  39. ASYNC_JOB *job;
  40. ASYNC_WAIT_CTX *waitctx;
  41. job = ASYNC_get_current_job();
  42. if (job == NULL)
  43. return 0;
  44. waitctx = ASYNC_get_wait_ctx(job);
  45. if (waitctx == NULL)
  46. return 0;
  47. /* First case: no fd added or removed */
  48. ASYNC_pause_job();
  49. /* Second case: one fd added */
  50. if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, waitctx, MAGIC_WAIT_FD, NULL, NULL))
  51. return 0;
  52. ASYNC_pause_job();
  53. /* Third case: all fd removed */
  54. if (!ASYNC_WAIT_CTX_clear_fd(waitctx, waitctx))
  55. return 0;
  56. ASYNC_pause_job();
  57. /* Last case: fd added and immediately removed */
  58. if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, waitctx, MAGIC_WAIT_FD, NULL, NULL))
  59. return 0;
  60. if (!ASYNC_WAIT_CTX_clear_fd(waitctx, waitctx))
  61. return 0;
  62. return 1;
  63. }
  64. static int blockpause(void *args)
  65. {
  66. ASYNC_block_pause();
  67. ASYNC_pause_job();
  68. ASYNC_unblock_pause();
  69. ASYNC_pause_job();
  70. return 1;
  71. }
  72. static int test_ASYNC_init_thread()
  73. {
  74. ASYNC_JOB *job1 = NULL, *job2 = NULL, *job3 = NULL;
  75. int funcret1, funcret2, funcret3;
  76. ASYNC_WAIT_CTX *waitctx = NULL;
  77. if ( !ASYNC_init_thread(2, 0)
  78. || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
  79. || ASYNC_start_job(&job1, waitctx, &funcret1, only_pause, NULL, 0)
  80. != ASYNC_PAUSE
  81. || ASYNC_start_job(&job2, waitctx, &funcret2, only_pause, NULL, 0)
  82. != ASYNC_PAUSE
  83. || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0)
  84. != ASYNC_NO_JOBS
  85. || ASYNC_start_job(&job1, waitctx, &funcret1, only_pause, NULL, 0)
  86. != ASYNC_FINISH
  87. || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0)
  88. != ASYNC_PAUSE
  89. || ASYNC_start_job(&job2, waitctx, &funcret2, only_pause, NULL, 0)
  90. != ASYNC_FINISH
  91. || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0)
  92. != ASYNC_FINISH
  93. || funcret1 != 1
  94. || funcret2 != 1
  95. || funcret3 != 1) {
  96. fprintf(stderr, "test_ASYNC_init_thread() failed\n");
  97. ASYNC_WAIT_CTX_free(waitctx);
  98. ASYNC_cleanup_thread();
  99. return 0;
  100. }
  101. ASYNC_WAIT_CTX_free(waitctx);
  102. ASYNC_cleanup_thread();
  103. return 1;
  104. }
  105. static int test_ASYNC_start_job()
  106. {
  107. ASYNC_JOB *job = NULL;
  108. int funcret;
  109. ASYNC_WAIT_CTX *waitctx = NULL;
  110. ctr = 0;
  111. if ( !ASYNC_init_thread(1, 0)
  112. || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
  113. || ASYNC_start_job(&job, waitctx, &funcret, add_two, NULL, 0)
  114. != ASYNC_PAUSE
  115. || ctr != 1
  116. || ASYNC_start_job(&job, waitctx, &funcret, add_two, NULL, 0)
  117. != ASYNC_FINISH
  118. || ctr != 2
  119. || funcret != 2) {
  120. fprintf(stderr, "test_ASYNC_start_job() failed\n");
  121. ASYNC_WAIT_CTX_free(waitctx);
  122. ASYNC_cleanup_thread();
  123. return 0;
  124. }
  125. ASYNC_WAIT_CTX_free(waitctx);
  126. ASYNC_cleanup_thread();
  127. return 1;
  128. }
  129. static int test_ASYNC_get_current_job()
  130. {
  131. ASYNC_JOB *job = NULL;
  132. int funcret;
  133. ASYNC_WAIT_CTX *waitctx = NULL;
  134. currjob = NULL;
  135. if ( !ASYNC_init_thread(1, 0)
  136. || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
  137. || ASYNC_start_job(&job, waitctx, &funcret, save_current, NULL, 0)
  138. != ASYNC_PAUSE
  139. || currjob != job
  140. || ASYNC_start_job(&job, waitctx, &funcret, save_current, NULL, 0)
  141. != ASYNC_FINISH
  142. || funcret != 1) {
  143. fprintf(stderr, "test_ASYNC_get_current_job() failed\n");
  144. ASYNC_WAIT_CTX_free(waitctx);
  145. ASYNC_cleanup_thread();
  146. return 0;
  147. }
  148. ASYNC_WAIT_CTX_free(waitctx);
  149. ASYNC_cleanup_thread();
  150. return 1;
  151. }
  152. static int test_ASYNC_WAIT_CTX_get_all_fds()
  153. {
  154. ASYNC_JOB *job = NULL;
  155. int funcret;
  156. ASYNC_WAIT_CTX *waitctx = NULL;
  157. OSSL_ASYNC_FD fd = OSSL_BAD_ASYNC_FD, delfd = OSSL_BAD_ASYNC_FD;
  158. size_t numfds, numdelfds;
  159. if ( !ASYNC_init_thread(1, 0)
  160. || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
  161. /* On first run we're not expecting any wait fds */
  162. || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
  163. != ASYNC_PAUSE
  164. || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
  165. || numfds != 0
  166. || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
  167. &numdelfds)
  168. || numfds != 0
  169. || numdelfds != 0
  170. /* On second run we're expecting one added fd */
  171. || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
  172. != ASYNC_PAUSE
  173. || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
  174. || numfds != 1
  175. || !ASYNC_WAIT_CTX_get_all_fds(waitctx, &fd, &numfds)
  176. || fd != MAGIC_WAIT_FD
  177. || (fd = OSSL_BAD_ASYNC_FD, 0) /* Assign to something else */
  178. || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
  179. &numdelfds)
  180. || numfds != 1
  181. || numdelfds != 0
  182. || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, &fd, &numfds, NULL,
  183. &numdelfds)
  184. || fd != MAGIC_WAIT_FD
  185. /* On third run we expect one deleted fd */
  186. || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
  187. != ASYNC_PAUSE
  188. || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
  189. || numfds != 0
  190. || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
  191. &numdelfds)
  192. || numfds != 0
  193. || numdelfds != 1
  194. || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, &delfd,
  195. &numdelfds)
  196. || delfd != MAGIC_WAIT_FD
  197. /* On last run we are not expecting any wait fd */
  198. || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
  199. != ASYNC_FINISH
  200. || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
  201. || numfds != 0
  202. || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
  203. &numdelfds)
  204. || numfds != 0
  205. || numdelfds != 0
  206. || funcret != 1) {
  207. fprintf(stderr, "test_ASYNC_get_wait_fd() failed\n");
  208. ASYNC_WAIT_CTX_free(waitctx);
  209. ASYNC_cleanup_thread();
  210. return 0;
  211. }
  212. ASYNC_WAIT_CTX_free(waitctx);
  213. ASYNC_cleanup_thread();
  214. return 1;
  215. }
  216. static int test_ASYNC_block_pause()
  217. {
  218. ASYNC_JOB *job = NULL;
  219. int funcret;
  220. ASYNC_WAIT_CTX *waitctx = NULL;
  221. if ( !ASYNC_init_thread(1, 0)
  222. || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
  223. || ASYNC_start_job(&job, waitctx, &funcret, blockpause, NULL, 0)
  224. != ASYNC_PAUSE
  225. || ASYNC_start_job(&job, waitctx, &funcret, blockpause, NULL, 0)
  226. != ASYNC_FINISH
  227. || funcret != 1) {
  228. fprintf(stderr, "test_ASYNC_block_pause() failed\n");
  229. ASYNC_WAIT_CTX_free(waitctx);
  230. ASYNC_cleanup_thread();
  231. return 0;
  232. }
  233. ASYNC_WAIT_CTX_free(waitctx);
  234. ASYNC_cleanup_thread();
  235. return 1;
  236. }
  237. int main(int argc, char **argv)
  238. {
  239. if (!ASYNC_is_capable()) {
  240. fprintf(stderr,
  241. "OpenSSL build is not ASYNC capable - skipping async tests\n");
  242. } else {
  243. CRYPTO_set_mem_debug(1);
  244. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  245. if ( !test_ASYNC_init_thread()
  246. || !test_ASYNC_start_job()
  247. || !test_ASYNC_get_current_job()
  248. || !test_ASYNC_WAIT_CTX_get_all_fds()
  249. || !test_ASYNC_block_pause()) {
  250. return 1;
  251. }
  252. }
  253. printf("PASS\n");
  254. return 0;
  255. }