params_test.c 20 KB

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