2 * Copyright 2019-2024 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
;
49 if (!ossl_assert(outlen
<= inlen
))
51 swap_copy(out
, in
, inlen
);
57 unsigned char value
[MAX_LEN
];
61 { 2, { 0x01, 0xe9 } },
62 { 2, { 0xff, 0x53 } },
63 { 3, { 0x16, 0xff, 0x7c } },
64 { 3, { 0xa8, 0x9c, 0x0e } },
65 { 4, { 0x38, 0x27, 0xbf, 0x3b } },
66 { 4, { 0x9f, 0x26, 0x48, 0x22 } },
67 { 5, { 0x30, 0x65, 0xfa, 0xe4, 0x81 } },
68 { 5, { 0xd1, 0x76, 0x01, 0x1b, 0xcd } },
69 { 8, { 0x59, 0xb2, 0x1a, 0xe9, 0x2a, 0xd8, 0x46, 0x40 } },
70 { 8, { 0xb4, 0xae, 0xbd, 0xb4, 0xdd, 0x04, 0xb1, 0x4c } },
71 { 16, { 0x61, 0xe8, 0x7e, 0x31, 0xe9, 0x33, 0x83, 0x3d,
72 0x87, 0x99, 0xc7, 0xd8, 0x5d, 0xa9, 0x8b, 0x42 } },
73 { 16, { 0xee, 0x6e, 0x8b, 0xc3, 0xec, 0xcf, 0x37, 0xcc,
74 0x89, 0x67, 0xf2, 0x68, 0x33, 0xa0, 0x14, 0xb0 } },
77 static int test_param_type_null(OSSL_PARAM
*param
)
84 switch(param
->data_type
) {
85 case OSSL_PARAM_INTEGER
:
86 if (param
->data_size
== sizeof(int32_t))
87 rc
= OSSL_PARAM_get_int32(param
, (int32_t *)&intval
);
88 else if (param
->data_size
== sizeof(uint64_t))
89 rc
= OSSL_PARAM_get_int64(param
, (int64_t *)&intval
);
93 case OSSL_PARAM_UNSIGNED_INTEGER
:
94 if (param
->data_size
== sizeof(uint32_t))
95 rc
= OSSL_PARAM_get_uint32(param
, (uint32_t *)&intval
);
96 else if (param
->data_size
== sizeof(uint64_t))
97 rc
= OSSL_PARAM_get_uint64(param
, &intval
);
99 rc
= OSSL_PARAM_get_BN(param
, &bn
);
101 case OSSL_PARAM_REAL
:
102 rc
= OSSL_PARAM_get_double(param
, &dval
);
104 case OSSL_PARAM_UTF8_STRING
:
105 case OSSL_PARAM_OCTET_STRING
:
106 case OSSL_PARAM_UTF8_PTR
:
107 case OSSL_PARAM_OCTET_PTR
:
108 /* these are allowed to be null */
114 * we expect the various OSSL_PARAM_get functions above
115 * to return failure when the data is set to NULL
120 static int test_param_type_extra(OSSL_PARAM
*param
, const unsigned char *cmp
,
126 unsigned char buf
[MAX_LEN
];
127 const int bit32
= param
->data_size
<= sizeof(int32_t);
128 const int sizet
= param
->data_size
<= sizeof(size_t);
129 const int signd
= param
->data_type
== OSSL_PARAM_INTEGER
;
132 * Set the unmodified sentinel directly because there is no param array
135 param
->return_size
= OSSL_PARAM_UNMODIFIED
;
137 if ((bit32
&& !TEST_true(OSSL_PARAM_get_int32(param
, &i32
)))
138 || !TEST_true(OSSL_PARAM_get_int64(param
, &i64
)))
142 && !TEST_true(OSSL_PARAM_get_uint32(param
, (uint32_t *)&i32
)))
143 || !TEST_true(OSSL_PARAM_get_uint64(param
, (uint64_t *)&i64
))
144 || (sizet
&& !TEST_true(OSSL_PARAM_get_size_t(param
, &s
))))
147 if (!TEST_false(OSSL_PARAM_modified(param
)))
150 /* Check signed types */
152 le_copy(buf
, sizeof(i32
), &i32
, sizeof(i32
));
153 sz
= sizeof(i32
) < width
? sizeof(i32
) : width
;
154 if (!TEST_mem_eq(buf
, sz
, cmp
, sz
))
157 le_copy(buf
, sizeof(i64
), &i64
, sizeof(i64
));
158 sz
= sizeof(i64
) < width
? sizeof(i64
) : width
;
159 if (!TEST_mem_eq(buf
, sz
, cmp
, sz
))
161 if (sizet
&& !signd
) {
162 le_copy(buf
, sizeof(s
), &s
, sizeof(s
));
163 sz
= sizeof(s
) < width
? sizeof(s
) : width
;
164 if (!TEST_mem_eq(buf
, sz
, cmp
, sz
))
168 /* Check a widening write if possible */
169 if (sizeof(size_t) > width
) {
171 if (!TEST_true(OSSL_PARAM_set_int32(param
, 12345))
172 || !TEST_true(OSSL_PARAM_get_int64(param
, &i64
))
173 || !TEST_size_t_eq((size_t)i64
, 12345))
176 if (!TEST_true(OSSL_PARAM_set_uint32(param
, 12345))
177 || !TEST_true(OSSL_PARAM_get_uint64(param
, (uint64_t *)&i64
))
178 || !TEST_size_t_eq((size_t)i64
, 12345))
181 if (!TEST_true(OSSL_PARAM_modified(param
)))
188 * The test cases for each of the bastic integral types are similar.
189 * For each type, a param of that type is set and an attempt to read it
190 * get is made. Finally, the above function is called to verify that
191 * the params can be read as other types.
193 * All the real work is done via byte buffers which are converted to machine
194 * byte order and to little endian for comparisons. Narrower values are best
195 * compared using little endian because their values and positions don't
199 static int test_param_int(int n
)
202 unsigned char buf
[MAX_LEN
], cmp
[sizeof(int)];
203 const size_t len
= raw_values
[n
].len
>= sizeof(int) ?
204 sizeof(int) : raw_values
[n
].len
;
205 OSSL_PARAM param
= OSSL_PARAM_int("a", NULL
);
207 if (!TEST_int_eq(test_param_type_null(¶m
), 1))
210 memset(buf
, 0, sizeof(buf
));
211 le_copy(buf
, sizeof(in
), raw_values
[n
].value
, sizeof(in
));
212 memcpy(&in
, buf
, sizeof(in
));
214 if (!TEST_true(OSSL_PARAM_set_int(¶m
, in
)))
216 le_copy(cmp
, sizeof(out
), &out
, sizeof(out
));
217 if (!TEST_mem_eq(cmp
, len
, raw_values
[n
].value
, len
))
220 if (!TEST_true(OSSL_PARAM_get_int(¶m
, &in
)))
222 le_copy(cmp
, sizeof(in
), &in
, sizeof(in
));
223 if (!TEST_mem_eq(cmp
, sizeof(in
), raw_values
[n
].value
, sizeof(in
)))
226 return test_param_type_extra(¶m
, raw_values
[n
].value
, sizeof(int));
229 static int test_param_long(int n
)
232 unsigned char buf
[MAX_LEN
], cmp
[sizeof(long int)];
233 const size_t len
= raw_values
[n
].len
>= sizeof(long int)
234 ? sizeof(long int) : raw_values
[n
].len
;
235 OSSL_PARAM param
= OSSL_PARAM_long("a", NULL
);
237 if (!TEST_int_eq(test_param_type_null(¶m
), 1))
240 memset(buf
, 0, sizeof(buf
));
241 le_copy(buf
, sizeof(in
), raw_values
[n
].value
, sizeof(in
));
242 memcpy(&in
, buf
, sizeof(in
));
244 if (!TEST_true(OSSL_PARAM_set_long(¶m
, in
)))
246 le_copy(cmp
, sizeof(out
), &out
, sizeof(out
));
247 if (!TEST_mem_eq(cmp
, len
, raw_values
[n
].value
, len
))
250 if (!TEST_true(OSSL_PARAM_get_long(¶m
, &in
)))
252 le_copy(cmp
, sizeof(in
), &in
, sizeof(in
));
253 if (!TEST_mem_eq(cmp
, sizeof(in
), raw_values
[n
].value
, sizeof(in
)))
256 return test_param_type_extra(¶m
, raw_values
[n
].value
, sizeof(long int));
259 static int test_param_uint(int n
)
261 unsigned int in
, out
;
262 unsigned char buf
[MAX_LEN
], cmp
[sizeof(unsigned int)];
263 const size_t len
= raw_values
[n
].len
>= sizeof(unsigned int) ? sizeof(unsigned int) : raw_values
[n
].len
;
264 OSSL_PARAM param
= OSSL_PARAM_uint("a", NULL
);
266 if (!TEST_int_eq(test_param_type_null(¶m
), 1))
269 memset(buf
, 0, sizeof(buf
));
270 le_copy(buf
, sizeof(in
), raw_values
[n
].value
, sizeof(in
));
271 memcpy(&in
, buf
, sizeof(in
));
273 if (!TEST_true(OSSL_PARAM_set_uint(¶m
, in
)))
275 le_copy(cmp
, sizeof(out
), &out
, sizeof(out
));
276 if (!TEST_mem_eq(cmp
, len
, raw_values
[n
].value
, len
))
279 if (!TEST_true(OSSL_PARAM_get_uint(¶m
, &in
)))
281 le_copy(cmp
, sizeof(in
), &in
, sizeof(in
));
282 if (!TEST_mem_eq(cmp
, sizeof(in
), raw_values
[n
].value
, sizeof(in
)))
285 return test_param_type_extra(¶m
, raw_values
[n
].value
, sizeof(unsigned int));
288 static int test_param_ulong(int n
)
290 unsigned long int in
, out
;
291 unsigned char buf
[MAX_LEN
], cmp
[sizeof(unsigned long int)];
292 const size_t len
= raw_values
[n
].len
>= sizeof(unsigned long int)
293 ? sizeof(unsigned long int) : raw_values
[n
].len
;
294 OSSL_PARAM param
= OSSL_PARAM_ulong("a", NULL
);
296 if (!TEST_int_eq(test_param_type_null(¶m
), 1))
299 memset(buf
, 0, sizeof(buf
));
300 le_copy(buf
, sizeof(in
), raw_values
[n
].value
, sizeof(in
));
301 memcpy(&in
, buf
, sizeof(in
));
303 if (!TEST_true(OSSL_PARAM_set_ulong(¶m
, in
)))
305 le_copy(cmp
, sizeof(out
), &out
, sizeof(out
));
306 if (!TEST_mem_eq(cmp
, len
, raw_values
[n
].value
, len
))
309 if (!TEST_true(OSSL_PARAM_get_ulong(¶m
, &in
)))
311 le_copy(cmp
, sizeof(in
), &in
, sizeof(in
));
312 if (!TEST_mem_eq(cmp
, sizeof(in
), raw_values
[n
].value
, sizeof(in
)))
315 return test_param_type_extra(¶m
, raw_values
[n
].value
, sizeof(unsigned long int));
318 static int test_param_int32(int n
)
321 unsigned char buf
[MAX_LEN
], cmp
[sizeof(int32_t)];
322 const size_t len
= raw_values
[n
].len
>= sizeof(int32_t)
323 ? sizeof(int32_t) : raw_values
[n
].len
;
324 OSSL_PARAM param
= OSSL_PARAM_int32("a", NULL
);
326 if (!TEST_int_eq(test_param_type_null(¶m
), 1))
329 memset(buf
, 0, sizeof(buf
));
330 le_copy(buf
, sizeof(in
), raw_values
[n
].value
, sizeof(in
));
331 memcpy(&in
, buf
, sizeof(in
));
333 if (!TEST_true(OSSL_PARAM_set_int32(¶m
, in
)))
335 le_copy(cmp
, sizeof(out
), &out
, sizeof(out
));
336 if (!TEST_mem_eq(cmp
, len
, raw_values
[n
].value
, len
))
339 if (!TEST_true(OSSL_PARAM_get_int32(¶m
, &in
)))
341 le_copy(cmp
, sizeof(in
), &in
, sizeof(in
));
342 if (!TEST_mem_eq(cmp
, sizeof(in
), raw_values
[n
].value
, sizeof(in
)))
345 return test_param_type_extra(¶m
, raw_values
[n
].value
, sizeof(int32_t));
348 static int test_param_uint32(int n
)
351 unsigned char buf
[MAX_LEN
], cmp
[sizeof(uint32_t)];
352 const size_t len
= raw_values
[n
].len
>= sizeof(uint32_t)
353 ? sizeof(uint32_t) : raw_values
[n
].len
;
354 OSSL_PARAM param
= OSSL_PARAM_uint32("a", NULL
);
356 if (!TEST_int_eq(test_param_type_null(¶m
), 1))
359 memset(buf
, 0, sizeof(buf
));
360 le_copy(buf
, sizeof(in
), raw_values
[n
].value
, sizeof(in
));
361 memcpy(&in
, buf
, sizeof(in
));
363 if (!TEST_true(OSSL_PARAM_set_uint32(¶m
, in
)))
365 le_copy(cmp
, sizeof(out
), &out
, sizeof(out
));
366 if (!TEST_mem_eq(cmp
, len
, raw_values
[n
].value
, len
))
369 if (!TEST_true(OSSL_PARAM_get_uint32(¶m
, &in
)))
371 le_copy(cmp
, sizeof(in
), &in
, sizeof(in
));
372 if (!TEST_mem_eq(cmp
, sizeof(in
), raw_values
[n
].value
, sizeof(in
)))
375 return test_param_type_extra(¶m
, raw_values
[n
].value
, sizeof(uint32_t));
378 static int test_param_int64(int n
)
381 unsigned char buf
[MAX_LEN
], cmp
[sizeof(int64_t)];
382 const size_t len
= raw_values
[n
].len
>= sizeof(int64_t)
383 ? sizeof(int64_t) : raw_values
[n
].len
;
384 OSSL_PARAM param
= OSSL_PARAM_int64("a", NULL
);
386 if (!TEST_int_eq(test_param_type_null(¶m
), 1))
389 memset(buf
, 0, sizeof(buf
));
390 le_copy(buf
, sizeof(in
), raw_values
[n
].value
, sizeof(in
));
391 memcpy(&in
, buf
, sizeof(in
));
393 if (!TEST_true(OSSL_PARAM_set_int64(¶m
, in
)))
395 le_copy(cmp
, sizeof(out
), &out
, sizeof(out
));
396 if (!TEST_mem_eq(cmp
, len
, raw_values
[n
].value
, len
))
399 if (!TEST_true(OSSL_PARAM_get_int64(¶m
, &in
)))
401 le_copy(cmp
, sizeof(in
), &in
, sizeof(in
));
402 if (!TEST_mem_eq(cmp
, sizeof(in
), raw_values
[n
].value
, sizeof(in
)))
405 return test_param_type_extra(¶m
, raw_values
[n
].value
, sizeof(int64_t));
408 static int test_param_uint64(int n
)
411 unsigned char buf
[MAX_LEN
], cmp
[sizeof(uint64_t)];
412 const size_t len
= raw_values
[n
].len
>= sizeof(uint64_t)
413 ? sizeof(uint64_t) : raw_values
[n
].len
;
414 OSSL_PARAM param
= OSSL_PARAM_uint64("a", NULL
);
416 if (!TEST_int_eq(test_param_type_null(¶m
), 1))
419 memset(buf
, 0, sizeof(buf
));
420 le_copy(buf
, sizeof(in
), raw_values
[n
].value
, sizeof(in
));
421 memcpy(&in
, buf
, sizeof(in
));
423 if (!TEST_true(OSSL_PARAM_set_uint64(¶m
, in
)))
425 le_copy(cmp
, sizeof(out
), &out
, sizeof(out
));
426 if (!TEST_mem_eq(cmp
, len
, raw_values
[n
].value
, len
))
429 if (!TEST_true(OSSL_PARAM_get_uint64(¶m
, &in
)))
431 le_copy(cmp
, sizeof(in
), &in
, sizeof(in
));
432 if (!TEST_mem_eq(cmp
, sizeof(in
), raw_values
[n
].value
, sizeof(in
)))
435 return test_param_type_extra(¶m
, raw_values
[n
].value
, sizeof(uint64_t));
438 static int test_param_size_t(int n
)
441 unsigned char buf
[MAX_LEN
], cmp
[sizeof(size_t)];
442 const size_t len
= raw_values
[n
].len
>= sizeof(size_t)
443 ? sizeof(size_t) : raw_values
[n
].len
;
444 OSSL_PARAM param
= OSSL_PARAM_size_t("a", NULL
);
446 if (!TEST_int_eq(test_param_type_null(¶m
), 1))
449 memset(buf
, 0, sizeof(buf
));
450 le_copy(buf
, sizeof(in
), raw_values
[n
].value
, sizeof(in
));
451 memcpy(&in
, buf
, sizeof(in
));
453 if (!TEST_true(OSSL_PARAM_set_size_t(¶m
, in
)))
455 le_copy(cmp
, sizeof(out
), &out
, sizeof(out
));
456 if (!TEST_mem_eq(cmp
, len
, raw_values
[n
].value
, len
))
459 if (!TEST_true(OSSL_PARAM_get_size_t(¶m
, &in
)))
461 le_copy(cmp
, sizeof(in
), &in
, sizeof(in
));
462 if (!TEST_mem_eq(cmp
, sizeof(in
), raw_values
[n
].value
, sizeof(in
)))
465 return test_param_type_extra(¶m
, raw_values
[n
].value
, sizeof(size_t));
468 static int test_param_time_t(int n
)
471 unsigned char buf
[MAX_LEN
], cmp
[sizeof(time_t)];
472 const size_t len
= raw_values
[n
].len
>= sizeof(time_t)
473 ? sizeof(time_t) : raw_values
[n
].len
;
474 OSSL_PARAM param
= OSSL_PARAM_time_t("a", NULL
);
476 if (!TEST_int_eq(test_param_type_null(¶m
), 1))
479 memset(buf
, 0, sizeof(buf
));
480 le_copy(buf
, sizeof(in
), raw_values
[n
].value
, sizeof(in
));
481 memcpy(&in
, buf
, sizeof(in
));
483 if (!TEST_true(OSSL_PARAM_set_time_t(¶m
, in
)))
485 le_copy(cmp
, sizeof(out
), &out
, sizeof(out
));
486 if (!TEST_mem_eq(cmp
, len
, raw_values
[n
].value
, len
))
489 if (!TEST_true(OSSL_PARAM_get_time_t(¶m
, &in
)))
491 le_copy(cmp
, sizeof(in
), &in
, sizeof(in
));
492 if (!TEST_mem_eq(cmp
, sizeof(in
), raw_values
[n
].value
, sizeof(in
)))
495 return test_param_type_extra(¶m
, raw_values
[n
].value
, sizeof(size_t));
498 static int test_param_bignum(int n
)
500 unsigned char buf
[MAX_LEN
], bnbuf
[MAX_LEN
];
501 const size_t len
= raw_values
[n
].len
;
502 BIGNUM
*b
= NULL
, *c
= NULL
;
503 OSSL_PARAM param
= OSSL_PARAM_DEFN("bn", OSSL_PARAM_UNSIGNED_INTEGER
,
507 if (!TEST_int_eq(test_param_type_null(¶m
), 1))
511 param
.data_size
= sizeof(bnbuf
);
513 if (!TEST_ptr(b
= BN_lebin2bn(raw_values
[n
].value
, (int)len
, NULL
)))
516 if (!TEST_true(OSSL_PARAM_set_BN(¶m
, b
)))
518 le_copy(buf
, len
, bnbuf
, sizeof(bnbuf
));
519 if (!TEST_mem_eq(raw_values
[n
].value
, len
, buf
, len
))
521 param
.data_size
= param
.return_size
;
522 if (!TEST_true(OSSL_PARAM_get_BN(¶m
, &c
))
523 || !TEST_BN_eq(b
, c
))
533 static int test_param_signed_bignum(int n
)
535 unsigned char buf
[MAX_LEN
], bnbuf
[MAX_LEN
];
536 const size_t len
= raw_values
[n
].len
;
537 BIGNUM
*b
= NULL
, *c
= NULL
;
538 OSSL_PARAM param
= OSSL_PARAM_DEFN("bn", OSSL_PARAM_INTEGER
, NULL
, 0);
541 if (!TEST_int_eq(test_param_type_null(¶m
), 1))
545 param
.data_size
= sizeof(bnbuf
);
547 if (!TEST_ptr(b
= BN_signed_lebin2bn(raw_values
[n
].value
, (int)len
, NULL
)))
550 /* raw_values are little endian */
551 if (!TEST_false(!!(raw_values
[n
].value
[len
- 1] & 0x80) ^ BN_is_negative(b
)))
553 if (!TEST_true(OSSL_PARAM_set_BN(¶m
, b
)))
555 le_copy(buf
, len
, bnbuf
, sizeof(bnbuf
));
556 if (!TEST_mem_eq(raw_values
[n
].value
, len
, buf
, len
))
558 param
.data_size
= param
.return_size
;
559 if (!TEST_true(OSSL_PARAM_get_BN(¶m
, &c
))
560 || !TEST_BN_eq(b
, c
)) {
561 BN_print_fp(stderr
, c
);
572 static int test_param_real(void)
575 OSSL_PARAM param
= OSSL_PARAM_double("r", NULL
);
577 if (!TEST_int_eq(test_param_type_null(¶m
), 1))
581 return TEST_true(OSSL_PARAM_set_double(¶m
, 3.14159))
582 && TEST_double_eq(p
, 3.14159);
585 static int test_param_construct(int tstid
)
587 static const char *int_names
[] = {
588 "int", "long", "int32", "int64"
590 static const char *uint_names
[] = {
591 "uint", "ulong", "uint32", "uint64", "size_t"
593 static const unsigned char bn_val
[16] = {
594 0xac, 0x75, 0x22, 0x7d, 0x81, 0x06, 0x7a, 0x23,
595 0xa6, 0xed, 0x87, 0xc7, 0xab, 0xf4, 0x73, 0x22
597 OSSL_PARAM
*p
= NULL
, *p1
= NULL
;
598 static const OSSL_PARAM params_empty
[] = {
601 OSSL_PARAM params
[20];
602 char buf
[100], buf2
[100], *bufp
, *bufp2
;
603 unsigned char ubuf
[100];
604 void *vp
, *vp2
, *vpn
= NULL
, *vpn2
= NULL
;
606 int i
, n
= 0, ret
= 0;
609 unsigned long int ul
;
616 BIGNUM
*bn
= NULL
, *bn2
= NULL
;
618 params
[n
++] = OSSL_PARAM_construct_int("int", &i
);
619 params
[n
++] = OSSL_PARAM_construct_uint("uint", &u
);
620 params
[n
++] = OSSL_PARAM_construct_long("long", &l
);
621 params
[n
++] = OSSL_PARAM_construct_ulong("ulong", &ul
);
622 params
[n
++] = OSSL_PARAM_construct_int32("int32", &i32
);
623 params
[n
++] = OSSL_PARAM_construct_int64("int64", &i64
);
624 params
[n
++] = OSSL_PARAM_construct_uint32("uint32", &u32
);
625 params
[n
++] = OSSL_PARAM_construct_uint64("uint64", &u64
);
626 params
[n
++] = OSSL_PARAM_construct_size_t("size_t", &s
);
627 params
[n
++] = OSSL_PARAM_construct_double("double", &d
);
628 params
[n
++] = OSSL_PARAM_construct_BN("bignum", ubuf
, sizeof(ubuf
));
629 params
[n
++] = OSSL_PARAM_construct_utf8_string("utf8str", buf
, sizeof(buf
));
630 params
[n
++] = OSSL_PARAM_construct_octet_string("octstr", buf
, sizeof(buf
));
631 params
[n
++] = OSSL_PARAM_construct_octet_string("octstr2", buf
, sizeof(buf
));
632 params
[n
++] = OSSL_PARAM_construct_utf8_ptr("utf8ptr", &bufp
, 0);
633 params
[n
++] = OSSL_PARAM_construct_octet_ptr("octptr", &vp
, 0);
634 params
[n
++] = OSSL_PARAM_construct_octet_ptr("octptr2", &vp
, 0);
635 params
[n
] = OSSL_PARAM_construct_end();
642 p
= OSSL_PARAM_merge(params
, params_empty
);
645 p
= OSSL_PARAM_dup(params
);
648 p1
= OSSL_PARAM_dup(params
);
649 p
= OSSL_PARAM_merge(p1
, params_empty
);
654 if (!TEST_ptr_null(OSSL_PARAM_locate(p
, "fnord")))
657 /* All signed integral types */
658 for (j
= 0; j
< OSSL_NELEM(int_names
); j
++) {
659 if (!TEST_ptr(cp
= OSSL_PARAM_locate(p
, int_names
[j
]))
660 || !TEST_true(OSSL_PARAM_set_int32(cp
, (int32_t)(3 + j
)))
661 || !TEST_true(OSSL_PARAM_get_int64(cp
, &i64
))
662 || !TEST_size_t_eq(cp
->data_size
, cp
->return_size
)
663 || !TEST_size_t_eq((size_t)i64
, 3 + j
)) {
664 TEST_note("iteration %zu var %s", j
+ 1, int_names
[j
]);
668 /* All unsigned integral types */
669 for (j
= 0; j
< OSSL_NELEM(uint_names
); j
++) {
670 if (!TEST_ptr(cp
= OSSL_PARAM_locate(p
, uint_names
[j
]))
671 || !TEST_true(OSSL_PARAM_set_uint32(cp
, (uint32_t)(3 + j
)))
672 || !TEST_true(OSSL_PARAM_get_uint64(cp
, &u64
))
673 || !TEST_size_t_eq(cp
->data_size
, cp
->return_size
)
674 || !TEST_size_t_eq((size_t)u64
, 3 + j
)) {
675 TEST_note("iteration %zu var %s", j
+ 1, uint_names
[j
]);
680 if (!TEST_ptr(cp
= OSSL_PARAM_locate(p
, "double"))
681 || !TEST_true(OSSL_PARAM_set_double(cp
, 3.14))
682 || !TEST_true(OSSL_PARAM_get_double(cp
, &d2
))
683 || !TEST_size_t_eq(cp
->return_size
, sizeof(double))
684 || !TEST_double_eq(d2
, 3.14)
685 || (tstid
<= 1 && !TEST_double_eq(d
, d2
)))
689 if (!TEST_ptr(cp
= OSSL_PARAM_locate(p
, "utf8str"))
690 || !TEST_true(OSSL_PARAM_set_utf8_string(cp
, "abcdef"))
691 || !TEST_size_t_eq(cp
->return_size
, sizeof("abcdef") - 1)
692 || !TEST_true(OSSL_PARAM_get_utf8_string(cp
, &bufp
, 0))
693 || !TEST_str_eq(bufp
, "abcdef")) {
699 if (!TEST_true(OSSL_PARAM_get_utf8_string(cp
, &bufp
, sizeof(buf2
)))
700 || !TEST_str_eq(buf2
, "abcdef"))
703 /* Note that the size of a UTF8 string does *NOT* include the NUL byte */
705 if (!TEST_ptr(cp
= OSSL_PARAM_locate(p
, "utf8ptr"))
706 || !TEST_true(OSSL_PARAM_set_utf8_ptr(cp
, "tuvwxyz"))
707 || !TEST_size_t_eq(cp
->return_size
, sizeof("tuvwxyz") - 1)
708 || !TEST_true(OSSL_PARAM_get_utf8_ptr(cp
, (const char **)&bufp2
))
709 || !TEST_str_eq(bufp2
, "tuvwxyz")
710 || (tstid
<= 1 && !TEST_ptr_eq(bufp2
, bufp
)))
713 if (!TEST_ptr(cp
= OSSL_PARAM_locate(p
, "octstr"))
714 || !TEST_true(OSSL_PARAM_set_octet_string(cp
, "abcdefghi",
715 sizeof("abcdefghi")))
716 || !TEST_size_t_eq(cp
->return_size
, sizeof("abcdefghi")))
718 /* Match the return size to avoid trailing garbage bytes */
719 cp
->data_size
= cp
->return_size
;
720 if (!TEST_true(OSSL_PARAM_get_octet_string(cp
, &vpn
, 0, &s
))
721 || !TEST_size_t_eq(s
, sizeof("abcdefghi"))
722 || !TEST_mem_eq(vpn
, sizeof("abcdefghi"),
723 "abcdefghi", sizeof("abcdefghi")))
726 if (!TEST_true(OSSL_PARAM_get_octet_string(cp
, &vp
, sizeof(buf2
), &s
))
727 || !TEST_size_t_eq(s
, sizeof("abcdefghi"))
728 || !TEST_mem_eq(vp
, sizeof("abcdefghi"),
729 "abcdefghi", sizeof("abcdefghi")))
731 /* OCTET string using OSSL_PARAM_set_octet_string_or_ptr */
732 if (!TEST_ptr(cp
= OSSL_PARAM_locate(p
, "octstr2"))
733 || !TEST_true(OSSL_PARAM_set_octet_string_or_ptr(cp
, "jklmnopqr",
734 sizeof("jklmnopqr")))
735 || !TEST_size_t_eq(cp
->return_size
, sizeof("jklmnopqr")))
737 /* Match the return size to avoid trailing garbage bytes */
738 cp
->data_size
= cp
->return_size
;
739 if (!TEST_true(OSSL_PARAM_get_octet_string(cp
, &vpn2
, 0, &s
))
740 || !TEST_size_t_eq(s
, sizeof("jklmnopqr"))
741 || !TEST_mem_eq(vpn2
, sizeof("jklmnopqr"),
742 "jklmnopqr", sizeof("jklmnopqr")))
745 if (!TEST_true(OSSL_PARAM_get_octet_string(cp
, &vp
, sizeof(buf2
), &s
))
746 || !TEST_size_t_eq(s
, sizeof("jklmnopqr"))
747 || !TEST_mem_eq(vp
, sizeof("jklmnopqr"),
748 "jklmnopqr", sizeof("jklmnopqr")))
752 if (!TEST_ptr(cp
= OSSL_PARAM_locate(p
, "octptr"))
753 || !TEST_true(OSSL_PARAM_set_octet_ptr(cp
, &ul
, sizeof(ul
)))
754 || !TEST_size_t_eq(cp
->return_size
, sizeof(ul
))
755 || (tstid
<= 1 && !TEST_ptr_eq(vp
, &ul
)))
757 /* Match the return size to avoid trailing garbage bytes */
758 cp
->data_size
= cp
->return_size
;
759 if (!TEST_true(OSSL_PARAM_get_octet_ptr(cp
, (const void **)&vp2
, &k
))
760 || !TEST_size_t_eq(k
, sizeof(ul
))
761 || (tstid
<= 1 && !TEST_ptr_eq(vp2
, vp
)))
763 /* OCTET pointer using OSSL_PARAM_set_octet_string_or_ptr */
767 if (!TEST_ptr(cp
= OSSL_PARAM_locate(p
, "octptr2"))
768 || !TEST_true(OSSL_PARAM_set_octet_string_or_ptr(cp
, &ul
, sizeof(ul
)))
769 || !TEST_size_t_eq(cp
->return_size
, sizeof(ul
))
770 || (tstid
<= 1 && !TEST_ptr_eq(vp
, &ul
)))
772 /* Match the return size to avoid trailing garbage bytes */
773 cp
->data_size
= cp
->return_size
;
774 if (!TEST_true(OSSL_PARAM_get_octet_ptr(cp
, (const void **)&vp2
, &k
))
775 || !TEST_size_t_eq(k
, sizeof(ul
))
776 || (tstid
<= 1 && !TEST_ptr_eq(vp2
, vp
)))
779 if (!TEST_ptr(cp
= OSSL_PARAM_locate(p
, "bignum"))
780 || !TEST_ptr(bn
= BN_lebin2bn(bn_val
, (int)sizeof(bn_val
), NULL
))
781 || !TEST_true(OSSL_PARAM_set_BN(cp
, bn
))
782 || !TEST_size_t_eq(cp
->data_size
, cp
->return_size
))
784 /* Match the return size to avoid trailing garbage bytes */
785 cp
->data_size
= cp
->return_size
;
786 if (!TEST_true(OSSL_PARAM_get_BN(cp
, &bn2
))
787 || !TEST_BN_eq(bn
, bn2
))
801 static int test_param_modified(void)
803 OSSL_PARAM param
[3] = { OSSL_PARAM_int("a", NULL
),
804 OSSL_PARAM_int("b", NULL
),
810 if (!TEST_false(OSSL_PARAM_modified(param
))
811 && !TEST_true(OSSL_PARAM_set_int32(param
, 1234))
812 && !TEST_true(OSSL_PARAM_modified(param
))
813 && !TEST_false(OSSL_PARAM_modified(param
+ 1))
814 && !TEST_true(OSSL_PARAM_set_int32(param
+ 1, 1))
815 && !TEST_true(OSSL_PARAM_modified(param
+ 1)))
817 OSSL_PARAM_set_all_unmodified(param
);
818 if (!TEST_false(OSSL_PARAM_modified(param
))
819 && !TEST_true(OSSL_PARAM_set_int32(param
, 4321))
820 && !TEST_true(OSSL_PARAM_modified(param
))
821 && !TEST_false(OSSL_PARAM_modified(param
+ 1))
822 && !TEST_true(OSSL_PARAM_set_int32(param
+ 1, 2))
823 && !TEST_true(OSSL_PARAM_modified(param
+ 1)))
828 static int test_param_copy_null(void)
831 int a
= 1, b
= 2, i
= 0;
832 OSSL_PARAM
*cp1
= NULL
, *cp2
= NULL
, *p
;
835 param
[i
++] = OSSL_PARAM_construct_int("a", &a
);
836 param
[i
++] = OSSL_PARAM_construct_int("b", &b
);
837 param
[i
] = OSSL_PARAM_construct_end();
839 ret
= TEST_ptr_null(OSSL_PARAM_dup(NULL
))
840 && TEST_ptr(cp1
= OSSL_PARAM_merge(NULL
, param
))
841 && TEST_ptr(p
= OSSL_PARAM_locate(cp1
, "a"))
842 && TEST_true(OSSL_PARAM_get_int(p
, &val
))
843 && TEST_int_eq(val
, 1)
844 && TEST_ptr(p
= OSSL_PARAM_locate(cp1
, "b"))
845 && TEST_true(OSSL_PARAM_get_int(p
, &val
))
846 && TEST_int_eq(val
, 2)
847 && TEST_ptr(cp2
= OSSL_PARAM_merge(param
, NULL
))
848 && TEST_ptr(p
= OSSL_PARAM_locate(cp2
, "a"))
849 && TEST_true(OSSL_PARAM_get_int(p
, &val
))
850 && TEST_int_eq(val
, 1)
851 && TEST_ptr(p
= OSSL_PARAM_locate(cp2
, "b"))
852 && TEST_true(OSSL_PARAM_get_int(p
, &val
))
853 && TEST_int_eq(val
, 2)
854 && TEST_ptr_null(OSSL_PARAM_merge(NULL
, NULL
));
855 OSSL_PARAM_free(cp2
);
856 OSSL_PARAM_free(cp1
);
859 static int test_param_merge(void)
862 int values
[] = {1, 2, 3, 4};
863 OSSL_PARAM
*p
= NULL
, *cp
= NULL
;
864 OSSL_PARAM param
[3], param1
[3];
866 param
[0] = OSSL_PARAM_construct_int("diff1", &values
[0]);
867 param
[1] = OSSL_PARAM_construct_int("same", &values
[1]);
868 param
[2] = OSSL_PARAM_construct_end();
869 param1
[0] = OSSL_PARAM_construct_int("diff2", &values
[2]);
870 param1
[1] = OSSL_PARAM_construct_int("same", &values
[3]);
871 param1
[2] = OSSL_PARAM_construct_end();
873 ret
= TEST_ptr(p
= OSSL_PARAM_merge(param
, param1
))
874 && TEST_ptr(cp
= OSSL_PARAM_locate(p
, "diff1"))
875 && TEST_true(OSSL_PARAM_get_int(p
, &val
))
876 && TEST_int_eq(val
, values
[0])
877 && TEST_ptr(cp
= OSSL_PARAM_locate(p
, "diff2"))
878 && TEST_true(OSSL_PARAM_get_int(cp
, &val
))
879 && TEST_int_eq(val
, values
[2])
880 && TEST_ptr(cp
= OSSL_PARAM_locate(p
, "same"))
881 && TEST_true(OSSL_PARAM_get_int(cp
, &val
))
882 && TEST_int_eq(val
, values
[3]);
887 int setup_tests(void)
889 ADD_ALL_TESTS(test_param_int
, OSSL_NELEM(raw_values
));
890 ADD_ALL_TESTS(test_param_long
, OSSL_NELEM(raw_values
));
891 ADD_ALL_TESTS(test_param_uint
, OSSL_NELEM(raw_values
));
892 ADD_ALL_TESTS(test_param_ulong
, OSSL_NELEM(raw_values
));
893 ADD_ALL_TESTS(test_param_int32
, OSSL_NELEM(raw_values
));
894 ADD_ALL_TESTS(test_param_uint32
, OSSL_NELEM(raw_values
));
895 ADD_ALL_TESTS(test_param_size_t
, OSSL_NELEM(raw_values
));
896 ADD_ALL_TESTS(test_param_time_t
, OSSL_NELEM(raw_values
));
897 ADD_ALL_TESTS(test_param_int64
, OSSL_NELEM(raw_values
));
898 ADD_ALL_TESTS(test_param_uint64
, OSSL_NELEM(raw_values
));
899 ADD_ALL_TESTS(test_param_bignum
, OSSL_NELEM(raw_values
));
900 ADD_ALL_TESTS(test_param_signed_bignum
, OSSL_NELEM(raw_values
));
901 ADD_TEST(test_param_real
);
902 ADD_ALL_TESTS(test_param_construct
, 4);
903 ADD_TEST(test_param_modified
);
904 ADD_TEST(test_param_copy_null
);
905 ADD_TEST(test_param_merge
);