Fix no-tls1_2
[openssl.git] / test / stack_test.c
index 43257669661a0898e9c56f9aa8d58be77a9d214c..680f68d60e69b51d3a301adf6a5f60d96f2b0e4e 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
 /*
  * 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
  *
  * 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
  */
 
  * https://www.openssl.org/source/license.html
  */
 
-/* ====================================================================
- * Copyright (c) 2017 Oracle and/or its affiliates.  All rights reserved.
- */
-
 #include <stdio.h>
 #include <string.h>
 
 #include <stdio.h>
 #include <string.h>
 
@@ -19,8 +16,7 @@
 #include <openssl/err.h>
 #include <openssl/crypto.h>
 
 #include <openssl/err.h>
 #include <openssl/crypto.h>
 
-#include "e_os.h"
-#include "test_main.h"
+#include "internal/nelem.h"
 #include "testutil.h"
 
 /* The macros below generate unused functions which error out one of the clang
 #include "testutil.h"
 
 /* The macros below generate unused functions which error out one of the clang
@@ -54,7 +50,7 @@ static int int_compare(const int *const *a, const int *const *b)
     return 0;
 }
 
     return 0;
 }
 
-static int test_int_stack(void)
+static int test_int_stack(int reserve)
 {
     static int v[] = { 1, 2, -4, 16, 999, 1, -173, 1, 9 };
     static int notpresent = -1;
 {
     static int v[] = { 1, 2, -4, 16, 999, 1, -173, 1, 9 };
     static int notpresent = -1;
@@ -88,23 +84,25 @@ static int test_int_stack(void)
     int i;
     int testresult = 0;
 
     int i;
     int testresult = 0;
 
+    if (!TEST_ptr(s)
+        || (reserve > 0 && !TEST_true(sk_sint_reserve(s, 5 * reserve))))
+        goto end;
+
     /* Check push and num */
     for (i = 0; i < n; i++) {
     /* Check push and num */
     for (i = 0; i < n; i++) {
-        if (sk_sint_num(s) != i) {
-            fprintf(stderr, "test int stack size %d\n", i);
+        if (!TEST_int_eq(sk_sint_num(s), i)) {
+            TEST_info("int stack size %d", i);
             goto end;
         }
         sk_sint_push(s, v + i);
     }
             goto end;
         }
         sk_sint_push(s, v + i);
     }
-    if (sk_sint_num(s) != n) {
-        fprintf(stderr, "test int stack size %d\n", n);
+    if (!TEST_int_eq(sk_sint_num(s), n))
         goto end;
         goto end;
-    }
 
     /* check the values */
     for (i = 0; i < n; i++)
 
     /* check the values */
     for (i = 0; i < n; i++)
-        if (sk_sint_value(s, i) != v + i) {
-            fprintf(stderr, "test int value %d\n", i);
+        if (!TEST_ptr_eq(sk_sint_value(s, i), v + i)) {
+            TEST_info("int value %d", i);
             goto end;
         }
 
             goto end;
         }
 
@@ -113,8 +111,8 @@ static int test_int_stack(void)
         int *val = (finds[i].unsorted == -1) ? &notpresent
                                              : v + finds[i].unsorted;
 
         int *val = (finds[i].unsorted == -1) ? &notpresent
                                              : v + finds[i].unsorted;
 
-        if (sk_sint_find(s, val) != finds[i].unsorted) {
-            fprintf(stderr, "test int unsorted find %d\n", i);
+        if (!TEST_int_eq(sk_sint_find(s, val), finds[i].unsorted)) {
+            TEST_info("int unsorted find %d", i);
             goto end;
         }
     }
             goto end;
         }
     }
@@ -124,48 +122,42 @@ static int test_int_stack(void)
         int *val = (finds[i].unsorted == -1) ? &notpresent
                                              : v + finds[i].unsorted;
 
         int *val = (finds[i].unsorted == -1) ? &notpresent
                                              : v + finds[i].unsorted;
 
-        if (sk_sint_find_ex(s, val) != finds[i].unsorted) {
-            fprintf(stderr, "test int unsorted find_ex %d\n", i);
+        if (!TEST_int_eq(sk_sint_find_ex(s, val), finds[i].unsorted)) {
+            TEST_info("int unsorted find_ex %d", i);
             goto end;
         }
     }
 
     /* sorting */
             goto end;
         }
     }
 
     /* sorting */
