2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
5 * Licensed under the OpenSSL license (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>
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(void)
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();
87 /* Check push and num */
88 for (i = 0; i < n; i++) {
89 if (!TEST_int_eq(sk_sint_num(s), i)) {
90 TEST_info("int stack size %d", i);
93 sk_sint_push(s, v + i);
95 if (!TEST_int_eq(sk_sint_num(s), n))
98 /* check the values */
99 for (i = 0; i < n; i++)
100 if (!TEST_ptr_eq(sk_sint_value(s, i), v + i)) {
101 TEST_info("int value %d", i);
105 /* find unsorted -- the pointers are compared */
106 for (i = 0; i < n_finds; i++) {
107 int *val = (finds[i].unsorted == -1) ? ¬present
108 : v + finds[i].unsorted;
110 if (!TEST_int_eq(sk_sint_find(s, val), finds[i].unsorted)) {
111 TEST_info("int unsorted find %d", i);
116 /* find_ex unsorted */
117 for (i = 0; i < n_finds; i++) {
118 int *val = (finds[i].unsorted == -1) ? ¬present
119 : v + finds[i].unsorted;
121 if (!TEST_int_eq(sk_sint_find_ex(s, val), finds[i].unsorted)) {
122 TEST_info("int unsorted find_ex %d", i);
128 if (!TEST_false(sk_sint_is_sorted(s)))
130 sk_sint_set_cmp_func(s, &int_compare);
132 if (!TEST_true(sk_sint_is_sorted(s)))
135 /* find sorted -- the value is matched so we don't need to locate it */
136 for (i = 0; i < n_finds; i++)
137 if (!TEST_int_eq(sk_sint_find(s, &finds[i].value), finds[i].sorted)) {
138 TEST_info("int sorted find %d", i);
143 for (i = 0; i < n_finds; i++)
144 if (!TEST_int_eq(sk_sint_find_ex(s, &finds[i].value), finds[i].ex)) {
145 TEST_info("int sorted find_ex present %d", i);
148 for (i = 0; i < n_exfinds; i++)
149 if (!TEST_int_eq(sk_sint_find_ex(s, &exfinds[i].value), exfinds[i].ex)){
150 TEST_info("int sorted find_ex absent %d", i);
155 if (!TEST_ptr_eq(sk_sint_shift(s), v + 6))
164 static int uchar_compare(const unsigned char *const *a,
165 const unsigned char *const *b)
167 return **a - (signed int)**b;
170 static int test_uchar_stack(void)
172 static const unsigned char v[] = { 1, 3, 7, 5, 255, 0 };
173 const int n = OSSL_NELEM(v);
174 STACK_OF(uchar) *s = sk_uchar_new(&uchar_compare), *r = NULL;
178 /* unshift and num */
179 for (i = 0; i < n; i++) {
180 if (!TEST_int_eq(sk_uchar_num(s), i)) {
181 TEST_info("uchar stack size %d", i);
184 sk_uchar_unshift(s, v + i);
186 if (!TEST_int_eq(sk_uchar_num(s), n))
191 if (!TEST_int_eq(sk_uchar_num(r), n))
196 for (i = 0; i < n; i++)
197 if (!TEST_ptr_eq(sk_uchar_pop(s), v + i)) {
198 TEST_info("uchar pop %d", i);
202 /* free -- we rely on the debug malloc to detect leakage here */
207 if (!TEST_int_eq(sk_uchar_num(r), n))
212 if (!TEST_int_eq(sk_uchar_num(r), 0))
216 sk_uchar_insert(r, v, 0);
217 sk_uchar_insert(r, v + 2, -1);
218 sk_uchar_insert(r, v + 1, 1);
219 for (i = 0; i < 3; i++)
220 if (!TEST_ptr_eq(sk_uchar_value(r, i), v + i)) {
221 TEST_info("uchar insert %d", i);
226 if (!TEST_ptr_null(sk_uchar_delete(r, 12)))
228 if (!TEST_ptr_eq(sk_uchar_delete(r, 1), v + 1))
232 sk_uchar_set(r, 1, v + 1);
233 for (i = 0; i < 2; i++)
234 if (!TEST_ptr_eq(sk_uchar_value(r, i), v + i)) {
235 TEST_info("uchar set %d", i);
246 static SS *SS_copy(const SS *p)
248 SS *q = OPENSSL_malloc(sizeof(*q));
251 memcpy(q, p, sizeof(*q));
255 static void SS_free(SS *p) {
259 static int test_SS_stack(void)
261 STACK_OF(SS) *s = sk_SS_new_null();
262 STACK_OF(SS) *r = NULL;
264 const int n = OSSL_NELEM(v);
268 /* allocate and push */
269 for (i = 0; i < n; i++) {
270 v[i] = OPENSSL_malloc(sizeof(*v[i]));
276 if (!TEST_int_eq(sk_SS_num(s), i)) {
277 TEST_info("SS stack size %d", i);
282 if (!TEST_int_eq(sk_SS_num(s), n))
286 r = sk_SS_deep_copy(s, &SS_copy, &SS_free);
289 for (i = 0; i < n; i++) {
290 p = sk_SS_value(r, i);
291 if (!TEST_ptr_ne(p, v[i])) {
292 TEST_info("SS deepcopy non-copy %d", i);
295 if (!TEST_int_eq(p->n, v[i]->n)) {
296 TEST_info("test SS deepcopy int %d", i);
299 if (!TEST_char_eq(p->c, v[i]->c)) {
300 TEST_info("SS deepcopy char %d", i);
305 /* pop_free - we rely on the malloc debug to catch the leak */
306 sk_SS_pop_free(r, &SS_free);
310 p = sk_SS_delete_ptr(s, v[3]);
314 if (!TEST_int_eq(sk_SS_num(s), n - 1))
316 for (i = 0; i < n-1; i++)
317 if (!TEST_ptr_eq(sk_SS_value(s, i), v[i<3 ? i : 1+i])) {
318 TEST_info("SS delete ptr item %d", i);
324 sk_SS_pop_free(r, &SS_free);
325 sk_SS_pop_free(s, &SS_free);
329 static int test_SU_stack(void)
331 STACK_OF(SU) *s = sk_SU_new_null();
333 const int n = OSSL_NELEM(v);
337 /* allocate and push */
338 for (i = 0; i < n; i++) {
343 if (!TEST_int_eq(sk_SU_num(s), i)) {
344 TEST_info("SU stack size %d", i);
347 sk_SU_push(s, v + i);
349 if (!TEST_int_eq(sk_SU_num(s), n))
352 /* check the pointers are correct */
353 for (i = 0; i < n; i++)
354 if (!TEST_ptr_eq(sk_SU_value(s, i), v + i)) {
355 TEST_info("SU pointer check %d", i);
365 void register_tests(void)
367 ADD_TEST(test_int_stack);
368 ADD_TEST(test_uchar_stack);
369 ADD_TEST(test_SS_stack);
370 ADD_TEST(test_SU_stack);