diff options
Diffstat (limited to 'deps/openssl/openssl/test/d2i_test.c')
-rw-r--r-- | deps/openssl/openssl/test/d2i_test.c | 124 |
1 files changed, 33 insertions, 91 deletions
diff --git a/deps/openssl/openssl/test/d2i_test.c b/deps/openssl/openssl/test/d2i_test.c index 5274270097..afea2dcb9f 100644 --- a/deps/openssl/openssl/test/d2i_test.c +++ b/deps/openssl/openssl/test/d2i_test.c @@ -1,5 +1,5 @@ /* - * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. + * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the OpenSSL license (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy @@ -20,10 +20,7 @@ #include <openssl/err.h> #include <openssl/x509.h> #include <openssl/x509v3.h> -#ifndef OPENSSL_NO_CMS -# include <openssl/cms.h> -#endif -#include "e_os.h" +#include "internal/nelem.h" static const ASN1_ITEM *item_type; static const char *test_file; @@ -44,18 +41,7 @@ typedef struct { static expected_error_t expected_error = ASN1_UNKNOWN; -typedef struct d2i_test_fixture { - const char *test_case_name; -} D2I_TEST_FIXTURE; - -static D2I_TEST_FIXTURE set_up(const char *const test_case_name) -{ - D2I_TEST_FIXTURE fixture; - fixture.test_case_name = test_case_name; - return fixture; -} - -static int execute_test(D2I_TEST_FIXTURE fixture) +static int test_bad_asn1(void) { BIO *bio = NULL; ASN1_VALUE *value = NULL; @@ -66,12 +52,12 @@ static int execute_test(D2I_TEST_FIXTURE fixture) int derlen; int len; - if ((bio = BIO_new_file(test_file, "r")) == NULL) + bio = BIO_new_file(test_file, "r"); + if (!TEST_ptr(bio)) return 0; if (expected_error == ASN1_BIO) { - value = ASN1_item_d2i_bio(item_type, bio, NULL); - if (value == NULL) + if (TEST_ptr_null(ASN1_item_d2i_bio(item_type, bio, NULL))) ret = 1; goto err; } @@ -82,12 +68,12 @@ static int execute_test(D2I_TEST_FIXTURE fixture) * decoder is called. */ len = BIO_read(bio, buf, sizeof(buf)); - if (len < 0) + if (!TEST_int_ge(len, 0)) goto err; value = ASN1_item_d2i(NULL, &buf_ptr, len, item_type); if (value == NULL) { - if (expected_error == ASN1_DECODE) + if (TEST_int_eq(expected_error, ASN1_DECODE)) ret = 1; goto err; } @@ -95,23 +81,24 @@ static int execute_test(D2I_TEST_FIXTURE fixture) derlen = ASN1_item_i2d(value, &der, item_type); if (der == NULL || derlen < 0) { - if (expected_error == ASN1_ENCODE) + if (TEST_int_eq(expected_error, ASN1_ENCODE)) ret = 1; goto err; } if (derlen != len || memcmp(der, buf, derlen) != 0) { - if (expected_error == ASN1_COMPARE) + if (TEST_int_eq(expected_error, ASN1_COMPARE)) ret = 1; goto err; } - if (expected_error == ASN1_OK) + if (TEST_int_eq(expected_error, ASN1_OK)) ret = 1; err: /* Don't indicate success for memory allocation errors */ - if (ret == 1 && ERR_GET_REASON(ERR_peek_error()) == ERR_R_MALLOC_FAILURE) + if (ret == 1 + && !TEST_false(ERR_GET_REASON(ERR_peek_error()) == ERR_R_MALLOC_FAILURE)) ret = 0; BIO_free(bio); OPENSSL_free(der); @@ -119,44 +106,16 @@ static int execute_test(D2I_TEST_FIXTURE fixture) return ret; } -static void tear_down(D2I_TEST_FIXTURE fixture) -{ - ERR_print_errors_fp(stderr); -} - -#define SETUP_D2I_TEST_FIXTURE() \ - SETUP_TEST_FIXTURE(D2I_TEST_FIXTURE, set_up) - -#define EXECUTE_D2I_TEST() \ - EXECUTE_TEST(execute_test, tear_down) - -static int test_bad_asn1() -{ - SETUP_D2I_TEST_FIXTURE(); - EXECUTE_D2I_TEST(); -} - /* - * Usage: d2i_test <type> <file>, e.g. + * Usage: d2i_test <name> <type> <file>, e.g. * d2i_test generalname bad_generalname.der */ -int main(int argc, char **argv) +int setup_tests(void) { - int result = 0; const char *test_type_name; const char *expected_error_string; - const char *p = getenv("OPENSSL_DEBUG_MEMORY"); size_t i; - static ASN1_ITEM_EXP *items[] = { - ASN1_ITEM_ref(ASN1_ANY), - ASN1_ITEM_ref(X509), - ASN1_ITEM_ref(GENERAL_NAME), - ASN1_ITEM_ref(ASN1_INTEGER), -#ifndef OPENSSL_NO_CMS - ASN1_ITEM_ref(CMS_ContentInfo) -#endif - }; static error_enum expected_errors[] = { {"OK", ASN1_OK}, @@ -166,35 +125,26 @@ int main(int argc, char **argv) {"compare", ASN1_COMPARE} }; - if (p != NULL && strcmp(p, "on") == 0) - CRYPTO_set_mem_debug(1); - CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); - - if (argc != 4) { - fprintf(stderr, - "Usage: d2i_test item_name expected_error file.der\n"); - return 1; + if (!TEST_ptr(test_type_name = test_get_argument(0)) + || !TEST_ptr(expected_error_string = test_get_argument(1)) + || !TEST_ptr(test_file = test_get_argument(2))) { + TEST_note("Usage: d2i_test item_name expected_error file.der"); + return 0; } - test_type_name = argv[1]; - expected_error_string = argv[2]; - test_file = argv[3]; + item_type = ASN1_ITEM_lookup(test_type_name); - for (i = 0; i < OSSL_NELEM(items); i++) { - const ASN1_ITEM *it = ASN1_ITEM_ptr(items[i]); - if (strcmp(test_type_name, it->sname) == 0) { - item_type = it; - break; - } - } if (item_type == NULL) { - fprintf(stderr, "Unknown type %s\n", test_type_name); - fprintf(stderr, "Supported types:\n"); - for (i = 0; i < OSSL_NELEM(items); i++) { - const ASN1_ITEM *it = ASN1_ITEM_ptr(items[i]); - fprintf(stderr, "\t%s\n", it->sname); + TEST_error("Unknown type %s", test_type_name); + TEST_note("Supported types:"); + for (i = 0;; i++) { + const ASN1_ITEM *it = ASN1_ITEM_get(i); + + if (it == NULL) + break; + TEST_note("\t%s", it->sname); } - return 1; + return 0; } for (i = 0; i < OSSL_NELEM(expected_errors); i++) { @@ -205,18 +155,10 @@ int main(int argc, char **argv) } if (expected_error == ASN1_UNKNOWN) { - fprintf(stderr, "Unknown expected error %s\n", expected_error_string); - return 1; + TEST_error("Unknown expected error %s\n", expected_error_string); + return 0; } ADD_TEST(test_bad_asn1); - - result = run_tests(argv[0]); - -#ifndef OPENSSL_NO_CRYPTO_MDEBUG - if (CRYPTO_mem_leaks_fp(stderr) <= 0) - result = 1; -#endif - - return result; + return 1; } |