testutil: always print errors on failure
[openssl.git] / test / d2i_test.c
index 49882a3828599ed4e6506f860d656e9c5a03d937..c536764baed7bb47b6b06055325eb6ca62f5c204 100644 (file)
@@ -1,11 +1,10 @@
 /*
  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
  *
- * Licensed under the OpenSSL licenses, (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+ * Licensed under the OpenSSL license (the "License").  You may not use
+ * this file except in compliance with the License.  You can obtain a copy
+ * in the file LICENSE in the source distribution or at
  * https://www.openssl.org/source/license.html
- * or in the file LICENSE in the source distribution.
  */
 
 /* Regression tests for ASN.1 parsing bugs. */
@@ -108,6 +107,9 @@ static int execute_test(D2I_TEST_FIXTURE fixture)
         ret = 1;
 
  err:
+    /* Don't indicate success for memory allocation errors */
+    if (ret == 1 && ERR_GET_REASON(ERR_peek_error()) == ERR_R_MALLOC_FAILURE)
+        ret = 0;
     BIO_free(bio);
     OPENSSL_free(der);
     ASN1_item_free(value, item_type);
@@ -116,7 +118,6 @@ static int execute_test(D2I_TEST_FIXTURE fixture)
 
 static void tear_down(D2I_TEST_FIXTURE fixture)
 {
-    ERR_print_errors_fp(stderr);
 }
 
 #define SETUP_D2I_TEST_FIXTURE() \
@@ -140,13 +141,9 @@ int main(int argc, char **argv)
     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)
-    };
 
     static error_enum expected_errors[] = {
         {"OK", ASN1_OK},
@@ -156,6 +153,10 @@ 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");
@@ -166,18 +167,16 @@ int main(int argc, char **argv)
     expected_error_string = argv[2];
     test_file = argv[3];
 
-    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;
-        }
-    }
+    item_type = ASN1_ITEM_lookup(test_type_name);
+
     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]);
+        for (i = 0;; i++) {
+            const ASN1_ITEM *it = ASN1_ITEM_get(i);
+
+            if (it == NULL)
+                break;
             fprintf(stderr, "\t%s\n", it->sname);
         }
         return 1;
@@ -199,5 +198,10 @@ int main(int argc, char **argv)
 
     result = run_tests(argv[0]);
 
+#ifndef OPENSSL_NO_CRYPTO_MDEBUG
+    if (CRYPTO_mem_leaks_fp(stderr) <= 0)
+        result = 1;
+#endif
+
     return result;
 }