params_test.c 23 KB

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