param_build_test.c 9.8 KB

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