2
0

asynctest.c 14 KB

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