提交 ababe86b 编写于 作者: E Emilia Kasper

testutil: return 1 on success

Require that test methods return 1 on success (not 0). This is more
customary for OpenSSL.
Reviewed-by: NRich Salz <rsalz@openssl.org>
上级 6e863f07
...@@ -266,7 +266,7 @@ end: ...@@ -266,7 +266,7 @@ end:
static int execute_cert_test(CT_TEST_FIXTURE fixture) static int execute_cert_test(CT_TEST_FIXTURE fixture)
{ {
int test_failed = 0; int success = 0;
X509 *cert = NULL, *issuer = NULL; X509 *cert = NULL, *issuer = NULL;
STACK_OF(SCT) *scts = NULL; STACK_OF(SCT) *scts = NULL;
SCT *sct = NULL; SCT *sct = NULL;
...@@ -282,7 +282,6 @@ static int execute_cert_test(CT_TEST_FIXTURE fixture) ...@@ -282,7 +282,6 @@ static int execute_cert_test(CT_TEST_FIXTURE fixture)
CT_TEST_MAX_FILE_SIZE - 1); CT_TEST_MAX_FILE_SIZE - 1);
if (sct_text_len < 0) { if (sct_text_len < 0) {
test_failed = 1;
fprintf(stderr, "Test data file not found: %s\n", fprintf(stderr, "Test data file not found: %s\n",
fixture.sct_text_file); fixture.sct_text_file);
goto end; goto end;
...@@ -299,7 +298,6 @@ static int execute_cert_test(CT_TEST_FIXTURE fixture) ...@@ -299,7 +298,6 @@ static int execute_cert_test(CT_TEST_FIXTURE fixture)
cert = load_pem_cert(fixture.certs_dir, fixture.certificate_file); cert = load_pem_cert(fixture.certs_dir, fixture.certificate_file);
if (cert == NULL) { if (cert == NULL) {
test_failed = 1;
fprintf(stderr, "Unable to load certificate: %s\n", fprintf(stderr, "Unable to load certificate: %s\n",
fixture.certificate_file); fixture.certificate_file);
goto end; goto end;
...@@ -311,7 +309,6 @@ static int execute_cert_test(CT_TEST_FIXTURE fixture) ...@@ -311,7 +309,6 @@ static int execute_cert_test(CT_TEST_FIXTURE fixture)
issuer = load_pem_cert(fixture.certs_dir, fixture.issuer_file); issuer = load_pem_cert(fixture.certs_dir, fixture.issuer_file);
if (issuer == NULL) { if (issuer == NULL) {
test_failed = 1;
fprintf(stderr, "Unable to load issuer certificate: %s\n", fprintf(stderr, "Unable to load issuer certificate: %s\n",
fixture.issuer_file); fixture.issuer_file);
goto end; goto end;
...@@ -325,16 +322,14 @@ static int execute_cert_test(CT_TEST_FIXTURE fixture) ...@@ -325,16 +322,14 @@ static int execute_cert_test(CT_TEST_FIXTURE fixture)
sct_extension = X509_get_ext(cert, sct_extension_index); sct_extension = X509_get_ext(cert, sct_extension_index);
if (fixture.expected_sct_count > 0) { if (fixture.expected_sct_count > 0) {
if (sct_extension == NULL) { if (sct_extension == NULL) {
test_failed = 1;
fprintf(stderr, "SCT extension not found in: %s\n", fprintf(stderr, "SCT extension not found in: %s\n",
fixture.certificate_file); fixture.certificate_file);
goto end; goto end;
} }
if (fixture.sct_text_file) { if (fixture.sct_text_file
test_failed = compare_extension_printout(sct_extension, && compare_extension_printout(sct_extension,
expected_sct_text); expected_sct_text)) {
if (test_failed != 0)
goto end; goto end;
} }
...@@ -349,7 +344,6 @@ static int execute_cert_test(CT_TEST_FIXTURE fixture) ...@@ -349,7 +344,6 @@ static int execute_cert_test(CT_TEST_FIXTURE fixture)
if (!SCT_set_source(sct_i, SCT_SOURCE_X509V3_EXTENSION)) { if (!SCT_set_source(sct_i, SCT_SOURCE_X509V3_EXTENSION)) {
fprintf(stderr, fprintf(stderr,
"Error setting SCT source to X509v3 extension\n"); "Error setting SCT source to X509v3 extension\n");
test_failed = 1;
goto end; goto end;
} }
} }
...@@ -357,7 +351,7 @@ static int execute_cert_test(CT_TEST_FIXTURE fixture) ...@@ -357,7 +351,7 @@ static int execute_cert_test(CT_TEST_FIXTURE fixture)
are_scts_validated = SCT_LIST_validate(scts, ct_policy_ctx); are_scts_validated = SCT_LIST_validate(scts, ct_policy_ctx);
if (are_scts_validated < 0) { if (are_scts_validated < 0) {
fprintf(stderr, "Error verifying SCTs\n"); fprintf(stderr, "Error verifying SCTs\n");
test_failed = 1; goto end;
} else if (!are_scts_validated) { } else if (!are_scts_validated) {
int invalid_sct_count = 0; int invalid_sct_count = 0;
int valid_sct_count = 0; int valid_sct_count = 0;
...@@ -390,14 +384,10 @@ static int execute_cert_test(CT_TEST_FIXTURE fixture) ...@@ -390,14 +384,10 @@ static int execute_cert_test(CT_TEST_FIXTURE fixture)
fixture.expected_sct_count, fixture.expected_sct_count,
unverified_sct_count); unverified_sct_count);
} }
test_failed = 1;
}
if (test_failed != 0)
goto end; goto end;
}
} }
} else if (sct_extension != NULL) { } else if (sct_extension != NULL) {
test_failed = 1;
fprintf(stderr, fprintf(stderr,
"Expected no SCTs, but found SCT extension in: %s\n", "Expected no SCTs, but found SCT extension in: %s\n",
fixture.certificate_file); fixture.certificate_file);
...@@ -408,21 +398,18 @@ static int execute_cert_test(CT_TEST_FIXTURE fixture) ...@@ -408,21 +398,18 @@ static int execute_cert_test(CT_TEST_FIXTURE fixture)
if (fixture.tls_sct != NULL) { if (fixture.tls_sct != NULL) {
const unsigned char *p = fixture.tls_sct; const unsigned char *p = fixture.tls_sct;
if (o2i_SCT(&sct, &p, fixture.tls_sct_len) == NULL) { if (o2i_SCT(&sct, &p, fixture.tls_sct_len) == NULL) {
test_failed = 1;
fprintf(stderr, "Failed to decode SCT from TLS format\n"); fprintf(stderr, "Failed to decode SCT from TLS format\n");
goto end; goto end;
} }
if (fixture.sct_text_file) { if (fixture.sct_text_file
test_failed = compare_sct_printout(sct, expected_sct_text); && compare_sct_printout(sct, expected_sct_text)) {
if (test_failed != 0)
goto end; goto end;
} }
tls_sct_len = i2o_SCT(sct, &tls_sct); tls_sct_len = i2o_SCT(sct, &tls_sct);
if (tls_sct_len != fixture.tls_sct_len || if (tls_sct_len != fixture.tls_sct_len ||
memcmp(fixture.tls_sct, tls_sct, tls_sct_len) != 0) { memcmp(fixture.tls_sct, tls_sct, tls_sct_len) != 0) {
test_failed = 1;
fprintf(stderr, "Failed to encode SCT into TLS format correctly\n"); fprintf(stderr, "Failed to encode SCT into TLS format correctly\n");
goto end; goto end;
} }
...@@ -430,16 +417,15 @@ static int execute_cert_test(CT_TEST_FIXTURE fixture) ...@@ -430,16 +417,15 @@ static int execute_cert_test(CT_TEST_FIXTURE fixture)
if (fixture.test_validity && cert != NULL) { if (fixture.test_validity && cert != NULL) {
int is_sct_validated = SCT_validate(sct, ct_policy_ctx); int is_sct_validated = SCT_validate(sct, ct_policy_ctx);
if (is_sct_validated < 0) { if (is_sct_validated < 0) {
test_failed = 1;
fprintf(stderr, "Error validating SCT\n"); fprintf(stderr, "Error validating SCT\n");
goto end; goto end;
} else if (!is_sct_validated) { } else if (!is_sct_validated) {
test_failed = 1;
fprintf(stderr, "SCT failed verification\n"); fprintf(stderr, "SCT failed verification\n");
goto end; goto end;
} }
} }
} }
success = 1;
end: end:
X509_free(cert); X509_free(cert);
...@@ -448,7 +434,7 @@ end: ...@@ -448,7 +434,7 @@ end:
SCT_free(sct); SCT_free(sct);
CT_POLICY_EVAL_CTX_free(ct_policy_ctx); CT_POLICY_EVAL_CTX_free(ct_policy_ctx);
OPENSSL_free(tls_sct); OPENSSL_free(tls_sct);
return test_failed; return success;
} }
#define SETUP_CT_TEST_FIXTURE() SETUP_TEST_FIXTURE(CT_TEST_FIXTURE, set_up) #define SETUP_CT_TEST_FIXTURE() SETUP_TEST_FIXTURE(CT_TEST_FIXTURE, set_up)
......
...@@ -40,13 +40,13 @@ static int execute_test(D2I_TEST_FIXTURE fixture) ...@@ -40,13 +40,13 @@ static int execute_test(D2I_TEST_FIXTURE fixture)
{ {
BIO *bio = NULL; BIO *bio = NULL;
ASN1_VALUE *value = NULL; ASN1_VALUE *value = NULL;
int ret = 1; int ret = 0;
unsigned char buf[2048]; unsigned char buf[2048];
const unsigned char *buf_ptr = buf; const unsigned char *buf_ptr = buf;
int len; int len;
if ((bio = BIO_new_file(test_file, "r")) == NULL) if ((bio = BIO_new_file(test_file, "r")) == NULL)
return 1; return 0;
/* /*
* We don't use ASN1_item_d2i_bio because it, apparently, * We don't use ASN1_item_d2i_bio because it, apparently,
...@@ -60,7 +60,7 @@ static int execute_test(D2I_TEST_FIXTURE fixture) ...@@ -60,7 +60,7 @@ static int execute_test(D2I_TEST_FIXTURE fixture)
if (value != NULL) if (value != NULL)
goto err; goto err;
ret = 0; ret = 1;
err: err:
BIO_free(bio); BIO_free(bio);
......
...@@ -140,8 +140,7 @@ static int check_test(HANDSHAKE_RESULT result, SSL_TEST_CTX *test_ctx) ...@@ -140,8 +140,7 @@ static int check_test(HANDSHAKE_RESULT result, SSL_TEST_CTX *test_ctx)
static int execute_test(SSL_TEST_FIXTURE fixture) static int execute_test(SSL_TEST_FIXTURE fixture)
{ {
/* TODO(emilia): this is confusing. Flip to return 1 on success. */ int ret = 0;
int ret = 1;
SSL_CTX *server_ctx = NULL, *client_ctx = NULL; SSL_CTX *server_ctx = NULL, *client_ctx = NULL;
SSL_TEST_CTX *test_ctx = NULL; SSL_TEST_CTX *test_ctx = NULL;
HANDSHAKE_RESULT result; HANDSHAKE_RESULT result;
...@@ -163,15 +162,14 @@ static int execute_test(SSL_TEST_FIXTURE fixture) ...@@ -163,15 +162,14 @@ static int execute_test(SSL_TEST_FIXTURE fixture)
result = do_handshake(server_ctx, client_ctx); result = do_handshake(server_ctx, client_ctx);
if (check_test(result, test_ctx)) ret = check_test(result, test_ctx);
ret = 0;
err: err:
CONF_modules_unload(0); CONF_modules_unload(0);
SSL_CTX_free(server_ctx); SSL_CTX_free(server_ctx);
SSL_CTX_free(client_ctx); SSL_CTX_free(client_ctx);
SSL_TEST_CTX_free(test_ctx); SSL_TEST_CTX_free(test_ctx);
if (ret != 0) if (ret != 1)
ERR_print_errors_fp(stderr); ERR_print_errors_fp(stderr);
return ret; return ret;
} }
......
...@@ -74,7 +74,7 @@ static SSL_TEST_CTX_TEST_FIXTURE set_up(const char *const test_case_name) ...@@ -74,7 +74,7 @@ static SSL_TEST_CTX_TEST_FIXTURE set_up(const char *const test_case_name)
static int execute_test(SSL_TEST_CTX_TEST_FIXTURE fixture) static int execute_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
{ {
int ret = 1; int success = 0;
SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section); SSL_TEST_CTX *ctx = SSL_TEST_CTX_create(conf, fixture.test_section);
...@@ -87,10 +87,10 @@ static int execute_test(SSL_TEST_CTX_TEST_FIXTURE fixture) ...@@ -87,10 +87,10 @@ static int execute_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
if (!SSL_TEST_CTX_equal(ctx, fixture.expected_ctx)) if (!SSL_TEST_CTX_equal(ctx, fixture.expected_ctx))
goto err; goto err;
ret = 0; success = 1;
err: err:
SSL_TEST_CTX_free(ctx); SSL_TEST_CTX_free(ctx);
return ret; return success;
} }
static int execute_failure_test(SSL_TEST_CTX_TEST_FIXTURE fixture) static int execute_failure_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
...@@ -101,10 +101,10 @@ static int execute_failure_test(SSL_TEST_CTX_TEST_FIXTURE fixture) ...@@ -101,10 +101,10 @@ static int execute_failure_test(SSL_TEST_CTX_TEST_FIXTURE fixture)
fprintf(stderr, "Parsing bad configuration %s succeeded.\n", fprintf(stderr, "Parsing bad configuration %s succeeded.\n",
fixture.test_section); fixture.test_section);
SSL_TEST_CTX_free(ctx); SSL_TEST_CTX_free(ctx);
return 1; return 0;
} }
return 0; return 1;
} }
static void tear_down(SSL_TEST_CTX_TEST_FIXTURE fixture) static void tear_down(SSL_TEST_CTX_TEST_FIXTURE fixture)
......
...@@ -113,14 +113,14 @@ int run_tests(const char *test_prog_name) ...@@ -113,14 +113,14 @@ int run_tests(const char *test_prog_name)
for (i = 0; i != num_tests; ++i) { for (i = 0; i != num_tests; ++i) {
if (all_tests[i].num == -1) { if (all_tests[i].num == -1) {
if (all_tests[i].test_fn()) { if (!all_tests[i].test_fn()) {
printf("** %s failed **\n--------\n", printf("** %s failed **\n--------\n",
all_tests[i].test_case_name); all_tests[i].test_case_name);
++num_failed; ++num_failed;
} }
} else { } else {
for (j = 0; j < all_tests[i].num; j++) { for (j = 0; j < all_tests[i].num; j++) {
if (all_tests[i].param_test_fn(j)) { if (!all_tests[i].param_test_fn(j)) {
printf("** %s failed test %d\n--------\n", printf("** %s failed test %d\n--------\n",
all_tests[i].test_case_name, j); all_tests[i].test_case_name, j);
++num_failed; ++num_failed;
......
...@@ -68,7 +68,7 @@ ...@@ -68,7 +68,7 @@
* *
* EXECUTE_TEST will pass fixture to execute_func() by value, call * EXECUTE_TEST will pass fixture to execute_func() by value, call
* tear_down(), and return the result of execute_func(). execute_func() should * tear_down(), and return the result of execute_func(). execute_func() should
* take a TEST_FIXTURE_TYPE by value and return zero on success or one on * take a TEST_FIXTURE_TYPE by value and return 1 on success and 0 on
* failure. * failure.
* *
* Unit tests can define their own SETUP_TEST_FIXTURE and EXECUTE_TEST * Unit tests can define their own SETUP_TEST_FIXTURE and EXECUTE_TEST
...@@ -94,7 +94,7 @@ ...@@ -94,7 +94,7 @@
int result = 0 int result = 0
# define EXECUTE_TEST(execute_func, tear_down)\ # define EXECUTE_TEST(execute_func, tear_down)\
if (execute_func(fixture) != 0) result = 1;\ result = execute_func(fixture);\
tear_down(fixture);\ tear_down(fixture);\
return result return result
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册