X-Git-Url: https://git.openssl.org/gitweb/?p=openssl.git;a=blobdiff_plain;f=test%2Fx509_check_cert_pkey_test.c;h=1cad49e8035ca7906d40b84e2b7e4fcced1c97d2;hp=e94b18b0b4ff3fe7f0986daeb2c45890cf5a3bb4;hb=2cf7fd698ec1375421f91338ff8a44e7da5238b6;hpb=e514ff0f7368b75b312d69da7b3a82ee57141967 diff --git a/test/x509_check_cert_pkey_test.c b/test/x509_check_cert_pkey_test.c index e94b18b0b4..1cad49e803 100644 --- a/test/x509_check_cert_pkey_test.c +++ b/test/x509_check_cert_pkey_test.c @@ -20,8 +20,12 @@ * t: API type, "cert" for X509_ and "req" for X509_REQ_ APIs. * e: expected, "ok" for success, "failed" for what should fail. */ -static int test_x509_check_cert_pkey(const char *c, const char *k, - const char *t, const char *e) +static const char *c; +static const char *k; +static const char *t; +static const char *e; + +static int test_x509_check_cert_pkey(void) { BIO *bio = NULL; X509 *x509 = NULL; @@ -45,94 +49,74 @@ static int test_x509_check_cert_pkey(const char *c, const char *k, if (strcmp(e, "ok") == 0) { expected = 1; } else if (strcmp(e, "failed") == 0) { - expected = 2; + expected = 0; } else { TEST_error("invalid 'expected'"); goto failed; } /* process private key */ - bio = BIO_new_file(k, "r"); - if (bio == NULL) { - TEST_error("create BIO for private key failed"); + if (!TEST_ptr(bio = BIO_new_file(k, "r"))) goto failed; - } - pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL); - if (pkey == NULL) { - TEST_error("read PEM private key failed"); + if (!TEST_ptr(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL))) goto failed; - } BIO_free(bio); /* process cert or cert request, use the same local var */ - bio = BIO_new_file(c, "r"); - if (bio == NULL) { - TEST_error("create BIO for cert or cert req failed"); + if (!TEST_ptr(bio = BIO_new_file(c, "r"))) goto failed; - } switch (type) { - case 1: - x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL); - if (x509 == NULL) { - TEST_error("read PEM x509 failed"); - goto failed; - } - - result = X509_check_private_key(x509, pkey); - break; - case 2: - x509_req = PEM_read_bio_X509_REQ(bio, NULL, NULL, NULL); - if (x509_req == NULL) { - TEST_error("read PEM x509 req failed"); - goto failed; - } - - result = X509_REQ_check_private_key(x509_req, pkey); - break; - default: - /* should never be here */ - break; - } - - if (expected == 1) { - /* expected == 1 means we expect an "ok" */ - if (!TEST_int_eq(result, 1)) { - TEST_error("check private key: expected: 1, got: %d", result); + case 1: + x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL); + if (x509 == NULL) { + TEST_error("read PEM x509 failed"); goto failed; } - } else { - if (!TEST_int_eq(result, 0)) { - TEST_error("check private key: expected: 0, got: %d", result); + + result = X509_check_private_key(x509, pkey); + break; + case 2: + x509_req = PEM_read_bio_X509_REQ(bio, NULL, NULL, NULL); + if (x509_req == NULL) { + TEST_error("read PEM x509 req failed"); goto failed; } + + result = X509_REQ_check_private_key(x509_req, pkey); + break; + default: + /* should never be here */ + break; } -out: - if (bio) - BIO_free(bio); - if (x509) - X509_free(x509); - if (x509_req) - X509_REQ_free(x509_req); - if (pkey) - EVP_PKEY_free(pkey); - return ret; + if (!TEST_int_eq(result, expected)) { + TEST_error("check private key: expected: %d, got: %d", expected, result); + goto failed; + } -failed: ret = 1; - goto out; +failed: + BIO_free(bio); + X509_free(x509); + X509_REQ_free(x509_req); + EVP_PKEY_free(pkey); + return ret; } -int test_main(int argc, char **argv) +int setup_tests(void) { - if (!TEST_int_eq(argc, 5)) { - TEST_info("usage: x509_check_cert_pkey cert.pem|cert.req" + if (!TEST_ptr(c = test_get_argument(0)) + || !TEST_ptr(k = test_get_argument(1)) + || !TEST_ptr(t = test_get_argument(2)) + || !TEST_ptr(e = test_get_argument(3))) { + TEST_note("usage: x509_check_cert_pkey cert.pem|cert.req" " key.pem cert|req "); - return 1; + return 0; } - return test_x509_check_cert_pkey(argv[1], argv[2], argv[3], argv[4]); + ADD_TEST(test_x509_check_cert_pkey); + return 1; }