Merge Nokia copyright notice into standard
[openssl.git] / test / d2i_test.c
index e12f2492421631384b0f54692d97834c08db6585..58fbe4af2f4c5fd7cdd36ab3d3d59925febb7505 100644 (file)
@@ -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
@@ -52,12 +52,12 @@ static int test_bad_asn1()
     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;
     }
@@ -68,12 +68,12 @@ static int test_bad_asn1()
      * 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;
     }
@@ -81,23 +81,24 @@ static int test_bad_asn1()
     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);
@@ -109,12 +110,10 @@ static int test_bad_asn1()
  * Usage: d2i_test <type> <file>, e.g.
  * d2i_test generalname bad_generalname.der
  */
-int main(int argc, char **argv)
+int test_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;
 
@@ -126,13 +125,8 @@ 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");
+    if (!TEST_int_eq(argc, 4)) {
+        fprintf(stderr, "Usage: d2i_test item_name expected_error file.der\n");
         return 1;
     }
 
@@ -143,14 +137,14 @@ int main(int argc, char **argv)
     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");
+        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;
-            fprintf(stderr, "\t%s\n", it->sname);
+            TEST_note("\t%s", it->sname);
         }
         return 1;
     }
@@ -163,18 +157,11 @@ int main(int argc, char **argv)
     }
 
     if (expected_error == ASN1_UNKNOWN) {
-        fprintf(stderr, "Unknown expected error %s\n", expected_error_string);
+        TEST_error("Unknown expected error %s\n", expected_error_string);
         return 1;
     }
 
     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 run_tests(argv[0]);
 }