-    if (sk_sint_is_sorted(s)) {
-        fprintf(stderr, "test int unsorted\n");
+    if (!TEST_false(sk_sint_is_sorted(s)))
         goto end;
         goto end;
-    }
     sk_sint_set_cmp_func(s, &int_compare);
     sk_sint_sort(s);
     sk_sint_set_cmp_func(s, &int_compare);
     sk_sint_sort(s);
-    if (!sk_sint_is_sorted(s)) {
-        fprintf(stderr, "test int sorted\n");
+    if (!TEST_true(sk_sint_is_sorted(s)))
         goto end;
         goto end;
-    }
 
     /* find sorted -- the value is matched so we don't need to locate it */
     for (i = 0; i < n_finds; i++)
 
     /* find sorted -- the value is matched so we don't need to locate it */
     for (i = 0; i < n_finds; i++)
-        if (sk_sint_find(s, &finds[i].value) != finds[i].sorted) {
-            fprintf(stderr, "test int sorted find %d\n", i);
+        if (!TEST_int_eq(sk_sint_find(s, &finds[i].value), finds[i].sorted)) {
+            TEST_info("int sorted find %d", i);
             goto end;
         }
 
     /* find_ex sorted */
     for (i = 0; i < n_finds; i++)
             goto end;
         }
 
     /* find_ex sorted */
     for (i = 0; i < n_finds; i++)
-        if (sk_sint_find_ex(s, &finds[i].value) != finds[i].ex) {
-            fprintf(stderr, "test int sorted find_ex present %d\n", i);
+        if (!TEST_int_eq(sk_sint_find_ex(s, &finds[i].value), finds[i].ex)) {
+            TEST_info("int sorted find_ex present %d", i);
             goto end;
         }
     for (i = 0; i < n_exfinds; i++)
             goto end;
         }
     for (i = 0; i < n_exfinds; i++)
-        if (sk_sint_find_ex(s, &exfinds[i].value) != exfinds[i].ex) {
-            fprintf(stderr, "test int sorted find_ex absent %d\n", i);
+        if (!TEST_int_eq(sk_sint_find_ex(s, &exfinds[i].value), exfinds[i].ex)){
+            TEST_info("int sorted find_ex absent %d", i);
             goto end;
         }
 
     /* shift */
             goto end;
         }
 
     /* shift */
-    if (sk_sint_shift(s) != v + 6) {
-        fprintf(stderr, "test int shift\n");
+    if (!TEST_ptr_eq(sk_sint_shift(s), v + 6))
         goto end;
         goto end;
-    }
 
     testresult = 1;
 end:
 
     testresult = 1;
 end:
@@ -179,7 +171,7 @@ static int uchar_compare(const unsigned char *const *a,
     return **a - (signed int)**b;
 }
 
     return **a - (signed int)**b;
 }
 
