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>
25 /* The macros below generate unused functions which error out one of the clang
26 * builds. We disable this check here.
29 #pragma clang diagnostic ignored "-Wunused-function"
42 DEFINE_SPECIAL_STACK_OF(sint, int)
43 DEFINE_SPECIAL_STACK_OF_CONST(uchar, unsigned char)
45 DEFINE_STACK_OF_CONST(SU)
47 static int int_compare(const int *const *a, const int *const *b)
56 static int test_int_stack(void)
58 static int v[] = { 1, 2, -4, 16, 999, 1, -173, 1, 9 };
59 static int notpresent = -1;
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 (!TEST_int_eq(sk_sint_num(s), i)) {
93 TEST_info("int stack size %d", i);
96 sk_sint_push(s, v + i);
98 if (!TEST_int_eq(sk_sint_num(s), n))
101 /* check the values */
102 for (i = 0; i < n; i++)
103 if (!TEST_ptr_eq(sk_sint_value(s, i), v + i)) {
104 TEST_info("int value %d", i);
108 /* find unsorted -- the pointers are compared */
109 for (i = 0; i < n_finds; i++) {
110 int *val = (finds[i].unsorted == -1) ? ¬present
111 : v + finds[i].unsorted;
113 if (!TEST_int_eq(sk_sint_find(s, val), finds[i].unsorted)) {
114 TEST_info("int unsorted find %d", i);
119 /* find_ex unsorted */
120 for (i = 0; i < n_finds; i++) {
121 int *val = (finds[i].unsorted == -1) ? ¬present
122 : v + finds[i].unsorted;
124 if (!TEST_int_eq(sk_sint_find_ex(s, val), finds[i].unsorted)) {
125 TEST_info("int unsorted find_ex %d", i);
131 if (!TEST_false(sk_sint_is_sorted(s)))
133 sk_sint_set_cmp_func(s, &int_compare);
135 if (!TEST_true(sk_sint_is_sorted(s)))
138 /* find sorted -- the value is matched so we don't need to locate it */
139 for (i = 0; i < n_finds; i++)
140 if (!TEST_int_eq(sk_sint_find(s, &finds[i].value), finds[i].sorted)) {
141 TEST_info("int sorted find %d", i);
146 for (i = 0; i < n_finds; i++)
147 if (!TEST_int_eq(sk_sint_find_ex(s, &finds[i].value), finds[i].ex)) {
148 TEST_info("int sorted find_ex present %d", i);
151 for (i = 0; i < n_exfinds; i++)
152 if (!TEST_int_eq(sk_sint_find_ex(s, &exfinds[i].value), exfinds[i].ex)){
153 TEST_info("int sorted find_ex absent %d", i);
158 if (!TEST_ptr_eq(sk_sint_shift(s), v + 6))
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 (!TEST_int_eq(sk_uchar_num(s), i)) {
184 TEST_info("uchar stack size %d", i);
187 sk_uchar_unshift(s, v + i);
189 if (!TEST_int_eq(sk_uchar_num(s), n))
194 if (!TEST_int_eq(sk_uchar_num(r), n))
199 for (i = 0; i < n; i++)
200 if (!TEST_ptr_eq(sk_uchar_pop(s), v + i)) {
201 TEST_info("uchar pop %d", i);
205 /* free -- we rely on the debug malloc to detect leakage here */
210 if (!TEST_int_eq(sk_uchar_num(r), n))
215 if (!TEST_int_eq(sk_uchar_num(r), 0))
219 sk_uchar_insert(r, v, 0);
220 sk_uchar_insert(r, v + 2, -1);
221 sk_uchar_insert(r, v + 1, 1);
222 for (i = 0; i < 3; i++)
223 if (!TEST_ptr_eq(sk_uchar_value(r, i), v + i)) {
224 TEST_info("uchar insert %d", i);
229 if (!TEST_ptr_null(sk_uchar_delete(r, 12)))
231 if (!TEST_ptr_eq(sk_uchar_delete(r, 1), v + 1))
235 sk_uchar_set(r, 1, v + 1);
236 for (i = 0; i < 2; i++)
237 if (!TEST_ptr_eq(sk_uchar_value(r, i), v + i)) {
238 TEST_info("uchar set %d", i);
249 static SS *SS_copy(const SS *p)
251 SS *q = OPENSSL_malloc(sizeof(*q));
254 memcpy(q, p, sizeof(*q));
258 static void SS_free(SS *p) {
262 static int test_SS_stack(void)
264 STACK_OF(SS) *s = sk_SS_new_null();
265 STACK_OF(SS) *r = NULL;
267 const int n = OSSL_NELEM(v);
271 /* allocate and push */
272 for (i = 0; i < n; i++) {
273 v[i] = OPENSSL_malloc(sizeof(*v[i]));
279 if (!TEST_int_eq(sk_SS_num(s), i)) {
280 TEST_info("SS stack size %d", i);
285 if (!TEST_int_eq(sk_SS_num(s), n))
289 r = sk_SS_deep_copy(s, &SS_copy, &SS_free);
292 for (i = 0; i < n; i++) {
293 p = sk_SS_value(r, i);
294 if (!TEST_ptr_ne(p, v[i])) {
295 TEST_info("SS deepcopy non-copy %d", i);
298 if (!TEST_int_eq(p->n, v[i]->n)) {
299 TEST_info("test SS deepcopy int %d", i);
302 if (!TEST_char_eq(p->c, v[i]->c)) {
303 TEST_info("SS deepcopy char %d", i);
308 /* pop_free - we rely on the malloc debug to catch the leak */
309 sk_SS_pop_free(r, &SS_free);
313 p = sk_SS_delete_ptr(s, v[3]);
317 if (!TEST_int_eq(sk_SS_num(s), n - 1))
319 for (i = 0; i < n-1; i++)
320 if (!TEST_ptr_eq(sk_SS_value(s, i), v[i<3 ? i : 1+i])) {
321 TEST_info("SS delete ptr item %d", i);
327 sk_SS_pop_free(r, &SS_free);
328 sk_SS_pop_free(s, &SS_free);
332 static int test_SU_stack(void)
334 STACK_OF(SU) *s = sk_SU_new_null();
336 const int n = OSSL_NELEM(v);
340 /* allocate and push */
341 for (i = 0; i < n; i++) {
346 if (!TEST_int_eq(sk_SU_num(s), i)) {
347 TEST_info("SU stack size %d", i);
350 sk_SU_push(s, v + i);
352 if (!TEST_int_eq(sk_SU_num(s), n))
355 /* check the pointers are correct */
356 for (i = 0; i < n; i++)
357 if (!TEST_ptr_eq(sk_SU_value(s, i), v + i)) {
358 TEST_info("SU pointer check %d", i);
368 void register_tests(void)
370 ADD_TEST(test_int_stack);
371 ADD_TEST(test_uchar_stack);
372 ADD_TEST(test_SS_stack);
373 ADD_TEST(test_SU_stack);