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 static int notpresent = -1;
61 const int n = OSSL_NELEM(v);
74 const int n_finds = OSSL_NELEM(finds);
86 const int n_exfinds = OSSL_NELEM(exfinds);
87 STACK_OF(sint) *s = sk_sint_new_null();
91 /* Check push and num */
92 for (i = 0; i < n; i++) {
93 if (sk_sint_num(s) != i) {
94 fprintf(stderr, "test int stack size %d\n", i);
97 sk_sint_push(s, v + i);
99 if (sk_sint_num(s) != n) {
100 fprintf(stderr, "test int stack size %d\n", n);
104 /* check the values */
105 for (i = 0; i < n; i++)
106 if (sk_sint_value(s, i) != v + i) {
107 fprintf(stderr, "test int value %d\n", i);
111 /* find unsorted -- the pointers are compared */
112 for (i = 0; i < n_finds; i++) {
113 int *val = (finds[i].unsorted == -1) ? ¬present
114 : v + finds[i].unsorted;
116 if (sk_sint_find(s, val) != finds[i].unsorted) {
117 fprintf(stderr, "test int unsorted find %d\n", i);
122 /* find_ex unsorted */
123 for (i = 0; i < n_finds; i++) {
124 int *val = (finds[i].unsorted == -1) ? ¬present
125 : v + finds[i].unsorted;
127 if (sk_sint_find_ex(s, val) != finds[i].unsorted) {
128 fprintf(stderr, "test int unsorted find_ex %d\n", i);
134 if (sk_sint_is_sorted(s)) {
135 fprintf(stderr, "test int unsorted\n");
138 sk_sint_set_cmp_func(s, &int_compare);
140 if (!sk_sint_is_sorted(s)) {
141 fprintf(stderr, "test int sorted\n");
145 /* find sorted -- the value is matched so we don't need to locate it */
146 for (i = 0; i < n_finds; i++)
147 if (sk_sint_find(s, &finds[i].value) != finds[i].sorted) {
148 fprintf(stderr, "test int sorted find %d\n", i);
153 for (i = 0; i < n_finds; i++)
154 if (sk_sint_find_ex(s, &finds[i].value) != finds[i].ex) {
155 fprintf(stderr, "test int sorted find_ex present %d\n", i);
158 for (i = 0; i < n_exfinds; i++)
159 if (sk_sint_find_ex(s, &exfinds[i].value) != exfinds[i].ex) {
160 fprintf(stderr, "test int sorted find_ex absent %d\n", i);
165 if (sk_sint_shift(s) != v + 6) {
166 fprintf(stderr, "test int shift\n");
176 static int uchar_compare(const unsigned char *const *a,
177 const unsigned char *const *b)
179 return **a - (signed int)**b;
182 static int test_uchar_stack(void)
184 static const unsigned char v[] = { 1, 3, 7, 5, 255, 0 };
185 const int n = OSSL_NELEM(v);
186 STACK_OF(uchar) *s = sk_uchar_new(&uchar_compare), *r = NULL;
190 /* unshift and num */
191 for (i = 0; i < n; i++) {
192 if (sk_uchar_num(s) != i) {
193 fprintf(stderr, "test uchar stack size %d\n", i);
196 sk_uchar_unshift(s, v + i);
198 if (sk_uchar_num(s) != n) {
199 fprintf(stderr, "test uchar stack size %d\n", n);
205 if (sk_uchar_num(r) != n) {
206 fprintf(stderr, "test uchar dup size %d\n", n);
212 for (i = 0; i < n; i++)
213 if (sk_uchar_pop(s) != v + i) {
214 fprintf(stderr, "test uchar pop %d\n", i);
218 /* free -- we rely on the debug malloc to detect leakage here */
223 if (sk_uchar_num(r) != n) {
224 fprintf(stderr, "test uchar dup size %d\n", n);
230 if (sk_uchar_num(r) != 0) {
231 fprintf(stderr, "test uchar zero %d\n", n);
236 sk_uchar_insert(r, v, 0);
237 sk_uchar_insert(r, v + 2, -1);
238 sk_uchar_insert(r, v + 1, 1);
239 for (i = 0; i < 3; i++)
240 if (sk_uchar_value(r, i) != v + i) {
241 fprintf(stderr, "test uchar insert %d\n", i);
246 if (sk_uchar_delete(r, 12) != NULL) {
247 fprintf(stderr, "test uchar delete missing %d\n", n);
250 if (sk_uchar_delete(r, 1) != v + 1) {
251 fprintf(stderr, "test uchar delete middle %d\n", n);
256 sk_uchar_set(r, 1, v + 1);
257 for (i = 0; i < 2; i++)
258 if (sk_uchar_value(r, i) != v + i) {
259 fprintf(stderr, "test uchar set %d\n", i);
270 static SS *SS_copy(const SS *p)
272 SS *q = OPENSSL_malloc(sizeof(*q));
275 memcpy(q, p, sizeof(*q));
279 static void SS_free(SS *p) {
283 static int test_SS_stack(void)
285 STACK_OF(SS) *s = sk_SS_new_null();
286 STACK_OF(SS) *r = NULL;
288 const int n = OSSL_NELEM(v);
292 /* allocate and push */
293 for (i = 0; i < n; i++) {
294 v[i] = OPENSSL_malloc(sizeof(*v[i]));
297 fprintf(stderr, "test SS memory allocation failure\n");
302 if (sk_SS_num(s) != i) {
303 fprintf(stderr, "test SS stack size %d\n", i);
308 if (sk_SS_num(s) != n) {
309 fprintf(stderr, "test SS size %d\n", n);
314 r = sk_SS_deep_copy(s, &SS_copy, &SS_free);
316 fprintf(stderr, "test SS deepcopy failure\n");
319 for (i = 0; i < n; i++) {
320 p = sk_SS_value(r, i);
322 fprintf(stderr, "test SS deepcopy non-copy %d\n", i);
325 if (p->n != v[i]->n || p->c != v[i]->c) {
326 fprintf(stderr, "test SS deepcopy values %d\n", i);
331 /* pop_free - we rely on the malloc debug to catch the leak */
332 sk_SS_pop_free(r, &SS_free);
336 if ((p = sk_SS_delete_ptr(s, v[3])) == NULL) {
337 fprintf(stderr, "test SS delete ptr not found\n");
341 if (sk_SS_num(s) != n-1) {
342 fprintf(stderr, "test SS delete ptr size\n");
345 for (i = 0; i < n-1; i++)
346 if (sk_SS_value(s, i) != v[i<3 ? i : 1+i]) {
347 fprintf(stderr, "test SS delete ptr item %d\n", i);
353 sk_SS_pop_free(r, &SS_free);
354 sk_SS_pop_free(s, &SS_free);
358 static int test_SU_stack(void)
360 STACK_OF(SU) *s = sk_SU_new_null();
362 const int n = OSSL_NELEM(v);
366 /* allocate and push */
367 for (i = 0; i < n; i++) {
372 if (sk_SU_num(s) != i) {
373 fprintf(stderr, "test SU stack size %d\n", i);
376 sk_SU_push(s, v + i);
378 if (sk_SU_num(s) != n) {
379 fprintf(stderr, "test SU size %d\n", n);
383 /* check the pointers are correct */
384 for (i = 0; i < n; i++)
385 if (sk_SU_value(s, i) != v + i) {
386 fprintf(stderr, "test SU pointer check %d\n", i);
396 void register_tests(void)
398 ADD_TEST(test_int_stack);
399 ADD_TEST(test_uchar_stack);
400 ADD_TEST(test_SS_stack);
401 ADD_TEST(test_SU_stack);