Invoke tear_down when exiting test_encode_tls_sct() prematurely
[openssl.git] / test / stack_test.c
index 25b2e001e1ec17111b99f4eee8981f57ce25c26e..d44e6fc93a309b0acbaf90265ebb8e16f0bd9a95 100644 (file)
@@ -1,16 +1,13 @@
 /*
- * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2017-2021 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
+ * Licensed under the Apache License 2.0 (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
  */
 
-/* ====================================================================
- * 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"
 
 /* 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;
 }
 
-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;
@@ -88,6 +84,10 @@ static int test_int_stack(void)
     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++) {
         if (!TEST_int_eq(sk_sint_num(s), i)) {
@@ -131,7 +131,7 @@ static int test_int_stack(void)
     /* sorting */
     if (!TEST_false(sk_sint_is_sorted(s)))
         goto end;
-    sk_sint_set_cmp_func(s, &int_compare);
+    (void)sk_sint_set_cmp_func(s, &int_compare);
     sk_sint_sort(s);
     if (!TEST_true(sk_sint_is_sorted(s)))
         goto end;
@@ -150,7 +150,7 @@ static int test_int_stack(void)
             goto end;
         }
     for (i = 0; i < n_exfinds; i++)
-        if (!TEST_int_eq(sk_sint_find_ex(s, &exfinds[i].value), exfinds[i].ex)){
+        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;
         }
@@ -171,7 +171,7 @@ static int uchar_compare(const unsigned char *const *a,
     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);
@@ -179,6 +179,10 @@ static int test_uchar_stack(void)
     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++) {
         if (!TEST_int_eq(sk_uchar_num(s), i)) {
@@ -191,13 +195,17 @@ static int test_uchar_stack(void)
         goto end;
 
     /* dup */
+    r = sk_uchar_dup(NULL);
+    if (sk_uchar_num(r) != 0)
+        goto end;
+    sk_uchar_free(r);
     r = sk_uchar_dup(s);
     if (!TEST_int_eq(sk_uchar_num(r), n))
         goto end;
     sk_uchar_sort(r);
 
     /* pop */
-    for (i = 0; i < 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;
@@ -233,7 +241,7 @@ static int test_uchar_stack(void)
         goto end;
 
     /* set */
-    sk_uchar_set(r, 1, v + 1);
+    (void)sk_uchar_set(r, 1, v + 1);
     for (i = 0; i < 2; i++)
         if (!TEST_ptr_eq(sk_uchar_value(r, i), v + i)) {
             TEST_info("uchar set %d", i);
@@ -287,6 +295,10 @@ static int test_SS_stack(void)
         goto end;
 
     /* deepcopy */
+    r = sk_SS_deep_copy(NULL, &SS_copy, &SS_free);
+    if (sk_SS_num(r) != 0)
+        goto end;
+    sk_SS_free(r);
     r = sk_SS_deep_copy(s, &SS_copy, &SS_free);
     if (!TEST_ptr(r))
         goto end;
@@ -366,10 +378,11 @@ end:
     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);
+    return 1;
 }