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