params_api_test.c 21 KB

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