]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/params_api_test.c
Update copyright year
[thirdparty/openssl.git] / test / params_api_test.c
1 /*
2 * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
4 *
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
9 */
10
11 #include <string.h>
12 #include "testutil.h"
13 #include "internal/nelem.h"
14 #include "internal/endian.h"
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
21 static 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
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 */
37 static void le_copy(unsigned char *out, size_t outlen,
38 const void *in, size_t inlen)
39 {
40 DECLARE_IS_ENDIAN;
41
42 if (IS_LITTLE_ENDIAN) {
43 memcpy(out, in, outlen);
44 } else {
45 if (outlen < inlen)
46 in = (const char *)in + inlen - outlen;
47 swap_copy(out, in, outlen);
48 }
49 }
50
51 static const struct {
52 size_t len;
53 unsigned char value[MAX_LEN];
54 } raw_values[] = {
55 { 1, { 0x47 } },
56 { 1, { 0xd0 } },
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 } },
71 };
72
73 static int test_param_type_extra(OSSL_PARAM *param, const unsigned char *cmp,
74 size_t width)
75 {
76 int32_t i32;
77 int64_t i64;
78 size_t s, sz;
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;
83
84 /*
85 * Set the unmodified sentinel directly because there is no param array
86 * for these tests.
87 */
88 param->return_size = OSSL_PARAM_UNMODIFIED;
89 if (signd) {
90 if ((bit32 && !TEST_true(OSSL_PARAM_get_int32(param, &i32)))
91 || !TEST_true(OSSL_PARAM_get_int64(param, &i64)))
92 return 0;
93 } else {
94 if ((bit32
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))))
98 return 0;
99 }
100 if (!TEST_false(OSSL_PARAM_modified(param)))
101 return 0;
102
103 /* Check signed types */
104 if (bit32) {
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))
108 return 0;
109 }
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))
113 return 0;
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))
118 return 0;
119 }
120
121 /* Check a widening write if possible */
122 if (sizeof(size_t) > width) {
123 if (signd) {
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))
127 return 0;
128 } else {
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))
132 return 0;
133 }
134 if (!TEST_true(OSSL_PARAM_modified(param)))
135 return 0;
136 }
137 return 1;
138 }
139
140 /*
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.
145 *
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
149 * change.
150 */
151
152 static int test_param_int(int n)
153 {
154 int in, out;
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);
159
160 memset(buf, 0, sizeof(buf));
161 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
162 memcpy(&in, buf, sizeof(in));
163 param.data = &out;
164 if (!TEST_true(OSSL_PARAM_set_int(&param, in)))
165 return 0;
166 le_copy(cmp, sizeof(out), &out, sizeof(out));
167 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
168 return 0;
169 in = 0;
170 if (!TEST_true(OSSL_PARAM_get_int(&param, &in)))
171 return 0;
172 le_copy(cmp, sizeof(in), &in, sizeof(in));
173 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
174 return 0;
175 param.data = &out;
176 return test_param_type_extra(&param, raw_values[n].value, sizeof(int));
177 }
178
179 static int test_param_long(int n)
180 {
181 long int in, out;
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);
186
187 memset(buf, 0, sizeof(buf));
188 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
189 memcpy(&in, buf, sizeof(in));
190 param.data = &out;
191 if (!TEST_true(OSSL_PARAM_set_long(&param, in)))
192 return 0;
193 le_copy(cmp, sizeof(out), &out, sizeof(out));
194 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
195 return 0;
196 in = 0;
197 if (!TEST_true(OSSL_PARAM_get_long(&param, &in)))
198 return 0;
199 le_copy(cmp, sizeof(in), &in, sizeof(in));
200 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
201 return 0;
202 param.data = &out;
203 return test_param_type_extra(&param, raw_values[n].value, sizeof(long int));
204 }
205
206 static int test_param_uint(int n)
207 {
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);
212
213 memset(buf, 0, sizeof(buf));
214 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
215 memcpy(&in, buf, sizeof(in));
216 param.data = &out;
217 if (!TEST_true(OSSL_PARAM_set_uint(&param, in)))
218 return 0;
219 le_copy(cmp, sizeof(out), &out, sizeof(out));
220 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
221 return 0;
222 in = 0;
223 if (!TEST_true(OSSL_PARAM_get_uint(&param, &in)))
224 return 0;
225 le_copy(cmp, sizeof(in), &in, sizeof(in));
226 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
227 return 0;
228 param.data = &out;
229 return test_param_type_extra(&param, raw_values[n].value, sizeof(unsigned int));
230 }
231
232 static int test_param_ulong(int n)
233 {
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);
239
240 memset(buf, 0, sizeof(buf));
241 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
242 memcpy(&in, buf, sizeof(in));
243 param.data = &out;
244 if (!TEST_true(OSSL_PARAM_set_ulong(&param, in)))
245 return 0;
246 le_copy(cmp, sizeof(out), &out, sizeof(out));
247 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
248 return 0;
249 in = 0;
250 if (!TEST_true(OSSL_PARAM_get_ulong(&param, &in)))
251 return 0;
252 le_copy(cmp, sizeof(in), &in, sizeof(in));
253 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
254 return 0;
255 param.data = &out;
256 return test_param_type_extra(&param, raw_values[n].value, sizeof(unsigned long int));
257 }
258
259 static int test_param_int32(int n)
260 {
261 int32_t in, out;
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);
266
267 memset(buf, 0, sizeof(buf));
268 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
269 memcpy(&in, buf, sizeof(in));
270 param.data = &out;
271 if (!TEST_true(OSSL_PARAM_set_int32(&param, in)))
272 return 0;
273 le_copy(cmp, sizeof(out), &out, sizeof(out));
274 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
275 return 0;
276 in = 0;
277 if (!TEST_true(OSSL_PARAM_get_int32(&param, &in)))
278 return 0;
279 le_copy(cmp, sizeof(in), &in, sizeof(in));
280 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
281 return 0;
282 param.data = &out;
283 return test_param_type_extra(&param, raw_values[n].value, sizeof(int32_t));
284 }
285
286 static int test_param_uint32(int n)
287 {
288 uint32_t in, out;
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);
293
294 memset(buf, 0, sizeof(buf));
295 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
296 memcpy(&in, buf, sizeof(in));
297 param.data = &out;
298 if (!TEST_true(OSSL_PARAM_set_uint32(&param, in)))
299 return 0;
300 le_copy(cmp, sizeof(out), &out, sizeof(out));
301 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
302 return 0;
303 in = 0;
304 if (!TEST_true(OSSL_PARAM_get_uint32(&param, &in)))
305 return 0;
306 le_copy(cmp, sizeof(in), &in, sizeof(in));
307 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
308 return 0;
309 param.data = &out;
310 return test_param_type_extra(&param, raw_values[n].value, sizeof(uint32_t));
311 }
312
313 static int test_param_int64(int n)
314 {
315 int64_t in, out;
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);
320
321 memset(buf, 0, sizeof(buf));
322 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
323 memcpy(&in, buf, sizeof(in));
324 param.data = &out;
325 if (!TEST_true(OSSL_PARAM_set_int64(&param, in)))
326 return 0;
327 le_copy(cmp, sizeof(out), &out, sizeof(out));
328 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
329 return 0;
330 in = 0;
331 if (!TEST_true(OSSL_PARAM_get_int64(&param, &in)))
332 return 0;
333 le_copy(cmp, sizeof(in), &in, sizeof(in));
334 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
335 return 0;
336 param.data = &out;
337 return test_param_type_extra(&param, raw_values[n].value, sizeof(int64_t));
338 }
339
340 static int test_param_uint64(int n)
341 {
342 uint64_t in, out;
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);
347
348 memset(buf, 0, sizeof(buf));
349 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
350 memcpy(&in, buf, sizeof(in));
351 param.data = &out;
352 if (!TEST_true(OSSL_PARAM_set_uint64(&param, in)))
353 return 0;
354 le_copy(cmp, sizeof(out), &out, sizeof(out));
355 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
356 return 0;
357 in = 0;
358 if (!TEST_true(OSSL_PARAM_get_uint64(&param, &in)))
359 return 0;
360 le_copy(cmp, sizeof(in), &in, sizeof(in));
361 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
362 return 0;
363 param.data = &out;
364 return test_param_type_extra(&param, raw_values[n].value, sizeof(uint64_t));
365 }
366
367 static int test_param_size_t(int n)
368 {
369 size_t in, out;
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);
374
375 memset(buf, 0, sizeof(buf));
376 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
377 memcpy(&in, buf, sizeof(in));
378 param.data = &out;
379 if (!TEST_true(OSSL_PARAM_set_size_t(&param, in)))
380 return 0;
381 le_copy(cmp, sizeof(out), &out, sizeof(out));
382 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
383 return 0;
384 in = 0;
385 if (!TEST_true(OSSL_PARAM_get_size_t(&param, &in)))
386 return 0;
387 le_copy(cmp, sizeof(in), &in, sizeof(in));
388 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
389 return 0;
390 param.data = &out;
391 return test_param_type_extra(&param, raw_values[n].value, sizeof(size_t));
392 }
393
394 static int test_param_time_t(int n)
395 {
396 time_t in, out;
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);
401
402 memset(buf, 0, sizeof(buf));
403 le_copy(buf, sizeof(in), raw_values[n].value, sizeof(in));
404 memcpy(&in, buf, sizeof(in));
405 param.data = &out;
406 if (!TEST_true(OSSL_PARAM_set_time_t(&param, in)))
407 return 0;
408 le_copy(cmp, sizeof(out), &out, sizeof(out));
409 if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
410 return 0;
411 in = 0;
412 if (!TEST_true(OSSL_PARAM_get_time_t(&param, &in)))
413 return 0;
414 le_copy(cmp, sizeof(in), &in, sizeof(in));
415 if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
416 return 0;
417 param.data = &out;
418 return test_param_type_extra(&param, raw_values[n].value, sizeof(size_t));
419 }
420
421 static int test_param_bignum(int n)
422 {
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,
427 NULL, 0);
428 int ret = 0;
429
430 param.data = bnbuf;
431 param.data_size = sizeof(bnbuf);
432
433 if (!TEST_ptr(b = BN_lebin2bn(raw_values[n].value, (int)len, NULL)))
434 goto err;
435
436 if (!TEST_true(OSSL_PARAM_set_BN(&param, b)))
437 goto err;
438 le_copy(buf, len, bnbuf, sizeof(bnbuf));
439 if (!TEST_mem_eq(raw_values[n].value, len, buf, len))
440 goto err;
441 param.data_size = param.return_size;
442 if (!TEST_true(OSSL_PARAM_get_BN(&param, &c))
443 || !TEST_BN_eq(b, c))
444 goto err;
445
446 ret = 1;
447 err:
448 BN_free(b);
449 BN_free(c);
450 return ret;
451 }
452
453 static int test_param_signed_bignum(int n)
454 {
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);
459 int ret = 0;
460
461 param.data = bnbuf;
462 param.data_size = sizeof(bnbuf);
463
464 if (!TEST_ptr(b = BN_signed_lebin2bn(raw_values[n].value, (int)len, NULL)))
465 goto err;
466
467 /* raw_values are little endian */
468 if (!TEST_false(!!(raw_values[n].value[len - 1] & 0x80) ^ BN_is_negative(b)))
469 goto err;
470 if (!TEST_true(OSSL_PARAM_set_BN(&param, b)))
471 goto err;
472 le_copy(buf, len, bnbuf, sizeof(bnbuf));
473 if (!TEST_mem_eq(raw_values[n].value, len, buf, len))
474 goto err;
475 param.data_size = param.return_size;
476 if (!TEST_true(OSSL_PARAM_get_BN(&param, &c))
477 || !TEST_BN_eq(b, c)) {
478 BN_print_fp(stderr, c);
479 goto err;
480 }
481
482 ret = 1;
483 err:
484 BN_free(b);
485 BN_free(c);
486 return ret;
487 }
488
489 static int test_param_real(void)
490 {
491 double p;
492 OSSL_PARAM param = OSSL_PARAM_double("r", NULL);
493
494 param.data = &p;
495 return TEST_true(OSSL_PARAM_set_double(&param, 3.14159))
496 && TEST_double_eq(p, 3.14159);
497 }
498
499 static int test_param_construct(int tstid)
500 {
501 static const char *int_names[] = {
502 "int", "long", "int32", "int64"
503 };
504 static const char *uint_names[] = {
505 "uint", "ulong", "uint32", "uint64", "size_t"
506 };
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
510 };
511 OSSL_PARAM *p = NULL, *p1 = NULL;
512 static const OSSL_PARAM params_empty[] = {
513 OSSL_PARAM_END
514 };
515 OSSL_PARAM params[20];
516 char buf[100], buf2[100], *bufp, *bufp2;
517 unsigned char ubuf[100];
518 void *vp, *vpn = NULL, *vp2;
519 OSSL_PARAM *cp;
520 int i, n = 0, ret = 0;
521 unsigned int u;
522 long int l;
523 unsigned long int ul;
524 int32_t i32;
525 uint32_t u32;
526 int64_t i64;
527 uint64_t u64;
528 size_t j, k, s;
529 double d, d2;
530 BIGNUM *bn = NULL, *bn2 = NULL;
531
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();
548
549 switch (tstid) {
550 case 0:
551 p = params;
552 break;
553 case 1:
554 p = OSSL_PARAM_merge(params, params_empty);
555 break;
556 case 2:
557 p = OSSL_PARAM_dup(params);
558 break;
559 default:
560 p1 = OSSL_PARAM_dup(params);
561 p = OSSL_PARAM_merge(p1, params_empty);
562 break;
563 }
564
565 /* Search failure */
566 if (!TEST_ptr_null(OSSL_PARAM_locate(p, "fnord")))
567 goto err;
568
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]);
577 goto err;
578 }
579 }
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]);
588 goto err;
589 }
590 }
591 /* Real */
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)))
598 goto err;
599 /* UTF8 string */
600 bufp = NULL;
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")) {
606 OPENSSL_free(bufp);
607 goto err;
608 }
609 OPENSSL_free(bufp);
610 bufp = buf2;
611 if (!TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, sizeof(buf2)))
612 || !TEST_str_eq(buf2, "abcdef"))
613 goto err;
614 /* UTF8 pointer */
615 /* Note that the size of a UTF8 string does *NOT* include the NUL byte */
616 bufp = buf;
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)))
623 goto err;
624 /* OCTET string */
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")))
629 goto err;
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")))
636 goto err;
637 vp = buf2;
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")))
642 goto err;
643 /* OCTET pointer */
644 vp = &l;
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)))
649 goto err;
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)))
655 goto err;
656 /* BIGNUM */
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))
661 goto err;
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))
666 goto err;
667 ret = 1;
668 err:
669 if (p != params)
670 OPENSSL_free(p);
671 OPENSSL_free(p1);
672 OPENSSL_free(vpn);
673 BN_free(bn);
674 BN_free(bn2);
675 return ret;
676 }
677
678 static int test_param_modified(void)
679 {
680 OSSL_PARAM param[3] = { OSSL_PARAM_int("a", NULL),
681 OSSL_PARAM_int("b", NULL),
682 OSSL_PARAM_END };
683 int a, b;
684
685 param->data = &a;
686 param[1].data = &b;
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)))
693 return 0;
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)))
701 return 0;
702 return 1;
703 }
704
705 static int test_param_copy_null(void)
706 {
707 int ret, val;
708 int a = 1, b = 2, i = 0;
709 OSSL_PARAM *cp1 = NULL, *cp2 = NULL, *p;
710 OSSL_PARAM param[3];
711
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();
715
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);
734 return ret;
735 }
736
737 int setup_tests(void)
738 {
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);
755 return 1;
756 }