2 * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
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
13 #include "internal/nelem.h"
14 #include "internal/endian.h"
15 #include <openssl/params.h>
16 #include <openssl/bn.h>
18 /* The maximum size of the static buffers used to test most things */
21 static void swap_copy(unsigned char *out, const void *in, size_t len)
25 for (j = 0; j < len; j++)
26 out[j] = ((unsigned char *)in)[len - j - 1];
30 * A memory copy that converts the native byte ordering either to or from
31 * little endian format.
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
37 static void le_copy(unsigned char *out, size_t outlen,
38 const void *in, size_t inlen)
42 if (IS_LITTLE_ENDIAN) {
43 memcpy(out, in, outlen);
46 in = (const char *)in + inlen - outlen;
47 swap_copy(out, in, outlen);
53 unsigned char value[MAX_LEN];
57 { 2, { 0x01, 0xe9 } },
58 { 2, { 0xff, 0x53 } },
59 { 3, { 0x16, 0xff, 0x7c } },
60 { 3, { 0xa8, 0x9c, 0x0e } },
61 { 4, { 0x38, 0x27, 0xbf, 0x3b } },
62 { 4, { 0x9f, 0x26, 0x48, 0x22 } },
63 { 5, { 0x30, 0x65, 0xfa, 0xe4, 0x81 } },
64 { 5, { 0xd1, 0x76, 0x01, 0x1b, 0xcd } },
65 { 8, { 0x59, 0xb2, 0x1a, 0xe9, 0x2a, 0xd8, 0x46, 0x40 } },
66 { 8, { 0xb4, 0xae, 0xbd, 0xb4, 0xdd, 0x04, 0xb1, 0x4c } },
67 { 16, { 0x61, 0xe8, 0x7e, 0x31, 0xe9, 0x33, 0x83, 0x3d,
68 0x87, 0x99, 0xc7, 0xd8, 0x5d, 0xa9, 0x8b, 0x42 } },
69 { 16, { 0xee, 0x6e, 0x8b, 0xc3, 0xec, 0xcf, 0x37, 0xcc,
70 0x89, 0x67, 0xf2, 0x68, 0x33, 0xa0, 0x14, 0xb0 } },
73 static int test_param_type_null(OSSL_PARAM *param)
80 switch(param->data_type) {
81 case OSSL_PARAM_INTEGER:
82 if (param->data_size == sizeof(int32_t))
83 rc = OSSL_PARAM_get_int32(param, (int32_t *)&intval);
84 else if (param->data_size == sizeof(uint64_t))
85 rc = OSSL_PARAM_get_int64(param, (int64_t *)&intval);
89 case OSSL_PARAM_UNSIGNED_INTEGER:
90 if (param->data_size == sizeof(uint32_t))
91 rc = OSSL_PARAM_get_uint32(param, (uint32_t *)&intval);
92 else if (param->data_size == sizeof(uint64_t))
93 rc = OSSL_PARAM_get_uint64(param, &intval);
95 rc = OSSL_PARAM_get_BN(param, &bn);
98 rc = OSSL_PARAM_get_double(param, &dval);
100 case OSSL_PARAM_UTF8_STRING:
101 case OSSL_PARAM_OCTET_STRING:
102 case OSSL_PARAM_UTF8_PTR:
103 case OSSL_PARAM_OCTET_PTR:
104 /* these are allowed to be null */
110 * we expect the various OSSL_PARAM_get functions above
111 * to return failure when the data is set to NULL
116 static int test_param_type_extra(OSSL_PARAM *param, const unsigned char *cmp,
122 unsigned char buf[MAX_LEN];
123 const int bit32 = param->data_size <= sizeof(int32_t);
124 const int sizet = param->data_size <= sizeof(size_t);
125 const int signd = param->data_type == OSSL_PARAM_INTEGER;
128 * Set the unmodified sentinel directly because there is no param array
131 param->return_size = OSSL_PARAM_UNMODIFIED;
133 if ((bit32 && !TEST_true(OSSL_PARAM_get_int32(param, &i32)))
134 || !TEST_true(OSSL_PARAM_get_int64(param, &i64)))
138 && !TEST_true(OSSL_PARAM_get_uint32(param, (uint32_t *)&i32)))
139 || !TEST_true(OSSL_PARAM_get_uint64(param, (uint64_t *)&i64))
140 || (sizet && !TEST_true(OSSL_PARAM_get_size_t(param, &s))))
143 if (!TEST_false(OSSL_PARAM_modified(param)))
146 /* Check signed types */
148 le_copy(buf, sizeof(i32), &i32, sizeof(i32));
149 sz = sizeof(i32) < width ? sizeof(i32) : width;
150 if (!TEST_mem_eq(buf, sz, cmp, sz))
153 le_copy(buf, sizeof(i64), &i64, sizeof(i64));
154 sz = sizeof(i64) < width ? sizeof(i64) : width;
155 if (!TEST_mem_eq(buf, sz, cmp, sz))
157 if (sizet && !signd) {
158 le_copy(buf, sizeof(s), &s, sizeof(s));
159 sz = sizeof(s) < width ? sizeof(s) : width;
160 if (!TEST_mem_eq(buf, sz, cmp, sz))
164 /* Check a widening write if possible */
165 if (sizeof(size_t) > width) {
167 if (!TEST_true(OSSL_PARAM_set_int32(param, 12345))
168 || !TEST_true(OSSL_PARAM_get_int64(param, &i64))
169 || !TEST_size_t_eq((size_t)i64, 12345))
172 if (!TEST_true(OSSL_PARAM_set_uint32(param, 12345))
173 || !TEST_true(OSSL_PARAM_get_uint64(param, (uint64_t *)&i64))
174 || !TEST_size_t_eq((size_t)i64, 12345))
177 if (!TEST_true(OSSL_PARAM_modified(param)))
184 * The test cases for each of the bastic integral types are similar.
185 * For each type, a param of that type is set and an attempt to read it
186 * get is made. Finally, the above function is called to verify that
187 * the params can be read as other types.
189 * All the real work is done via byte buffers which are converted to machine
190 * byte order and to little endian for comparisons. Narrower values are best
191 * compared using little endian because their values and positions don't
195 static int test_param_int(int n)
198 unsigned char buf[MAX_LEN], cmp[sizeof(int)];
199 const size_t len = raw_values[n].len >= sizeof(int) ?
200 sizeof(int) : raw_values[n].len;
201 OSSL_PARAM param = OSSL_PARAM_int("a", NULL);
203 if (!TEST_int_eq(test_param_type_null(¶m), 1))
206 memset(buf, 0, sizeof(buf));
207 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
208 memcpy(&in, buf, sizeof(in));
210 if (!TEST_true(OSSL_PARAM_set_int(¶m, in)))
212 le_copy(cmp, sizeof(out), &out, sizeof(out));
213 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
216 if (!TEST_true(OSSL_PARAM_get_int(¶m, &in)))
218 le_copy(cmp, sizeof(in), &in, sizeof(in));
219 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
222 return test_param_type_extra(¶m, raw_values[n].value, sizeof(int));
225 static int test_param_long(int n)
228 unsigned char buf[MAX_LEN], cmp[sizeof(long int)];
229 const size_t len = raw_values[n].len >= sizeof(long int)
230 ? sizeof(long int) : raw_values[n].len;
231 OSSL_PARAM param = OSSL_PARAM_long("a", NULL);
233 if (!TEST_int_eq(test_param_type_null(¶m), 1))
236 memset(buf, 0, sizeof(buf));
237 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
238 memcpy(&in, buf, sizeof(in));
240 if (!TEST_true(OSSL_PARAM_set_long(¶m, in)))
242 le_copy(cmp, sizeof(out), &out, sizeof(out));
243 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
246 if (!TEST_true(OSSL_PARAM_get_long(¶m, &in)))
248 le_copy(cmp, sizeof(in), &in, sizeof(in));
249 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
252 return test_param_type_extra(¶m, raw_values[n].value, sizeof(long int));
255 static int test_param_uint(int n)
257 unsigned int in, out;
258 unsigned char buf[MAX_LEN], cmp[sizeof(unsigned int)];
259 const size_t len = raw_values[n].len >= sizeof(unsigned int) ? sizeof(unsigned int) : raw_values[n].len;
260 OSSL_PARAM param = OSSL_PARAM_uint("a", NULL);
262 if (!TEST_int_eq(test_param_type_null(¶m), 1))
265 memset(buf, 0, sizeof(buf));
266 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
267 memcpy(&in, buf, sizeof(in));
269 if (!TEST_true(OSSL_PARAM_set_uint(¶m, in)))
271 le_copy(cmp, sizeof(out), &out, sizeof(out));
272 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
275 if (!TEST_true(OSSL_PARAM_get_uint(¶m, &in)))
277 le_copy(cmp, sizeof(in), &in, sizeof(in));
278 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
281 return test_param_type_extra(¶m, raw_values[n].value, sizeof(unsigned int));
284 static int test_param_ulong(int n)
286 unsigned long int in, out;
287 unsigned char buf[MAX_LEN], cmp[sizeof(unsigned long int)];
288 const size_t len = raw_values[n].len >= sizeof(unsigned long int)
289 ? sizeof(unsigned long int) : raw_values[n].len;
290 OSSL_PARAM param = OSSL_PARAM_ulong("a", NULL);
292 if (!TEST_int_eq(test_param_type_null(¶m), 1))
295 memset(buf, 0, sizeof(buf));
296 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
297 memcpy(&in, buf, sizeof(in));
299 if (!TEST_true(OSSL_PARAM_set_ulong(¶m, in)))
301 le_copy(cmp, sizeof(out), &out, sizeof(out));
302 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
305 if (!TEST_true(OSSL_PARAM_get_ulong(¶m, &in)))
307 le_copy(cmp, sizeof(in), &in, sizeof(in));
308 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
311 return test_param_type_extra(¶m, raw_values[n].value, sizeof(unsigned long int));
314 static int test_param_int32(int n)
317 unsigned char buf[MAX_LEN], cmp[sizeof(int32_t)];
318 const size_t len = raw_values[n].len >= sizeof(int32_t)
319 ? sizeof(int32_t) : raw_values[n].len;
320 OSSL_PARAM param = OSSL_PARAM_int32("a", NULL);
322 if (!TEST_int_eq(test_param_type_null(¶m), 1))
325 memset(buf, 0, sizeof(buf));
326 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
327 memcpy(&in, buf, sizeof(in));
329 if (!TEST_true(OSSL_PARAM_set_int32(¶m, in)))
331 le_copy(cmp, sizeof(out), &out, sizeof(out));
332 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
335 if (!TEST_true(OSSL_PARAM_get_int32(¶m, &in)))
337 le_copy(cmp, sizeof(in), &in, sizeof(in));
338 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
341 return test_param_type_extra(¶m, raw_values[n].value, sizeof(int32_t));
344 static int test_param_uint32(int n)
347 unsigned char buf[MAX_LEN], cmp[sizeof(uint32_t)];
348 const size_t len = raw_values[n].len >= sizeof(uint32_t)
349 ? sizeof(uint32_t) : raw_values[n].len;
350 OSSL_PARAM param = OSSL_PARAM_uint32("a", NULL);
352 if (!TEST_int_eq(test_param_type_null(¶m), 1))
355 memset(buf, 0, sizeof(buf));
356 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
357 memcpy(&in, buf, sizeof(in));
359 if (!TEST_true(OSSL_PARAM_set_uint32(¶m, in)))
361 le_copy(cmp, sizeof(out), &out, sizeof(out));
362 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
365 if (!TEST_true(OSSL_PARAM_get_uint32(¶m, &in)))
367 le_copy(cmp, sizeof(in), &in, sizeof(in));
368 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
371 return test_param_type_extra(¶m, raw_values[n].value, sizeof(uint32_t));
374 static int test_param_int64(int n)
377 unsigned char buf[MAX_LEN], cmp[sizeof(int64_t)];
378 const size_t len = raw_values[n].len >= sizeof(int64_t)
379 ? sizeof(int64_t) : raw_values[n].len;
380 OSSL_PARAM param = OSSL_PARAM_int64("a", NULL);
382 if (!TEST_int_eq(test_param_type_null(¶m), 1))
385 memset(buf, 0, sizeof(buf));
386 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
387 memcpy(&in, buf, sizeof(in));
389 if (!TEST_true(OSSL_PARAM_set_int64(¶m, in)))
391 le_copy(cmp, sizeof(out), &out, sizeof(out));
392 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
395 if (!TEST_true(OSSL_PARAM_get_int64(¶m, &in)))
397 le_copy(cmp, sizeof(in), &in, sizeof(in));
398 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
401 return test_param_type_extra(¶m, raw_values[n].value, sizeof(int64_t));
404 static int test_param_uint64(int n)
407 unsigned char buf[MAX_LEN], cmp[sizeof(uint64_t)];
408 const size_t len = raw_values[n].len >= sizeof(uint64_t)
409 ? sizeof(uint64_t) : raw_values[n].len;
410 OSSL_PARAM param = OSSL_PARAM_uint64("a", NULL);
412 if (!TEST_int_eq(test_param_type_null(¶m), 1))
415 memset(buf, 0, sizeof(buf));
416 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
417 memcpy(&in, buf, sizeof(in));
419 if (!TEST_true(OSSL_PARAM_set_uint64(¶m, in)))
421 le_copy(cmp, sizeof(out), &out, sizeof(out));
422 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
425 if (!TEST_true(OSSL_PARAM_get_uint64(¶m, &in)))
427 le_copy(cmp, sizeof(in), &in, sizeof(in));
428 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
431 return test_param_type_extra(¶m, raw_values[n].value, sizeof(uint64_t));
434 static int test_param_size_t(int n)
437 unsigned char buf[MAX_LEN], cmp[sizeof(size_t)];
438 const size_t len = raw_values[n].len >= sizeof(size_t)
439 ? sizeof(size_t) : raw_values[n].len;
440 OSSL_PARAM param = OSSL_PARAM_size_t("a", NULL);
442 if (!TEST_int_eq(test_param_type_null(¶m), 1))
445 memset(buf, 0, sizeof(buf));
446 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
447 memcpy(&in, buf, sizeof(in));
449 if (!TEST_true(OSSL_PARAM_set_size_t(¶m, in)))
451 le_copy(cmp, sizeof(out), &out, sizeof(out));
452 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
455 if (!TEST_true(OSSL_PARAM_get_size_t(¶m, &in)))
457 le_copy(cmp, sizeof(in), &in, sizeof(in));
458 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
461 return test_param_type_extra(¶m, raw_values[n].value, sizeof(size_t));
464 static int test_param_time_t(int n)
467 unsigned char buf[MAX_LEN], cmp[sizeof(time_t)];
468 const size_t len = raw_values[n].len >= sizeof(time_t)
469 ? sizeof(time_t) : raw_values[n].len;
470 OSSL_PARAM param = OSSL_PARAM_time_t("a", NULL);
472 if (!TEST_int_eq(test_param_type_null(¶m), 1))
475 memset(buf, 0, sizeof(buf));
476 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
477 memcpy(&in, buf, sizeof(in));
479 if (!TEST_true(OSSL_PARAM_set_time_t(¶m, in)))
481 le_copy(cmp, sizeof(out), &out, sizeof(out));
482 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
485 if (!TEST_true(OSSL_PARAM_get_time_t(¶m, &in)))
487 le_copy(cmp, sizeof(in), &in, sizeof(in));
488 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
491 return test_param_type_extra(¶m, raw_values[n].value, sizeof(size_t));
494 static int test_param_bignum(int n)
496 unsigned char buf[MAX_LEN], bnbuf[MAX_LEN];
497 const size_t len = raw_values[n].len;
498 BIGNUM *b = NULL, *c = NULL;
499 OSSL_PARAM param = OSSL_PARAM_DEFN("bn", OSSL_PARAM_UNSIGNED_INTEGER,
503 if (!TEST_int_eq(test_param_type_null(¶m), 1))
507 param.data_size = sizeof(bnbuf);
509 if (!TEST_ptr(b = BN_lebin2bn(raw_values[n].value, (int)len, NULL)))
512 if (!TEST_true(OSSL_PARAM_set_BN(¶m, b)))
514 le_copy(buf, len, bnbuf, sizeof(bnbuf));
515 if (!TEST_mem_eq(raw_values[n].value, len, buf, len))
517 param.data_size = param.return_size;
518 if (!TEST_true(OSSL_PARAM_get_BN(¶m, &c))
519 || !TEST_BN_eq(b, c))
529 static int test_param_signed_bignum(int n)
531 unsigned char buf[MAX_LEN], bnbuf[MAX_LEN];
532 const size_t len = raw_values[n].len;
533 BIGNUM *b = NULL, *c = NULL;
534 OSSL_PARAM param = OSSL_PARAM_DEFN("bn", OSSL_PARAM_INTEGER, NULL, 0);
537 if (!TEST_int_eq(test_param_type_null(¶m), 1))
541 param.data_size = sizeof(bnbuf);
543 if (!TEST_ptr(b = BN_signed_lebin2bn(raw_values[n].value, (int)len, NULL)))
546 /* raw_values are little endian */
547 if (!TEST_false(!!(raw_values[n].value[len - 1] & 0x80) ^ BN_is_negative(b)))
549 if (!TEST_true(OSSL_PARAM_set_BN(¶m, b)))
551 le_copy(buf, len, bnbuf, sizeof(bnbuf));
552 if (!TEST_mem_eq(raw_values[n].value, len, buf, len))
554 param.data_size = param.return_size;
555 if (!TEST_true(OSSL_PARAM_get_BN(¶m, &c))
556 || !TEST_BN_eq(b, c)) {
557 BN_print_fp(stderr, c);
568 static int test_param_real(void)
571 OSSL_PARAM param = OSSL_PARAM_double("r", NULL);
573 if (!TEST_int_eq(test_param_type_null(¶m), 1))
577 return TEST_true(OSSL_PARAM_set_double(¶m, 3.14159))
578 && TEST_double_eq(p, 3.14159);
581 static int test_param_construct(int tstid)
583 static const char *int_names[] = {
584 "int", "long", "int32", "int64"
586 static const char *uint_names[] = {
587 "uint", "ulong", "uint32", "uint64", "size_t"
589 static const unsigned char bn_val[16] = {
590 0xac, 0x75, 0x22, 0x7d, 0x81, 0x06, 0x7a, 0x23,
591 0xa6, 0xed, 0x87, 0xc7, 0xab, 0xf4, 0x73, 0x22
593 OSSL_PARAM *p = NULL, *p1 = NULL;
594 static const OSSL_PARAM params_empty[] = {
597 OSSL_PARAM params[20];
598 char buf[100], buf2[100], *bufp, *bufp2;
599 unsigned char ubuf[100];
600 void *vp, *vpn = NULL, *vp2;
602 int i, n = 0, ret = 0;
605 unsigned long int ul;
612 BIGNUM *bn = NULL, *bn2 = NULL;
614 params[n++] = OSSL_PARAM_construct_int("int", &i);
615 params[n++] = OSSL_PARAM_construct_uint("uint", &u);
616 params[n++] = OSSL_PARAM_construct_long("long", &l);
617 params[n++] = OSSL_PARAM_construct_ulong("ulong", &ul);
618 params[n++] = OSSL_PARAM_construct_int32("int32", &i32);
619 params[n++] = OSSL_PARAM_construct_int64("int64", &i64);
620 params[n++] = OSSL_PARAM_construct_uint32("uint32", &u32);
621 params[n++] = OSSL_PARAM_construct_uint64("uint64", &u64);
622 params[n++] = OSSL_PARAM_construct_size_t("size_t", &s);
623 params[n++] = OSSL_PARAM_construct_double("double", &d);
624 params[n++] = OSSL_PARAM_construct_BN("bignum", ubuf, sizeof(ubuf));
625 params[n++] = OSSL_PARAM_construct_utf8_string("utf8str", buf, sizeof(buf));
626 params[n++] = OSSL_PARAM_construct_octet_string("octstr", buf, sizeof(buf));
627 params[n++] = OSSL_PARAM_construct_utf8_ptr("utf8ptr", &bufp, 0);
628 params[n++] = OSSL_PARAM_construct_octet_ptr("octptr", &vp, 0);
629 params[n] = OSSL_PARAM_construct_end();
636 p = OSSL_PARAM_merge(params, params_empty);
639 p = OSSL_PARAM_dup(params);
642 p1 = OSSL_PARAM_dup(params);
643 p = OSSL_PARAM_merge(p1, params_empty);
648 if (!TEST_ptr_null(OSSL_PARAM_locate(p, "fnord")))
651 /* All signed integral types */
652 for (j = 0; j < OSSL_NELEM(int_names); j++) {
653 if (!TEST_ptr(cp = OSSL_PARAM_locate(p, int_names[j]))
654 || !TEST_true(OSSL_PARAM_set_int32(cp, (int32_t)(3 + j)))
655 || !TEST_true(OSSL_PARAM_get_int64(cp, &i64))
656 || !TEST_size_t_eq(cp->data_size, cp->return_size)
657 || !TEST_size_t_eq((size_t)i64, 3 + j)) {
658 TEST_note("iteration %zu var %s", j + 1, int_names[j]);
662 /* All unsigned integral types */
663 for (j = 0; j < OSSL_NELEM(uint_names); j++) {
664 if (!TEST_ptr(cp = OSSL_PARAM_locate(p, uint_names[j]))
665 || !TEST_true(OSSL_PARAM_set_uint32(cp, (uint32_t)(3 + j)))
666 || !TEST_true(OSSL_PARAM_get_uint64(cp, &u64))
667 || !TEST_size_t_eq(cp->data_size, cp->return_size)
668 || !TEST_size_t_eq((size_t)u64, 3 + j)) {
669 TEST_note("iteration %zu var %s", j + 1, uint_names[j]);
674 if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "double"))
675 || !TEST_true(OSSL_PARAM_set_double(cp, 3.14))
676 || !TEST_true(OSSL_PARAM_get_double(cp, &d2))
677 || !TEST_size_t_eq(cp->return_size, sizeof(double))
678 || !TEST_double_eq(d2, 3.14)
679 || (tstid <= 1 && !TEST_double_eq(d, d2)))
683 if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "utf8str"))
684 || !TEST_true(OSSL_PARAM_set_utf8_string(cp, "abcdef"))
685 || !TEST_size_t_eq(cp->return_size, sizeof("abcdef") - 1)
686 || !TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, 0))
687 || !TEST_str_eq(bufp, "abcdef")) {
693 if (!TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, sizeof(buf2)))
694 || !TEST_str_eq(buf2, "abcdef"))
697 /* Note that the size of a UTF8 string does *NOT* include the NUL byte */
699 if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "utf8ptr"))
700 || !TEST_true(OSSL_PARAM_set_utf8_ptr(cp, "tuvwxyz"))
701 || !TEST_size_t_eq(cp->return_size, sizeof("tuvwxyz") - 1)
702 || !TEST_true(OSSL_PARAM_get_utf8_ptr(cp, (const char **)&bufp2))
703 || !TEST_str_eq(bufp2, "tuvwxyz")
704 || (tstid <= 1 && !TEST_ptr_eq(bufp2, bufp)))
707 if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "octstr"))
708 || !TEST_true(OSSL_PARAM_set_octet_string(cp, "abcdefghi",
709 sizeof("abcdefghi")))
710 || !TEST_size_t_eq(cp->return_size, sizeof("abcdefghi")))
712 /* Match the return size to avoid trailing garbage bytes */
713 cp->data_size = cp->return_size;
714 if (!TEST_true(OSSL_PARAM_get_octet_string(cp, &vpn, 0, &s))
715 || !TEST_size_t_eq(s, sizeof("abcdefghi"))
716 || !TEST_mem_eq(vpn, sizeof("abcdefghi"),
717 "abcdefghi", sizeof("abcdefghi")))
720 if (!TEST_true(OSSL_PARAM_get_octet_string(cp, &vp, sizeof(buf2), &s))
721 || !TEST_size_t_eq(s, sizeof("abcdefghi"))
722 || !TEST_mem_eq(vp, sizeof("abcdefghi"),
723 "abcdefghi", sizeof("abcdefghi")))
727 if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "octptr"))
728 || !TEST_true(OSSL_PARAM_set_octet_ptr(cp, &ul, sizeof(ul)))
729 || !TEST_size_t_eq(cp->return_size, sizeof(ul))
730 || (tstid <= 1 && !TEST_ptr_eq(vp, &ul)))
732 /* Match the return size to avoid trailing garbage bytes */
733 cp->data_size = cp->return_size;
734 if (!TEST_true(OSSL_PARAM_get_octet_ptr(cp, (const void **)&vp2, &k))
735 || !TEST_size_t_eq(k, sizeof(ul))
736 || (tstid <= 1 && !TEST_ptr_eq(vp2, vp)))
739 if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "bignum"))
740 || !TEST_ptr(bn = BN_lebin2bn(bn_val, (int)sizeof(bn_val), NULL))
741 || !TEST_true(OSSL_PARAM_set_BN(cp, bn))
742 || !TEST_size_t_eq(cp->data_size, cp->return_size))
744 /* Match the return size to avoid trailing garbage bytes */
745 cp->data_size = cp->return_size;
746 if (!TEST_true(OSSL_PARAM_get_BN(cp, &bn2))
747 || !TEST_BN_eq(bn, bn2))
760 static int test_param_modified(void)
762 OSSL_PARAM param[3] = { OSSL_PARAM_int("a", NULL),
763 OSSL_PARAM_int("b", NULL),
769 if (!TEST_false(OSSL_PARAM_modified(param))
770 && !TEST_true(OSSL_PARAM_set_int32(param, 1234))
771 && !TEST_true(OSSL_PARAM_modified(param))
772 && !TEST_false(OSSL_PARAM_modified(param + 1))
773 && !TEST_true(OSSL_PARAM_set_int32(param + 1, 1))
774 && !TEST_true(OSSL_PARAM_modified(param + 1)))
776 OSSL_PARAM_set_all_unmodified(param);
777 if (!TEST_false(OSSL_PARAM_modified(param))
778 && !TEST_true(OSSL_PARAM_set_int32(param, 4321))
779 && !TEST_true(OSSL_PARAM_modified(param))
780 && !TEST_false(OSSL_PARAM_modified(param + 1))
781 && !TEST_true(OSSL_PARAM_set_int32(param + 1, 2))
782 && !TEST_true(OSSL_PARAM_modified(param + 1)))
787 static int test_param_copy_null(void)
790 int a = 1, b = 2, i = 0;
791 OSSL_PARAM *cp1 = NULL, *cp2 = NULL, *p;
794 param[i++] = OSSL_PARAM_construct_int("a", &a);
795 param[i++] = OSSL_PARAM_construct_int("b", &b);
796 param[i] = OSSL_PARAM_construct_end();
798 ret = TEST_ptr_null(OSSL_PARAM_dup(NULL))
799 && TEST_ptr(cp1 = OSSL_PARAM_merge(NULL, param))
800 && TEST_ptr(p = OSSL_PARAM_locate(cp1, "a"))
801 && TEST_true(OSSL_PARAM_get_int(p, &val))
802 && TEST_int_eq(val, 1)
803 && TEST_ptr(p = OSSL_PARAM_locate(cp1, "b"))
804 && TEST_true(OSSL_PARAM_get_int(p, &val))
805 && TEST_int_eq(val, 2)
806 && TEST_ptr(cp2 = OSSL_PARAM_merge(param, NULL))
807 && TEST_ptr(p = OSSL_PARAM_locate(cp2, "a"))
808 && TEST_true(OSSL_PARAM_get_int(p, &val))
809 && TEST_int_eq(val, 1)
810 && TEST_ptr(p = OSSL_PARAM_locate(cp2, "b"))
811 && TEST_true(OSSL_PARAM_get_int(p, &val))
812 && TEST_int_eq(val, 2)
813 && TEST_ptr_null(OSSL_PARAM_merge(NULL, NULL));
814 OSSL_PARAM_free(cp2);
815 OSSL_PARAM_free(cp1);
819 int setup_tests(void)
821 ADD_ALL_TESTS(test_param_int, OSSL_NELEM(raw_values));
822 ADD_ALL_TESTS(test_param_long, OSSL_NELEM(raw_values));
823 ADD_ALL_TESTS(test_param_uint, OSSL_NELEM(raw_values));
824 ADD_ALL_TESTS(test_param_ulong, OSSL_NELEM(raw_values));
825 ADD_ALL_TESTS(test_param_int32, OSSL_NELEM(raw_values));
826 ADD_ALL_TESTS(test_param_uint32, OSSL_NELEM(raw_values));
827 ADD_ALL_TESTS(test_param_size_t, OSSL_NELEM(raw_values));
828 ADD_ALL_TESTS(test_param_time_t, OSSL_NELEM(raw_values));
829 ADD_ALL_TESTS(test_param_int64, OSSL_NELEM(raw_values));
830 ADD_ALL_TESTS(test_param_uint64, OSSL_NELEM(raw_values));
831 ADD_ALL_TESTS(test_param_bignum, OSSL_NELEM(raw_values));
832 ADD_ALL_TESTS(test_param_signed_bignum, OSSL_NELEM(raw_values));
833 ADD_TEST(test_param_real);
834 ADD_ALL_TESTS(test_param_construct, 4);
835 ADD_TEST(test_param_modified);
836 ADD_TEST(test_param_copy_null);