params_conversion_test.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Copyright 2019-2022 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 <openssl/params.h>
  12. #include "testutil.h"
  13. /* On machines that dont support <inttypes.h> just disable the tests */
  14. #if !defined(OPENSSL_NO_INTTYPES_H)
  15. # ifdef OPENSSL_SYS_VMS
  16. # define strtoumax strtoull
  17. # define strtoimax strtoll
  18. # endif
  19. typedef struct {
  20. OSSL_PARAM *param;
  21. int32_t i32;
  22. int64_t i64;
  23. uint32_t u32;
  24. uint64_t u64;
  25. double d;
  26. int valid_i32, valid_i64, valid_u32, valid_u64, valid_d;
  27. void *ref, *datum;
  28. size_t size;
  29. } PARAM_CONVERSION;
  30. static int param_conversion_load_stanza(PARAM_CONVERSION *pc, const STANZA *s)
  31. {
  32. static int32_t datum_i32, ref_i32;
  33. static int64_t datum_i64, ref_i64;
  34. static uint32_t datum_u32, ref_u32;
  35. static uint64_t datum_u64, ref_u64;
  36. static double datum_d, ref_d;
  37. static OSSL_PARAM params[] = {
  38. OSSL_PARAM_int32("int32", &datum_i32),
  39. OSSL_PARAM_int64("int64", &datum_i64),
  40. OSSL_PARAM_uint32("uint32", &datum_u32),
  41. OSSL_PARAM_uint64("uint64", &datum_u64),
  42. OSSL_PARAM_double("double", &datum_d),
  43. OSSL_PARAM_END
  44. };
  45. int def_i32 = 0, def_i64 = 0, def_u32 = 0, def_u64 = 0, def_d = 0;
  46. const PAIR *pp = s->pairs;
  47. const char *type = NULL;
  48. char *p;
  49. int i;
  50. memset(pc, 0, sizeof(*pc));
  51. for (i = 0; i < s->numpairs; i++, pp++) {
  52. p = "";
  53. if (OPENSSL_strcasecmp(pp->key, "type") == 0) {
  54. if (type != NULL) {
  55. TEST_info("Line %d: multiple type lines", s->curr);
  56. return 0;
  57. }
  58. pc->param = OSSL_PARAM_locate(params, type = pp->value);
  59. if (pc->param == NULL) {
  60. TEST_info("Line %d: unknown type line", s->curr);
  61. return 0;
  62. }
  63. } else if (OPENSSL_strcasecmp(pp->key, "int32") == 0) {
  64. if (def_i32++) {
  65. TEST_info("Line %d: multiple int32 lines", s->curr);
  66. return 0;
  67. }
  68. if (OPENSSL_strcasecmp(pp->value, "invalid") != 0) {
  69. pc->valid_i32 = 1;
  70. pc->i32 = (int32_t)strtoimax(pp->value, &p, 10);
  71. }
  72. } else if (OPENSSL_strcasecmp(pp->key, "int64") == 0) {
  73. if (def_i64++) {
  74. TEST_info("Line %d: multiple int64 lines", s->curr);
  75. return 0;
  76. }
  77. if (OPENSSL_strcasecmp(pp->value, "invalid") != 0) {
  78. pc->valid_i64 = 1;
  79. pc->i64 = (int64_t)strtoimax(pp->value, &p, 10);
  80. }
  81. } else if (OPENSSL_strcasecmp(pp->key, "uint32") == 0) {
  82. if (def_u32++) {
  83. TEST_info("Line %d: multiple uint32 lines", s->curr);
  84. return 0;
  85. }
  86. if (OPENSSL_strcasecmp(pp->value, "invalid") != 0) {
  87. pc->valid_u32 = 1;
  88. pc->u32 = (uint32_t)strtoumax(pp->value, &p, 10);
  89. }
  90. } else if (OPENSSL_strcasecmp(pp->key, "uint64") == 0) {
  91. if (def_u64++) {
  92. TEST_info("Line %d: multiple uint64 lines", s->curr);
  93. return 0;
  94. }
  95. if (OPENSSL_strcasecmp(pp->value, "invalid") != 0) {
  96. pc->valid_u64 = 1;
  97. pc->u64 = (uint64_t)strtoumax(pp->value, &p, 10);
  98. }
  99. } else if (OPENSSL_strcasecmp(pp->key, "double") == 0) {
  100. if (def_d++) {
  101. TEST_info("Line %d: multiple double lines", s->curr);
  102. return 0;
  103. }
  104. if (OPENSSL_strcasecmp(pp->value, "invalid") != 0) {
  105. pc->valid_d = 1;
  106. pc->d = strtod(pp->value, &p);
  107. }
  108. } else {
  109. TEST_info("Line %d: unknown keyword %s", s->curr, pp->key);
  110. return 0;
  111. }
  112. if (*p != '\0') {
  113. TEST_info("Line %d: extra characters at end '%s' for %s",
  114. s->curr, p, pp->key);
  115. return 0;
  116. }
  117. }
  118. if (!TEST_ptr(type)) {
  119. TEST_info("Line %d: type not found", s->curr);
  120. return 0;
  121. }
  122. if (OPENSSL_strcasecmp(type, "int32") == 0) {
  123. if (!TEST_true(def_i32) || !TEST_true(pc->valid_i32)) {
  124. TEST_note("errant int32 on line %d", s->curr);
  125. return 0;
  126. }
  127. datum_i32 = ref_i32 = pc->i32;
  128. pc->datum = &datum_i32;
  129. pc->ref = &ref_i32;
  130. pc->size = sizeof(ref_i32);
  131. } else if (OPENSSL_strcasecmp(type, "int64") == 0) {
  132. if (!TEST_true(def_i64) || !TEST_true(pc->valid_i64)) {
  133. TEST_note("errant int64 on line %d", s->curr);
  134. return 0;
  135. }
  136. datum_i64 = ref_i64 = pc->i64;
  137. pc->datum = &datum_i64;
  138. pc->ref = &ref_i64;
  139. pc->size = sizeof(ref_i64);
  140. } else if (OPENSSL_strcasecmp(type, "uint32") == 0) {
  141. if (!TEST_true(def_u32) || !TEST_true(pc->valid_u32)) {
  142. TEST_note("errant uint32 on line %d", s->curr);
  143. return 0;
  144. }
  145. datum_u32 = ref_u32 = pc->u32;
  146. pc->datum = &datum_u32;
  147. pc->ref = &ref_u32;
  148. pc->size = sizeof(ref_u32);
  149. } else if (OPENSSL_strcasecmp(type, "uint64") == 0) {
  150. if (!TEST_true(def_u64) || !TEST_true(pc->valid_u64)) {
  151. TEST_note("errant uint64 on line %d", s->curr);
  152. return 0;
  153. }
  154. datum_u64 = ref_u64 = pc->u64;
  155. pc->datum = &datum_u64;
  156. pc->ref = &ref_u64;
  157. pc->size = sizeof(ref_u64);
  158. } else if (OPENSSL_strcasecmp(type, "double") == 0) {
  159. if (!TEST_true(def_d) || !TEST_true(pc->valid_d)) {
  160. TEST_note("errant double on line %d", s->curr);
  161. return 0;
  162. }
  163. datum_d = ref_d = pc->d;
  164. pc->datum = &datum_d;
  165. pc->ref = &ref_d;
  166. pc->size = sizeof(ref_d);
  167. } else {
  168. TEST_error("type unknown at line %d", s->curr);
  169. return 0;
  170. }
  171. return 1;
  172. }
  173. static int param_conversion_test(const PARAM_CONVERSION *pc, int line)
  174. {
  175. int32_t i32;
  176. int64_t i64;
  177. uint32_t u32;
  178. uint64_t u64;
  179. double d;
  180. if (!pc->valid_i32) {
  181. if (!TEST_false(OSSL_PARAM_get_int32(pc->param, &i32))
  182. || !TEST_ulong_ne(ERR_get_error(), 0)) {
  183. TEST_note("unexpected valid conversion to int32 on line %d", line);
  184. return 0;
  185. }
  186. } else {
  187. if (!TEST_true(OSSL_PARAM_get_int32(pc->param, &i32))
  188. || !TEST_true(i32 == pc->i32)) {
  189. TEST_note("unexpected conversion to int32 on line %d", line);
  190. return 0;
  191. }
  192. memset(pc->datum, 44, pc->size);
  193. if (!TEST_true(OSSL_PARAM_set_int32(pc->param, i32))
  194. || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
  195. TEST_note("unexpected valid conversion from int32 on line %d",
  196. line);
  197. return 0;
  198. }
  199. }
  200. if (!pc->valid_i64) {
  201. if (!TEST_false(OSSL_PARAM_get_int64(pc->param, &i64))
  202. || !TEST_ulong_ne(ERR_get_error(), 0)) {
  203. TEST_note("unexpected valid conversion to int64 on line %d", line);
  204. return 0;
  205. }
  206. } else {
  207. if (!TEST_true(OSSL_PARAM_get_int64(pc->param, &i64))
  208. || !TEST_true(i64 == pc->i64)) {
  209. TEST_note("unexpected conversion to int64 on line %d", line);
  210. return 0;
  211. }
  212. memset(pc->datum, 44, pc->size);
  213. if (!TEST_true(OSSL_PARAM_set_int64(pc->param, i64))
  214. || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
  215. TEST_note("unexpected valid conversion from int64 on line %d",
  216. line);
  217. return 0;
  218. }
  219. }
  220. if (!pc->valid_u32) {
  221. if (!TEST_false(OSSL_PARAM_get_uint32(pc->param, &u32))
  222. || !TEST_ulong_ne(ERR_get_error(), 0)) {
  223. TEST_note("unexpected valid conversion to uint32 on line %d", line);
  224. return 0;
  225. }
  226. } else {
  227. if (!TEST_true(OSSL_PARAM_get_uint32(pc->param, &u32))
  228. || !TEST_true(u32 == pc->u32)) {
  229. TEST_note("unexpected conversion to uint32 on line %d", line);
  230. return 0;
  231. }
  232. memset(pc->datum, 44, pc->size);
  233. if (!TEST_true(OSSL_PARAM_set_uint32(pc->param, u32))
  234. || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
  235. TEST_note("unexpected valid conversion from uint32 on line %d",
  236. line);
  237. return 0;
  238. }
  239. }
  240. if (!pc->valid_u64) {
  241. if (!TEST_false(OSSL_PARAM_get_uint64(pc->param, &u64))
  242. || !TEST_ulong_ne(ERR_get_error(), 0)) {
  243. TEST_note("unexpected valid conversion to uint64 on line %d", line);
  244. return 0;
  245. }
  246. } else {
  247. if (!TEST_true(OSSL_PARAM_get_uint64(pc->param, &u64))
  248. || !TEST_true(u64 == pc->u64)) {
  249. TEST_note("unexpected conversion to uint64 on line %d", line);
  250. return 0;
  251. }
  252. memset(pc->datum, 44, pc->size);
  253. if (!TEST_true(OSSL_PARAM_set_uint64(pc->param, u64))
  254. || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
  255. TEST_note("unexpected valid conversion from uint64 on line %d",
  256. line);
  257. return 0;
  258. }
  259. }
  260. if (!pc->valid_d) {
  261. if (!TEST_false(OSSL_PARAM_get_double(pc->param, &d))
  262. || !TEST_ulong_ne(ERR_get_error(), 0)) {
  263. TEST_note("unexpected valid conversion to double on line %d", line);
  264. return 0;
  265. }
  266. } else {
  267. if (!TEST_true(OSSL_PARAM_get_double(pc->param, &d))) {
  268. TEST_note("unable to convert to double on line %d", line);
  269. return 0;
  270. }
  271. /*
  272. * Check for not a number (NaN) without using the libm functions.
  273. * When d is a NaN, the standard requires d == d to be false.
  274. * It's less clear if d != d should be true even though it generally is.
  275. * Hence we use the equality test and a not.
  276. */
  277. if (!(d == d)) {
  278. /*
  279. * We've encountered a NaN so check it's really meant to be a NaN.
  280. * We ignore the case where the two values are both different NaN,
  281. * that's not resolvable without knowing the underlying format
  282. * or using libm functions.
  283. */
  284. if (!TEST_false(pc->d == pc->d)) {
  285. TEST_note("unexpected NaN on line %d", line);
  286. return 0;
  287. }
  288. } else if (!TEST_true(d == pc->d)) {
  289. TEST_note("unexpected conversion to double on line %d", line);
  290. return 0;
  291. }
  292. memset(pc->datum, 44, pc->size);
  293. if (!TEST_true(OSSL_PARAM_set_double(pc->param, d))
  294. || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
  295. TEST_note("unexpected valid conversion from double on line %d",
  296. line);
  297. return 0;
  298. }
  299. }
  300. return 1;
  301. }
  302. static int run_param_file_tests(int i)
  303. {
  304. STANZA *s;
  305. PARAM_CONVERSION pc;
  306. const char *testfile = test_get_argument(i);
  307. int res = 1;
  308. if (!TEST_ptr(s = OPENSSL_zalloc(sizeof(*s))))
  309. return 0;
  310. if (!test_start_file(s, testfile)) {
  311. OPENSSL_free(s);
  312. return 0;
  313. }
  314. while (!BIO_eof(s->fp)) {
  315. if (!test_readstanza(s)) {
  316. res = 0;
  317. goto end;
  318. }
  319. if (s->numpairs != 0)
  320. if (!param_conversion_load_stanza(&pc, s)
  321. || !param_conversion_test(&pc, s->curr))
  322. res = 0;
  323. test_clearstanza(s);
  324. }
  325. end:
  326. test_end_file(s);
  327. OPENSSL_free(s);
  328. return res;
  329. }
  330. #endif /* OPENSSL_NO_INTTYPES_H */
  331. OPT_TEST_DECLARE_USAGE("file...\n")
  332. int setup_tests(void)
  333. {
  334. size_t n;
  335. if (!test_skip_common_options()) {
  336. TEST_error("Error parsing test options\n");
  337. return 0;
  338. }
  339. n = test_get_argument_count();
  340. if (n == 0)
  341. return 0;
  342. #if !defined(OPENSSL_NO_INTTYPES_H)
  343. ADD_ALL_TESTS(run_param_file_tests, n);
  344. #endif /* OPENSSL_NO_INTTYPES_H */
  345. return 1;
  346. }