Configure: disable new trace api by default
[openssl.git] / test / params_api_test.c
1 /*
2  * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2019, Oracle and/or its affiliates.  All rights reserved.
4  *
5  * Licensed under the Apache License 2.0 (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 <string.h>
12 #include "testutil.h"
13 #include "internal/nelem.h"
14 #include <openssl/params.h>
15 #include <openssl/bn.h>
16
17 /* The maximum size of the static buffers used to test most things */
18 #define MAX_LEN 20
19
20 static void swap_copy(unsigned char *out, const void *in, size_t len)
21 {
22     size_t j;
23
24     for (j = 0; j < len; j++)
25         out[j] = ((unsigned char *)in)[len - j - 1];
26 }
27
28 static void copy_to_le(unsigned char *out, const void *in, size_t len)
29 {
30 #ifdef B_ENDIAN
31     swap_copy(out, in, len);
32 #else
33     memcpy(out, in, len);
34 #endif
35 }
36
37 static void copy_be_to_native(unsigned char *out, const void *in, size_t len)
38 {
39 #ifdef B_ENDIAN
40     memcpy(out, in, len);
41 #else
42     swap_copy(out, in, len);
43 #endif
44 }
45
46 static const struct {
47     size_t len;
48     unsigned char value[MAX_LEN];
49 } raw_values[] = {
50     { 4, { 0x38, 0x27, 0xbf, 0x3b } },
51     { 4, { 0x9f, 0x26, 0x48, 0x22 } },
52     { 8, { 0x59, 0xb2, 0x1a, 0xe9, 0x2a, 0xd8, 0x46, 0x40 } },
53     { 8, { 0xb4, 0xae, 0xbd, 0xb4, 0xdd, 0x04, 0xb1, 0x4c } },
54     { 16, { 0x61, 0xe8, 0x7e, 0x31, 0xe9, 0x33, 0x83, 0x3d,
55             0x87, 0x99, 0xc7, 0xd8, 0x5d, 0xa9, 0x8b, 0x42 } },
56     { 16, { 0xee, 0x6e, 0x8b, 0xc3, 0xec, 0xcf, 0x37, 0xcc,
57             0x89, 0x67, 0xf2, 0x68, 0x33, 0xa0, 0x14, 0xb0 } },
58 };
59
60 static int test_param_type_extra(const OSSL_PARAM *param, unsigned char *cmp,
61                                  size_t width)
62 {
63     int32_t i32;
64     int64_t i64;
65     size_t s, sz;
66     unsigned char buf[MAX_LEN];
67     const int bit32 = param->data_size == sizeof(int32_t);
68     const int sizet = bit32 && sizeof(size_t) > sizeof(int32_t);
69     const int signd = param->data_type == OSSL_PARAM_INTEGER;
70
71     if (signd) {
72         if ((bit32 && !TEST_true(OSSL_PARAM_get_int32(param, &i32)))
73             || !TEST_true(OSSL_PARAM_get_int64(param, &i64)))
74             return 0;
75     } else {
76         if ((bit32
77              && !TEST_true(OSSL_PARAM_get_uint32(param, (uint32_t *)&i32)))
78             || !TEST_true(OSSL_PARAM_get_uint64(param, (uint64_t *)&i64))
79             || (sizet && !TEST_true(OSSL_PARAM_get_size_t(param, &s))))
80             return 0;
81     }
82
83     /* Check signed types */
84     if (bit32) {
85         copy_to_le(buf, &i32, sizeof(i32));
86         sz = sizeof(i32) < width ? sizeof(i32) : width;
87         if (!TEST_mem_eq(buf, sz, cmp, sz))
88             return 0;
89     }
90     copy_to_le(buf, &i64, sizeof(i64));
91         sz = sizeof(i64) < width ? sizeof(i64) : width;
92     if (!TEST_mem_eq(buf, sz, cmp, sz))
93         return 0;
94     if (sizet && !signd) {
95         copy_to_le(buf, &s, sizeof(s));
96         sz = sizeof(s) < width ? sizeof(s) : width;
97         if (!TEST_mem_eq(buf, sz, cmp, sz))
98             return 0;
99     }
100
101     /* Check a widening write if possible */
102     if (sizeof(size_t) > width) {
103         if (signd) {
104             if (!TEST_true(OSSL_PARAM_set_int32(param, 12345))
105                 || !TEST_true(OSSL_PARAM_get_int64(param, &i64))
106                 || !TEST_size_t_eq(i64, 12345))
107                 return 0;
108         } else {
109             if (!TEST_true(OSSL_PARAM_set_uint32(param, 12345))
110                 || !TEST_true(OSSL_PARAM_get_uint64(param, (uint64_t *)&i64))
111                 || !TEST_size_t_eq(i64, 12345))
112                 return 0;
113         }
114     }
115     return 1;
116 }
117
118 /*
119  * The test cases for each of the bastic integral types are similar.
120  * For each type, a param of that type is set and an attempt to read it
121  * get is made.  Finally, the above function is called to verify that
122  * the params can be read as other types.
123  *
124  * All the real work is done via byte buffers which are converted to machine
125  * byte order and to little endian for comparisons.  Narrower values are best
126  * compared using little endian because their values and positions don't
127  * change.
128  */
129
130 static int test_param_int(int n)
131 {
132     int in, out;
133     unsigned char buf[MAX_LEN], le[MAX_LEN], cmp[sizeof(int)];
134     const size_t len = raw_values[n].len >= sizeof(int) ?
135                        sizeof(int) : raw_values[n].len;
136     OSSL_PARAM param = OSSL_PARAM_int("a", NULL);
137
138     memset(buf, 0, sizeof(buf));
139     memset(le, 0, sizeof(le));
140     copy_be_to_native(buf, raw_values[n].value, len);
141     swap_copy(le, raw_values[n].value, len);
142     memcpy(&in, buf, sizeof(in));
143     param.data = &out;
144     if (!TEST_true(OSSL_PARAM_set_int(&param, in)))
145         return 0;
146     copy_to_le(cmp, &out, sizeof(out));
147     if (!TEST_mem_eq(cmp, len, le, len))
148         return 0;
149     in = 0;
150     param.data = buf;
151     if (!TEST_true(OSSL_PARAM_get_int(&param, &in)))
152         return 0;
153     copy_to_le(cmp, &in, sizeof(in));
154     if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
155         return 0;
156     param.data = &out;
157     return test_param_type_extra(&param, le, sizeof(int));
158 }
159
160 static int test_param_long(int n)
161 {
162     long int in, out;
163     unsigned char buf[MAX_LEN], le[MAX_LEN], cmp[sizeof(long int)];
164     const size_t len = raw_values[n].len >= sizeof(long int)
165                        ? sizeof(long int) : raw_values[n].len;
166     OSSL_PARAM param = OSSL_PARAM_long("a", NULL);
167
168     memset(buf, 0, sizeof(buf));
169     memset(le, 0, sizeof(le));
170     copy_be_to_native(buf, raw_values[n].value, len);
171     swap_copy(le, raw_values[n].value, len);
172     memcpy(&in, buf, sizeof(in));
173     param.data = &out;
174     if (!TEST_true(OSSL_PARAM_set_long(&param, in)))
175         return 0;
176     copy_to_le(cmp, &out, sizeof(out));
177     if (!TEST_mem_eq(cmp, len, le, len))
178         return 0;
179     in = 0;
180     param.data = buf;
181     if (!TEST_true(OSSL_PARAM_get_long(&param, &in)))
182         return 0;
183     copy_to_le(cmp, &in, sizeof(in));
184     if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
185         return 0;
186     param.data = &out;
187     return test_param_type_extra(&param, le, sizeof(long int));
188 }
189
190 static int test_param_uint(int n)
191 {
192     unsigned int in, out;
193     unsigned char buf[MAX_LEN], le[MAX_LEN], cmp[sizeof(unsigned int)];
194     const size_t len = raw_values[n].len >= sizeof(unsigned int) ? sizeof(unsigned int) : raw_values[n].len;
195     OSSL_PARAM param = OSSL_PARAM_uint("a", NULL);
196     memset(buf, 0, sizeof(buf));
197     memset(le, 0, sizeof(le));
198     copy_be_to_native(buf, raw_values[n].value, len);
199     swap_copy(le, raw_values[n].value, len);
200     memcpy(&in, buf, sizeof(in));
201     param.data = &out;
202     if (!TEST_true(OSSL_PARAM_set_uint(&param, in)))
203         return 0;
204     copy_to_le(cmp, &out, sizeof(out));
205     if (!TEST_mem_eq(cmp, len, le, len))
206         return 0;
207     in = 0;
208     param.data = buf;
209     if (!TEST_true(OSSL_PARAM_get_uint(&param, &in)))
210         return 0;
211     copy_to_le(cmp, &in, sizeof(in));
212     if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
213         return 0;
214     param.data = &out;
215     return test_param_type_extra(&param, le, sizeof(unsigned int));
216 }
217
218 static int test_param_ulong(int n)
219 {
220     unsigned long int in, out;
221     unsigned char buf[MAX_LEN], le[MAX_LEN], cmp[sizeof(unsigned long int)];
222     const size_t len = raw_values[n].len >= sizeof(unsigned long int)
223                        ? sizeof(unsigned long int) : raw_values[n].len;
224     OSSL_PARAM param = OSSL_PARAM_ulong("a", NULL);
225     memset(buf, 0, sizeof(buf));
226     memset(le, 0, sizeof(le));
227     copy_be_to_native(buf, raw_values[n].value, len);
228     swap_copy(le, raw_values[n].value, len);
229     memcpy(&in, buf, sizeof(in));
230     param.data = &out;
231     if (!TEST_true(OSSL_PARAM_set_ulong(&param, in)))
232         return 0;
233     copy_to_le(cmp, &out, sizeof(out));
234     if (!TEST_mem_eq(cmp, len, le, len))
235         return 0;
236     in = 0;
237     param.data = buf;
238     if (!TEST_true(OSSL_PARAM_get_ulong(&param, &in)))
239         return 0;
240     copy_to_le(cmp, &in, sizeof(in));
241     if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
242         return 0;
243     param.data = &out;
244     return test_param_type_extra(&param, le, sizeof(unsigned long int));
245 }
246
247 static int test_param_int32(int n)
248 {
249     int32_t in, out;
250     unsigned char buf[MAX_LEN], le[MAX_LEN], cmp[sizeof(int32_t)];
251     const size_t len = raw_values[n].len >= sizeof(int32_t)
252                        ? sizeof(int32_t) : raw_values[n].len;
253     OSSL_PARAM param = OSSL_PARAM_int32("a", NULL);
254     memset(buf, 0, sizeof(buf));
255     memset(le, 0, sizeof(le));
256     copy_be_to_native(buf, raw_values[n].value, len);
257     swap_copy(le, raw_values[n].value, len);
258     memcpy(&in, buf, sizeof(in));
259     param.data = &out;
260     if (!TEST_true(OSSL_PARAM_set_int32(&param, in)))
261         return 0;
262     copy_to_le(cmp, &out, sizeof(out));
263     if (!TEST_mem_eq(cmp, len, le, len))
264         return 0;
265     in = 0;
266     param.data = buf;
267     if (!TEST_true(OSSL_PARAM_get_int32(&param, &in)))
268         return 0;
269     copy_to_le(cmp, &in, sizeof(in));
270     if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
271         return 0;
272     param.data = &out;
273     return test_param_type_extra(&param, le, sizeof(int32_t));
274 }
275
276 static int test_param_uint32(int n)
277 {
278     uint32_t in, out;
279     unsigned char buf[MAX_LEN], le[MAX_LEN], cmp[sizeof(uint32_t)];
280     const size_t len = raw_values[n].len >= sizeof(uint32_t)
281                        ? sizeof(uint32_t) : raw_values[n].len;
282     OSSL_PARAM param = OSSL_PARAM_uint32("a", NULL);
283     memset(buf, 0, sizeof(buf));
284     memset(le, 0, sizeof(le));
285     copy_be_to_native(buf, raw_values[n].value, len);
286     swap_copy(le, raw_values[n].value, len);
287     memcpy(&in, buf, sizeof(in));
288     param.data = &out;
289     if (!TEST_true(OSSL_PARAM_set_uint32(&param, in)))
290         return 0;
291     copy_to_le(cmp, &out, sizeof(out));
292     if (!TEST_mem_eq(cmp, len, le, len))
293         return 0;
294     in = 0;
295     param.data = buf;
296     if (!TEST_true(OSSL_PARAM_get_uint32(&param, &in)))
297         return 0;
298     copy_to_le(cmp, &in, sizeof(in));
299     if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
300         return 0;
301     param.data = &out;
302     return test_param_type_extra(&param, le, sizeof(uint32_t));
303 }
304
305 static int test_param_int64(int n)
306 {
307     int64_t in, out;
308     unsigned char buf[MAX_LEN], le[MAX_LEN], cmp[sizeof(int64_t)];
309     const size_t len = raw_values[n].len >= sizeof(int64_t)
310                        ? sizeof(int64_t) : raw_values[n].len;
311     OSSL_PARAM param = OSSL_PARAM_int64("a", NULL);
312     memset(buf, 0, sizeof(buf));
313     memset(le, 0, sizeof(le));
314     copy_be_to_native(buf, raw_values[n].value, len);
315     swap_copy(le, raw_values[n].value, len);
316     memcpy(&in, buf, sizeof(in));
317     param.data = &out;
318     if (!TEST_true(OSSL_PARAM_set_int64(&param, in)))
319         return 0;
320     copy_to_le(cmp, &out, sizeof(out));
321     if (!TEST_mem_eq(cmp, len, le, len))
322         return 0;
323     in = 0;
324     param.data = buf;
325     if (!TEST_true(OSSL_PARAM_get_int64(&param, &in)))
326         return 0;
327     copy_to_le(cmp, &in, sizeof(in));
328     if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
329         return 0;
330     param.data = &out;
331     return test_param_type_extra(&param, le, sizeof(int64_t));
332 }
333
334 static int test_param_uint64(int n)
335 {
336     uint64_t in, out;
337     unsigned char buf[MAX_LEN], le[MAX_LEN], cmp[sizeof(uint64_t)];
338     const size_t len = raw_values[n].len >= sizeof(uint64_t)
339                        ? sizeof(uint64_t) : raw_values[n].len;
340     OSSL_PARAM param = OSSL_PARAM_uint64("a", NULL);
341     memset(buf, 0, sizeof(buf));
342     memset(le, 0, sizeof(le));
343     copy_be_to_native(buf, raw_values[n].value, len);
344     swap_copy(le, raw_values[n].value, len);
345     memcpy(&in, buf, sizeof(in));
346     param.data = &out;
347     if (!TEST_true(OSSL_PARAM_set_uint64(&param, in)))
348         return 0;
349     copy_to_le(cmp, &out, sizeof(out));
350     if (!TEST_mem_eq(cmp, len, le, len))
351         return 0;
352     in = 0;
353     param.data = buf;
354     if (!TEST_true(OSSL_PARAM_get_uint64(&param, &in)))
355         return 0;
356     copy_to_le(cmp, &in, sizeof(in));
357     if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
358         return 0;
359     param.data = &out;
360     return test_param_type_extra(&param, le, sizeof(uint64_t));
361 }
362
363 static int test_param_size_t(int n)
364 {
365     size_t in, out;
366     unsigned char buf[MAX_LEN], le[MAX_LEN], cmp[sizeof(size_t)];
367     const size_t len = raw_values[n].len >= sizeof(size_t)
368                        ? sizeof(size_t) : raw_values[n].len;
369     OSSL_PARAM param = OSSL_PARAM_size_t("a", NULL);
370     memset(buf, 0, sizeof(buf));
371     memset(le, 0, sizeof(le));
372     copy_be_to_native(buf, raw_values[n].value, len);
373     swap_copy(le, raw_values[n].value, len);
374     memcpy(&in, buf, sizeof(in));
375     param.data = &out;
376     if (!TEST_true(OSSL_PARAM_set_size_t(&param, in)))
377         return 0;
378     copy_to_le(cmp, &out, sizeof(out));
379     if (!TEST_mem_eq(cmp, len, le, len))
380         return 0;
381     in = 0;
382     param.data = buf;
383     if (!TEST_true(OSSL_PARAM_get_size_t(&param, &in)))
384         return 0;
385     copy_to_le(cmp, &in, sizeof(in));
386     if (!TEST_mem_eq(cmp, sizeof(in), le, sizeof(in)))
387         return 0;
388     param.data = &out;
389     return test_param_type_extra(&param, le, sizeof(size_t));
390 }
391
392 static int test_param_bignum(int n)
393 {
394     unsigned char buf[MAX_LEN], bnbuf[MAX_LEN], le[MAX_LEN];
395     const size_t len = raw_values[n].len;
396     size_t bnsize;
397     BIGNUM *b = NULL, *c = NULL;
398     OSSL_PARAM param = OSSL_PARAM_DEFN("bn", OSSL_PARAM_UNSIGNED_INTEGER,
399                                        NULL, 0, NULL);
400     int ret = 0;
401
402     param.data = bnbuf;
403     param.data_size = len;
404     param.return_size = &bnsize;
405
406     copy_be_to_native(buf, raw_values[n].value, len);
407     swap_copy(le, raw_values[n].value, len);
408     if (!TEST_ptr(b = BN_bin2bn(raw_values[n].value, (int)len, NULL)))
409         goto err;
410
411     if (!TEST_true(OSSL_PARAM_set_BN(&param, b))
412         || !TEST_mem_eq(bnbuf, bnsize, buf, bnsize))
413         goto err;
414     param.data_size = *param.return_size;
415     if (!TEST_true(OSSL_PARAM_get_BN(&param, &c))
416         || !TEST_BN_eq(b, c))
417         goto err;
418
419     ret = 1;
420 err:
421     BN_free(b);
422     BN_free(c);
423     return ret;
424 }
425
426 static int test_param_real(void)
427 {
428     double p;
429     OSSL_PARAM param = OSSL_PARAM_double("r", NULL);
430
431     param.data = &p;
432     return TEST_true(OSSL_PARAM_set_double(&param, 3.14159))
433            && TEST_double_eq(p, 3.14159);
434 }
435
436 /*
437  * The tests are a bit special in that they are trying to do both sides
438  * of the param passing.  This means that the OSSL_PARAM structure needs to
439  * be updated so that a get call matches size with the corresponding set call.
440  * This is not a problem in normal usage because the owner of the OSSL_PARAM
441  * "knows" the size of what it wants to put in and gets the size back via the
442  * return_size pointer when it needs to get data out.  That is, the owner
443  * does not need to call these APIs since it has direct access.
444  *
445  * The result is that the tests need the locate call to return a non-const
446  * pointer at times.  Hence the cast here.
447  */
448 static OSSL_PARAM *locate(OSSL_PARAM *p, const char *name)
449 {
450     return (OSSL_PARAM *)OSSL_PARAM_locate(p, name);
451 }
452
453 static int test_param_construct(void)
454 {
455     static const char *int_names[] = {
456         "int", "long", "int32", "int64"
457     };
458     static const char *uint_names[] = {
459         "uint", "ulong", "uint32", "uint64", "size_t"
460     };
461     static const unsigned char bn_val[16] = {
462         0xac, 0x75, 0x22, 0x7d, 0x81, 0x06, 0x7a, 0x23,
463         0xa6, 0xed, 0x87, 0xc7, 0xab, 0xf4, 0x73, 0x22
464     };
465     OSSL_PARAM params[20];
466     char buf[100], buf2[100], *bufp, *bufp2;
467     unsigned char ubuf[100];
468     void *vp, *vp2;
469     OSSL_PARAM *p;
470     const OSSL_PARAM *cp;
471     static const OSSL_PARAM pend = OSSL_PARAM_END;
472     int i, n = 0, ret = 0;
473     unsigned int u;
474     long int l;
475     unsigned long int ul;
476     int32_t i32;
477     uint32_t u32;
478     int64_t i64;
479     uint64_t u64;
480     size_t j, k, s, sz;
481     double d, d2;
482     BIGNUM *bn = NULL, *bn2 = NULL;
483
484     params[n++] = OSSL_PARAM_construct_int("int", &i, &sz);
485     params[n++] = OSSL_PARAM_construct_uint("uint", &u, &sz);
486     params[n++] = OSSL_PARAM_construct_long("long", &l, &sz);
487     params[n++] = OSSL_PARAM_construct_ulong("ulong", &ul, &sz);
488     params[n++] = OSSL_PARAM_construct_int32("int32", &i32, &sz);
489     params[n++] = OSSL_PARAM_construct_int64("int64", &i64, &sz);
490     params[n++] = OSSL_PARAM_construct_uint32("uint32", &u32, &sz);
491     params[n++] = OSSL_PARAM_construct_uint64("uint64", &u64, &sz);
492     params[n++] = OSSL_PARAM_construct_size_t("size_t", &s, &sz);
493     params[n++] = OSSL_PARAM_construct_double("double", &d, &sz);
494     params[n++] = OSSL_PARAM_construct_BN("bignum", ubuf, sizeof(ubuf), &sz);
495     params[n++] = OSSL_PARAM_construct_utf8_string("utf8str", buf, sizeof(buf),
496                                                    &sz);
497     params[n++] = OSSL_PARAM_construct_octet_string("octstr", buf, sizeof(buf),
498                                                     &sz);
499     params[n++] = OSSL_PARAM_construct_utf8_ptr("utf8ptr", &bufp, &sz);
500     params[n++] = OSSL_PARAM_construct_octet_ptr("octptr", &vp, &sz);
501     params[n] = pend;
502
503     /* Search failure */
504     if (!TEST_ptr_null(OSSL_PARAM_locate(params, "fnord")))
505         goto err;
506
507     /* All signed integral types */
508     for (j = 0; j < OSSL_NELEM(int_names); j++) {
509         if (!TEST_ptr(cp = OSSL_PARAM_locate(params, int_names[j]))
510             || !TEST_true(OSSL_PARAM_set_int32(cp, (int32_t)(3 + j)))
511             || !TEST_true(OSSL_PARAM_get_int64(cp, &i64))
512             || !TEST_size_t_eq(cp->data_size, sz)
513             || !TEST_size_t_eq((size_t)i64, 3 + j)) {
514             TEST_note("iteration %zu var %s", j + 1, int_names[j]);
515             goto err;
516         }
517     }
518     /* All unsigned integral types */
519     for (j = 0; j < OSSL_NELEM(uint_names); j++) {
520         if (!TEST_ptr(cp = OSSL_PARAM_locate(params, uint_names[j]))
521             || !TEST_true(OSSL_PARAM_set_uint32(cp, (uint32_t)(3 + j)))
522             || !TEST_true(OSSL_PARAM_get_uint64(cp, &u64))
523             || !TEST_size_t_eq(cp->data_size, sz)
524             || !TEST_size_t_eq((size_t)u64, 3 + j)) {
525             TEST_note("iteration %zu var %s", j + 1, uint_names[j]);
526             goto err;
527         }
528     }
529     /* Real */
530     if (!TEST_ptr(cp = OSSL_PARAM_locate(params, "double"))
531         || !TEST_true(OSSL_PARAM_set_double(cp, 3.14))
532         || !TEST_true(OSSL_PARAM_get_double(cp, &d2))
533         || !TEST_size_t_eq(sz, sizeof(double))
534         || !TEST_double_eq(d, d2))
535         goto err;
536     /* UTF8 string */
537     bufp = NULL;
538     if (!TEST_ptr(cp = OSSL_PARAM_locate(params, "utf8str"))
539         || !TEST_true(OSSL_PARAM_set_utf8_string(cp, "abcdef"))
540         || !TEST_size_t_eq(sz, sizeof("abcdef"))
541         || !TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, 0))
542         || !TEST_str_eq(bufp, "abcdef"))
543         goto err;
544     OPENSSL_free(bufp);
545     bufp = buf2;
546     if (!TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, sizeof(buf2)))
547         || !TEST_str_eq(buf2, "abcdef"))
548         goto err;
549     /* UTF8 pointer */
550     bufp = buf;
551     sz = 0;
552     if (!TEST_ptr(cp = OSSL_PARAM_locate(params, "utf8ptr"))
553         || !TEST_true(OSSL_PARAM_set_utf8_ptr(cp, "tuvwxyz"))
554         || !TEST_size_t_eq(sz, sizeof("tuvwxyz"))
555         || !TEST_str_eq(bufp, "tuvwxyz")
556         || !TEST_true(OSSL_PARAM_get_utf8_ptr(cp, (const char **)&bufp2))
557         || !TEST_ptr_eq(bufp2, bufp))
558         goto err;
559     /* OCTET string */
560     vp = NULL;
561     if (!TEST_ptr(p = locate(params, "octstr"))
562         || !TEST_true(OSSL_PARAM_set_octet_string(p, "abcdefghi",
563                                                   sizeof("abcdefghi")))
564         || !TEST_size_t_eq(sz, sizeof("abcdefghi")))
565         goto err;
566     /* Match the return size to avoid trailing garbage bytes */
567     p->data_size = *p->return_size;
568     if (!TEST_true(OSSL_PARAM_get_octet_string(p, &vp, 0, &s))
569         || !TEST_size_t_eq(s, sizeof("abcdefghi"))
570         || !TEST_mem_eq(vp, sizeof("abcdefghi"),
571                         "abcdefghi", sizeof("abcdefghi")))
572         goto err;
573     OPENSSL_free(vp);
574     vp = buf2;
575     if (!TEST_true(OSSL_PARAM_get_octet_string(p, &vp, sizeof(buf2), &s))
576         || !TEST_size_t_eq(s, sizeof("abcdefghi"))
577         || !TEST_mem_eq(vp, sizeof("abcdefghi"),
578                         "abcdefghi", sizeof("abcdefghi")))
579         goto err;
580     /* OCTET pointer */
581     vp = &l;
582     sz = 0;
583     if (!TEST_ptr(p = locate(params, "octptr"))
584         || !TEST_true(OSSL_PARAM_set_octet_ptr(p, &ul, sizeof(ul)))
585         || !TEST_size_t_eq(sz, sizeof(ul))
586         || !TEST_ptr_eq(vp, &ul))
587         goto err;
588     /* Match the return size to avoid trailing garbage bytes */
589     p->data_size = *p->return_size;
590     if (!TEST_true(OSSL_PARAM_get_octet_ptr(p, (const void **)&vp2, &k))
591         || !TEST_size_t_eq(k, sizeof(ul))
592         || !TEST_ptr_eq(vp2, vp))
593         goto err;
594     /* BIGNUM */
595     if (!TEST_ptr(p = locate(params, "bignum"))
596         || !TEST_ptr(bn = BN_lebin2bn(bn_val, (int)sizeof(bn_val), NULL))
597         || !TEST_true(OSSL_PARAM_set_BN(p, bn))
598         || !TEST_size_t_eq(sz, sizeof(bn_val)))
599         goto err;
600     /* Match the return size to avoid trailing garbage bytes */
601     p->data_size = *p->return_size;
602     if(!TEST_true(OSSL_PARAM_get_BN(p, &bn2))
603         || !TEST_BN_eq(bn, bn2))
604         goto err;
605     ret = 1;
606 err:
607     BN_free(bn);
608     BN_free(bn2);
609     return ret;
610 }
611
612 int setup_tests(void)
613 {
614     ADD_ALL_TESTS(test_param_int, OSSL_NELEM(raw_values));
615     ADD_ALL_TESTS(test_param_long, OSSL_NELEM(raw_values));
616     ADD_ALL_TESTS(test_param_uint, OSSL_NELEM(raw_values));
617     ADD_ALL_TESTS(test_param_ulong, OSSL_NELEM(raw_values));
618     ADD_ALL_TESTS(test_param_int32, OSSL_NELEM(raw_values));
619     ADD_ALL_TESTS(test_param_uint32, OSSL_NELEM(raw_values));
620     ADD_ALL_TESTS(test_param_size_t, OSSL_NELEM(raw_values));
621     ADD_ALL_TESTS(test_param_int64, OSSL_NELEM(raw_values));
622     ADD_ALL_TESTS(test_param_uint64, OSSL_NELEM(raw_values));
623     ADD_ALL_TESTS(test_param_bignum, OSSL_NELEM(raw_values));
624     ADD_TEST(test_param_real);
625     ADD_TEST(test_param_construct);
626     return 1;
627 }