]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/stack_test.c
2 * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
14 #include <openssl/opensslconf.h>
15 #include <openssl/safestack.h>
16 #include <openssl/err.h>
17 #include <openssl/crypto.h>
19 #include "internal/nelem.h"
22 /* The macros below generate unused functions which error out one of the clang
23 * builds. We disable this check here.
26 #pragma clang diagnostic ignored "-Wunused-function"
39 DEFINE_SPECIAL_STACK_OF(sint
, int)
40 DEFINE_SPECIAL_STACK_OF_CONST(uchar
, unsigned char)
42 DEFINE_STACK_OF_CONST(SU
)
44 static int int_compare(const int *const *a
, const int *const *b
)
53 static int test_int_stack(int reserve
)
55 static int v
[] = { 1, 2, -4, 16, 999, 1, -173, 1, 9 };
56 static int notpresent
= -1;
57 const int n
= OSSL_NELEM(v
);
70 const int n_finds
= OSSL_NELEM(finds
);
82 const int n_exfinds
= OSSL_NELEM(exfinds
);
83 STACK_OF(sint
) *s
= sk_sint_new_null();
88 || (reserve
> 0 && !TEST_true(sk_sint_reserve(s
, 5 * reserve
))))
91 /* Check push and num */
92 for (i
= 0; i
< n
; i
++) {
93 if (!TEST_int_eq(sk_sint_num(s
), i
)) {
94 TEST_info("int stack size %d", i
);
97 sk_sint_push(s
, v
+ i
);
99 if (!TEST_int_eq(sk_sint_num(s
), n
))
102 /* check the values */
103 for (i
= 0; i
< n
; i
++)
104 if (!TEST_ptr_eq(sk_sint_value(s
, i
), v
+ i
)) {
105 TEST_info("int value %d", i
);
109 /* find unsorted -- the pointers are compared */
110 for (i
= 0; i
< n_finds
; i
++) {
111 int *val
= (finds
[i
].unsorted
== -1) ? ¬present
112 : v
+ finds
[i
].unsorted
;
114 if (!TEST_int_eq(sk_sint_find(s
, val
), finds
[i
].unsorted
)) {
115 TEST_info("int unsorted find %d", i
);
120 /* find_ex unsorted */
121 for (i
= 0; i
< n_finds
; i
++) {
122 int *val
= (finds
[i
].unsorted
== -1) ? ¬present
123 : v
+ finds
[i
].unsorted
;
125 if (!TEST_int_eq(sk_sint_find_ex(s
, val
), finds
[i
].unsorted
)) {
126 TEST_info("int unsorted find_ex %d", i
);
132 if (!TEST_false(sk_sint_is_sorted(s
)))
134 (void)sk_sint_set_cmp_func(s
, &int_compare
);
136 if (!TEST_true(sk_sint_is_sorted(s
)))
139 /* find sorted -- the value is matched so we don't need to locate it */
140 for (i
= 0; i
< n_finds
; i
++)
141 if (!TEST_int_eq(sk_sint_find(s
, &finds
[i
].value
), finds
[i
].sorted
)) {
142 TEST_info("int sorted find %d", i
);
147 for (i
= 0; i
< n_finds
; i
++)
148 if (!TEST_int_eq(sk_sint_find_ex(s
, &finds
[i
].value
), finds
[i
].ex
)) {
149 TEST_info("int sorted find_ex present %d", i
);
152 for (i
= 0; i
< n_exfinds
; i
++)
153 if (!TEST_int_eq(sk_sint_find_ex(s
, &exfinds
[i
].value
), exfinds
[i
].ex
)){
154 TEST_info("int sorted find_ex absent %d", i
);
159 if (!TEST_ptr_eq(sk_sint_shift(s
), v
+ 6))
168 static int uchar_compare(const unsigned char *const *a
,
169 const unsigned char *const *b
)
171 return **a
- (signed int)**b
;
174 static int test_uchar_stack(int reserve
)
176 static const unsigned char v
[] = { 1, 3, 7, 5, 255, 0 };
177 const int n
= OSSL_NELEM(v
);
178 STACK_OF(uchar
) *s
= sk_uchar_new(&uchar_compare
), *r
= NULL
;
183 || (reserve
> 0 && !TEST_true(sk_uchar_reserve(s
, 5 * reserve
))))
186 /* unshift and num */
187 for (i
= 0; i
< n
; i
++) {
188 if (!TEST_int_eq(sk_uchar_num(s
), i
)) {
189 TEST_info("uchar stack size %d", i
);
192 sk_uchar_unshift(s
, v
+ i
);
194 if (!TEST_int_eq(sk_uchar_num(s
), n
))
198 r
= sk_uchar_dup(NULL
);
199 if (sk_uchar_num(r
) != 0)
203 if (!TEST_int_eq(sk_uchar_num(r
), n
))
208 for (i
= 0; i
< n
; i
++)
209 if (!TEST_ptr_eq(sk_uchar_pop(s
), v
+ i
)) {
210 TEST_info("uchar pop %d", i
);
214 /* free -- we rely on the debug malloc to detect leakage here */
219 if (!TEST_int_eq(sk_uchar_num(r
), n
))
224 if (!TEST_int_eq(sk_uchar_num(r
), 0))
228 sk_uchar_insert(r
, v
, 0);
229 sk_uchar_insert(r
, v
+ 2, -1);
230 sk_uchar_insert(r
, v
+ 1, 1);
231 for (i
= 0; i
< 3; i
++)
232 if (!TEST_ptr_eq(sk_uchar_value(r
, i
), v
+ i
)) {
233 TEST_info("uchar insert %d", i
);
238 if (!TEST_ptr_null(sk_uchar_delete(r
, 12)))
240 if (!TEST_ptr_eq(sk_uchar_delete(r
, 1), v
+ 1))
244 (void)sk_uchar_set(r
, 1, v
+ 1);
245 for (i
= 0; i
< 2; i
++)
246 if (!TEST_ptr_eq(sk_uchar_value(r
, i
), v
+ i
)) {
247 TEST_info("uchar set %d", i
);
258 static SS
*SS_copy(const SS
*p
)
260 SS
*q
= OPENSSL_malloc(sizeof(*q
));
263 memcpy(q
, p
, sizeof(*q
));
267 static void SS_free(SS
*p
) {
271 static int test_SS_stack(void)
273 STACK_OF(SS
) *s
= sk_SS_new_null();
274 STACK_OF(SS
) *r
= NULL
;
276 const int n
= OSSL_NELEM(v
);
280 /* allocate and push */
281 for (i
= 0; i
< n
; i
++) {
282 v
[i
] = OPENSSL_malloc(sizeof(*v
[i
]));
288 if (!TEST_int_eq(sk_SS_num(s
), i
)) {
289 TEST_info("SS stack size %d", i
);
294 if (!TEST_int_eq(sk_SS_num(s
), n
))
298 r
= sk_SS_deep_copy(NULL
, &SS_copy
, &SS_free
);
299 if (sk_SS_num(r
) != 0)
302 r
= sk_SS_deep_copy(s
, &SS_copy
, &SS_free
);
305 for (i
= 0; i
< n
; i
++) {
306 p
= sk_SS_value(r
, i
);
307 if (!TEST_ptr_ne(p
, v
[i
])) {
308 TEST_info("SS deepcopy non-copy %d", i
);
311 if (!TEST_int_eq(p
->n
, v
[i
]->n
)) {
312 TEST_info("test SS deepcopy int %d", i
);
315 if (!TEST_char_eq(p
->c
, v
[i
]->c
)) {
316 TEST_info("SS deepcopy char %d", i
);
321 /* pop_free - we rely on the malloc debug to catch the leak */
322 sk_SS_pop_free(r
, &SS_free
);
326 p
= sk_SS_delete_ptr(s
, v
[3]);
330 if (!TEST_int_eq(sk_SS_num(s
), n
- 1))
332 for (i
= 0; i
< n
-1; i
++)
333 if (!TEST_ptr_eq(sk_SS_value(s
, i
), v
[i
<3 ? i
: 1+i
])) {
334 TEST_info("SS delete ptr item %d", i
);
340 sk_SS_pop_free(r
, &SS_free
);
341 sk_SS_pop_free(s
, &SS_free
);
345 static int test_SU_stack(void)
347 STACK_OF(SU
) *s
= sk_SU_new_null();
349 const int n
= OSSL_NELEM(v
);
353 /* allocate and push */
354 for (i
= 0; i
< n
; i
++) {
359 if (!TEST_int_eq(sk_SU_num(s
), i
)) {
360 TEST_info("SU stack size %d", i
);
363 sk_SU_push(s
, v
+ i
);
365 if (!TEST_int_eq(sk_SU_num(s
), n
))
368 /* check the pointers are correct */
369 for (i
= 0; i
< n
; i
++)
370 if (!TEST_ptr_eq(sk_SU_value(s
, i
), v
+ i
)) {
371 TEST_info("SU pointer check %d", i
);
381 int setup_tests(void)
383 ADD_ALL_TESTS(test_int_stack
, 4);
384 ADD_ALL_TESTS(test_uchar_stack
, 4);
385 ADD_TEST(test_SS_stack
);
386 ADD_TEST(test_SU_stack
);