params_test.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /*
  2. * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * https://www.openssl.org/source/license.html
  8. * or in the file LICENSE in the source distribution.
  9. */
  10. /*
  11. * This program tests the use of OSSL_PARAM, currently in raw form.
  12. */
  13. #include <string.h>
  14. #include <openssl/bn.h>
  15. #include <openssl/core.h>
  16. #include <openssl/params.h>
  17. #include "internal/nelem.h"
  18. #include "testutil.h"
  19. /*-
  20. * PROVIDER SECTION
  21. * ================
  22. *
  23. * Even though it's not necessarily ONLY providers doing this part,
  24. * they are naturally going to be the most common users of
  25. * set_params and get_params functions.
  26. */
  27. /*
  28. * In real use cases, setters and getters would take an object with
  29. * which the parameters are associated. This structure is a cheap
  30. * simulation.
  31. */
  32. struct object_st {
  33. /*
  34. * Documented as a native integer, of the size given by sizeof(int).
  35. * Assumed data type OSSL_PARAM_INTEGER
  36. */
  37. int p1;
  38. /*
  39. * Documented as a native double, of the size given by sizeof(double).
  40. * Assumed data type OSSL_PARAM_REAL
  41. */
  42. double p2;
  43. /*
  44. * Documented as an arbitrarly large unsigned integer.
  45. * The data size must be large enough to accommodate.
  46. * Assumed data type OSSL_PARAM_UNSIGNED_INTEGER
  47. */
  48. BIGNUM *p3;
  49. /*
  50. * Documented as a C string.
  51. * The data size must be large enough to accommodate.
  52. * Assumed data type OSSL_PARAM_UTF8_STRING
  53. */
  54. char *p4;
  55. size_t p4_l;
  56. /*
  57. * Documented as a C string.
  58. * Assumed data type OSSL_PARAM_UTF8_STRING
  59. */
  60. char p5[256];
  61. size_t p5_l;
  62. /*
  63. * Documented as a pointer to a constant C string.
  64. * Assumed data type OSSL_PARAM_UTF8_PTR
  65. */
  66. const char *p6;
  67. size_t p6_l;
  68. };
  69. #define p1_init 42 /* The ultimate answer */
  70. #define p2_init 6.283 /* Magic number */
  71. /* Stolen from evp_data, BLAKE2s256 test */
  72. #define p3_init \
  73. "4142434445464748494a4b4c4d4e4f50" \
  74. "5152535455565758595a616263646566" \
  75. "6768696a6b6c6d6e6f70717273747576" \
  76. "7778797a30313233343536373839"
  77. #define p4_init "BLAKE2s256" /* Random string */
  78. #define p5_init "Hellow World" /* Random string */
  79. #define p6_init OPENSSL_FULL_VERSION_STR /* Static string */
  80. static void cleanup_object(void *vobj)
  81. {
  82. struct object_st *obj = vobj;
  83. BN_free(obj->p3);
  84. obj->p3 = NULL;
  85. OPENSSL_free(obj->p4);
  86. obj->p4 = NULL;
  87. OPENSSL_free(obj);
  88. }
  89. static void *init_object(void)
  90. {
  91. struct object_st *obj = OPENSSL_zalloc(sizeof(*obj));
  92. obj->p1 = p1_init;
  93. obj->p2 = p2_init;
  94. if (!TEST_true(BN_hex2bn(&obj->p3, p3_init)))
  95. goto fail;
  96. if (!TEST_ptr(obj->p4 = OPENSSL_strdup(p4_init)))
  97. goto fail;
  98. strcpy(obj->p5, p5_init);
  99. obj->p6 = p6_init;
  100. return obj;
  101. fail:
  102. cleanup_object(obj);
  103. obj = NULL;
  104. return NULL;
  105. }
  106. /*
  107. * RAW provider, which handles the parameters in a very raw manner,
  108. * with no fancy API and very minimal checking. The application that
  109. * calls these to set or request parameters MUST get its OSSL_PARAM
  110. * array right.
  111. */
  112. static int raw_set_params(void *vobj, const OSSL_PARAM *params)
  113. {
  114. struct object_st *obj = vobj;
  115. for (; params->key != NULL; params++)
  116. if (strcmp(params->key, "p1") == 0) {
  117. obj->p1 = *(int *)params->data;
  118. } else if (strcmp(params->key, "p2") == 0) {
  119. obj->p2 = *(double *)params->data;
  120. } else if (strcmp(params->key, "p3") == 0) {
  121. BN_free(obj->p3);
  122. if (!TEST_ptr(obj->p3 = BN_native2bn(params->data,
  123. params->data_size, NULL)))
  124. return 0;
  125. } else if (strcmp(params->key, "p4") == 0) {
  126. OPENSSL_free(obj->p4);
  127. if (!TEST_ptr(obj->p4 = OPENSSL_strndup(params->data,
  128. params->data_size)))
  129. return 0;
  130. obj->p4_l = strlen(obj->p4);
  131. } else if (strcmp(params->key, "p5") == 0) {
  132. /*
  133. * Protect obj->p5 against too much data. This should not
  134. * happen, we don't use that long strings.
  135. */
  136. size_t data_length =
  137. OPENSSL_strnlen(params->data, params->data_size);
  138. if (!TEST_size_t_lt(data_length, sizeof(obj->p5)))
  139. return 0;
  140. strncpy(obj->p5, params->data, data_length);
  141. obj->p5[data_length] = '\0';
  142. obj->p5_l = strlen(obj->p5);
  143. } else if (strcmp(params->key, "p6") == 0) {
  144. obj->p6 = *(const char **)params->data;
  145. obj->p6_l = params->data_size;
  146. }
  147. return 1;
  148. }
  149. static int raw_get_params(void *vobj, OSSL_PARAM *params)
  150. {
  151. struct object_st *obj = vobj;
  152. for (; params->key != NULL; params++)
  153. if (strcmp(params->key, "p1") == 0) {
  154. params->return_size = sizeof(obj->p1);
  155. *(int *)params->data = obj->p1;
  156. } else if (strcmp(params->key, "p2") == 0) {
  157. params->return_size = sizeof(obj->p2);
  158. *(double *)params->data = obj->p2;
  159. } else if (strcmp(params->key, "p3") == 0) {
  160. params->return_size = BN_num_bytes(obj->p3);
  161. if (!TEST_size_t_ge(params->data_size, params->return_size))
  162. return 0;
  163. BN_bn2nativepad(obj->p3, params->data, params->return_size);
  164. } else if (strcmp(params->key, "p4") == 0) {
  165. params->return_size = strlen(obj->p4);
  166. if (!TEST_size_t_gt(params->data_size, params->return_size))
  167. return 0;
  168. strcpy(params->data, obj->p4);
  169. } else if (strcmp(params->key, "p5") == 0) {
  170. params->return_size = strlen(obj->p5);
  171. if (!TEST_size_t_gt(params->data_size, params->return_size))
  172. return 0;
  173. strcpy(params->data, obj->p5);
  174. } else if (strcmp(params->key, "p6") == 0) {
  175. params->return_size = strlen(obj->p6);
  176. *(const char **)params->data = obj->p6;
  177. }
  178. return 1;
  179. }
  180. /*
  181. * API provider, which handles the parameters using the API from params.h
  182. */
  183. static int api_set_params(void *vobj, const OSSL_PARAM *params)
  184. {
  185. struct object_st *obj = vobj;
  186. const OSSL_PARAM *p = NULL;
  187. if ((p = OSSL_PARAM_locate_const(params, "p1")) != NULL
  188. && !TEST_true(OSSL_PARAM_get_int(p, &obj->p1)))
  189. return 0;
  190. if ((p = OSSL_PARAM_locate_const(params, "p2")) != NULL
  191. && !TEST_true(OSSL_PARAM_get_double(p, &obj->p2)))
  192. return 0;
  193. if ((p = OSSL_PARAM_locate_const(params, "p3")) != NULL
  194. && !TEST_true(OSSL_PARAM_get_BN(p, &obj->p3)))
  195. return 0;
  196. if ((p = OSSL_PARAM_locate_const(params, "p4")) != NULL) {
  197. OPENSSL_free(obj->p4);
  198. obj->p4 = NULL;
  199. /* If the value pointer is NULL, we get it automatically allocated */
  200. if (!TEST_true(OSSL_PARAM_get_utf8_string(p, &obj->p4, 0)))
  201. return 0;
  202. }
  203. if ((p = OSSL_PARAM_locate_const(params, "p5")) != NULL) {
  204. char *p5_ptr = obj->p5;
  205. if (!TEST_true(OSSL_PARAM_get_utf8_string(p, &p5_ptr, sizeof(obj->p5))))
  206. return 0;
  207. obj->p5_l = strlen(obj->p5);
  208. }
  209. if ((p = OSSL_PARAM_locate_const(params, "p6")) != NULL) {
  210. if (!TEST_true(OSSL_PARAM_get_utf8_ptr(p, &obj->p6)))
  211. return 0;
  212. obj->p6_l = strlen(obj->p6);
  213. }
  214. return 1;
  215. }
  216. static int api_get_params(void *vobj, OSSL_PARAM *params)
  217. {
  218. struct object_st *obj = vobj;
  219. OSSL_PARAM *p = NULL;
  220. if ((p = OSSL_PARAM_locate(params, "p1")) != NULL
  221. && !TEST_true(OSSL_PARAM_set_int(p, obj->p1)))
  222. return 0;
  223. if ((p = OSSL_PARAM_locate(params, "p2")) != NULL
  224. && !TEST_true(OSSL_PARAM_set_double(p, obj->p2)))
  225. return 0;
  226. if ((p = OSSL_PARAM_locate(params, "p3")) != NULL
  227. && !TEST_true(OSSL_PARAM_set_BN(p, obj->p3)))
  228. return 0;
  229. if ((p = OSSL_PARAM_locate(params, "p4")) != NULL
  230. && !TEST_true(OSSL_PARAM_set_utf8_string(p, obj->p4)))
  231. return 0;
  232. if ((p = OSSL_PARAM_locate(params, "p5")) != NULL
  233. && !TEST_true(OSSL_PARAM_set_utf8_string(p, obj->p5)))
  234. return 0;
  235. if ((p = OSSL_PARAM_locate(params, "p6")) != NULL
  236. && !TEST_true(OSSL_PARAM_set_utf8_ptr(p, obj->p6)))
  237. return 0;
  238. return 1;
  239. }
  240. /*
  241. * This structure only simulates a provider dispatch, the real deal is
  242. * a bit more code that's not necessary in these tests.
  243. */
  244. struct provider_dispatch_st {
  245. int (*set_params)(void *obj, const OSSL_PARAM *params);
  246. int (*get_params)(void *obj, OSSL_PARAM *params);
  247. };
  248. /* "raw" provider */
  249. static const struct provider_dispatch_st provider_raw = {
  250. raw_set_params, raw_get_params
  251. };
  252. /* "api" provider */
  253. static const struct provider_dispatch_st provider_api = {
  254. api_set_params, api_get_params
  255. };
  256. /*-
  257. * APPLICATION SECTION
  258. * ===================
  259. */
  260. /* In all our tests, these are variables that get manipulated as parameters
  261. *
  262. * These arrays consistently do nothing with the "p2" parameter, and
  263. * always include a "foo" parameter. This is to check that the
  264. * set_params and get_params calls ignore the lack of parameters that
  265. * the application isn't interested in, as well as ignore parameters
  266. * they don't understand (the application may have one big bag of
  267. * parameters).
  268. */
  269. static int app_p1; /* "p1" */
  270. static double app_p2; /* "p2" is ignored */
  271. static BIGNUM *app_p3 = NULL; /* "p3" */
  272. static unsigned char bignumbin[4096]; /* "p3" */
  273. static char app_p4[256]; /* "p4" */
  274. static char app_p5[256]; /* "p5" */
  275. static const char *app_p6 = NULL; /* "p6" */
  276. static unsigned char foo[1]; /* "foo" */
  277. #define app_p1_init 17 /* A random number */
  278. #define app_p2_init 47.11 /* Another random number */
  279. #define app_p3_init "deadbeef" /* Classic */
  280. #define app_p4_init "Hello"
  281. #define app_p5_init "World"
  282. #define app_p6_init "Cookie"
  283. #define app_foo_init 'z'
  284. static int cleanup_app_variables(void)
  285. {
  286. BN_free(app_p3);
  287. app_p3 = NULL;
  288. return 1;
  289. }
  290. static int init_app_variables(void)
  291. {
  292. int l = 0;
  293. cleanup_app_variables();
  294. app_p1 = app_p1_init;
  295. app_p2 = app_p2_init;
  296. if (!BN_hex2bn(&app_p3, app_p3_init)
  297. || (l = BN_bn2nativepad(app_p3, bignumbin, sizeof(bignumbin))) < 0)
  298. return 0;
  299. strcpy(app_p4, app_p4_init);
  300. strcpy(app_p5, app_p5_init);
  301. app_p6 = app_p6_init;
  302. foo[0] = app_foo_init;
  303. return 1;
  304. }
  305. /*
  306. * Here, we define test OSSL_PARAM arrays
  307. */
  308. /* An array of OSSL_PARAM, specific in the most raw manner possible */
  309. static OSSL_PARAM static_raw_params[] = {
  310. { "p1", OSSL_PARAM_INTEGER, &app_p1, sizeof(app_p1), 0 },
  311. { "p3", OSSL_PARAM_UNSIGNED_INTEGER, &bignumbin, sizeof(bignumbin), 0 },
  312. { "p4", OSSL_PARAM_UTF8_STRING, &app_p4, sizeof(app_p4), 0 },
  313. { "p5", OSSL_PARAM_UTF8_STRING, &app_p5, sizeof(app_p5), 0 },
  314. /* sizeof(app_p6_init) - 1, because we know that's what we're using */
  315. { "p6", OSSL_PARAM_UTF8_PTR, &app_p6, sizeof(app_p6_init) - 1, 0 },
  316. { "foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo), 0 },
  317. { NULL, 0, NULL, 0, 0 }
  318. };
  319. /* The same array of OSSL_PARAM, specified with the macros from params.h */
  320. static OSSL_PARAM static_api_params[] = {
  321. OSSL_PARAM_int("p1", &app_p1),
  322. OSSL_PARAM_BN("p3", &bignumbin, sizeof(bignumbin)),
  323. OSSL_PARAM_DEFN("p4", OSSL_PARAM_UTF8_STRING, &app_p4, sizeof(app_p4)),
  324. OSSL_PARAM_DEFN("p5", OSSL_PARAM_UTF8_STRING, &app_p5, sizeof(app_p5)),
  325. /* sizeof(app_p6_init), because we know that's what we're using */
  326. OSSL_PARAM_DEFN("p6", OSSL_PARAM_UTF8_PTR, &app_p6,
  327. sizeof(app_p6_init) - 1),
  328. OSSL_PARAM_DEFN("foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo)),
  329. OSSL_PARAM_END
  330. };
  331. /*
  332. * The same array again, but constructed at run-time
  333. * This exercises the OSSL_PARAM constructor functions
  334. */
  335. static OSSL_PARAM *construct_api_params(void)
  336. {
  337. size_t n = 0;
  338. static OSSL_PARAM params[10];
  339. params[n++] = OSSL_PARAM_construct_int("p1", &app_p1);
  340. params[n++] = OSSL_PARAM_construct_BN("p3", bignumbin, sizeof(bignumbin));
  341. params[n++] = OSSL_PARAM_construct_utf8_string("p4", app_p4,
  342. sizeof(app_p4));
  343. params[n++] = OSSL_PARAM_construct_utf8_string("p5", app_p5,
  344. sizeof(app_p5));
  345. /* sizeof(app_p6_init), because we know that's what we're using */
  346. params[n++] = OSSL_PARAM_construct_utf8_ptr("p6", (char **)&app_p6,
  347. sizeof(app_p6_init));
  348. params[n++] = OSSL_PARAM_construct_octet_string("foo", &foo, sizeof(foo));
  349. params[n++] = OSSL_PARAM_construct_end();
  350. return params;
  351. }
  352. struct param_owner_st {
  353. OSSL_PARAM *static_params;
  354. OSSL_PARAM *(*constructed_params)(void);
  355. };
  356. static const struct param_owner_st raw_params = {
  357. static_raw_params, NULL
  358. };
  359. static const struct param_owner_st api_params = {
  360. static_api_params, construct_api_params
  361. };
  362. /*-
  363. * TESTING
  364. * =======
  365. */
  366. /*
  367. * Test cases to combine parameters with "provider side" functions
  368. */
  369. static struct {
  370. const struct provider_dispatch_st *prov;
  371. const struct param_owner_st *app;
  372. const char *desc;
  373. } test_cases[] = {
  374. /* Tests within specific methods */
  375. { &provider_raw, &raw_params, "raw provider vs raw params" },
  376. { &provider_api, &api_params, "api provider vs api params" },
  377. /* Mixed methods */
  378. { &provider_raw, &api_params, "raw provider vs api params" },
  379. { &provider_api, &raw_params, "api provider vs raw params" },
  380. };
  381. /* Generic tester of combinations of "providers" and params */
  382. static int test_case_variant(OSSL_PARAM *params, const struct provider_dispatch_st *prov)
  383. {
  384. BIGNUM *verify_p3 = NULL;
  385. void *obj = NULL;
  386. int errcnt = 0;
  387. OSSL_PARAM *p;
  388. /*
  389. * Initialize
  390. */
  391. if (!TEST_ptr(obj = init_object())
  392. || !TEST_true(BN_hex2bn(&verify_p3, p3_init))) {
  393. errcnt++;
  394. goto fin;
  395. }
  396. /*
  397. * Get parameters a first time, just to see that getting works and
  398. * gets us the values we expect.
  399. */
  400. init_app_variables();
  401. if (!TEST_true(prov->get_params(obj, params))
  402. || !TEST_int_eq(app_p1, p1_init) /* "provider" value */
  403. || !TEST_double_eq(app_p2, app_p2_init) /* Should remain untouched */
  404. || !TEST_ptr(p = OSSL_PARAM_locate(params, "p3"))
  405. || !TEST_ptr(BN_native2bn(bignumbin, p->return_size, app_p3))
  406. || !TEST_BN_eq(app_p3, verify_p3) /* "provider" value */
  407. || !TEST_str_eq(app_p4, p4_init) /* "provider" value */
  408. || !TEST_ptr(p = OSSL_PARAM_locate(params, "p5"))
  409. || !TEST_size_t_eq(p->return_size,
  410. sizeof(p5_init) - 1) /* "provider" value */
  411. || !TEST_str_eq(app_p5, p5_init) /* "provider" value */
  412. || !TEST_ptr(p = OSSL_PARAM_locate(params, "p6"))
  413. || !TEST_size_t_eq(p->return_size,
  414. sizeof(p6_init) - 1) /* "provider" value */
  415. || !TEST_str_eq(app_p6, p6_init) /* "provider" value */
  416. || !TEST_char_eq(foo[0], app_foo_init) /* Should remain untouched */
  417. || !TEST_ptr(p = OSSL_PARAM_locate(params, "foo")))
  418. errcnt++;
  419. /*
  420. * Set parameters, then sneak into the object itself and check
  421. * that its attributes got set (or ignored) properly.
  422. */
  423. init_app_variables();
  424. if (!TEST_true(prov->set_params(obj, params))) {
  425. errcnt++;
  426. } else {
  427. struct object_st *sneakpeek = obj;
  428. if (!TEST_int_eq(sneakpeek->p1, app_p1) /* app value set */
  429. || !TEST_double_eq(sneakpeek->p2, p2_init) /* Should remain untouched */
  430. || !TEST_BN_eq(sneakpeek->p3, app_p3) /* app value set */
  431. || !TEST_str_eq(sneakpeek->p4, app_p4) /* app value set */
  432. || !TEST_str_eq(sneakpeek->p5, app_p5) /* app value set */
  433. || !TEST_str_eq(sneakpeek->p6, app_p6)) /* app value set */
  434. errcnt++;
  435. }
  436. /*
  437. * Get parameters again, checking that we get different values
  438. * than earlier where relevant.
  439. */
  440. BN_free(verify_p3);
  441. verify_p3 = NULL;
  442. if (!TEST_true(BN_hex2bn(&verify_p3, app_p3_init))) {
  443. errcnt++;
  444. goto fin;
  445. }
  446. if (!TEST_true(prov->get_params(obj, params))
  447. || !TEST_int_eq(app_p1, app_p1_init) /* app value */
  448. || !TEST_double_eq(app_p2, app_p2_init) /* Should remain untouched */
  449. || !TEST_ptr(p = OSSL_PARAM_locate(params, "p3"))
  450. || !TEST_ptr(BN_native2bn(bignumbin, p->return_size, app_p3))
  451. || !TEST_BN_eq(app_p3, verify_p3) /* app value */
  452. || !TEST_str_eq(app_p4, app_p4_init) /* app value */
  453. || !TEST_ptr(p = OSSL_PARAM_locate(params, "p5"))
  454. || !TEST_size_t_eq(p->return_size,
  455. sizeof(app_p5_init) - 1) /* app value */
  456. || !TEST_str_eq(app_p5, app_p5_init) /* app value */
  457. || !TEST_ptr(p = OSSL_PARAM_locate(params, "p6"))
  458. || !TEST_size_t_eq(p->return_size,
  459. sizeof(app_p6_init) - 1) /* app value */
  460. || !TEST_str_eq(app_p6, app_p6_init) /* app value */
  461. || !TEST_char_eq(foo[0], app_foo_init) /* Should remain untouched */
  462. || !TEST_ptr(p = OSSL_PARAM_locate(params, "foo")))
  463. errcnt++;
  464. fin:
  465. BN_free(verify_p3);
  466. verify_p3 = NULL;
  467. cleanup_app_variables();
  468. cleanup_object(obj);
  469. return errcnt == 0;
  470. }
  471. static int test_case(int i)
  472. {
  473. TEST_info("Case: %s", test_cases[i].desc);
  474. return test_case_variant(test_cases[i].app->static_params,
  475. test_cases[i].prov)
  476. && (test_cases[i].app->constructed_params == NULL
  477. || test_case_variant(test_cases[i].app->constructed_params(),
  478. test_cases[i].prov));
  479. }
  480. /*-
  481. * OSSL_PARAM_allocate_from_text() tests
  482. * =====================================
  483. */
  484. static const OSSL_PARAM params_from_text[] = {
  485. OSSL_PARAM_int32("int", NULL),
  486. OSSL_PARAM_DEFN("short", OSSL_PARAM_INTEGER, NULL, sizeof(int16_t)),
  487. OSSL_PARAM_DEFN("ushort", OSSL_PARAM_UNSIGNED_INTEGER, NULL, sizeof(uint16_t)),
  488. OSSL_PARAM_END,
  489. };
  490. struct int_from_text_test_st {
  491. const char *argname;
  492. const char *strval;
  493. long int intval;
  494. int res;
  495. };
  496. static struct int_from_text_test_st int_from_text_test_cases[] = {
  497. { "int", "", 0, 0 },
  498. { "int", "0", 0, 1 },
  499. { "int", "101", 101, 1 },
  500. { "int", "-102", -102, 1 },
  501. { "int", "12A", 12, 1 }, /* incomplete */
  502. { "int", "0x12B", 0x12B, 1 },
  503. { "hexint", "12C", 0x12C, 1 },
  504. { "hexint", "0x12D", 0, 1 }, /* zero */
  505. /* test check of the target buffer size */
  506. { "int", "0x7fffffff", INT32_MAX, 1 },
  507. { "int", "2147483647", INT32_MAX, 1 },
  508. { "int", "2147483648", 0, 0 }, /* too small buffer */
  509. { "int", "-2147483648", INT32_MIN, 1 },
  510. { "int", "-2147483649", 0, 0 }, /* too small buffer */
  511. { "short", "0x7fff", INT16_MAX, 1 },
  512. { "short", "32767", INT16_MAX, 1 },
  513. { "short", "32768", 0, 0 }, /* too small buffer */
  514. { "ushort", "0xffff", UINT16_MAX, 1 },
  515. { "ushort", "65535", UINT16_MAX, 1 },
  516. { "ushort", "65536", 0, 0 }, /* too small buffer */
  517. };
  518. static int check_int_from_text(const struct int_from_text_test_st a)
  519. {
  520. OSSL_PARAM param;
  521. long int val = 0;
  522. int res;
  523. if (!OSSL_PARAM_allocate_from_text(&param, params_from_text,
  524. a.argname, a.strval, 0, NULL)) {
  525. if (a.res)
  526. TEST_error("errant %s param \"%s\"", a.argname, a.strval);
  527. return !a.res;
  528. }
  529. res = OSSL_PARAM_get_long(&param, &val);
  530. OPENSSL_free(param.data);
  531. if (res ^ a.res || val != a.intval) {
  532. TEST_error("errant %s \"%s\" %li != %li",
  533. a.argname, a.strval, a.intval, val);
  534. return 0;
  535. }
  536. return a.res;
  537. }
  538. static int test_allocate_from_text(int i)
  539. {
  540. return check_int_from_text(int_from_text_test_cases[i]);
  541. }
  542. int setup_tests(void)
  543. {
  544. ADD_ALL_TESTS(test_case, OSSL_NELEM(test_cases));
  545. ADD_ALL_TESTS(test_allocate_from_text, OSSL_NELEM(int_from_text_test_cases));
  546. return 1;
  547. }