Updates following review of SSL_stateless() code
[openssl.git] / test / lhash_test.c
index b5f2ec8d7f91bddbf330f6b012e86c445f26aab2..162286b7cdd6150fa47a98373e4a2c45547247d6 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright (c) 2017, Oracle and/or its affiliates.  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
@@ -7,10 +8,6 @@
  * https://www.openssl.org/source/license.html
  */
 
-/*
- * Copyright (c) 2017 Oracle and/or its affiliates.  All rights reserved.
- */
-
 #include <stdio.h>
 #include <string.h>
 
@@ -19,8 +16,7 @@
 #include <openssl/err.h>
 #include <openssl/crypto.h>
 
-#include "e_os.h"
-#include "test_main.h"
+#include "internal/nelem.h"
 #include "testutil.h"
 
 /*
@@ -81,95 +77,81 @@ static int test_int_lhash(void)
         { 999,      1 },
         { 37,       0 },
         { 1,        0 },
-        { 34,       1 }     
+        { 34,       1 }
     };
     const unsigned int n_dels = OSSL_NELEM(dels);
     LHASH_OF(int) *h = lh_int_new(&int_hash, &int_cmp);
     unsigned int i;
     int testresult = 0, j, *p;
 
-    if (h == NULL) {
-        fprintf(stderr, "test lhash int allocation\n");
+    if (!TEST_ptr(h))
         goto end;
-    }
 
     /* insert */
     for (i = 0; i < n_int_tests; i++)
