1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci * Copyright (c) 2017, Oracle and/or its affiliates.  All rights reserved.
4e1051a39Sopenharmony_ci *
5e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
6e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
7e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
8e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
9e1051a39Sopenharmony_ci */
10e1051a39Sopenharmony_ci
11e1051a39Sopenharmony_ci#include <stdio.h>
12e1051a39Sopenharmony_ci#include <string.h>
13e1051a39Sopenharmony_ci
14e1051a39Sopenharmony_ci#include <openssl/opensslconf.h>
15e1051a39Sopenharmony_ci#include <openssl/safestack.h>
16e1051a39Sopenharmony_ci#include <openssl/err.h>
17e1051a39Sopenharmony_ci#include <openssl/crypto.h>
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_ci#include "internal/nelem.h"
20e1051a39Sopenharmony_ci#include "testutil.h"
21e1051a39Sopenharmony_ci
22e1051a39Sopenharmony_ci/* The macros below generate unused functions which error out one of the clang
23e1051a39Sopenharmony_ci * builds.  We disable this check here.
24e1051a39Sopenharmony_ci */
25e1051a39Sopenharmony_ci#ifdef __clang__
26e1051a39Sopenharmony_ci#pragma clang diagnostic ignored "-Wunused-function"
27e1051a39Sopenharmony_ci#endif
28e1051a39Sopenharmony_ci
29e1051a39Sopenharmony_citypedef struct {
30e1051a39Sopenharmony_ci    int n;
31e1051a39Sopenharmony_ci    char c;
32e1051a39Sopenharmony_ci} SS;
33e1051a39Sopenharmony_ci
34e1051a39Sopenharmony_citypedef union {
35e1051a39Sopenharmony_ci    int n;
36e1051a39Sopenharmony_ci    char c;
37e1051a39Sopenharmony_ci} SU;
38e1051a39Sopenharmony_ci
39e1051a39Sopenharmony_ciDEFINE_SPECIAL_STACK_OF(sint, int)
40e1051a39Sopenharmony_ciDEFINE_SPECIAL_STACK_OF_CONST(uchar, unsigned char)
41e1051a39Sopenharmony_ciDEFINE_STACK_OF(SS)
42e1051a39Sopenharmony_ciDEFINE_STACK_OF_CONST(SU)
43e1051a39Sopenharmony_ci
44e1051a39Sopenharmony_cistatic int int_compare(const int *const *a, const int *const *b)
45e1051a39Sopenharmony_ci{
46e1051a39Sopenharmony_ci    if (**a < **b)
47e1051a39Sopenharmony_ci        return -1;
48e1051a39Sopenharmony_ci    if (**a > **b)
49e1051a39Sopenharmony_ci        return 1;
50e1051a39Sopenharmony_ci    return 0;
51e1051a39Sopenharmony_ci}
52e1051a39Sopenharmony_ci
53e1051a39Sopenharmony_cistatic int test_int_stack(int reserve)
54e1051a39Sopenharmony_ci{
55e1051a39Sopenharmony_ci    static int v[] = { 1, 2, -4, 16, 999, 1, -173, 1, 9 };
56e1051a39Sopenharmony_ci    static int notpresent = -1;
57e1051a39Sopenharmony_ci    const int n = OSSL_NELEM(v);
58e1051a39Sopenharmony_ci    static struct {
59e1051a39Sopenharmony_ci        int value;
60e1051a39Sopenharmony_ci        int unsorted;
61e1051a39Sopenharmony_ci        int sorted;
62e1051a39Sopenharmony_ci        int ex;
63e1051a39Sopenharmony_ci    } finds[] = {
64e1051a39Sopenharmony_ci        { 2,    1,  5,  5   },
65e1051a39Sopenharmony_ci        { 9,    7,  6,  6   },
66e1051a39Sopenharmony_ci        { -173, 5,  0,  0   },
67e1051a39Sopenharmony_ci        { 999,  3,  8,  8   },
68e1051a39Sopenharmony_ci        { 0,   -1, -1,  1   }
69e1051a39Sopenharmony_ci    };
70e1051a39Sopenharmony_ci    const int n_finds = OSSL_NELEM(finds);
71e1051a39Sopenharmony_ci    static struct {
72e1051a39Sopenharmony_ci        int value;
73e1051a39Sopenharmony_ci        int ex;
74e1051a39Sopenharmony_ci    } exfinds[] = {
75e1051a39Sopenharmony_ci        { 3,    5   },
76e1051a39Sopenharmony_ci        { 1000, 8   },
77e1051a39Sopenharmony_ci        { 20,   8   },
78e1051a39Sopenharmony_ci        { -999, 0   },
79e1051a39Sopenharmony_ci        { -5,   0   },
80e1051a39Sopenharmony_ci        { 8,    5   }
81e1051a39Sopenharmony_ci    };
82e1051a39Sopenharmony_ci    const int n_exfinds = OSSL_NELEM(exfinds);
83e1051a39Sopenharmony_ci    STACK_OF(sint) *s = sk_sint_new_null();
84e1051a39Sopenharmony_ci    int i;
85e1051a39Sopenharmony_ci    int testresult = 0;
86e1051a39Sopenharmony_ci
87e1051a39Sopenharmony_ci    if (!TEST_ptr(s)
88e1051a39Sopenharmony_ci        || (reserve > 0 && !TEST_true(sk_sint_reserve(s, 5 * reserve))))
89e1051a39Sopenharmony_ci        goto end;
90e1051a39Sopenharmony_ci
91e1051a39Sopenharmony_ci    /* Check push and num */
92e1051a39Sopenharmony_ci    for (i = 0; i < n; i++) {
93e1051a39Sopenharmony_ci        if (!TEST_int_eq(sk_sint_num(s), i)) {
94e1051a39Sopenharmony_ci            TEST_info("int stack size %d", i);
95e1051a39Sopenharmony_ci            goto end;
96e1051a39Sopenharmony_ci        }
97e1051a39Sopenharmony_ci        sk_sint_push(s, v + i);
98e1051a39Sopenharmony_ci    }
99e1051a39Sopenharmony_ci    if (!TEST_int_eq(sk_sint_num(s), n))
100e1051a39Sopenharmony_ci        goto end;
101e1051a39Sopenharmony_ci
102e1051a39Sopenharmony_ci    /* check the values */
103e1051a39Sopenharmony_ci    for (i = 0; i < n; i++)
104e1051a39Sopenharmony_ci        if (!TEST_ptr_eq(sk_sint_value(s, i), v + i)) {
105e1051a39Sopenharmony_ci            TEST_info("int value %d", i);
106e1051a39Sopenharmony_ci            goto end;
107e1051a39Sopenharmony_ci        }
108e1051a39Sopenharmony_ci
109e1051a39Sopenharmony_ci    /* find unsorted -- the pointers are compared */
110e1051a39Sopenharmony_ci    for (i = 0; i < n_finds; i++) {
111e1051a39Sopenharmony_ci        int *val = (finds[i].unsorted == -1) ? &notpresent
112e1051a39Sopenharmony_ci                                             : v + finds[i].unsorted;
113e1051a39Sopenharmony_ci
114e1051a39Sopenharmony_ci        if (!TEST_int_eq(sk_sint_find(s, val), finds[i].unsorted)) {
115e1051a39Sopenharmony_ci            TEST_info("int unsorted find %d", i);
116e1051a39Sopenharmony_ci            goto end;
117e1051a39Sopenharmony_ci        }
118e1051a39Sopenharmony_ci    }
119e1051a39Sopenharmony_ci
120e1051a39Sopenharmony_ci    /* find_ex unsorted */
121e1051a39Sopenharmony_ci    for (i = 0; i < n_finds; i++) {
122e1051a39Sopenharmony_ci        int *val = (finds[i].unsorted == -1) ? &notpresent
123e1051a39Sopenharmony_ci                                             : v + finds[i].unsorted;
124e1051a39Sopenharmony_ci
125e1051a39Sopenharmony_ci        if (!TEST_int_eq(sk_sint_find_ex(s, val), finds[i].unsorted)) {
126e1051a39Sopenharmony_ci            TEST_info("int unsorted find_ex %d", i);
127e1051a39Sopenharmony_ci            goto end;
128e1051a39Sopenharmony_ci        }
129e1051a39Sopenharmony_ci    }
130e1051a39Sopenharmony_ci
131e1051a39Sopenharmony_ci    /* sorting */
132e1051a39Sopenharmony_ci    if (!TEST_false(sk_sint_is_sorted(s)))
133e1051a39Sopenharmony_ci        goto end;
134e1051a39Sopenharmony_ci    (void)sk_sint_set_cmp_func(s, &int_compare);
135e1051a39Sopenharmony_ci    sk_sint_sort(s);
136e1051a39Sopenharmony_ci    if (!TEST_true(sk_sint_is_sorted(s)))
137e1051a39Sopenharmony_ci        goto end;
138e1051a39Sopenharmony_ci
139e1051a39Sopenharmony_ci    /* find sorted -- the value is matched so we don't need to locate it */
140e1051a39Sopenharmony_ci    for (i = 0; i < n_finds; i++)
141e1051a39Sopenharmony_ci        if (!TEST_int_eq(sk_sint_find(s, &finds[i].value), finds[i].sorted)) {
142e1051a39Sopenharmony_ci            TEST_info("int sorted find %d", i);
143e1051a39Sopenharmony_ci            goto end;
144e1051a39Sopenharmony_ci        }
145e1051a39Sopenharmony_ci
146e1051a39Sopenharmony_ci    /* find_ex sorted */
147e1051a39Sopenharmony_ci    for (i = 0; i < n_finds; i++)
148e1051a39Sopenharmony_ci        if (!TEST_int_eq(sk_sint_find_ex(s, &finds[i].value), finds[i].ex)) {
149e1051a39Sopenharmony_ci            TEST_info("int sorted find_ex present %d", i);
150e1051a39Sopenharmony_ci            goto end;
151e1051a39Sopenharmony_ci        }
152e1051a39Sopenharmony_ci    for (i = 0; i < n_exfinds; i++)
153e1051a39Sopenharmony_ci        if (!TEST_int_eq(sk_sint_find_ex(s, &exfinds[i].value), exfinds[i].ex)){
154e1051a39Sopenharmony_ci            TEST_info("int sorted find_ex absent %d", i);
155e1051a39Sopenharmony_ci            goto end;
156e1051a39Sopenharmony_ci        }
157e1051a39Sopenharmony_ci
158e1051a39Sopenharmony_ci    /* shift */
159e1051a39Sopenharmony_ci    if (!TEST_ptr_eq(sk_sint_shift(s), v + 6))
160e1051a39Sopenharmony_ci        goto end;
161e1051a39Sopenharmony_ci
162e1051a39Sopenharmony_ci    testresult = 1;
163e1051a39Sopenharmony_ciend:
164e1051a39Sopenharmony_ci    sk_sint_free(s);
165e1051a39Sopenharmony_ci    return testresult;
166e1051a39Sopenharmony_ci}
167e1051a39Sopenharmony_ci
168e1051a39Sopenharmony_cistatic int uchar_compare(const unsigned char *const *a,
169e1051a39Sopenharmony_ci                         const unsigned char *const *b)
170e1051a39Sopenharmony_ci{
171e1051a39Sopenharmony_ci    return **a - (signed int)**b;
172e1051a39Sopenharmony_ci}
173e1051a39Sopenharmony_ci
174e1051a39Sopenharmony_cistatic int test_uchar_stack(int reserve)
175e1051a39Sopenharmony_ci{
176e1051a39Sopenharmony_ci    static const unsigned char v[] = { 1, 3, 7, 5, 255, 0 };
177e1051a39Sopenharmony_ci    const int n = OSSL_NELEM(v);
178e1051a39Sopenharmony_ci    STACK_OF(uchar) *s = sk_uchar_new(&uchar_compare), *r = NULL;
179e1051a39Sopenharmony_ci    int i;
180e1051a39Sopenharmony_ci    int testresult = 0;
181e1051a39Sopenharmony_ci
182e1051a39Sopenharmony_ci    if (!TEST_ptr(s)
183e1051a39Sopenharmony_ci        || (reserve > 0 && !TEST_true(sk_uchar_reserve(s, 5 * reserve))))
184e1051a39Sopenharmony_ci        goto end;
185e1051a39Sopenharmony_ci
186e1051a39Sopenharmony_ci    /* unshift and num */
187e1051a39Sopenharmony_ci    for (i = 0; i < n; i++) {
188e1051a39Sopenharmony_ci        if (!TEST_int_eq(sk_uchar_num(s), i)) {
189e1051a39Sopenharmony_ci            TEST_info("uchar stack size %d", i);
190e1051a39Sopenharmony_ci            goto end;
191e1051a39Sopenharmony_ci        }
192e1051a39Sopenharmony_ci        sk_uchar_unshift(s, v + i);
193e1051a39Sopenharmony_ci    }
194e1051a39Sopenharmony_ci    if (!TEST_int_eq(sk_uchar_num(s), n))
195e1051a39Sopenharmony_ci        goto end;
196e1051a39Sopenharmony_ci
197e1051a39Sopenharmony_ci    /* dup */
198e1051a39Sopenharmony_ci    r = sk_uchar_dup(NULL);
199e1051a39Sopenharmony_ci    if (sk_uchar_num(r) != 0)
200e1051a39Sopenharmony_ci        goto end;
201e1051a39Sopenharmony_ci    sk_uchar_free(r);
202e1051a39Sopenharmony_ci    r = sk_uchar_dup(s);
203e1051a39Sopenharmony_ci    if (!TEST_int_eq(sk_uchar_num(r), n))
204e1051a39Sopenharmony_ci        goto end;
205e1051a39Sopenharmony_ci    sk_uchar_sort(r);
206e1051a39Sopenharmony_ci
207e1051a39Sopenharmony_ci    /* pop */
208e1051a39Sopenharmony_ci    for (i = 0; i < n; i++)
209e1051a39Sopenharmony_ci        if (!TEST_ptr_eq(sk_uchar_pop(s), v + i)) {
210e1051a39Sopenharmony_ci            TEST_info("uchar pop %d", i);
211e1051a39Sopenharmony_ci            goto end;
212e1051a39Sopenharmony_ci        }
213e1051a39Sopenharmony_ci
214e1051a39Sopenharmony_ci    /* free -- we rely on the debug malloc to detect leakage here */
215e1051a39Sopenharmony_ci    sk_uchar_free(s);
216e1051a39Sopenharmony_ci    s = NULL;
217e1051a39Sopenharmony_ci
218e1051a39Sopenharmony_ci    /* dup again */
219e1051a39Sopenharmony_ci    if (!TEST_int_eq(sk_uchar_num(r), n))
220e1051a39Sopenharmony_ci        goto end;
221e1051a39Sopenharmony_ci
222e1051a39Sopenharmony_ci    /* zero */
223e1051a39Sopenharmony_ci    sk_uchar_zero(r);
224e1051a39Sopenharmony_ci    if (!TEST_int_eq(sk_uchar_num(r), 0))
225e1051a39Sopenharmony_ci        goto end;
226e1051a39Sopenharmony_ci
227e1051a39Sopenharmony_ci    /* insert */
228e1051a39Sopenharmony_ci    sk_uchar_insert(r, v, 0);
229e1051a39Sopenharmony_ci    sk_uchar_insert(r, v + 2, -1);
230e1051a39Sopenharmony_ci    sk_uchar_insert(r, v + 1, 1);
231e1051a39Sopenharmony_ci    for (i = 0; i < 3; i++)
232e1051a39Sopenharmony_ci        if (!TEST_ptr_eq(sk_uchar_value(r, i), v + i)) {
233e1051a39Sopenharmony_ci            TEST_info("uchar insert %d", i);
234e1051a39Sopenharmony_ci            goto end;
235e1051a39Sopenharmony_ci        }
236e1051a39Sopenharmony_ci
237e1051a39Sopenharmony_ci    /* delete */
238e1051a39Sopenharmony_ci    if (!TEST_ptr_null(sk_uchar_delete(r, 12)))
239e1051a39Sopenharmony_ci        goto end;
240e1051a39Sopenharmony_ci    if (!TEST_ptr_eq(sk_uchar_delete(r, 1), v + 1))
241e1051a39Sopenharmony_ci        goto end;
242e1051a39Sopenharmony_ci
243e1051a39Sopenharmony_ci    /* set */
244e1051a39Sopenharmony_ci    (void)sk_uchar_set(r, 1, v + 1);
245e1051a39Sopenharmony_ci    for (i = 0; i < 2; i++)
246e1051a39Sopenharmony_ci        if (!TEST_ptr_eq(sk_uchar_value(r, i), v + i)) {
247e1051a39Sopenharmony_ci            TEST_info("uchar set %d", i);
248e1051a39Sopenharmony_ci            goto end;
249e1051a39Sopenharmony_ci        }
250e1051a39Sopenharmony_ci
251e1051a39Sopenharmony_ci    testresult = 1;
252e1051a39Sopenharmony_ciend:
253e1051a39Sopenharmony_ci    sk_uchar_free(r);
254e1051a39Sopenharmony_ci    sk_uchar_free(s);
255e1051a39Sopenharmony_ci    return testresult;
256e1051a39Sopenharmony_ci}
257e1051a39Sopenharmony_ci
258e1051a39Sopenharmony_cistatic SS *SS_copy(const SS *p)
259e1051a39Sopenharmony_ci{
260e1051a39Sopenharmony_ci    SS *q = OPENSSL_malloc(sizeof(*q));
261e1051a39Sopenharmony_ci
262e1051a39Sopenharmony_ci    if (q != NULL)
263e1051a39Sopenharmony_ci        memcpy(q, p, sizeof(*q));
264e1051a39Sopenharmony_ci    return q;
265e1051a39Sopenharmony_ci}
266e1051a39Sopenharmony_ci
267e1051a39Sopenharmony_cistatic void SS_free(SS *p) {
268e1051a39Sopenharmony_ci    OPENSSL_free(p);
269e1051a39Sopenharmony_ci}
270e1051a39Sopenharmony_ci
271e1051a39Sopenharmony_cistatic int test_SS_stack(void)
272e1051a39Sopenharmony_ci{
273e1051a39Sopenharmony_ci    STACK_OF(SS) *s = sk_SS_new_null();
274e1051a39Sopenharmony_ci    STACK_OF(SS) *r = NULL;
275e1051a39Sopenharmony_ci    SS *v[10], *p;
276e1051a39Sopenharmony_ci    const int n = OSSL_NELEM(v);
277e1051a39Sopenharmony_ci    int i;
278e1051a39Sopenharmony_ci    int testresult = 0;
279e1051a39Sopenharmony_ci
280e1051a39Sopenharmony_ci    /* allocate and push */
281e1051a39Sopenharmony_ci    for (i = 0; i < n; i++) {
282e1051a39Sopenharmony_ci        v[i] = OPENSSL_malloc(sizeof(*v[i]));
283e1051a39Sopenharmony_ci
284e1051a39Sopenharmony_ci        if (!TEST_ptr(v[i]))
285e1051a39Sopenharmony_ci            goto end;
286e1051a39Sopenharmony_ci        v[i]->n = i;
287e1051a39Sopenharmony_ci        v[i]->c = 'A' + i;
288e1051a39Sopenharmony_ci        if (!TEST_int_eq(sk_SS_num(s), i)) {
289e1051a39Sopenharmony_ci            TEST_info("SS stack size %d", i);
290e1051a39Sopenharmony_ci            goto end;
291e1051a39Sopenharmony_ci        }
292e1051a39Sopenharmony_ci        sk_SS_push(s, v[i]);
293e1051a39Sopenharmony_ci    }
294e1051a39Sopenharmony_ci    if (!TEST_int_eq(sk_SS_num(s), n))
295e1051a39Sopenharmony_ci        goto end;
296e1051a39Sopenharmony_ci
297e1051a39Sopenharmony_ci    /* deepcopy */
298e1051a39Sopenharmony_ci    r = sk_SS_deep_copy(NULL, &SS_copy, &SS_free);
299e1051a39Sopenharmony_ci    if (sk_SS_num(r) != 0)
300e1051a39Sopenharmony_ci        goto end;
301e1051a39Sopenharmony_ci    sk_SS_free(r);
302e1051a39Sopenharmony_ci    r = sk_SS_deep_copy(s, &SS_copy, &SS_free);
303e1051a39Sopenharmony_ci    if (!TEST_ptr(r))
304e1051a39Sopenharmony_ci        goto end;
305e1051a39Sopenharmony_ci    for (i = 0; i < n; i++) {
306e1051a39Sopenharmony_ci        p = sk_SS_value(r, i);
307e1051a39Sopenharmony_ci        if (!TEST_ptr_ne(p, v[i])) {
308e1051a39Sopenharmony_ci            TEST_info("SS deepcopy non-copy %d", i);
309e1051a39Sopenharmony_ci            goto end;
310e1051a39Sopenharmony_ci        }
311e1051a39Sopenharmony_ci        if (!TEST_int_eq(p->n, v[i]->n)) {
312e1051a39Sopenharmony_ci            TEST_info("test SS deepcopy int %d", i);
313e1051a39Sopenharmony_ci            goto end;
314e1051a39Sopenharmony_ci        }
315e1051a39Sopenharmony_ci        if (!TEST_char_eq(p->c, v[i]->c)) {
316e1051a39Sopenharmony_ci            TEST_info("SS deepcopy char %d", i);
317e1051a39Sopenharmony_ci            goto end;
318e1051a39Sopenharmony_ci        }
319e1051a39Sopenharmony_ci    }
320e1051a39Sopenharmony_ci
321e1051a39Sopenharmony_ci    /* pop_free - we rely on the malloc debug to catch the leak */
322e1051a39Sopenharmony_ci    sk_SS_pop_free(r, &SS_free);
323e1051a39Sopenharmony_ci    r = NULL;
324e1051a39Sopenharmony_ci
325e1051a39Sopenharmony_ci    /* delete_ptr */
326e1051a39Sopenharmony_ci    p = sk_SS_delete_ptr(s, v[3]);
327e1051a39Sopenharmony_ci    if (!TEST_ptr(p))
328e1051a39Sopenharmony_ci        goto end;
329e1051a39Sopenharmony_ci    SS_free(p);
330e1051a39Sopenharmony_ci    if (!TEST_int_eq(sk_SS_num(s), n - 1))
331e1051a39Sopenharmony_ci        goto end;
332e1051a39Sopenharmony_ci    for (i = 0; i < n-1; i++)
333e1051a39Sopenharmony_ci        if (!TEST_ptr_eq(sk_SS_value(s, i), v[i<3 ? i : 1+i])) {
334e1051a39Sopenharmony_ci            TEST_info("SS delete ptr item %d", i);
335e1051a39Sopenharmony_ci            goto end;
336e1051a39Sopenharmony_ci        }
337e1051a39Sopenharmony_ci
338e1051a39Sopenharmony_ci    testresult = 1;
339e1051a39Sopenharmony_ciend:
340e1051a39Sopenharmony_ci    sk_SS_pop_free(r, &SS_free);
341e1051a39Sopenharmony_ci    sk_SS_pop_free(s, &SS_free);
342e1051a39Sopenharmony_ci    return testresult;
343e1051a39Sopenharmony_ci}
344e1051a39Sopenharmony_ci
345e1051a39Sopenharmony_cistatic int test_SU_stack(void)
346e1051a39Sopenharmony_ci{
347e1051a39Sopenharmony_ci    STACK_OF(SU) *s = sk_SU_new_null();
348e1051a39Sopenharmony_ci    SU v[10];
349e1051a39Sopenharmony_ci    const int n = OSSL_NELEM(v);
350e1051a39Sopenharmony_ci    int i;
351e1051a39Sopenharmony_ci    int testresult = 0;
352e1051a39Sopenharmony_ci
353e1051a39Sopenharmony_ci    /* allocate and push */
354e1051a39Sopenharmony_ci    for (i = 0; i < n; i++) {
355e1051a39Sopenharmony_ci        if ((i & 1) == 0)
356e1051a39Sopenharmony_ci            v[i].n = i;
357e1051a39Sopenharmony_ci        else
358e1051a39Sopenharmony_ci            v[i].c = 'A' + i;
359e1051a39Sopenharmony_ci        if (!TEST_int_eq(sk_SU_num(s), i)) {
360e1051a39Sopenharmony_ci            TEST_info("SU stack size %d", i);
361e1051a39Sopenharmony_ci            goto end;
362e1051a39Sopenharmony_ci        }
363e1051a39Sopenharmony_ci        sk_SU_push(s, v + i);
364e1051a39Sopenharmony_ci    }
365e1051a39Sopenharmony_ci    if (!TEST_int_eq(sk_SU_num(s), n))
366e1051a39Sopenharmony_ci        goto end;
367e1051a39Sopenharmony_ci
368e1051a39Sopenharmony_ci    /* check the pointers are correct */
369e1051a39Sopenharmony_ci    for (i = 0; i < n; i++)
370e1051a39Sopenharmony_ci        if (!TEST_ptr_eq(sk_SU_value(s, i),  v + i)) {
371e1051a39Sopenharmony_ci            TEST_info("SU pointer check %d", i);
372e1051a39Sopenharmony_ci            goto end;
373e1051a39Sopenharmony_ci        }
374e1051a39Sopenharmony_ci
375e1051a39Sopenharmony_ci    testresult = 1;
376e1051a39Sopenharmony_ciend:
377e1051a39Sopenharmony_ci    sk_SU_free(s);
378e1051a39Sopenharmony_ci    return testresult;
379e1051a39Sopenharmony_ci}
380e1051a39Sopenharmony_ci
381e1051a39Sopenharmony_ciint setup_tests(void)
382e1051a39Sopenharmony_ci{
383e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_int_stack, 4);
384e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_uchar_stack, 4);
385e1051a39Sopenharmony_ci    ADD_TEST(test_SS_stack);
386e1051a39Sopenharmony_ci    ADD_TEST(test_SU_stack);
387e1051a39Sopenharmony_ci    return 1;
388e1051a39Sopenharmony_ci}
389