param_build_test.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 "openssl/param_build.h"
  13. #include "internal/nelem.h"
  14. #include "testutil.h"
  15. static int template_public_test(void)
  16. {
  17. OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
  18. OSSL_PARAM *params = NULL, *p;
  19. BIGNUM *bn = NULL, *bn_res = NULL;
  20. int i;
  21. long int l;
  22. int32_t i32;
  23. int64_t i64;
  24. double d;
  25. char *utf = NULL;
  26. const char *cutf;
  27. int res = 0;
  28. if (!TEST_ptr(bld)
  29. || !TEST_true(OSSL_PARAM_BLD_push_int(bld, "i", -6))
  30. || !TEST_true(OSSL_PARAM_BLD_push_long(bld, "l", 42))
  31. || !TEST_true(OSSL_PARAM_BLD_push_int32(bld, "i32", 1532))
  32. || !TEST_true(OSSL_PARAM_BLD_push_int64(bld, "i64", -9999999))
  33. || !TEST_true(OSSL_PARAM_BLD_push_double(bld, "d", 1.61803398875))
  34. || !TEST_ptr(bn = BN_new())
  35. || !TEST_true(BN_set_word(bn, 1729))
  36. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber", bn))
  37. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, "utf8_s", "foo",
  38. sizeof("foo")))
  39. || !TEST_true(OSSL_PARAM_BLD_push_utf8_ptr(bld, "utf8_p", "bar-boom",
  40. 0))
  41. || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
  42. /* Check int */
  43. || !TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
  44. || !TEST_true(OSSL_PARAM_get_int(p, &i))
  45. || !TEST_str_eq(p->key, "i")
  46. || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
  47. || !TEST_size_t_eq(p->data_size, sizeof(int))
  48. || !TEST_int_eq(i, -6)
  49. /* Check int32 */
  50. || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32"))
  51. || !TEST_true(OSSL_PARAM_get_int32(p, &i32))
  52. || !TEST_str_eq(p->key, "i32")
  53. || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
  54. || !TEST_size_t_eq(p->data_size, sizeof(int32_t))
  55. || !TEST_int_eq((int)i32, 1532)
  56. /* Check int64 */
  57. || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64"))
  58. || !TEST_str_eq(p->key, "i64")
  59. || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
  60. || !TEST_size_t_eq(p->data_size, sizeof(int64_t))
  61. || !TEST_true(OSSL_PARAM_get_int64(p, &i64))
  62. || !TEST_long_eq((long)i64, -9999999)
  63. /* Check long */
  64. || !TEST_ptr(p = OSSL_PARAM_locate(params, "l"))
  65. || !TEST_str_eq(p->key, "l")
  66. || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
  67. || !TEST_size_t_eq(p->data_size, sizeof(long int))
  68. || !TEST_true(OSSL_PARAM_get_long(p, &l))
  69. || !TEST_long_eq(l, 42)
  70. /* Check double */
  71. || !TEST_ptr(p = OSSL_PARAM_locate(params, "d"))
  72. || !TEST_true(OSSL_PARAM_get_double(p, &d))
  73. || !TEST_str_eq(p->key, "d")
  74. || !TEST_uint_eq(p->data_type, OSSL_PARAM_REAL)
  75. || !TEST_size_t_eq(p->data_size, sizeof(double))
  76. || !TEST_double_eq(d, 1.61803398875)
  77. /* Check UTF8 string */
  78. || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_s"))
  79. || !TEST_str_eq(p->data, "foo")
  80. || !TEST_true(OSSL_PARAM_get_utf8_string(p, &utf, 0))
  81. || !TEST_str_eq(utf, "foo")
  82. /* Check UTF8 pointer */
  83. || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_p"))
  84. || !TEST_true(OSSL_PARAM_get_utf8_ptr(p, &cutf))
  85. || !TEST_str_eq(cutf, "bar-boom")
  86. /* Check BN */
  87. || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber"))
  88. || !TEST_str_eq(p->key, "bignumber")
  89. || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
  90. || !TEST_true(OSSL_PARAM_get_BN(p, &bn_res))
  91. || !TEST_int_eq(BN_cmp(bn_res, bn), 0))
  92. goto err;
  93. res = 1;
  94. err:
  95. OSSL_PARAM_BLD_free_params(params);
  96. OSSL_PARAM_BLD_free(bld);
  97. OPENSSL_free(utf);
  98. BN_free(bn);
  99. BN_free(bn_res);
  100. return res;
  101. }
  102. static int template_private_test(void)
  103. {
  104. static int data1[] = { 2, 3, 5, 7, 11, 15, 17 };
  105. static unsigned char data2[] = { 2, 4, 6, 8, 10 };
  106. OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
  107. OSSL_PARAM *params = NULL, *p;
  108. unsigned int i;
  109. unsigned long int l;
  110. uint32_t i32;
  111. uint64_t i64;
  112. size_t st;
  113. BIGNUM *bn = NULL, *bn_res = NULL;
  114. int res = 0;
  115. if (!TEST_ptr(bld)
  116. || !TEST_true(OSSL_PARAM_BLD_push_uint(bld, "i", 6))
  117. || !TEST_true(OSSL_PARAM_BLD_push_ulong(bld, "l", 42))
  118. || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld, "i32", 1532))
  119. || !TEST_true(OSSL_PARAM_BLD_push_uint64(bld, "i64", 9999999))
  120. || !TEST_true(OSSL_PARAM_BLD_push_size_t(bld, "st", 65537))
  121. || !TEST_ptr(bn = BN_secure_new())
  122. || !TEST_true(BN_set_word(bn, 1729))
  123. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber", bn))
  124. || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, "oct_s", data1,
  125. sizeof(data1)))
  126. || !TEST_true(OSSL_PARAM_BLD_push_octet_ptr(bld, "oct_p", data2,
  127. sizeof(data2)))
  128. || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
  129. /* Check unsigned int */
  130. || !TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
  131. || !TEST_true(OSSL_PARAM_get_uint(p, &i))
  132. || !TEST_str_eq(p->key, "i")
  133. || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
  134. || !TEST_size_t_eq(p->data_size, sizeof(int))
  135. || !TEST_uint_eq(i, 6)
  136. /* Check unsigned int32 */
  137. || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32"))
  138. || !TEST_true(OSSL_PARAM_get_uint32(p, &i32))
  139. || !TEST_str_eq(p->key, "i32")
  140. || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
  141. || !TEST_size_t_eq(p->data_size, sizeof(int32_t))
  142. || !TEST_uint_eq((unsigned int)i32, 1532)
  143. /* Check unsigned int64 */
  144. || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64"))
  145. || !TEST_str_eq(p->key, "i64")
  146. || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
  147. || !TEST_size_t_eq(p->data_size, sizeof(int64_t))
  148. || !TEST_true(OSSL_PARAM_get_uint64(p, &i64))
  149. || !TEST_ulong_eq((unsigned long)i64, 9999999)
  150. /* Check unsigned long int */
  151. || !TEST_ptr(p = OSSL_PARAM_locate(params, "l"))
  152. || !TEST_str_eq(p->key, "l")
  153. || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
  154. || !TEST_size_t_eq(p->data_size, sizeof(unsigned long int))
  155. || !TEST_true(OSSL_PARAM_get_ulong(p, &l))
  156. || !TEST_ulong_eq(l, 42)
  157. /* Check size_t */
  158. || !TEST_ptr(p = OSSL_PARAM_locate(params, "st"))
  159. || !TEST_str_eq(p->key, "st")
  160. || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
  161. || !TEST_size_t_eq(p->data_size, sizeof(size_t))
  162. || !TEST_true(OSSL_PARAM_get_size_t(p, &st))
  163. || !TEST_size_t_eq(st, 65537)
  164. /* Check octet string */
  165. || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_s"))
  166. || !TEST_str_eq(p->key, "oct_s")
  167. || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_STRING)
  168. || !TEST_mem_eq(p->data, p->data_size, data1, sizeof(data1))
  169. /* Check octet pointer */
  170. || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_p"))
  171. || !TEST_str_eq(p->key, "oct_p")
  172. || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_PTR)
  173. || !TEST_mem_eq(*(void **)p->data, p->data_size, data2, sizeof(data2))
  174. /* Check BN */
  175. || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber"))
  176. || !TEST_str_eq(p->key, "bignumber")
  177. || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
  178. || !TEST_true(OSSL_PARAM_get_BN(p, &bn_res))
  179. || !TEST_int_eq(BN_cmp(bn_res, bn), 0))
  180. goto err;
  181. res = 1;
  182. err:
  183. OSSL_PARAM_BLD_free_params(params);
  184. OSSL_PARAM_BLD_free(bld);
  185. BN_free(bn);
  186. BN_free(bn_res);
  187. return res;
  188. }
  189. static int builder_limit_test(void)
  190. {
  191. const int n = 100;
  192. char names[100][3];
  193. OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
  194. OSSL_PARAM *params = NULL;
  195. int i, res = 0;
  196. if (!TEST_ptr(bld))
  197. goto err;
  198. for (i = 0; i < n; i++) {
  199. names[i][0] = 'A' + (i / 26) - 1;
  200. names[i][0] = 'a' + (i % 26) - 1;
  201. names[i][2] = '\0';
  202. if (!TEST_true(OSSL_PARAM_BLD_push_int(bld, names[i], 3 * i + 1)))
  203. goto err;
  204. }
  205. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  206. goto err;
  207. /* Count the elements in the params arrary, expecting n */
  208. for (i = 0; params[i].key != NULL; i++);
  209. if (!TEST_int_eq(i, n))
  210. goto err;
  211. /* Verify that the build, cleared the builder structure */
  212. OSSL_PARAM_BLD_free_params(params);
  213. params = NULL;
  214. if (!TEST_true(OSSL_PARAM_BLD_push_int(bld, "g", 2))
  215. || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  216. goto err;
  217. /* Count the elements in the params arrary, expecting 1 */
  218. for (i = 0; params[i].key != NULL; i++);
  219. if (!TEST_int_eq(i, 1))
  220. goto err;
  221. res = 1;
  222. err:
  223. OSSL_PARAM_BLD_free_params(params);
  224. OSSL_PARAM_BLD_free(bld);
  225. return res;
  226. }
  227. int setup_tests(void)
  228. {
  229. ADD_TEST(template_public_test);
  230. ADD_TEST(template_private_test);
  231. ADD_TEST(builder_limit_test);
  232. return 1;
  233. }