2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
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
12 #include <openssl/params.h>
13 #include "openssl/param_build.h"
14 #include "internal/nelem.h"
17 static int template_public_test(void)
19 OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
20 OSSL_PARAM *params = NULL, *p;
21 BIGNUM *bn = NULL, *bn_res = NULL;
32 || !TEST_true(OSSL_PARAM_BLD_push_int(bld, "i", -6))
33 || !TEST_true(OSSL_PARAM_BLD_push_long(bld, "l", 42))
34 || !TEST_true(OSSL_PARAM_BLD_push_int32(bld, "i32", 1532))
35 || !TEST_true(OSSL_PARAM_BLD_push_int64(bld, "i64", -9999999))
36 || !TEST_true(OSSL_PARAM_BLD_push_double(bld, "d", 1.61803398875))
37 || !TEST_ptr(bn = BN_new())
38 || !TEST_true(BN_set_word(bn, 1729))
39 || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber", bn))
40 || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, "utf8_s", "foo",
42 || !TEST_true(OSSL_PARAM_BLD_push_utf8_ptr(bld, "utf8_p", "bar-boom",
44 || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
46 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
47 || !TEST_true(OSSL_PARAM_get_int(p, &i))
48 || !TEST_str_eq(p->key, "i")
49 || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
50 || !TEST_size_t_eq(p->data_size, sizeof(int))
51 || !TEST_int_eq(i, -6)
53 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32"))
54 || !TEST_true(OSSL_PARAM_get_int32(p, &i32))
55 || !TEST_str_eq(p->key, "i32")
56 || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
57 || !TEST_size_t_eq(p->data_size, sizeof(int32_t))
58 || !TEST_int_eq((int)i32, 1532)
60 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64"))
61 || !TEST_str_eq(p->key, "i64")
62 || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
63 || !TEST_size_t_eq(p->data_size, sizeof(int64_t))
64 || !TEST_true(OSSL_PARAM_get_int64(p, &i64))
65 || !TEST_long_eq((long)i64, -9999999)
67 || !TEST_ptr(p = OSSL_PARAM_locate(params, "l"))
68 || !TEST_str_eq(p->key, "l")
69 || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
70 || !TEST_size_t_eq(p->data_size, sizeof(long int))
71 || !TEST_true(OSSL_PARAM_get_long(p, &l))
72 || !TEST_long_eq(l, 42)
74 || !TEST_ptr(p = OSSL_PARAM_locate(params, "d"))
75 || !TEST_true(OSSL_PARAM_get_double(p, &d))
76 || !TEST_str_eq(p->key, "d")
77 || !TEST_uint_eq(p->data_type, OSSL_PARAM_REAL)
78 || !TEST_size_t_eq(p->data_size, sizeof(double))
79 || !TEST_double_eq(d, 1.61803398875)
80 /* Check UTF8 string */
81 || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_s"))
82 || !TEST_str_eq(p->data, "foo")
83 || !TEST_true(OSSL_PARAM_get_utf8_string(p, &utf, 0))
84 || !TEST_str_eq(utf, "foo")
85 /* Check UTF8 pointer */
86 || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_p"))
87 || !TEST_true(OSSL_PARAM_get_utf8_ptr(p, &cutf))
88 || !TEST_str_eq(cutf, "bar-boom")
90 || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber"))
91 || !TEST_str_eq(p->key, "bignumber")
92 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
93 || !TEST_true(OSSL_PARAM_get_BN(p, &bn_res))
94 || !TEST_int_eq(BN_cmp(bn_res, bn), 0))
98 OSSL_PARAM_BLD_free_params(params);
99 OSSL_PARAM_BLD_free(bld);
106 static int template_private_test(void)
108 static int data1[] = { 2, 3, 5, 7, 11, 15, 17 };
109 static unsigned char data2[] = { 2, 4, 6, 8, 10 };
110 OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
111 OSSL_PARAM *params = NULL, *p;
117 BIGNUM *bn = NULL, *bn_res = NULL;
121 || !TEST_true(OSSL_PARAM_BLD_push_uint(bld, "i", 6))
122 || !TEST_true(OSSL_PARAM_BLD_push_ulong(bld, "l", 42))
123 || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld, "i32", 1532))
124 || !TEST_true(OSSL_PARAM_BLD_push_uint64(bld, "i64", 9999999))
125 || !TEST_true(OSSL_PARAM_BLD_push_size_t(bld, "st", 65537))
126 || !TEST_ptr(bn = BN_secure_new())
127 || !TEST_true(BN_set_word(bn, 1729))
128 || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber", bn))
129 || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, "oct_s", data1,
131 || !TEST_true(OSSL_PARAM_BLD_push_octet_ptr(bld, "oct_p", data2,
133 || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
134 /* Check unsigned int */
135 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
136 || !TEST_true(OSSL_PARAM_get_uint(p, &i))
137 || !TEST_str_eq(p->key, "i")
138 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
139 || !TEST_size_t_eq(p->data_size, sizeof(int))
140 || !TEST_uint_eq(i, 6)
141 /* Check unsigned int32 */
142 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32"))
143 || !TEST_true(OSSL_PARAM_get_uint32(p, &i32))
144 || !TEST_str_eq(p->key, "i32")
145 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
146 || !TEST_size_t_eq(p->data_size, sizeof(int32_t))
147 || !TEST_uint_eq((unsigned int)i32, 1532)
148 /* Check unsigned int64 */
149 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64"))
150 || !TEST_str_eq(p->key, "i64")
151 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
152 || !TEST_size_t_eq(p->data_size, sizeof(int64_t))
153 || !TEST_true(OSSL_PARAM_get_uint64(p, &i64))
154 || !TEST_ulong_eq((unsigned long)i64, 9999999)
155 /* Check unsigned long int */
156 || !TEST_ptr(p = OSSL_PARAM_locate(params, "l"))
157 || !TEST_str_eq(p->key, "l")
158 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
159 || !TEST_size_t_eq(p->data_size, sizeof(unsigned long int))
160 || !TEST_true(OSSL_PARAM_get_ulong(p, &l))
161 || !TEST_ulong_eq(l, 42)
163 || !TEST_ptr(p = OSSL_PARAM_locate(params, "st"))
164 || !TEST_str_eq(p->key, "st")
165 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
166 || !TEST_size_t_eq(p->data_size, sizeof(size_t))
167 || !TEST_true(OSSL_PARAM_get_size_t(p, &st))
168 || !TEST_size_t_eq(st, 65537)
169 /* Check octet string */
170 || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_s"))
171 || !TEST_str_eq(p->key, "oct_s")
172 || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_STRING)
173 || !TEST_mem_eq(p->data, p->data_size, data1, sizeof(data1))
174 /* Check octet pointer */
175 || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_p"))
176 || !TEST_str_eq(p->key, "oct_p")
177 || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_PTR)
178 || !TEST_mem_eq(*(void **)p->data, p->data_size, data2, sizeof(data2))
180 || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber"))
181 || !TEST_str_eq(p->key, "bignumber")
182 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
183 || !TEST_true(OSSL_PARAM_get_BN(p, &bn_res))
184 || !TEST_int_eq(BN_cmp(bn_res, bn), 0))
188 OSSL_PARAM_BLD_free_params(params);
189 OSSL_PARAM_BLD_free(bld);
195 static int builder_limit_test(void)
199 OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
200 OSSL_PARAM *params = NULL;
206 for (i = 0; i < n; i++) {
207 names[i][0] = 'A' + (i / 26) - 1;
208 names[i][1] = 'a' + (i % 26) - 1;
210 if (!TEST_true(OSSL_PARAM_BLD_push_int(bld, names[i], 3 * i + 1)))
213 if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
215 /* Count the elements in the params arrary, expecting n */
216 for (i = 0; params[i].key != NULL; i++);
217 if (!TEST_int_eq(i, n))
220 /* Verify that the build, cleared the builder structure */
221 OSSL_PARAM_BLD_free_params(params);
224 if (!TEST_true(OSSL_PARAM_BLD_push_int(bld, "g", 2))
225 || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
227 /* Count the elements in the params arrary, expecting 1 */
228 for (i = 0; params[i].key != NULL; i++);
229 if (!TEST_int_eq(i, 1))
233 OSSL_PARAM_BLD_free_params(params);
234 OSSL_PARAM_BLD_free(bld);
238 int setup_tests(void)
240 ADD_TEST(template_public_test);
241 ADD_TEST(template_private_test);
242 ADD_TEST(builder_limit_test);