-static int test_uchar_stack(void)
+static int test_uchar_stack(int reserve)
 {
     static const unsigned char v[] = { 1, 3, 7, 5, 255, 0 };
     const int n = OSSL_NELEM(v);
 {
     static const unsigned char v[] = { 1, 3, 7, 5, 255, 0 };
     const int n = OSSL_NELEM(v);
@@ -187,31 +179,31 @@ static int test_uchar_stack(void)
     int i;
     int testresult = 0;
 
     int i;
     int testresult = 0;
 
+    if (!TEST_ptr(s)
+        || (reserve > 0 && !TEST_true(sk_uchar_reserve(s, 5 * reserve))))
+        goto end;
+
     /* unshift and num */
     for (i = 0; i < n; i++) {
     /* unshift and num */
     for (i = 0; i < n; i++) {
-        if (sk_uchar_num(s) != i) {
-            fprintf(stderr, "test uchar stack size %d\n", i);
+        if (!TEST_int_eq(sk_uchar_num(s), i)) {
+            TEST_info("uchar stack size %d", i);
             goto end;
         }
         sk_uchar_unshift(s, v + i);
     }
             goto end;
         }
         sk_uchar_unshift(s, v + i);
     }
-    if (sk_uchar_num(s) != n) {
-        fprintf(stderr, "test uchar stack size %d\n", n);
+    if (!TEST_int_eq(sk_uchar_num(s), n))
         goto end;
         goto end;
-    }
 
     /* dup */
     r = sk_uchar_dup(s);
 
     /* dup */
     r = sk_uchar_dup(s);
-    if (sk_uchar_num(r) != n) {
-        fprintf(stderr, "test uchar dup size %d\n", n);
+    if (!TEST_int_eq(sk_uchar_num(r), n))
         goto end;
         goto end;
-    }
     sk_uchar_sort(r);
 
     /* pop */
     sk_uchar_sort(r);
 
     /* pop */
-    for (i = 0; i < n; i++) 
-        if (sk_uchar_pop(s) != v + i) {
-            fprintf(stderr, "test uchar pop %d\n", i);
+    for (i = 0; i < n; i++)
+        if (!TEST_ptr_eq(sk_uchar_pop(s), v + i)) {
+            TEST_info("uchar pop %d", i);
             goto end;
         }
 
             goto end;
         }
 
@@ -220,43 +212,35 @@ static int test_uchar_stack(void)
     s = NULL;
 
     /* dup again */
     s = NULL;
 
     /* dup again */
-    if (sk_uchar_num(r) != n) {
-        fprintf(stderr, "test uchar dup size %d\n", n);
+    if (!TEST_int_eq(sk_uchar_num(r), n))
         goto end;
         goto end;
-    }
 
     /* zero */
     sk_uchar_zero(r);
 
     /* zero */
     sk_uchar_zero(r);
-    if (sk_uchar_num(r) != 0) {
-        fprintf(stderr, "test uchar zero %d\n", n);
+    if (!TEST_int_eq(sk_uchar_num(r), 0))
         goto end;
         goto end;
-    }
 
     /* insert */
     sk_uchar_insert(r, v, 0);
     sk_uchar_insert(r, v + 2, -1);
     sk_uchar_insert(r, v + 1, 1);
     for (i = 0; i < 3; i++)
 
     /* insert */
     sk_uchar_insert(r, v, 0);
     sk_uchar_insert(r, v + 2, -1);
     sk_uchar_insert(r, v + 1, 1);
     for (i = 0; i < 3; i++)
-        if (sk_uchar_value(r, i) != v + i) {
-            fprintf(stderr, "test uchar insert %d\n", i);
+        if (!TEST_ptr_eq(sk_uchar_value(r, i), v + i)) {
+            TEST_info("uchar insert %d", i);
             goto end;
         }
 
     /* delete */
             goto end;
         }
 
     /* delete */
-    if (sk_uchar_delete(r, 12) != NULL) {
-        fprintf(stderr, "test uchar delete missing %d\n", n);
+    if (!TEST_ptr_null(sk_uchar_delete(r, 12)))
         goto end;
         goto end;
-    }
-    if (sk_uchar_delete(r, 1) != v + 1) {
-        fprintf(stderr, "test uchar delete middle %d\n", n);
+    if (!TEST_ptr_eq(sk_uchar_delete(r, 1), v + 1))
         goto end;
         goto end;
-    }
 
     /* set */
     sk_uchar_set(r, 1, v + 1);
     for (i = 0; i < 2; i++)
 
     /* set */
     sk_uchar_set(r, 1, v + 1);
     for (i = 0; i < 2; i++)
-        if (sk_uchar_value(r, i) != v + i) {
-            fprintf(stderr, "test uchar set %d\n", i);
+        if (!TEST_ptr_eq(sk_uchar_value(r, i), v + i)) {
+            TEST_info("uchar set %d", i);
             goto end;
         }
 
             goto end;
         }
 
@@ -293,37 +277,35 @@ static int test_SS_stack(void)
     for (i = 0; i < n; i++) {
         v[i] = OPENSSL_malloc(sizeof(*v[i]));
 
     for (i = 0; i < n; i++) {
         v[i] = OPENSSL_malloc(sizeof(*v[i]));
 
-        if (v[i] == NULL) {
-            fprintf(stderr, "test SS memory allocation failure\n");
+        if (!TEST_ptr(v[i]))
             goto end;
             goto end;
-        }
         v[i]->n = i;
         v[i]->c = 'A' + i;
         v[i]->n = i;
         v[i]->c = 'A' + i;
-        if (sk_SS_num(s) != i) {
-            fprintf(stderr, "test SS stack size %d\n", i);
+        if (!TEST_int_eq(sk_SS_num(s), i)) {
+            TEST_info("SS stack size %d", i);
             goto end;
         }
         sk_SS_push(s, v[i]);
     }
             goto end;
         }
         sk_SS_push(s, v[i]);
     }
-    if (sk_SS_num(s) != n) {
-        fprintf(stderr, "test SS size %d\n", n);
+    if (!TEST_int_eq(sk_SS_num(s), n))
         goto end;
         goto end;
-    }
 
     /* deepcopy */
     r = sk_SS_deep_copy(s, &SS_copy, &SS_free);
 
     /* deepcopy */
     r = sk_SS_deep_copy(s, &SS_copy, &SS_free);
-    if (r == NULL) {
-        fprintf(stderr, "test SS deepcopy failure\n");
+    if (!TEST_ptr(r))
         goto end;
         goto end;
-    }
     for (i = 0; i < n; i++) {
         p = sk_SS_value(r, i);
     for (i = 0; i < n; i++) {
         p = sk_SS_value(r, i);
-        if (p == v[i]) {
-            fprintf(stderr, "test SS deepcopy non-copy %d\n", i);
+        if (!TEST_ptr_ne(p, v[i])) {
+            TEST_info("SS deepcopy non-copy %d", i);
+            goto end;
+        }
+        if (!TEST_int_eq(p->n, v[i]->n)) {
+            TEST_info("test SS deepcopy int %d", i);
             goto end;
         }
             goto end;
         }
-        if (p->n != v[i]->n || p->c != v[i]->c) {
-            fprintf(stderr, "test SS deepcopy values %d\n", i);
+        if (!TEST_char_eq(p->c, v[i]->c)) {
+            TEST_info("SS deepcopy char %d", i);
             goto end;
         }
     }
             goto end;
         }
     }
@@ -333,18 +315,15 @@ static int test_SS_stack(void)
     r = NULL;
 
     /* delete_ptr */
     r = NULL;
 
     /* delete_ptr */
-    if ((p = sk_SS_delete_ptr(s, v[3])) == NULL) {
-        fprintf(stderr, "test SS delete ptr not found\n");
+    p = sk_SS_delete_ptr(s, v[3]);
+    if (!TEST_ptr(p))
         goto end;
         goto end;
-    }
     SS_free(p);
     SS_free(p);
-    if (sk_SS_num(s) != n-1) {
-        fprintf(stderr, "test SS delete ptr size\n");
+    if (!TEST_int_eq(sk_SS_num(s), n - 1))
         goto end;
         goto end;
-    }
     for (i = 0; i < n-1; i++)
     for (i = 0; i < n-1; i++)
-        if (sk_SS_value(s, i) != v[i<3 ? i : 1+i]) {
-            fprintf(stderr, "test SS delete ptr item %d\n", i);
+        if (!TEST_ptr_eq(sk_SS_value(s, i), v[i<3 ? i : 1+i])) {
+            TEST_info("SS delete ptr item %d", i);
             goto end;
         }
 
             goto end;
         }
 
@@ -369,21 +348,19 @@ static int test_SU_stack(void)
             v[i].n = i;
         else
             v[i].c = 'A' + i;
             v[i].n = i;
         else
             v[i].c = 'A' + i;
-        if (sk_SU_num(s) != i) {
-            fprintf(stderr, "test SU stack size %d\n", i);
+        if (!TEST_int_eq(sk_SU_num(s), i)) {
+            TEST_info("SU stack size %d", i);
             goto end;
         }
         sk_SU_push(s, v + i);
     }
             goto end;
         }
         sk_SU_push(s, v + i);
     }
