Add a test to make sure ASYNC aware code gets the right default libctx

Even if a fibre changes the default libctx - or the main application code
changes it, the "current" default libctx should remain consistent.

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/12228)
This commit is contained in:
Matt Caswell 2020-06-26 11:02:29 +01:00 committed by Richard Levitte
parent 6c689e58f7
commit 92db29e5e8
1 changed files with 109 additions and 1 deletions

View File

@ -43,6 +43,39 @@ static int save_current(void *args)
return 1;
}
static int change_deflt_libctx(void *args)
{
OPENSSL_CTX *libctx = OPENSSL_CTX_new();
OPENSSL_CTX *oldctx, *tmpctx;
int ret = 0;
if (libctx == NULL)
return 0;
oldctx = OPENSSL_CTX_set0_default(libctx);
ASYNC_pause_job();
/* Check the libctx is set up as we expect */
tmpctx = OPENSSL_CTX_set0_default(oldctx);
if (tmpctx != libctx)
goto err;
/* Set it back again to continue to use our own libctx */
oldctx = OPENSSL_CTX_set0_default(libctx);
ASYNC_pause_job();
/* Check the libctx is set up as we expect */
tmpctx = OPENSSL_CTX_set0_default(oldctx);
if (tmpctx != libctx)
goto err;
ret = 1;
err:
OPENSSL_CTX_free(libctx);
return ret;
}
#define MAGIC_WAIT_FD ((OSSL_ASYNC_FD)99)
static int waitfd(void *args)
{
@ -306,6 +339,80 @@ static int test_ASYNC_block_pause(void)
return 1;
}
static int test_ASYNC_start_job_with_libctx(void)
{
ASYNC_JOB *job = NULL;
int funcret;
ASYNC_WAIT_CTX *waitctx = NULL;
OPENSSL_CTX *libctx = OPENSSL_CTX_new();
OPENSSL_CTX *oldctx, *tmpctx, *globalctx;
int ret = 0;
if (libctx == NULL) {
fprintf(stderr,
"test_ASYNC_start_job_with_libctx() failed to create libctx\n");
goto err;
}
globalctx = oldctx = OPENSSL_CTX_set0_default(libctx);
if ((waitctx = ASYNC_WAIT_CTX_new()) == NULL
|| ASYNC_start_job(&job, waitctx, &funcret, change_deflt_libctx,
NULL, 0)
!= ASYNC_PAUSE) {
fprintf(stderr,
"test_ASYNC_start_job_with_libctx() failed to start job\n");
goto err;
}
/* Reset the libctx temporarily to find out what it is*/
tmpctx = OPENSSL_CTX_set0_default(oldctx);
oldctx = OPENSSL_CTX_set0_default(tmpctx);
if (tmpctx != libctx) {
fprintf(stderr,
"test_ASYNC_start_job_with_libctx() failed - unexpected libctx\n");
goto err;
}
if (ASYNC_start_job(&job, waitctx, &funcret, change_deflt_libctx, NULL, 0)
!= ASYNC_PAUSE) {
fprintf(stderr,
"test_ASYNC_start_job_with_libctx() - restarting job failed\n");
goto err;
}
/* Reset the libctx and continue with the global default libctx */
tmpctx = OPENSSL_CTX_set0_default(oldctx);
if (tmpctx != libctx) {
fprintf(stderr,
"test_ASYNC_start_job_with_libctx() failed - unexpected libctx\n");
goto err;
}
if (ASYNC_start_job(&job, waitctx, &funcret, change_deflt_libctx, NULL, 0)
!= ASYNC_FINISH
|| funcret != 1) {
fprintf(stderr,
"test_ASYNC_start_job_with_libctx() - finishing job failed\n");
goto err;
}
/* Reset the libctx temporarily to find out what it is*/
tmpctx = OPENSSL_CTX_set0_default(libctx);
OPENSSL_CTX_set0_default(tmpctx);
if (tmpctx != globalctx) {
fprintf(stderr,
"test_ASYNC_start_job_with_libctx() failed - global libctx check failed\n");
goto err;
}
ret = 1;
err:
ASYNC_WAIT_CTX_free(waitctx);
OPENSSL_CTX_free(libctx);
return ret;
}
int main(int argc, char **argv)
{
if (!ASYNC_is_capable()) {
@ -317,7 +424,8 @@ int main(int argc, char **argv)
|| !test_ASYNC_start_job()
|| !test_ASYNC_get_current_job()
|| !test_ASYNC_WAIT_CTX_get_all_fds()
|| !test_ASYNC_block_pause()) {
|| !test_ASYNC_block_pause()
|| !test_ASYNC_start_job_with_libctx()) {
return 1;
}
}