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