asynctest.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. #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(void)
  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_callback(void *arg)
  106. {
  107. printf("callback test pass\n");
  108. return 1;
  109. }
  110. static int test_ASYNC_callback_status(void)
  111. {
  112. ASYNC_WAIT_CTX *waitctx = NULL;
  113. int set_arg = 100;
  114. ASYNC_callback_fn get_callback;
  115. void *get_arg;
  116. int set_status = 1;
  117. if ( !ASYNC_init_thread(1, 0)
  118. || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
  119. || ASYNC_WAIT_CTX_set_callback(waitctx, test_callback, (void*)&set_arg)
  120. != 1
  121. || ASYNC_WAIT_CTX_get_callback(waitctx, &get_callback, &get_arg)
  122. != 1
  123. || test_callback != get_callback
  124. || get_arg != (void*)&set_arg
  125. || (*get_callback)(get_arg) != 1
  126. || ASYNC_WAIT_CTX_set_status(waitctx, set_status) != 1
  127. || set_status != ASYNC_WAIT_CTX_get_status(waitctx)) {
  128. fprintf(stderr, "test_ASYNC_callback_status() failed\n");
  129. ASYNC_WAIT_CTX_free(waitctx);
  130. ASYNC_cleanup_thread();
  131. return 0;
  132. }
  133. ASYNC_WAIT_CTX_free(waitctx);
  134. ASYNC_cleanup_thread();
  135. return 1;
  136. }
  137. static int test_ASYNC_start_job(void)
  138. {
  139. ASYNC_JOB *job = NULL;
  140. int funcret;
  141. ASYNC_WAIT_CTX *waitctx = NULL;
  142. ctr = 0;
  143. if ( !ASYNC_init_thread(1, 0)
  144. || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
  145. || ASYNC_start_job(&job, waitctx, &funcret, add_two, NULL, 0)
  146. != ASYNC_PAUSE
  147. || ctr != 1
  148. || ASYNC_start_job(&job, waitctx, &funcret, add_two, NULL, 0)
  149. != ASYNC_FINISH
  150. || ctr != 2
  151. || funcret != 2) {
  152. fprintf(stderr, "test_ASYNC_start_job() failed\n");
  153. ASYNC_WAIT_CTX_free(waitctx);
  154. ASYNC_cleanup_thread();
  155. return 0;
  156. }
  157. ASYNC_WAIT_CTX_free(waitctx);
  158. ASYNC_cleanup_thread();
  159. return 1;
  160. }
  161. static int test_ASYNC_get_current_job(void)
  162. {
  163. ASYNC_JOB *job = NULL;
  164. int funcret;
  165. ASYNC_WAIT_CTX *waitctx = NULL;
  166. currjob = NULL;
  167. if ( !ASYNC_init_thread(1, 0)
  168. || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
  169. || ASYNC_start_job(&job, waitctx, &funcret, save_current, NULL, 0)
  170. != ASYNC_PAUSE
  171. || currjob != job
  172. || ASYNC_start_job(&job, waitctx, &funcret, save_current, NULL, 0)
  173. != ASYNC_FINISH
  174. || funcret != 1) {
  175. fprintf(stderr, "test_ASYNC_get_current_job() failed\n");
  176. ASYNC_WAIT_CTX_free(waitctx);
  177. ASYNC_cleanup_thread();
  178. return 0;
  179. }
  180. ASYNC_WAIT_CTX_free(waitctx);
  181. ASYNC_cleanup_thread();
  182. return 1;
  183. }
  184. static int test_ASYNC_WAIT_CTX_get_all_fds(void)
  185. {
  186. ASYNC_JOB *job = NULL;
  187. int funcret;
  188. ASYNC_WAIT_CTX *waitctx = NULL;
  189. OSSL_ASYNC_FD fd = OSSL_BAD_ASYNC_FD, delfd = OSSL_BAD_ASYNC_FD;
  190. size_t numfds, numdelfds;
  191. if ( !ASYNC_init_thread(1, 0)
  192. || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
  193. /* On first run we're not expecting any wait fds */
  194. || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
  195. != ASYNC_PAUSE
  196. || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
  197. || numfds != 0
  198. || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
  199. &numdelfds)
  200. || numfds != 0
  201. || numdelfds != 0
  202. /* On second run we're expecting one added fd */
  203. || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
  204. != ASYNC_PAUSE
  205. || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
  206. || numfds != 1
  207. || !ASYNC_WAIT_CTX_get_all_fds(waitctx, &fd, &numfds)
  208. || fd != MAGIC_WAIT_FD
  209. || (fd = OSSL_BAD_ASYNC_FD, 0) /* Assign to something else */
  210. || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
  211. &numdelfds)
  212. || numfds != 1
  213. || numdelfds != 0
  214. || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, &fd, &numfds, NULL,
  215. &numdelfds)
  216. || fd != MAGIC_WAIT_FD
  217. /* On third run we expect one deleted fd */
  218. || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
  219. != ASYNC_PAUSE
  220. || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
  221. || numfds != 0
  222. || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
  223. &numdelfds)
  224. || numfds != 0
  225. || numdelfds != 1
  226. || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, &delfd,
  227. &numdelfds)
  228. || delfd != MAGIC_WAIT_FD
  229. /* On last run we are not expecting any wait fd */
  230. || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
  231. != ASYNC_FINISH
  232. || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
  233. || numfds != 0
  234. || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
  235. &numdelfds)
  236. || numfds != 0
  237. || numdelfds != 0
  238. || funcret != 1) {
  239. fprintf(stderr, "test_ASYNC_get_wait_fd() failed\n");
  240. ASYNC_WAIT_CTX_free(waitctx);
  241. ASYNC_cleanup_thread();
  242. return 0;
  243. }
  244. ASYNC_WAIT_CTX_free(waitctx);
  245. ASYNC_cleanup_thread();
  246. return 1;
  247. }
  248. static int test_ASYNC_block_pause(void)
  249. {
  250. ASYNC_JOB *job = NULL;
  251. int funcret;
  252. ASYNC_WAIT_CTX *waitctx = NULL;
  253. if ( !ASYNC_init_thread(1, 0)
  254. || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
  255. || ASYNC_start_job(&job, waitctx, &funcret, blockpause, NULL, 0)
  256. != ASYNC_PAUSE
  257. || ASYNC_start_job(&job, waitctx, &funcret, blockpause, NULL, 0)
  258. != ASYNC_FINISH
  259. || funcret != 1) {
  260. fprintf(stderr, "test_ASYNC_block_pause() failed\n");
  261. ASYNC_WAIT_CTX_free(waitctx);
  262. ASYNC_cleanup_thread();
  263. return 0;
  264. }
  265. ASYNC_WAIT_CTX_free(waitctx);
  266. ASYNC_cleanup_thread();
  267. return 1;
  268. }
  269. int main(int argc, char **argv)
  270. {
  271. if (!ASYNC_is_capable()) {
  272. fprintf(stderr,
  273. "OpenSSL build is not ASYNC capable - skipping async tests\n");
  274. } else {
  275. CRYPTO_set_mem_debug(1);
  276. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  277. if ( !test_ASYNC_init_thread()
  278. || !test_ASYNC_callback_status()
  279. || !test_ASYNC_start_job()
  280. || !test_ASYNC_get_current_job()
  281. || !test_ASYNC_WAIT_CTX_get_all_fds()
  282. || !test_ASYNC_block_pause()) {
  283. return 1;
  284. }
  285. }
  286. printf("PASS\n");
  287. return 0;
  288. }