Skip to content

Commit

Permalink
Test loading a PEM file from multiple threads
Browse files Browse the repository at this point in the history
The new decoder optimisation has some locking involved so we confirm that
reading a PEM file from multiple threads works as expected.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from #21426)
  • Loading branch information
mattcaswell authored and paulidale committed Jul 16, 2023
1 parent 32d3c3a commit 29f25a1
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions test/threadstest.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <openssl/aes.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/pem.h>
#include <openssl/evp.h>
#include "internal/tsan_assist.h"
#include "internal/nelem.h"
#include "testutil.h"
Expand Down Expand Up @@ -741,6 +743,55 @@ static int test_bio_dgram_pair(void)
}
#endif

static const char *pemdataraw[] = {
"-----BEGIN RSA PRIVATE KEY-----\n",
"MIIBOgIBAAJBAMFcGsaxxdgiuuGmCkVImy4h99CqT7jwY3pexPGcnUFtR2Fh36Bp\n",
"oncwtkZ4cAgtvd4Qs8PkxUdp6p/DlUmObdkCAwEAAQJAUR44xX6zB3eaeyvTRzms\n",
"kHADrPCmPWnr8dxsNwiDGHzrMKLN+i/HAam+97HxIKVWNDH2ba9Mf1SA8xu9dcHZ\n",
"AQIhAOHPCLxbtQFVxlnhSyxYeb7O323c3QulPNn3bhOipElpAiEA2zZpBE8ZXVnL\n",
"74QjG4zINlDfH+EOEtjJJ3RtaYDugvECIBtsQDxXytChsRgDQ1TcXdStXPcDppie\n",
"dZhm8yhRTTBZAiAZjE/U9rsIDC0ebxIAZfn3iplWh84yGB3pgUI3J5WkoQIhAInE\n",
"HTUY5WRj5riZtkyGnbm3DvF+1eMtO2lYV+OuLcfE\n",
"-----END RSA PRIVATE KEY-----\n",
NULL
};

static void test_pem_read_one(void)
{
EVP_PKEY *key = NULL;
BIO *pem = NULL;
char *pemdata;
size_t len;

pemdata = glue_strings(pemdataraw, &len);
if (pemdata == NULL) {
multi_set_success(0);
goto err;
}

pem = BIO_new_mem_buf(pemdata, len);
if (pem == NULL) {
multi_set_success(0);
goto err;
}

key = PEM_read_bio_PrivateKey(pem, NULL, NULL, NULL);
if (key == NULL)
multi_set_success(0);

err:
EVP_PKEY_free(key);
BIO_free(pem);
OPENSSL_free(pemdata);
}

/* Test reading PEM files in multiple threads */
static int test_pem_read(void)
{
return thread_run_test(&test_pem_read_one, MAXIMUM_THREADS,
&test_pem_read_one, 1, default_provider);
}

typedef enum OPTION_choice {
OPT_ERR = -1,
OPT_EOF = 0,
Expand Down Expand Up @@ -816,6 +867,7 @@ int setup_tests(void)
#if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
ADD_TEST(test_bio_dgram_pair);
#endif
ADD_TEST(test_pem_read);
return 1;
}

Expand Down

0 comments on commit 29f25a1

Please sign in to comment.