2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* ====================================================================
11 * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
17 #include <openssl/opensslconf.h>
18 #include <openssl/safestack.h>
19 #include <openssl/err.h>
20 #include <openssl/crypto.h>
23 #include "test_main.h"
26 /* The macros below generate unused functions which error out one of the clang
27 * builds. We disable this check here.
30 #pragma clang diagnostic ignored "-Wunused-function"
43 DEFINE_SPECIAL_STACK_OF(sint, int)
44 DEFINE_SPECIAL_STACK_OF_CONST(uchar, unsigned char)
46 DEFINE_STACK_OF_CONST(SU)
48 static int int_compare(const int *const *a, const int *const *b)
57 static int test_int_stack(void)
59 static int v[] = { 1, 2, -4, 16, 999, 1, -173, 1, 9 };
60 const int n = OSSL_NELEM(v);
73 const int n_finds = OSSL_NELEM(finds);
85 const int n_exfinds = OSSL_NELEM(exfinds);
86 STACK_OF(sint) *s = sk_sint_new_null();
90 /* Check push and num */
91 for (i = 0; i < n; i++) {
92 if (sk_sint_num(s) != i) {
93 fprintf(stderr, "test int stack size %d\n", i);
96 sk_sint_push(s, v + i);
98 if (sk_sint_num(s) != n) {
99 fprintf(stderr, "test int stack size %d\n", n);
103 /* check the values */
104 for (i = 0; i < n; i++)
105 if (sk_sint_value(s, i) != v + i) {
106 fprintf(stderr, "test int value %d\n", i);
110 /* find unsorted -- the pointers are compared */
111 for (i = 0; i < n_finds; i++)
112 if (sk_sint_find(s, v + finds[i].unsorted) != finds[i].unsorted) {
113 fprintf(stderr, "test int unsorted find %d\n", i);
117 /* find_ex unsorted */
118 for (i = 0; i < n_finds; i++)
119 if (sk_sint_find_ex(s, v + finds[i].unsorted) != finds[i].unsorted) {
120 fprintf(stderr, "test int unsorted find_ex %d\n", i);
125 if (sk_sint_is_sorted(s)) {
126 fprintf(stderr, "test int unsorted\n");
129 sk_sint_set_cmp_func(s, &int_compare);
131 if (!sk_sint_is_sorted(s)) {
132 fprintf(stderr, "test int sorted\n");
136 /* find sorted -- the value is matched so we don't need to locate it */
137 for (i = 0; i < n_finds; i++)
138 if (sk_sint_find(s, &finds[i].value) != finds[i].sorted) {
139 fprintf(stderr, "test int sorted find %d\n", i);
144 for (i = 0; i < n_finds; i++)
145 if (sk_sint_find_ex(s, &finds[i].value) != finds[i].ex) {
146 fprintf(stderr, "test int sorted find_ex present %d\n", i);
149 for (i = 0; i < n_exfinds; i++)
150 if (sk_sint_find_ex(s, &exfinds[i].value) != exfinds[i].ex) {
151 fprintf(stderr, "test int sorted find_ex absent %d\n", i);
156 if (sk_sint_shift(s) != v + 6) {
157 fprintf(stderr, "test int shift\n");
167 static int uchar_compare(const unsigned char *const *a,
168 const unsigned char *const *b)
170 return **a - (signed int)**b;
173 static int test_uchar_stack(void)
175 static const unsigned char v[] = { 1, 3, 7, 5, 255, 0 };
176 const int n = OSSL_NELEM(v);
177 STACK_OF(uchar) *s = sk_uchar_new(&uchar_compare), *r = NULL;
181 /* unshift and num */
182 for (i = 0; i < n; i++) {
183 if (sk_uchar_num(s) != i) {
184 fprintf(stderr, "test uchar stack size %d\n", i);
187 sk_uchar_unshift(s, v + i);
189 if (sk_uchar_num(s) != n) {
190 fprintf(stderr, "test uchar stack size %d\n", n);
196 if (sk_uchar_num(r) != n) {
197 fprintf(stderr, "test uchar dup size %d\n", n);
203 for (i = 0; i < n; i++)
204 if (sk_uchar_pop(s) != v + i) {
205 fprintf(stderr, "test uchar pop %d\n", i);
209 /* free -- we rely on the debug malloc to detect leakage here */
214 if (sk_uchar_num(r) != n) {
215 fprintf(stderr, "test uchar dup size %d\n", n);
221 if (sk_uchar_num(r) != 0) {
222 fprintf(stderr, "test uchar zero %d\n", n);
227 sk_uchar_insert(r, v, 0);
228 sk_uchar_insert(r, v + 2, -1);
229 sk_uchar_insert(r, v + 1, 1);
230 for (i = 0; i < 3; i++)
231 if (sk_uchar_value(r, i) != v + i) {
232 fprintf(stderr, "test uchar insert %d\n", i);
237 if (sk_uchar_delete(r, 12) != NULL) {
238 fprintf(stderr, "test uchar delete missing %d\n", n);
241 if (sk_uchar_delete(r, 1) != v + 1) {
242 fprintf(stderr, "test uchar delete middle %d\n", n);
247 sk_uchar_set(r, 1, v + 1);
248 for (i = 0; i < 2; i++)
249 if (sk_uchar_value(r, i) != v + i) {
250 fprintf(stderr, "test uchar set %d\n", i);
261 static SS *SS_copy(const SS *p)
263 SS *q = OPENSSL_malloc(sizeof(*q));
266 memcpy(q, p, sizeof(*q));
270 static void SS_free(SS *p) {
274 static int test_SS_stack(void)
276 STACK_OF(SS) *s = sk_SS_new_null();
277 STACK_OF(SS) *r = NULL;
279 const int n = OSSL_NELEM(v);
283 /* allocate and push */
284 for (i = 0; i < n; i++) {
285 v[i] = OPENSSL_malloc(sizeof(*v[i]));
288 fprintf(stderr, "test SS memory allocation failure\n");
293 if (sk_SS_num(s) != i) {
294 fprintf(stderr, "test SS stack size %d\n", i);
299 if (sk_SS_num(s) != n) {
300 fprintf(stderr, "test SS size %d\n", n);
305 r = sk_SS_deep_copy(s, &SS_copy, &SS_free);
307 fprintf(stderr, "test SS deepcopy failure\n");
310 for (i = 0; i < n; i++) {
311 p = sk_SS_value(r, i);
313 fprintf(stderr, "test SS deepcopy non-copy %d\n", i);
316 if (p->n != v[i]->n || p->c != v[i]->c) {
317 fprintf(stderr, "test SS deepcopy values %d\n", i);
322 /* pop_free - we rely on the malloc debug to catch the leak */
323 sk_SS_pop_free(r, &SS_free);
327 if ((p = sk_SS_delete_ptr(s, v[3])) == NULL) {
328 fprintf(stderr, "test SS delete ptr not found\n");
332 if (sk_SS_num(s) != n-1) {
333 fprintf(stderr, "test SS delete ptr size\n");
336 for (i = 0; i < n-1; i++)
337 if (sk_SS_value(s, i) != v[i<3 ? i : 1+i]) {
338 fprintf(stderr, "test SS delete ptr item %d\n", i);
344 sk_SS_pop_free(r, &SS_free);
345 sk_SS_pop_free(s, &SS_free);
349 static int test_SU_stack(void)
351 STACK_OF(SU) *s = sk_SU_new_null();
353 const int n = OSSL_NELEM(v);
357 /* allocate and push */
358 for (i = 0; i < n; i++) {
363 if (sk_SU_num(s) != i) {
364 fprintf(stderr, "test SU stack size %d\n", i);
367 sk_SU_push(s, v + i);
369 if (sk_SU_num(s) != n) {
370 fprintf(stderr, "test SU size %d\n", n);
374 /* check the pointers are correct */
375 for (i = 0; i < n; i++)
376 if (sk_SU_value(s, i) != v + i) {
377 fprintf(stderr, "test SU pointer check %d\n", i);
387 void register_tests(void)
389 ADD_TEST(test_int_stack);
390 ADD_TEST(test_uchar_stack);
391 ADD_TEST(test_SS_stack);
392 ADD_TEST(test_SU_stack);