-        if (lh_int_insert(h, int_tests + i) != NULL) {
-            fprintf(stderr, "test lhash int insert %d\n", i);
+        if (!TEST_ptr_null(lh_int_insert(h, int_tests + i))) {
+            TEST_info("int insert %d", i);
             goto end;
         }
 
     /* num_items */
-    if (lh_int_num_items(h) != n_int_tests) {
-            fprintf(stderr, "test lhash int num items\n");
-            goto end;
-    }
+    if (!TEST_int_eq(lh_int_num_items(h), n_int_tests))
+        goto end;
 
     /* retrieve */
     for (i = 0; i < n_int_tests; i++)
-        if (*lh_int_retrieve(h, int_tests + i) != int_tests[i]) {
-            fprintf(stderr, "test lhash int retrieve value %d\n", i);
+        if (!TEST_int_eq(*lh_int_retrieve(h, int_tests + i), int_tests[i])) {
+            TEST_info("lhash int retrieve value %d", i);
             goto end;
         }
     for (i = 0; i < n_int_tests; i++)
-        if (lh_int_retrieve(h, int_tests + i) != int_tests + i) {
-            fprintf(stderr, "test lhash int retrieve address %d\n", i);
+        if (!TEST_ptr_eq(lh_int_retrieve(h, int_tests + i), int_tests + i)) {
+            TEST_info("lhash int retrieve address %d", i);
             goto end;
         }
     j = 1;
-    if (lh_int_retrieve(h, &j) != int_tests + 2) {
-        fprintf(stderr, "test lhash int retrieve other\n");
+    if (!TEST_ptr_eq(lh_int_retrieve(h, &j), int_tests + 2))
         goto end;
-    }
 
     /* replace */
     j = 13;
-    if ((p = lh_int_insert(h, &j)) == NULL) {
-        fprintf(stderr, "test lhash int replacement insert\n");
+    if (!TEST_ptr(p = lh_int_insert(h, &j)))
         goto end;
-    }
-    if (p != int_tests + 1) {
-        fprintf(stderr, "test lhash int replacement pointer\n");
+    if (!TEST_ptr_eq(p, int_tests + 1))
         goto end;
-    }
-    if (lh_int_retrieve(h, int_tests + 1) != &j) {
-        fprintf(stderr, "test lhash int replacement variable\n");
+    if (!TEST_ptr_eq(lh_int_retrieve(h, int_tests + 1), &j))
         goto end;
-    }
 
     /* do_all */
     memset(int_found, 0, sizeof(int_found));
     lh_int_doall(h, &int_doall);
     for (i = 0; i < n_int_tests; i++)
-        if (int_found[i] != 1) {
-            fprintf(stderr, "test lhash int doall %d\n", i);
+        if (!TEST_int_eq(int_found[i], 1)) {
+            TEST_info("lhash int doall %d", i);
             goto end;
         }
-    
+
     /* do_all_arg */
     memset(int_found, 0, sizeof(int_found));
     lh_int_doall_short(h, int_doall_arg, int_found);
     for (i = 0; i < n_int_tests; i++)
-        if (int_found[i] != 1) {
-            fprintf(stderr, "test lhash int doall arg %d\n", i);
+        if (!TEST_int_eq(int_found[i], 1)) {
+            TEST_info("lhash int doall arg %d", i);
             goto end;
         }
-    
+
     /* delete */
     for (i = 0; i < n_dels; i++) {
         const int b = lh_int_delete(h, &dels[i].data) == NULL;
-        if ((b ^ dels[i].null) != 0) {
-            fprintf(stderr, "test lhash int delete %d\n", i);
+        if (!TEST_int_eq(b ^ dels[i].null,  0)) {
+            TEST_info("lhash int delete %d", i);
             goto end;
         }
     }
 
     /* error */
-    if (lh_int_error(h) != 0) {
-        fprintf(stderr, "test lhash int error\n");
+    if (!TEST_int_eq(lh_int_error(h), 0))
         goto end;
-    }
 
     testresult = 1;
 end:
@@ -189,16 +171,14 @@ static int test_stress(void)
     unsigned int i;
     int testresult = 0, *p;
 
-    if (h == NULL) {
-        fprintf(stderr, "test lhash stress allocation\n");
+    if (!TEST_ptr(h))
         goto end;
-    }
 
     /* insert */
     for (i = 0; i < n; i++) {
         p = OPENSSL_malloc(sizeof(i));
-        if (p == NULL) {
-            fprintf(stderr, "test lhash stress out of memory %d\n", i);
+        if (!TEST_ptr(p)) {
+            TEST_info("lhash stress out of memory %d", i);
             goto end;
         }
         *p = 3 * i + 1;
@@ -206,35 +186,33 @@ static int test_stress(void)
     }
 
     /* num_items */
-    if (lh_int_num_items(h) != n) {
-            fprintf(stderr, "test lhash stress num items\n");
+    if (!TEST_int_eq(lh_int_num_items(h), n))
             goto end;
-    }
 
-    fprintf(stderr, "hash full statistics:\n");
-    OPENSSL_LH_stats((OPENSSL_LHASH *)h, stderr);
-    fprintf(stderr, "\nhash full node usage:\n");
-    OPENSSL_LH_node_usage_stats((OPENSSL_LHASH *)h, stderr);
+    TEST_info("hash full statistics:");
+    OPENSSL_LH_stats_bio((OPENSSL_LHASH *)h, bio_err);
+    TEST_note("hash full node usage:");
+    OPENSSL_LH_node_usage_stats_bio((OPENSSL_LHASH *)h, bio_err);
 
     /* delete in a different order */
     for (i = 0; i < n; i++) {
         const int j = (7 * i + 4) % n * 3 + 1;
 
-        if ((p = lh_int_delete(h, &j)) == NULL) {
-            fprintf(stderr, "test lhash stress delete %d\n", i);
+        if (!TEST_ptr(p = lh_int_delete(h, &j))) {
+            TEST_info("lhash stress delete %d\n", i);
             goto end;
         }
-        if (*p != j) {
-            fprintf(stderr, "test lhash stress bad value %d\n", i);
+        if (!TEST_int_eq(*p, j)) {
+            TEST_info("lhash stress bad value %d", i);
             goto end;
         }
         OPENSSL_free(p);
     }
 
-    fprintf(stderr, "\nhash empty statistics:\n");
-    OPENSSL_LH_stats((OPENSSL_LHASH *)h, stderr);
-    fprintf(stderr, "\nhash empty node usage:\n");
-    OPENSSL_LH_node_usage_stats((OPENSSL_LHASH *)h, stderr);
+    TEST_info("hash empty statistics:");
+    OPENSSL_LH_stats_bio((OPENSSL_LHASH *)h, bio_err);
+    TEST_note("hash empty node usage:");
+    OPENSSL_LH_node_usage_stats_bio((OPENSSL_LHASH *)h, bio_err);
 
     testresult = 1;
 end:
@@ -242,8 +220,9 @@ end:
     return testresult;
 }
 
-void register_tests(void)
+int setup_tests(void)
 {
     ADD_TEST(test_int_lhash);
     ADD_TEST(test_stress);
+    return 1;
 }