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_extra(OSSL_PARAM
*param
, const unsigned char *cmp
,
79 unsigned char buf
[MAX_LEN
];
80 const int bit32
= param
->data_size
<= sizeof(int32_t);
81 const int sizet
= param
->data_size
<= sizeof(size_t);
82 const int signd
= param
->data_type
== OSSL_PARAM_INTEGER
;
85 * Set the unmodified sentinel directly because there is no param array
88 param
->return_size
= OSSL_PARAM_UNMODIFIED
;
90 if ((bit32
&& !TEST_true(OSSL_PARAM_get_int32(param
, &i32
)))
91 || !TEST_true(OSSL_PARAM_get_int64(param
, &i64
)))
95 && !TEST_true(OSSL_PARAM_get_uint32(param
, (uint32_t *)&i32
)))
96 || !TEST_true(OSSL_PARAM_get_uint64(param
, (uint64_t *)&i64
))
97 || (sizet
&& !TEST_true(OSSL_PARAM_get_size_t(param
, &s
))))
100 if (!TEST_false(OSSL_PARAM_modified(param
)))
103 /* Check signed types */
105 le_copy(buf
, sizeof(i32
), &i32
, sizeof(i32
));
106 sz
= sizeof(i32
) < width
? sizeof(i32
) : width
;
107 if (!TEST_mem_eq(buf
, sz
, cmp
, sz
))
110 le_copy(buf
, sizeof(i64
), &i64
, sizeof(i64
));
111 sz
= sizeof(i64
) < width
? sizeof(i64
) : width
;
112 if (!TEST_mem_eq(buf
, sz
, cmp
, sz
))
114 if (sizet
&& !signd
) {
115 le_copy(buf
, sizeof(s
), &s
, sizeof(s
));
116 sz
= sizeof(s
) < width
? sizeof(s
) : width
;
117 if (!TEST_mem_eq(buf
, sz
, cmp
, sz
))
121 /* Check a widening write if possible */
122 if (sizeof(size_t) > width
) {
124 if (!TEST_true(OSSL_PARAM_set_int32(param
, 12345))
125 || !TEST_true(OSSL_PARAM_get_int64(param
, &i64
))
126 || !TEST_size_t_eq((size_t)i64
, 12345))
129 if (!TEST_true(OSSL_PARAM_set_uint32(param
, 12345))
130 || !TEST_true(OSSL_PARAM_get_uint64(param
, (uint64_t *)&i64
))
131 || !TEST_size_t_eq((size_t)i64
, 12345))
134 if (!TEST_true(OSSL_PARAM_modified(param
)))
141 * The test cases for each of the bastic integral types are similar.
142 * For each type, a param of that type is set and an attempt to read it
143 * get is made. Finally, the above function is called to verify that
144 * the params can be read as other types.
146 * All the real work is done via byte buffers which are converted to machine
147 * byte order and to little endian for comparisons. Narrower values are best
148 * compared using little endian because their values and positions don't
152 static int test_param_int(int n
)
155 unsigned char buf
[MAX_LEN
], cmp
[sizeof(int)];
156 const size_t len
= raw_values
[n
].len
>= sizeof(int) ?
157 sizeof(int) : raw_values
[n
].len
;
158 OSSL_PARAM param
= OSSL_PARAM_int("a", NULL
);
160 memset(buf
, 0, sizeof(buf
));
161 le_copy(buf
, sizeof(in
), raw_values
[n
].value
, sizeof(in
));
162 memcpy(&in
, buf
, sizeof(in
));
164 if (!TEST_true(OSSL_PARAM_set_int(¶m
, in
)))
166 le_copy(cmp
, sizeof(out
), &out
, sizeof(out
));
167 if (!TEST_mem_eq(cmp
, len
, raw_values
[n
].value
, len
))
170 if (!TEST_true(OSSL_PARAM_get_int(¶m
, &in
)))
172 le_copy(cmp
, sizeof(in
), &in
, sizeof(in
));
173 if (!TEST_mem_eq(cmp
, sizeof(in
), raw_values
[n
].value
, sizeof(in
)))
176 return test_param_type_extra(¶m
, raw_values
[n
].value
, sizeof(int));
179 static int test_param_long(int n
)
182 unsigned char buf
[MAX_LEN
], cmp
[sizeof(long int)];
183 const size_t len
= raw_values
[n
].len
>= sizeof(long int)
184 ? sizeof(long int) : raw_values
[n
].len
;
185 OSSL_PARAM param
= OSSL_PARAM_long("a", NULL
);
187 memset(buf
, 0, sizeof(buf
));
188 le_copy(buf
, sizeof(in
), raw_values
[n
].value
, sizeof(in
));
189 memcpy(&in
, buf
, sizeof(in
));
191 if (!TEST_true(OSSL_PARAM_set_long(¶m
, in
)))
193 le_copy(cmp
, sizeof(out
), &out
, sizeof(out
));
194 if (!TEST_mem_eq(cmp
, len
, raw_values
[n
].value
, len
))
197 if (!TEST_true(OSSL_PARAM_get_long(¶m
, &in
)))
199 le_copy(cmp
, sizeof(in
), &in
, sizeof(in
));
200 if (!TEST_mem_eq(cmp
, sizeof(in
), raw_values
[n
].value
, sizeof(in
)))
203 return test_param_type_extra(¶m
, raw_values
[n
].value
, sizeof(long int));
206 static int test_param_uint(int n
)
208 unsigned int in
, out
;
209 unsigned char buf
[MAX_LEN
], cmp
[sizeof(unsigned int)];
210 const size_t len
= raw_values
[n
].len
>= sizeof(unsigned int) ? sizeof(unsigned int) : raw_values
[n
].len
;
211 OSSL_PARAM param
= OSSL_PARAM_uint("a", NULL
);
213 memset(buf
, 0, sizeof(buf
));
214 le_copy(buf
, sizeof(in
), raw_values
[n
].value
, sizeof(in
));
215 memcpy(&in
, buf
, sizeof(in
));
217 if (!TEST_true(OSSL_PARAM_set_uint(¶m
, in
)))
219 le_copy(cmp
, sizeof(out
), &out
, sizeof(out
));
220 if (!TEST_mem_eq(cmp
, len
, raw_values
[n
].value
, len
))
223 if (!TEST_true(OSSL_PARAM_get_uint(¶m
, &in
)))
225 le_copy(cmp
, sizeof(in
), &in
, sizeof(in
));
226 if (!TEST_mem_eq(cmp
, sizeof(in
), raw_values
[n
].value
, sizeof(in
)))
229 return test_param_type_extra(¶m
, raw_values
[n
].value
, sizeof(unsigned int));
232 static int test_param_ulong(int n
)
234 unsigned long int in
, out
;
235 unsigned char buf
[MAX_LEN
], cmp
[sizeof(unsigned long int)];
236 const size_t len
= raw_values
[n
].len
>= sizeof(unsigned long int)
237 ? sizeof(unsigned long int) : raw_values
[n
].len
;
238 OSSL_PARAM param
= OSSL_PARAM_ulong("a", NULL
);
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_ulong(¶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_ulong(¶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(unsigned long int));
259 static int test_param_int32(int n
)
262 unsigned char buf
[MAX_LEN
], cmp
[sizeof(int32_t)];
263 const size_t len
= raw_values
[n
].len
>= sizeof(int32_t)
264 ? sizeof(int32_t) : raw_values
[n
].len
;
265 OSSL_PARAM param
= OSSL_PARAM_int32("a", NULL
);
267 memset(buf
, 0, sizeof(buf
));
268 le_copy(buf
, sizeof(in
), raw_values
[n
].value
, sizeof(in
));
269 memcpy(&in
, buf
, sizeof(in
));
271 if (!TEST_true(OSSL_PARAM_set_int32(¶m
, in
)))
273 le_copy(cmp
, sizeof(out
), &out
, sizeof(out
));
274 if (!TEST_mem_eq(cmp
, len
, raw_values
[n
].value
, len
))
277 if (!TEST_true(OSSL_PARAM_get_int32(¶m
, &in
)))
279 le_copy(cmp
, sizeof(in
), &in
, sizeof(in
));
280 if (!TEST_mem_eq(cmp
, sizeof(in
), raw_values
[n
].value
, sizeof(in
)))
283 return test_param_type_extra(¶m
, raw_values
[n
].value
, sizeof(int32_t));
286 static int test_param_uint32(int n
)
289 unsigned char buf
[MAX_LEN
], cmp
[sizeof(uint32_t)];
290 const size_t len
= raw_values
[n
].len
>= sizeof(uint32_t)
291 ? sizeof(uint32_t) : raw_values
[n
].len
;
292 OSSL_PARAM param
= OSSL_PARAM_uint32("a", NULL
);
294 memset(buf
, 0, sizeof(buf
));
295 le_copy(buf
, sizeof(in
), raw_values
[n
].value
, sizeof(in
));
296 memcpy(&in
, buf
, sizeof(in
));
298 if (!TEST_true(OSSL_PARAM_set_uint32(¶m
, in
)))
300 le_copy(cmp
, sizeof(out
), &out
, sizeof(out
));
301 if (!TEST_mem_eq(cmp
, len
, raw_values
[n
].value
, len
))
304 if (!TEST_true(OSSL_PARAM_get_uint32(¶m
, &in
)))
306 le_copy(cmp
, sizeof(in
), &in
, sizeof(in
));
307 if (!TEST_mem_eq(cmp
, sizeof(in
), raw_values
[n
].value
, sizeof(in
)))
310 return test_param_type_extra(¶m
, raw_values
[n
].value
, sizeof(uint32_t));
313 static int test_param_int64(int n
)
316 unsigned char buf
[MAX_LEN
], cmp
[sizeof(int64_t)];
317 const size_t len
= raw_values
[n
].len
>= sizeof(int64_t)
318 ? sizeof(int64_t) : raw_values
[n
].len
;
319 OSSL_PARAM param
= OSSL_PARAM_int64("a", NULL
);
321 memset(buf
, 0, sizeof(buf
));
322 le_copy(buf
, sizeof(in
), raw_values
[n
].value
, sizeof(in
));
323 memcpy(&in
, buf
, sizeof(in
));
325 if (!TEST_true(OSSL_PARAM_set_int64(¶m
, in
)))
327 le_copy(cmp
, sizeof(out
), &out
, sizeof(out
));
328 if (!TEST_mem_eq(cmp
, len
, raw_values
[n
].value
, len
))
331 if (!TEST_true(OSSL_PARAM_get_int64(¶m
, &in
)))
333 le_copy(cmp
, sizeof(in
), &in
, sizeof(in
));
334 if (!TEST_mem_eq(cmp
, sizeof(in
), raw_values
[n
].value
, sizeof(in
)))
337 return test_param_type_extra(¶m
, raw_values
[n
].value
, sizeof(int64_t));
340 static int test_param_uint64(int n
)
343 unsigned char buf
[MAX_LEN
], cmp
[sizeof(uint64_t)];
344 const size_t len
= raw_values
[n
].len
>= sizeof(uint64_t)
345 ? sizeof(uint64_t) : raw_values
[n
].len
;
346 OSSL_PARAM param
= OSSL_PARAM_uint64("a", NULL
);
348 memset(buf
, 0, sizeof(buf
));
349 le_copy(buf
, sizeof(in
), raw_values
[n
].value
, sizeof(in
));
350 memcpy(&in
, buf
, sizeof(in
));
352 if (!TEST_true(OSSL_PARAM_set_uint64(¶m
, in
)))
354 le_copy(cmp
, sizeof(out
), &out
, sizeof(out
));
355 if (!TEST_mem_eq(cmp
, len
, raw_values
[n
].value
, len
))
358 if (!TEST_true(OSSL_PARAM_get_uint64(¶m
, &in
)))
360 le_copy(cmp
, sizeof(in
), &in
, sizeof(in
));
361 if (!TEST_mem_eq(cmp
, sizeof(in
), raw_values
[n
].value
, sizeof(in
)))
364 return test_param_type_extra(¶m
, raw_values
[n
].value
, sizeof(uint64_t));
367 static int test_param_size_t(int n
)
370 unsigned char buf
[MAX_LEN
], cmp
[sizeof(size_t)];
371 const size_t len
= raw_values
[n
].len
>= sizeof(size_t)
372 ? sizeof(size_t) : raw_values
[n
].len
;
373 OSSL_PARAM param
= OSSL_PARAM_size_t("a", NULL
);
375 memset(buf
, 0, sizeof(buf
));
376 le_copy(buf
, sizeof(in
), raw_values
[n
].value
, sizeof(in
));
377 memcpy(&in
, buf
, sizeof(in
));
379 if (!TEST_true(OSSL_PARAM_set_size_t(¶m
, in
)))
381 le_copy(cmp
, sizeof(out
), &out
, sizeof(out
));
382 if (!TEST_mem_eq(cmp
, len
, raw_values
[n
].value
, len
))
385 if (!TEST_true(OSSL_PARAM_get_size_t(¶m
, &in
)))
387 le_copy(cmp
, sizeof(in
), &in
, sizeof(in
));
388 if (!TEST_mem_eq(cmp
, sizeof(in
), raw_values
[n
].value
, sizeof(in
)))
391 return test_param_type_extra(¶m
, raw_values
[n
].value
, sizeof(size_t));
394 static int test_param_time_t(int n
)
397 unsigned char buf
[MAX_LEN
], cmp
[sizeof(time_t)];
398 const size_t len
= raw_values
[n
].len
>= sizeof(time_t)
399 ? sizeof(time_t) : raw_values
[n
].len
;
400 OSSL_PARAM param
= OSSL_PARAM_time_t("a", NULL
);
402 memset(buf
, 0, sizeof(buf
));
403 le_copy(buf
, sizeof(in
), raw_values
[n
].value
, sizeof(in
));
404 memcpy(&in
, buf
, sizeof(in
));
406 if (!TEST_true(OSSL_PARAM_set_time_t(¶m
, in
)))
408 le_copy(cmp
, sizeof(out
), &out
, sizeof(out
));
409 if (!TEST_mem_eq(cmp
, len
, raw_values
[n
].value
, len
))
412 if (!TEST_true(OSSL_PARAM_get_time_t(¶m
, &in
)))
414 le_copy(cmp
, sizeof(in
), &in
, sizeof(in
));
415 if (!TEST_mem_eq(cmp
, sizeof(in
), raw_values
[n
].value
, sizeof(in
)))
418 return test_param_type_extra(¶m
, raw_values
[n
].value
, sizeof(size_t));
421 static int test_param_bignum(int n
)
423 unsigned char buf
[MAX_LEN
], bnbuf
[MAX_LEN
];
424 const size_t len
= raw_values
[n
].len
;
425 BIGNUM
*b
= NULL
, *c
= NULL
;
426 OSSL_PARAM param
= OSSL_PARAM_DEFN("bn", OSSL_PARAM_UNSIGNED_INTEGER
,
431 param
.data_size
= sizeof(bnbuf
);
433 if (!TEST_ptr(b
= BN_lebin2bn(raw_values
[n
].value
, (int)len
, NULL
)))
436 if (!TEST_true(OSSL_PARAM_set_BN(¶m
, b
)))
438 le_copy(buf
, len
, bnbuf
, sizeof(bnbuf
));
439 if (!TEST_mem_eq(raw_values
[n
].value
, len
, buf
, len
))
441 param
.data_size
= param
.return_size
;
442 if (!TEST_true(OSSL_PARAM_get_BN(¶m
, &c
))
443 || !TEST_BN_eq(b
, c
))
453 static int test_param_signed_bignum(int n
)
455 unsigned char buf
[MAX_LEN
], bnbuf
[MAX_LEN
];
456 const size_t len
= raw_values
[n
].len
;
457 BIGNUM
*b
= NULL
, *c
= NULL
;
458 OSSL_PARAM param
= OSSL_PARAM_DEFN("bn", OSSL_PARAM_INTEGER
, NULL
, 0);
462 param
.data_size
= sizeof(bnbuf
);
464 if (!TEST_ptr(b
= BN_signed_lebin2bn(raw_values
[n
].value
, (int)len
, NULL
)))
467 /* raw_values are little endian */
468 if (!TEST_false(!!(raw_values
[n
].value
[len
- 1] & 0x80) ^ BN_is_negative(b
)))
470 if (!TEST_true(OSSL_PARAM_set_BN(¶m
, b
)))
472 le_copy(buf
, len
, bnbuf
, sizeof(bnbuf
));
473 if (!TEST_mem_eq(raw_values
[n
].value
, len
, buf
, len
))
475 param
.data_size
= param
.return_size
;
476 if (!TEST_true(OSSL_PARAM_get_BN(¶m
, &c
))
477 || !TEST_BN_eq(b
, c
)) {
478 BN_print_fp(stderr
, c
);
489 static int test_param_real(void)
492 OSSL_PARAM param
= OSSL_PARAM_double("r", NULL
);
495 return TEST_true(OSSL_PARAM_set_double(¶m
, 3.14159))
496 && TEST_double_eq(p
, 3.14159);
499 static int test_param_construct(int tstid
)
501 static const char *int_names
[] = {
502 "int", "long", "int32", "int64"
504 static const char *uint_names
[] = {
505 "uint", "ulong", "uint32", "uint64", "size_t"
507 static const unsigned char bn_val
[16] = {
508 0xac, 0x75, 0x22, 0x7d, 0x81, 0x06, 0x7a, 0x23,
509 0xa6, 0xed, 0x87, 0xc7, 0xab, 0xf4, 0x73, 0x22
511 OSSL_PARAM
*p
= NULL
, *p1
= NULL
;
512 static const OSSL_PARAM params_empty
[] = {
515 OSSL_PARAM params
[20];
516 char buf
[100], buf2
[100], *bufp
, *bufp2
;
517 unsigned char ubuf
[100];
518 void *vp
, *vpn
= NULL
, *vp2
;
520 int i
, n
= 0, ret
= 0;
523 unsigned long int ul
;
530 BIGNUM
*bn
= NULL
, *bn2
= NULL
;
532 params
[n
++] = OSSL_PARAM_construct_int("int", &i
);
533 params
[n
++] = OSSL_PARAM_construct_uint("uint", &u
);
534 params
[n
++] = OSSL_PARAM_construct_long("long", &l
);
535 params
[n
++] = OSSL_PARAM_construct_ulong("ulong", &ul
);
536 params
[n
++] = OSSL_PARAM_construct_int32("int32", &i32
);
537 params
[n
++] = OSSL_PARAM_construct_int64("int64", &i64
);
538 params
[n
++] = OSSL_PARAM_construct_uint32("uint32", &u32
);
539 params
[n
++] = OSSL_PARAM_construct_uint64("uint64", &u64
);
540 params
[n
++] = OSSL_PARAM_construct_size_t("size_t", &s
);
541 params
[n
++] = OSSL_PARAM_construct_double("double", &d
);
542 params
[n
++] = OSSL_PARAM_construct_BN("bignum", ubuf
, sizeof(ubuf
));
543 params
[n
++] = OSSL_PARAM_construct_utf8_string("utf8str", buf
, sizeof(buf
));
544 params
[n
++] = OSSL_PARAM_construct_octet_string("octstr", buf
, sizeof(buf
));
545 params
[n
++] = OSSL_PARAM_construct_utf8_ptr("utf8ptr", &bufp
, 0);
546 params
[n
++] = OSSL_PARAM_construct_octet_ptr("octptr", &vp
, 0);
547 params
[n
] = OSSL_PARAM_construct_end();
554 p
= OSSL_PARAM_merge(params
, params_empty
);
557 p
= OSSL_PARAM_dup(params
);
560 p1
= OSSL_PARAM_dup(params
);
561 p
= OSSL_PARAM_merge(p1
, params_empty
);
566 if (!TEST_ptr_null(OSSL_PARAM_locate(p
, "fnord")))
569 /* All signed integral types */
570 for (j
= 0; j
< OSSL_NELEM(int_names
); j
++) {
571 if (!TEST_ptr(cp
= OSSL_PARAM_locate(p
, int_names
[j
]))
572 || !TEST_true(OSSL_PARAM_set_int32(cp
, (int32_t)(3 + j
)))
573 || !TEST_true(OSSL_PARAM_get_int64(cp
, &i64
))
574 || !TEST_size_t_eq(cp
->data_size
, cp
->return_size
)
575 || !TEST_size_t_eq((size_t)i64
, 3 + j
)) {
576 TEST_note("iteration %zu var %s", j
+ 1, int_names
[j
]);
580 /* All unsigned integral types */
581 for (j
= 0; j
< OSSL_NELEM(uint_names
); j
++) {
582 if (!TEST_ptr(cp
= OSSL_PARAM_locate(p
, uint_names
[j
]))
583 || !TEST_true(OSSL_PARAM_set_uint32(cp
, (uint32_t)(3 + j
)))
584 || !TEST_true(OSSL_PARAM_get_uint64(cp
, &u64
))
585 || !TEST_size_t_eq(cp
->data_size
, cp
->return_size
)
586 || !TEST_size_t_eq((size_t)u64
, 3 + j
)) {
587 TEST_note("iteration %zu var %s", j
+ 1, uint_names
[j
]);
592 if (!TEST_ptr(cp
= OSSL_PARAM_locate(p
, "double"))
593 || !TEST_true(OSSL_PARAM_set_double(cp
, 3.14))
594 || !TEST_true(OSSL_PARAM_get_double(cp
, &d2
))
595 || !TEST_size_t_eq(cp
->return_size
, sizeof(double))
596 || !TEST_double_eq(d2
, 3.14)
597 || (tstid
<= 1 && !TEST_double_eq(d
, d2
)))
601 if (!TEST_ptr(cp
= OSSL_PARAM_locate(p
, "utf8str"))
602 || !TEST_true(OSSL_PARAM_set_utf8_string(cp
, "abcdef"))
603 || !TEST_size_t_eq(cp
->return_size
, sizeof("abcdef") - 1)
604 || !TEST_true(OSSL_PARAM_get_utf8_string(cp
, &bufp
, 0))
605 || !TEST_str_eq(bufp
, "abcdef")) {
611 if (!TEST_true(OSSL_PARAM_get_utf8_string(cp
, &bufp
, sizeof(buf2
)))
612 || !TEST_str_eq(buf2
, "abcdef"))
615 /* Note that the size of a UTF8 string does *NOT* include the NUL byte */
617 if (!TEST_ptr(cp
= OSSL_PARAM_locate(p
, "utf8ptr"))
618 || !TEST_true(OSSL_PARAM_set_utf8_ptr(cp
, "tuvwxyz"))
619 || !TEST_size_t_eq(cp
->return_size
, sizeof("tuvwxyz") - 1)
620 || !TEST_true(OSSL_PARAM_get_utf8_ptr(cp
, (const char **)&bufp2
))
621 || !TEST_str_eq(bufp2
, "tuvwxyz")
622 || (tstid
<= 1 && !TEST_ptr_eq(bufp2
, bufp
)))
625 if (!TEST_ptr(cp
= OSSL_PARAM_locate(p
, "octstr"))
626 || !TEST_true(OSSL_PARAM_set_octet_string(cp
, "abcdefghi",
627 sizeof("abcdefghi")))
628 || !TEST_size_t_eq(cp
->return_size
, sizeof("abcdefghi")))
630 /* Match the return size to avoid trailing garbage bytes */
631 cp
->data_size
= cp
->return_size
;
632 if (!TEST_true(OSSL_PARAM_get_octet_string(cp
, &vpn
, 0, &s
))
633 || !TEST_size_t_eq(s
, sizeof("abcdefghi"))
634 || !TEST_mem_eq(vpn
, sizeof("abcdefghi"),
635 "abcdefghi", sizeof("abcdefghi")))
638 if (!TEST_true(OSSL_PARAM_get_octet_string(cp
, &vp
, sizeof(buf2
), &s
))
639 || !TEST_size_t_eq(s
, sizeof("abcdefghi"))
640 || !TEST_mem_eq(vp
, sizeof("abcdefghi"),
641 "abcdefghi", sizeof("abcdefghi")))
645 if (!TEST_ptr(cp
= OSSL_PARAM_locate(p
, "octptr"))
646 || !TEST_true(OSSL_PARAM_set_octet_ptr(cp
, &ul
, sizeof(ul
)))
647 || !TEST_size_t_eq(cp
->return_size
, sizeof(ul
))
648 || (tstid
<= 1 && !TEST_ptr_eq(vp
, &ul
)))
650 /* Match the return size to avoid trailing garbage bytes */
651 cp
->data_size
= cp
->return_size
;
652 if (!TEST_true(OSSL_PARAM_get_octet_ptr(cp
, (const void **)&vp2
, &k
))
653 || !TEST_size_t_eq(k
, sizeof(ul
))
654 || (tstid
<= 1 && !TEST_ptr_eq(vp2
, vp
)))
657 if (!TEST_ptr(cp
= OSSL_PARAM_locate(p
, "bignum"))
658 || !TEST_ptr(bn
= BN_lebin2bn(bn_val
, (int)sizeof(bn_val
), NULL
))
659 || !TEST_true(OSSL_PARAM_set_BN(cp
, bn
))
660 || !TEST_size_t_eq(cp
->data_size
, cp
->return_size
))
662 /* Match the return size to avoid trailing garbage bytes */
663 cp
->data_size
= cp
->return_size
;
664 if (!TEST_true(OSSL_PARAM_get_BN(cp
, &bn2
))
665 || !TEST_BN_eq(bn
, bn2
))
678 static int test_param_modified(void)
680 OSSL_PARAM param
[3] = { OSSL_PARAM_int("a", NULL
),
681 OSSL_PARAM_int("b", NULL
),
687 if (!TEST_false(OSSL_PARAM_modified(param
))
688 && !TEST_true(OSSL_PARAM_set_int32(param
, 1234))
689 && !TEST_true(OSSL_PARAM_modified(param
))
690 && !TEST_false(OSSL_PARAM_modified(param
+ 1))
691 && !TEST_true(OSSL_PARAM_set_int32(param
+ 1, 1))
692 && !TEST_true(OSSL_PARAM_modified(param
+ 1)))
694 OSSL_PARAM_set_all_unmodified(param
);
695 if (!TEST_false(OSSL_PARAM_modified(param
))
696 && !TEST_true(OSSL_PARAM_set_int32(param
, 4321))
697 && !TEST_true(OSSL_PARAM_modified(param
))
698 && !TEST_false(OSSL_PARAM_modified(param
+ 1))
699 && !TEST_true(OSSL_PARAM_set_int32(param
+ 1, 2))
700 && !TEST_true(OSSL_PARAM_modified(param
+ 1)))
705 static int test_param_copy_null(void)
708 int a
= 1, b
= 2, i
= 0;
709 OSSL_PARAM
*cp1
= NULL
, *cp2
= NULL
, *p
;
712 param
[i
++] = OSSL_PARAM_construct_int("a", &a
);
713 param
[i
++] = OSSL_PARAM_construct_int("b", &b
);
714 param
[i
] = OSSL_PARAM_construct_end();
716 ret
= TEST_ptr_null(OSSL_PARAM_dup(NULL
))
717 && TEST_ptr(cp1
= OSSL_PARAM_merge(NULL
, param
))
718 && TEST_ptr(p
= OSSL_PARAM_locate(cp1
, "a"))
719 && TEST_true(OSSL_PARAM_get_int(p
, &val
))
720 && TEST_int_eq(val
, 1)
721 && TEST_ptr(p
= OSSL_PARAM_locate(cp1
, "b"))
722 && TEST_true(OSSL_PARAM_get_int(p
, &val
))
723 && TEST_int_eq(val
, 2)
724 && TEST_ptr(cp2
= OSSL_PARAM_merge(param
, NULL
))
725 && TEST_ptr(p
= OSSL_PARAM_locate(cp2
, "a"))
726 && TEST_true(OSSL_PARAM_get_int(p
, &val
))
727 && TEST_int_eq(val
, 1)
728 && TEST_ptr(p
= OSSL_PARAM_locate(cp2
, "b"))
729 && TEST_true(OSSL_PARAM_get_int(p
, &val
))
730 && TEST_int_eq(val
, 2)
731 && TEST_ptr_null(OSSL_PARAM_merge(NULL
, NULL
));
732 OSSL_PARAM_free(cp2
);
733 OSSL_PARAM_free(cp1
);
737 int setup_tests(void)
739 ADD_ALL_TESTS(test_param_int
, OSSL_NELEM(raw_values
));
740 ADD_ALL_TESTS(test_param_long
, OSSL_NELEM(raw_values
));
741 ADD_ALL_TESTS(test_param_uint
, OSSL_NELEM(raw_values
));
742 ADD_ALL_TESTS(test_param_ulong
, OSSL_NELEM(raw_values
));
743 ADD_ALL_TESTS(test_param_int32
, OSSL_NELEM(raw_values
));
744 ADD_ALL_TESTS(test_param_uint32
, OSSL_NELEM(raw_values
));
745 ADD_ALL_TESTS(test_param_size_t
, OSSL_NELEM(raw_values
));
746 ADD_ALL_TESTS(test_param_time_t
, OSSL_NELEM(raw_values
));
747 ADD_ALL_TESTS(test_param_int64
, OSSL_NELEM(raw_values
));
748 ADD_ALL_TESTS(test_param_uint64
, OSSL_NELEM(raw_values
));
749 ADD_ALL_TESTS(test_param_bignum
, OSSL_NELEM(raw_values
));
750 ADD_ALL_TESTS(test_param_signed_bignum
, OSSL_NELEM(raw_values
));
751 ADD_TEST(test_param_real
);
752 ADD_ALL_TESTS(test_param_construct
, 4);
753 ADD_TEST(test_param_modified
);
754 ADD_TEST(test_param_copy_null
);