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