params_conversion_test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 <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_WINDOWS
  16. # define strcasecmp _stricmp
  17. # endif
  18. typedef struct {
  19. const OSSL_PARAM *param;
  20. int32_t i32;
  21. int64_t i64;
  22. uint32_t u32;
  23. uint64_t u64;
  24. double d;
  25. int valid_i32, valid_i64, valid_u32, valid_u64, valid_d;
  26. void *ref, *datum;
  27. size_t size;
  28. } PARAM_CONVERSION;
  29. static int param_conversion_load_stanza(PARAM_CONVERSION *pc, const STANZA *s)
  30. {
  31. static int32_t datum_i32, ref_i32;
  32. static int64_t datum_i64, ref_i64;
  33. static uint32_t datum_u32, ref_u32;
  34. static uint64_t datum_u64, ref_u64;
  35. static double datum_d, ref_d;
  36. static const OSSL_PARAM params[] = {
  37. OSSL_PARAM_int32("int32", &datum_i32),
  38. OSSL_PARAM_int64("int64", &datum_i64),
  39. OSSL_PARAM_uint32("uint32", &datum_u32),
  40. OSSL_PARAM_uint64("uint64", &datum_u64),
  41. OSSL_PARAM_double("double", &datum_d),
  42. OSSL_PARAM_END
  43. };
  44. int def_i32 = 0, def_i64 = 0, def_u32 = 0, def_u64 = 0, def_d = 0;
  45. const PAIR *pp = s->pairs;
  46. const char *type = NULL;
  47. char *p;
  48. int i;
  49. memset(pc, 0, sizeof(*pc));
  50. for (i = 0; i < s->numpairs; i++, pp++) {
  51. p = "";
  52. if (strcasecmp(pp->key, "type") == 0) {
  53. if (type != NULL) {
  54. TEST_info("Line %d: multiple type lines", s->curr);
  55. return 0;
  56. }
  57. pc->param = OSSL_PARAM_locate(params, type = pp->value);
  58. if (pc->param == NULL) {
  59. TEST_info("Line %d: unknown type line", s->curr);
  60. return 0;
  61. }
  62. } else if (strcasecmp(pp->key, "int32") == 0) {
  63. if (def_i32++) {
  64. TEST_info("Line %d: multiple int32 lines", s->curr);
  65. return 0;
  66. }
  67. if (strcasecmp(pp->value, "invalid") != 0) {
  68. pc->valid_i32 = 1;
  69. pc->i32 = (int32_t)strtoimax(pp->value, &p, 10);
  70. }
  71. } else if (strcasecmp(pp->key, "int64") == 0) {
  72. if (def_i64++) {
  73. TEST_info("Line %d: multiple int64 lines", s->curr);
  74. return 0;
  75. }
  76. if (strcasecmp(pp->value, "invalid") != 0) {
  77. pc->valid_i64 = 1;
  78. pc->i64 = (int64_t)strtoimax(pp->value, &p, 10);
  79. }
  80. } else if (strcasecmp(pp->key, "uint32") == 0) {
  81. if (def_u32++) {
  82. TEST_info("Line %d: multiple uint32 lines", s->curr);
  83. return 0;
  84. }
  85. if (strcasecmp(pp->value, "invalid") != 0) {
  86. pc->valid_u32 = 1;
  87. pc->u32 = (uint32_t)strtoumax(pp->value, &p, 10);
  88. }
  89. } else if (strcasecmp(pp->key, "uint64") == 0) {
  90. if (def_u64++) {
  91. TEST_info("Line %d: multiple uint64 lines", s->curr);
  92. return 0;
  93. }
  94. if (strcasecmp(pp->value, "invalid") != 0) {
  95. pc->valid_u64 = 1;
  96. pc->u64 = (uint64_t)strtoumax(pp->value, &p, 10);
  97. }
  98. } else if (strcasecmp(pp->key, "double") == 0) {
  99. if (def_d++) {
  100. TEST_info("Line %d: multiple double lines", s->curr);
  101. return 0;
  102. }
  103. if (strcasecmp(pp->value, "invalid") != 0) {
  104. pc->valid_d = 1;
  105. pc->d = strtod(pp->value, &p);
  106. }
  107. } else {
  108. TEST_info("Line %d: unknown keyword %s", s->curr, pp->key);
  109. return 0;
  110. }
  111. if (*p != '\0') {
  112. TEST_info("Line %d: extra characters at end '%s' for %s",
  113. s->curr, p, pp->key);
  114. return 0;
  115. }
  116. }
  117. if (!TEST_ptr(type)) {
  118. TEST_info("Line %d: type not found", s->curr);
  119. return 0;
  120. }
  121. if (strcasecmp(type, "int32") == 0) {
  122. if (!TEST_true(def_i32) || !TEST_true(pc->valid_i32)) {
  123. TEST_note("errant int32 on line %d", s->curr);
  124. return 0;
  125. }
  126. datum_i32 = ref_i32 = pc->i32;
  127. pc->datum = &datum_i32;
  128. pc->ref = &ref_i32;
  129. pc->size = sizeof(ref_i32);
  130. } else if (strcasecmp(type, "int64") == 0) {
  131. if (!TEST_true(def_i64) || !TEST_true(pc->valid_i64)) {
  132. TEST_note("errant int64 on line %d", s->curr);
  133. return 0;
  134. }
  135. datum_i64 = ref_i64 = pc->i64;
  136. pc->datum = &datum_i64;
  137. pc->ref = &ref_i64;
  138. pc->size = sizeof(ref_i64);
  139. } else if (strcasecmp(type, "uint32") == 0) {
  140. if (!TEST_true(def_u32) || !TEST_true(pc->valid_u32)) {
  141. TEST_note("errant uint32 on line %d", s->curr);
  142. return 0;
  143. }
  144. datum_u32 = ref_u32 = pc->u32;
  145. pc->datum = &datum_u32;
  146. pc->ref = &ref_u32;
  147. pc->size = sizeof(ref_u32);
  148. } else if (strcasecmp(type, "uint64") == 0) {
  149. if (!TEST_true(def_u64) || !TEST_true(pc->valid_u64)) {
  150. TEST_note("errant uint64 on line %d", s->curr);
  151. return 0;
  152. }
  153. datum_u64 = ref_u64 = pc->u64;
  154. pc->datum = &datum_u64;
  155. pc->ref = &ref_u64;
  156. pc->size = sizeof(ref_u64);
  157. } else if (strcasecmp(type, "double") == 0) {
  158. if (!TEST_true(def_d) || !TEST_true(pc->valid_d)) {
  159. TEST_note("errant double on line %d", s->curr);
  160. return 0;
  161. }
  162. datum_d = ref_d = pc->d;
  163. pc->datum = &datum_d;
  164. pc->ref = &ref_d;
  165. pc->size = sizeof(ref_d);
  166. } else {
  167. TEST_error("type unknown at line %d", s->curr);
  168. return 0;
  169. }
  170. return 1;
  171. }
  172. static int param_conversion_test(const PARAM_CONVERSION *pc, int line)
  173. {
  174. int32_t i32;
  175. int64_t i64;
  176. uint32_t u32;
  177. uint64_t u64;
  178. double d;
  179. if (!pc->valid_i32) {
  180. if (!TEST_false(OSSL_PARAM_get_int32(pc->param, &i32))) {
  181. TEST_note("unexpected valid conversion to int32 on line %d", line);
  182. return 0;
  183. }
  184. } else {
  185. if (!TEST_true(OSSL_PARAM_get_int32(pc->param, &i32))
  186. || !TEST_true(i32 == pc->i32)) {
  187. TEST_note("unexpected conversion to int32 on line %d", line);
  188. return 0;
  189. }
  190. memset(pc->datum, 44, pc->size);
  191. if (!TEST_true(OSSL_PARAM_set_int32(pc->param, i32))
  192. || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
  193. TEST_note("unexpected valid conversion from int32 on line %d",
  194. line);
  195. return 0;
  196. }
  197. }
  198. if (!pc->valid_i64) {
  199. if (!TEST_false(OSSL_PARAM_get_int64(pc->param, &i64))) {
  200. TEST_note("unexpected valid conversion to int64 on line %d", line);
  201. return 0;
  202. }
  203. } else {
  204. if (!TEST_true(OSSL_PARAM_get_int64(pc->param, &i64))
  205. || !TEST_true(i64 == pc->i64)) {
  206. TEST_note("unexpected conversion to int64 on line %d", line);
  207. return 0;
  208. }
  209. memset(pc->datum, 44, pc->size);
  210. if (!TEST_true(OSSL_PARAM_set_int64(pc->param, i64))
  211. || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
  212. TEST_note("unexpected valid conversion from int64 on line %d",
  213. line);
  214. return 0;
  215. }
  216. }
  217. if (!pc->valid_u32) {
  218. if (!TEST_false(OSSL_PARAM_get_uint32(pc->param, &u32))) {
  219. TEST_note("unexpected valid conversion to uint32 on line %d", line);
  220. return 0;
  221. }
  222. } else {
  223. if (!TEST_true(OSSL_PARAM_get_uint32(pc->param, &u32))
  224. || !TEST_true(u32 == pc->u32)) {
  225. TEST_note("unexpected conversion to uint32 on line %d", line);
  226. return 0;
  227. }
  228. memset(pc->datum, 44, pc->size);
  229. if (!TEST_true(OSSL_PARAM_set_uint32(pc->param, u32))
  230. || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
  231. TEST_note("unexpected valid conversion from uint32 on line %d",
  232. line);
  233. return 0;
  234. }
  235. }
  236. if (!pc->valid_u64) {
  237. if (!TEST_false(OSSL_PARAM_get_uint64(pc->param, &u64))) {
  238. TEST_note("unexpected valid conversion to uint64 on line %d", line);
  239. return 0;
  240. }
  241. } else {
  242. if (!TEST_true(OSSL_PARAM_get_uint64(pc->param, &u64))
  243. || !TEST_true(u64 == pc->u64)) {
  244. TEST_note("unexpected conversion to uint64 on line %d", line);
  245. return 0;
  246. }
  247. memset(pc->datum, 44, pc->size);
  248. if (!TEST_true(OSSL_PARAM_set_uint64(pc->param, u64))
  249. || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
  250. TEST_note("unexpected valid conversion from uint64 on line %d",
  251. line);
  252. return 0;
  253. }
  254. }
  255. if (!pc->valid_d) {
  256. if (!TEST_false(OSSL_PARAM_get_double(pc->param, &d))) {
  257. TEST_note("unexpected valid conversion to double on line %d", line);
  258. return 0;
  259. }
  260. } else {
  261. if (!TEST_true(OSSL_PARAM_get_double(pc->param, &d))
  262. || !TEST_true(d == pc->d)) {
  263. TEST_note("unexpected conversion to double on line %d", line);
  264. return 0;
  265. }
  266. memset(pc->datum, 44, pc->size);
  267. if (!TEST_true(OSSL_PARAM_set_double(pc->param, d))
  268. || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
  269. TEST_note("unexpected valid conversion from double on line %d",
  270. line);
  271. return 0;
  272. }
  273. }
  274. return 1;
  275. }
  276. static int run_param_file_tests(int i)
  277. {
  278. STANZA *s;
  279. PARAM_CONVERSION pc;
  280. const char *testfile = test_get_argument(i);
  281. int res = 1;
  282. if (!TEST_ptr(s = OPENSSL_zalloc(sizeof(*s))))
  283. return 0;
  284. if (!test_start_file(s, testfile)) {
  285. OPENSSL_free(s);
  286. return 0;
  287. }
  288. while (!BIO_eof(s->fp)) {
  289. if (!test_readstanza(s)) {
  290. res = 0;
  291. goto end;
  292. }
  293. if (s->numpairs != 0)
  294. if (!param_conversion_load_stanza(&pc, s)
  295. || !param_conversion_test(&pc, s->curr))
  296. res = 0;
  297. test_clearstanza(s);
  298. }
  299. end:
  300. test_end_file(s);
  301. OPENSSL_free(s);
  302. return res;
  303. }
  304. #endif /* OPENSSL_NO_INTTYPES_H */
  305. OPT_TEST_DECLARE_USAGE("file...\n")
  306. int setup_tests(void)
  307. {
  308. size_t n = test_get_argument_count();
  309. if (n == 0)
  310. return 0;
  311. #if !defined(OPENSSL_NO_INTTYPES_H)
  312. ADD_ALL_TESTS(run_param_file_tests, n);
  313. #endif /* OPENSSL_NO_INTTYPES_H */
  314. return 1;
  315. }