asynctest.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * Copyright 2015-2020 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. static int change_deflt_libctx(void *args)
  37. {
  38. OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
  39. OSSL_LIB_CTX *oldctx, *tmpctx;
  40. int ret = 0;
  41. if (libctx == NULL)
  42. return 0;
  43. oldctx = OSSL_LIB_CTX_set0_default(libctx);
  44. ASYNC_pause_job();
  45. /* Check the libctx is set up as we expect */
  46. tmpctx = OSSL_LIB_CTX_set0_default(oldctx);
  47. if (tmpctx != libctx)
  48. goto err;
  49. /* Set it back again to continue to use our own libctx */
  50. oldctx = OSSL_LIB_CTX_set0_default(libctx);
  51. ASYNC_pause_job();
  52. /* Check the libctx is set up as we expect */
  53. tmpctx = OSSL_LIB_CTX_set0_default(oldctx);
  54. if (tmpctx != libctx)
  55. goto err;
  56. ret = 1;
  57. err:
  58. OSSL_LIB_CTX_free(libctx);
  59. return ret;
  60. }
  61. #define MAGIC_WAIT_FD ((OSSL_ASYNC_FD)99)
  62. static int waitfd(void *args)
  63. {
  64. ASYNC_JOB *job;
  65. ASYNC_WAIT_CTX *waitctx;
  66. job = ASYNC_get_current_job();
  67. if (job == NULL)
  68. return 0;
  69. waitctx = ASYNC_get_wait_ctx(job);
  70. if (waitctx == NULL)
  71. return 0;
  72. /* First case: no fd added or removed */
  73. ASYNC_pause_job();
  74. /* Second case: one fd added */
  75. if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, waitctx, MAGIC_WAIT_FD, NULL, NULL))
  76. return 0;
  77. ASYNC_pause_job();
  78. /* Third case: all fd removed */
  79. if (!ASYNC_WAIT_CTX_clear_fd(waitctx, waitctx))
  80. return 0;
  81. ASYNC_pause_job();
  82. /* Last case: fd added and immediately removed */
  83. if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, waitctx, MAGIC_WAIT_FD, NULL, NULL))
  84. return 0;
  85. if (!ASYNC_WAIT_CTX_clear_fd(waitctx, waitctx))
  86. return 0;
  87. return 1;
  88. }
  89. static int blockpause(void *args)
  90. {
  91. ASYNC_block_pause();
  92. ASYNC_pause_job();
  93. ASYNC_unblock_pause();
  94. ASYNC_pause_job();
  95. return 1;
  96. }
  97. static int test_ASYNC_init_thread(void)
  98. {
  99. ASYNC_JOB *job1 = NULL, *job2 = NULL, *job3 = NULL;
  100. int funcret1, funcret2, funcret3;
  101. ASYNC_WAIT_CTX *waitctx = NULL;
  102. if ( !ASYNC_init_thread(2, 0)
  103. || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
  104. || ASYNC_start_job(&job1, waitctx, &funcret1, only_pause, NULL, 0)
  105. != ASYNC_PAUSE
  106. || ASYNC_start_job(&job2, waitctx, &funcret2, only_pause, NULL, 0)
  107. != ASYNC_PAUSE
  108. || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0)
  109. != ASYNC_NO_JOBS
  110. || ASYNC_start_job(&job1, waitctx, &funcret1, only_pause, NULL, 0)
  111. != ASYNC_FINISH
  112. || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0)
  113. != ASYNC_PAUSE
  114. || ASYNC_start_job(&job2, waitctx, &funcret2, only_pause, NULL, 0)
  115. != ASYNC_FINISH
  116. || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0)
  117. != ASYNC_FINISH
  118. || funcret1 != 1
  119. || funcret2 != 1
  120. || funcret3 != 1) {
  121. fprintf(stderr, "test_ASYNC_init_thread() failed\n");
  122. ASYNC_WAIT_CTX_free(waitctx);
  123. ASYNC_cleanup_thread();
  124. return 0;
  125. }
  126. ASYNC_WAIT_CTX_free(waitctx);
  127. ASYNC_cleanup_thread();
  128. return 1;
  129. }
  130. static int test_callback(void *arg)
  131. {
  132. printf("callback test pass\n");
  133. return 1;
  134. }
  135. static int test_ASYNC_callback_status(void)
  136. {
  137. ASYNC_WAIT_CTX *waitctx = NULL;
  138. int set_arg = 100;
  139. ASYNC_callback_fn get_callback;
  140. void *get_arg;
  141. int set_status = 1;
  142. if ( !ASYNC_init_thread(1, 0)
  143. || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
  144. || ASYNC_WAIT_CTX_set_callback(waitctx, test_callback, (void*)&set_arg)
  145. != 1
  146. || ASYNC_WAIT_CTX_get_callback(waitctx, &get_callback, &get_arg)
  147. != 1
  148. || test_callback != get_callback
  149. || get_arg != (void*)&set_arg
  150. || (*get_callback)(get_arg) != 1
  151. || ASYNC_WAIT_CTX_set_status(waitctx, set_status) != 1
  152. || set_status != ASYNC_WAIT_CTX_get_status(waitctx)) {
  153. fprintf(stderr, "test_ASYNC_callback_status() failed\n");
  154. ASYNC_WAIT_CTX_free(waitctx);
  155. ASYNC_cleanup_thread();
  156. return 0;
  157. }
  158. ASYNC_WAIT_CTX_free(waitctx);
  159. ASYNC_cleanup_thread();
  160. return 1;
  161. }
  162. static int test_ASYNC_start_job(void)
  163. {
  164. ASYNC_JOB *job = NULL;
  165. int funcret;
  166. ASYNC_WAIT_CTX *waitctx = NULL;
  167. ctr = 0;
  168. if ( !ASYNC_init_thread(1, 0)
  169. || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
  170. || ASYNC_start_job(&job, waitctx, &funcret, add_two, NULL, 0)
  171. != ASYNC_PAUSE
  172. || ctr != 1
  173. || ASYNC_start_job(&job, waitctx, &funcret, add_two, NULL, 0)
  174. != ASYNC_FINISH
  175. || ctr != 2
  176. || funcret != 2) {
  177. fprintf(stderr, "test_ASYNC_start_job() failed\n");
  178. ASYNC_WAIT_CTX_free(waitctx);
  179. ASYNC_cleanup_thread();
  180. return 0;
  181. }
  182. ASYNC_WAIT_CTX_free(waitctx);
  183. ASYNC_cleanup_thread();
  184. return 1;
  185. }
  186. static int test_ASYNC_get_current_job(void)
  187. {
  188. ASYNC_JOB *job = NULL;
  189. int funcret;
  190. ASYNC_WAIT_CTX *waitctx = NULL;
  191. currjob = NULL;
  192. if ( !ASYNC_init_thread(1, 0)
  193. || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
  194. || ASYNC_start_job(&job, waitctx, &funcret, save_current, NULL, 0)
  195. != ASYNC_PAUSE
  196. || currjob != job
  197. || ASYNC_start_job(&job, waitctx, &funcret, save_current, NULL, 0)
  198. != ASYNC_FINISH
  199. || funcret != 1) {
  200. fprintf(stderr, "test_ASYNC_get_current_job() failed\n");
  201. ASYNC_WAIT_CTX_free(waitctx);
  202. ASYNC_cleanup_thread();
  203. return 0;
  204. }
  205. ASYNC_WAIT_CTX_free(waitctx);
  206. ASYNC_cleanup_thread();
  207. return 1;
  208. }
  209. static int test_ASYNC_WAIT_CTX_get_all_fds(void)
  210. {
  211. ASYNC_JOB *job = NULL;
  212. int funcret;
  213. ASYNC_WAIT_CTX *waitctx = NULL;
  214. OSSL_ASYNC_FD fd = OSSL_BAD_ASYNC_FD, delfd = OSSL_BAD_ASYNC_FD;
  215. size_t numfds, numdelfds;
  216. if ( !ASYNC_init_thread(1, 0)
  217. || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
  218. /* On first run we're not expecting any wait fds */
  219. || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
  220. != ASYNC_PAUSE
  221. || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
  222. || numfds != 0
  223. || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
  224. &numdelfds)
  225. || numfds != 0
  226. || numdelfds != 0
  227. /* On second run we're expecting one added fd */
  228. || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
  229. != ASYNC_PAUSE
  230. || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
  231. || numfds != 1
  232. || !ASYNC_WAIT_CTX_get_all_fds(waitctx, &fd, &numfds)
  233. || fd != MAGIC_WAIT_FD
  234. || (fd = OSSL_BAD_ASYNC_FD, 0) /* Assign to something else */
  235. || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
  236. &numdelfds)
  237. || numfds != 1
  238. || numdelfds != 0
  239. || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, &fd, &numfds, NULL,
  240. &numdelfds)
  241. || fd != MAGIC_WAIT_FD
  242. /* On third run we expect one deleted fd */
  243. || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
  244. != ASYNC_PAUSE
  245. || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
  246. || numfds != 0
  247. || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
  248. &numdelfds)
  249. || numfds != 0
  250. || numdelfds != 1
  251. || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, &delfd,
  252. &numdelfds)
  253. || delfd != MAGIC_WAIT_FD
  254. /* On last run we are not expecting any wait fd */
  255. || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
  256. != ASYNC_FINISH
  257. || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
  258. || numfds != 0
  259. || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
  260. &numdelfds)
  261. || numfds != 0
  262. || numdelfds != 0
  263. || funcret != 1) {
  264. fprintf(stderr, "test_ASYNC_get_wait_fd() failed\n");
  265. ASYNC_WAIT_CTX_free(waitctx);
  266. ASYNC_cleanup_thread();
  267. return 0;
  268. }
  269. ASYNC_WAIT_CTX_free(waitctx);
  270. ASYNC_cleanup_thread();
  271. return 1;
  272. }
  273. static int test_ASYNC_block_pause(void)
  274. {
  275. ASYNC_JOB *job = NULL;
  276. int funcret;
  277. ASYNC_WAIT_CTX *waitctx = NULL;
  278. if ( !ASYNC_init_thread(1, 0)
  279. || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
  280. || ASYNC_start_job(&job, waitctx, &funcret, blockpause, NULL, 0)
  281. != ASYNC_PAUSE
  282. || ASYNC_start_job(&job, waitctx, &funcret, blockpause, NULL, 0)
  283. != ASYNC_FINISH
  284. || funcret != 1) {
  285. fprintf(stderr, "test_ASYNC_block_pause() failed\n");
  286. ASYNC_WAIT_CTX_free(waitctx);
  287. ASYNC_cleanup_thread();
  288. return 0;
  289. }
  290. ASYNC_WAIT_CTX_free(waitctx);
  291. ASYNC_cleanup_thread();
  292. return 1;
  293. }
  294. static int test_ASYNC_start_job_ex(void)
  295. {
  296. ASYNC_JOB *job = NULL;
  297. int funcret;
  298. ASYNC_WAIT_CTX *waitctx = NULL;
  299. OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
  300. OSSL_LIB_CTX *oldctx, *tmpctx, *globalctx;
  301. int ret = 0;
  302. if (libctx == NULL) {
  303. fprintf(stderr,
  304. "test_ASYNC_start_job_ex() failed to create libctx\n");
  305. goto err;
  306. }
  307. globalctx = oldctx = OSSL_LIB_CTX_set0_default(libctx);
  308. if ((waitctx = ASYNC_WAIT_CTX_new()) == NULL
  309. || ASYNC_start_job(&job, waitctx, &funcret, change_deflt_libctx,
  310. NULL, 0)
  311. != ASYNC_PAUSE) {
  312. fprintf(stderr,
  313. "test_ASYNC_start_job_ex() failed to start job\n");
  314. goto err;
  315. }
  316. /* Reset the libctx temporarily to find out what it is*/
  317. tmpctx = OSSL_LIB_CTX_set0_default(oldctx);
  318. oldctx = OSSL_LIB_CTX_set0_default(tmpctx);
  319. if (tmpctx != libctx) {
  320. fprintf(stderr,
  321. "test_ASYNC_start_job_ex() failed - unexpected libctx\n");
  322. goto err;
  323. }
  324. if (ASYNC_start_job(&job, waitctx, &funcret, change_deflt_libctx, NULL, 0)
  325. != ASYNC_PAUSE) {
  326. fprintf(stderr,
  327. "test_ASYNC_start_job_ex() - restarting job failed\n");
  328. goto err;
  329. }
  330. /* Reset the libctx and continue with the global default libctx */
  331. tmpctx = OSSL_LIB_CTX_set0_default(oldctx);
  332. if (tmpctx != libctx) {
  333. fprintf(stderr,
  334. "test_ASYNC_start_job_ex() failed - unexpected libctx\n");
  335. goto err;
  336. }
  337. if (ASYNC_start_job(&job, waitctx, &funcret, change_deflt_libctx, NULL, 0)
  338. != ASYNC_FINISH
  339. || funcret != 1) {
  340. fprintf(stderr,
  341. "test_ASYNC_start_job_ex() - finishing job failed\n");
  342. goto err;
  343. }
  344. /* Reset the libctx temporarily to find out what it is*/
  345. tmpctx = OSSL_LIB_CTX_set0_default(libctx);
  346. OSSL_LIB_CTX_set0_default(tmpctx);
  347. if (tmpctx != globalctx) {
  348. fprintf(stderr,
  349. "test_ASYNC_start_job_ex() failed - global libctx check failed\n");
  350. goto err;
  351. }
  352. ret = 1;
  353. err:
  354. ASYNC_WAIT_CTX_free(waitctx);
  355. OSSL_LIB_CTX_free(libctx);
  356. return ret;
  357. }
  358. int main(int argc, char **argv)
  359. {
  360. if (!ASYNC_is_capable()) {
  361. fprintf(stderr,
  362. "OpenSSL build is not ASYNC capable - skipping async tests\n");
  363. } else {
  364. if (!test_ASYNC_init_thread()
  365. || !test_ASYNC_callback_status()
  366. || !test_ASYNC_start_job()
  367. || !test_ASYNC_get_current_job()
  368. || !test_ASYNC_WAIT_CTX_get_all_fds()
  369. || !test_ASYNC_block_pause()
  370. || !test_ASYNC_start_job_ex()) {
  371. return 1;
  372. }
  373. }
  374. printf("PASS\n");
  375. return 0;
  376. }