-    if (sk_SU_num(s) != n) {
-        fprintf(stderr, "test SU size %d\n", n);
+    if (!TEST_int_eq(sk_SU_num(s), n))
         goto end;
         goto end;
-    }
 
     /* check the pointers are correct */
     for (i = 0; i < n; i++)
 
     /* check the pointers are correct */
     for (i = 0; i < n; i++)
-        if (sk_SU_value(s, i) != v + i) {
-            fprintf(stderr, "test SU pointer check %d\n", i);
+        if (!TEST_ptr_eq(sk_SU_value(s, i),  v + i)) {
+            TEST_info("SU pointer check %d", i);
             goto end;
         }
 
             goto end;
         }
 
@@ -393,10 +370,11 @@ end:
     return testresult;
 }
 
     return testresult;
 }
 
-void register_tests(void)
+int setup_tests(void)
 {
 {
-    ADD_TEST(test_int_stack);
-    ADD_TEST(test_uchar_stack);
+    ADD_ALL_TESTS(test_int_stack, 4);
+    ADD_ALL_TESTS(test_uchar_stack, 4);
     ADD_TEST(test_SS_stack);
     ADD_TEST(test_SU_stack);
     ADD_TEST(test_SS_stack);
     ADD_TEST(test_SU_stack);
+    return 1;
 }
 }