Print Ed25519 in s_client/s_server
[openssl.git] / test / stack_test.c
1 /*
2  * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2017, Oracle and/or its affiliates.  All rights reserved.
4  *
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
9  */
10
11 #include <stdio.h>
12 #include <string.h>
13
14 #include <openssl/opensslconf.h>
15 #include <openssl/safestack.h>
16 #include <openssl/err.h>
17 #include <openssl/crypto.h>
18
19 #include "e_os.h"
20 #include "testutil.h"
21
22 /* The macros below generate unused functions which error out one of the clang
23  * builds.  We disable this check here.
24  */
25 #ifdef __clang__
26 #pragma clang diagnostic ignored "-Wunused-function"
27 #endif
28
29 typedef struct {
30     int n;
31     char c;
32 } SS;
33
34 typedef union {
35     int n;
36     char c;
37 } SU;
38
39 DEFINE_SPECIAL_STACK_OF(sint, int)
40 DEFINE_SPECIAL_STACK_OF_CONST(uchar, unsigned char)
41 DEFINE_STACK_OF(SS)
42 DEFINE_STACK_OF_CONST(SU)
43
44 static int int_compare(const int *const *a, const int *const *b)
45 {
46     if (**a < **b)
47         return -1;
48     if (**a > **b)
49         return 1;
50     return 0;
51 }
52
53 static int test_int_stack(void)
54 {
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);
58     static struct {
59         int value;
60         int unsorted;
61         int sorted;
62         int ex;
63     } finds[] = {
64         { 2,    1,  5,  5   },
65         { 9,    7,  6,  6   },
66         { -173, 5,  0,  0   },
67         { 999,  3,  8,  8   },
68         { 0,   -1, -1,  1   }
69     };
70     const int n_finds = OSSL_NELEM(finds);
71     static struct {
72         int value;
73         int ex;
74     } exfinds[] = {
75         { 3,    5   },
76         { 1000, 8   },
77         { 20,   8   },
78         { -999, 0   },
79         { -5,   0   },
80         { 8,    5   }
81     };
82     const int n_exfinds = OSSL_NELEM(exfinds);
83     STACK_OF(sint) *s = sk_sint_new_null();
84     int i;
85     int testresult = 0;
86
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);
91             goto end;
92         }
93         sk_sint_push(s, v + i);
94     }
95     if (!TEST_int_eq(sk_sint_num(s), n))
96         goto end;
97
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);
102             goto end;
103         }
104
105     /* find unsorted -- the pointers are compared */
106     for (i = 0; i < n_finds; i++) {
107         int *val = (finds[i].unsorted == -1) ? &notpresent
108                                              : v + finds[i].unsorted;
109
110         if (!TEST_int_eq(sk_sint_find(s, val), finds[i].unsorted)) {
111             TEST_info("int unsorted find %d", i);
112             goto end;
113         }
114     }
115
116     /* find_ex unsorted */
117     for (i = 0; i < n_finds; i++) {
118         int *val = (finds[i].unsorted == -1) ? &notpresent
119                                              : v + finds[i].unsorted;
120
121         if (!TEST_int_eq(sk_sint_find_ex(s, val), finds[i].unsorted)) {
122             TEST_info("int unsorted find_ex %d", i);
123             goto end;
124         }
125     }
126
127     /* sorting */
128     if (!TEST_false(sk_sint_is_sorted(s)))
129         goto end;
130     sk_sint_set_cmp_func(s, &int_compare);
131     sk_sint_sort(s);
132     if (!TEST_true(sk_sint_is_sorted(s)))
133         goto end;
134
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);
139             goto end;
140         }
141
142     /* find_ex sorted */
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);
146             goto end;
147         }
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);
151             goto end;
152         }
153
154     /* shift */
155     if (!TEST_ptr_eq(sk_sint_shift(s), v + 6))
156         goto end;
157
158     testresult = 1;
159 end:
160     sk_sint_free(s);
161     return testresult;
162 }
163
164 static int uchar_compare(const unsigned char *const *a,
165                          const unsigned char *const *b)
166 {
167     return **a - (signed int)**b;
168 }
169
170 static int test_uchar_stack(void)
171 {
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;
175     int i;
176     int testresult = 0;
177
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);
182             goto end;
183         }
184         sk_uchar_unshift(s, v + i);
185     }
186     if (!TEST_int_eq(sk_uchar_num(s), n))
187         goto end;
188
189     /* dup */
190     r = sk_uchar_dup(s);
191     if (!TEST_int_eq(sk_uchar_num(r), n))
192         goto end;
193     sk_uchar_sort(r);
194
195     /* pop */
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);
199             goto end;
200         }
201
202     /* free -- we rely on the debug malloc to detect leakage here */
203     sk_uchar_free(s);
204     s = NULL;
205
206     /* dup again */
207     if (!TEST_int_eq(sk_uchar_num(r), n))
208         goto end;
209
210     /* zero */
211     sk_uchar_zero(r);
212     if (!TEST_int_eq(sk_uchar_num(r), 0))
213         goto end;
214
215     /* insert */
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);
222             goto end;
223         }
224
225     /* delete */
226     if (!TEST_ptr_null(sk_uchar_delete(r, 12)))
227         goto end;
228     if (!TEST_ptr_eq(sk_uchar_delete(r, 1), v + 1))
229         goto end;
230
231     /* set */
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);
236             goto end;
237         }
238
239     testresult = 1;
240 end:
241     sk_uchar_free(r);
242     sk_uchar_free(s);
243     return testresult;
244 }
245
246 static SS *SS_copy(const SS *p)
247 {
248     SS *q = OPENSSL_malloc(sizeof(*q));
249
250     if (q != NULL)
251         memcpy(q, p, sizeof(*q));
252     return q;
253 }
254
255 static void SS_free(SS *p) {
256     OPENSSL_free(p);
257 }
258
259 static int test_SS_stack(void)
260 {
261     STACK_OF(SS) *s = sk_SS_new_null();
262     STACK_OF(SS) *r = NULL;
263     SS *v[10], *p;
264     const int n = OSSL_NELEM(v);
265     int i;
266     int testresult = 0;
267
268     /* allocate and push */
269     for (i = 0; i < n; i++) {
270         v[i] = OPENSSL_malloc(sizeof(*v[i]));
271
272         if (!TEST_ptr(v[i]))
273             goto end;
274         v[i]->n = i;
275         v[i]->c = 'A' + i;
276         if (!TEST_int_eq(sk_SS_num(s), i)) {
277             TEST_info("SS stack size %d", i);
278             goto end;
279         }
280         sk_SS_push(s, v[i]);
281     }
282     if (!TEST_int_eq(sk_SS_num(s), n))
283         goto end;
284
285     /* deepcopy */
286     r = sk_SS_deep_copy(s, &SS_copy, &SS_free);
287     if (!TEST_ptr(r))
288         goto end;
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);
293             goto end;
294         }
295         if (!TEST_int_eq(p->n, v[i]->n)) {
296             TEST_info("test SS deepcopy int %d", i);
297             goto end;
298         }
299         if (!TEST_char_eq(p->c, v[i]->c)) {
300             TEST_info("SS deepcopy char %d", i);
301             goto end;
302         }
303     }
304
305     /* pop_free - we rely on the malloc debug to catch the leak */
306     sk_SS_pop_free(r, &SS_free);
307     r = NULL;
308
309     /* delete_ptr */
310     p = sk_SS_delete_ptr(s, v[3]);
311     if (!TEST_ptr(p))
312         goto end;
313     SS_free(p);
314     if (!TEST_int_eq(sk_SS_num(s), n - 1))
315         goto end;
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);
319             goto end;
320         }
321
322     testresult = 1;
323 end:
324     sk_SS_pop_free(r, &SS_free);
325     sk_SS_pop_free(s, &SS_free);
326     return testresult;
327 }
328
329 static int test_SU_stack(void)
330 {
331     STACK_OF(SU) *s = sk_SU_new_null();
332     SU v[10];
333     const int n = OSSL_NELEM(v);
334     int i;
335     int testresult = 0;
336
337     /* allocate and push */
338     for (i = 0; i < n; i++) {
339         if ((i & 1) == 0)
340             v[i].n = i;
341         else
342             v[i].c = 'A' + i;
343         if (!TEST_int_eq(sk_SU_num(s), i)) {
344             TEST_info("SU stack size %d", i);
345             goto end;
346         }
347         sk_SU_push(s, v + i);
348     }
349     if (!TEST_int_eq(sk_SU_num(s), n))
350         goto end;
351
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);
356             goto end;
357         }
358
359     testresult = 1;
360 end:
361     sk_SU_free(s);
362     return testresult;
363 }
364
365 void register_tests(void)
366 {
367     ADD_TEST(test_int_stack);
368     ADD_TEST(test_uchar_stack);
369     ADD_TEST(test_SS_stack);
370     ADD_TEST(test_SU_stack);
371 }