2 * Copyright 2019 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 "internal/param_build.h"
14 #include "internal/nelem.h"
17 static int template_public_test(void)
20 OSSL_PARAM
*params
= NULL
, *p
;
21 BIGNUM
*bn
= NULL
, *bn_res
= NULL
;
31 ossl_param_bld_init(&bld
);
32 if (!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
);
105 static int template_private_test(void)
107 static int data1
[] = { 2, 3, 5, 7, 11, 15, 17 };
108 static unsigned char data2
[] = { 2, 4, 6, 8, 10 };
110 OSSL_PARAM
*params
= NULL
, *p
;
116 BIGNUM
*bn
= NULL
, *bn_res
= NULL
;
119 ossl_param_bld_init(&bld
);
120 if (!TEST_true(ossl_param_bld_push_uint(&bld
, "i", 6))
121 || !TEST_true(ossl_param_bld_push_ulong(&bld
, "l", 42))
122 || !TEST_true(ossl_param_bld_push_uint32(&bld
, "i32", 1532))
123 || !TEST_true(ossl_param_bld_push_uint64(&bld
, "i64", 9999999))
124 || !TEST_true(ossl_param_bld_push_size_t(&bld
, "st", 65537))
125 || !TEST_ptr(bn
= BN_secure_new())
126 || !TEST_true(BN_set_word(bn
, 1729))
127 || !TEST_true(ossl_param_bld_push_BN(&bld
, "bignumber", bn
))
128 || !TEST_true(ossl_param_bld_push_octet_string(&bld
, "oct_s", data1
,
130 || !TEST_true(ossl_param_bld_push_octet_ptr(&bld
, "oct_p", data2
,
132 || !TEST_ptr(params
= ossl_param_bld_to_param(&bld
))
133 /* Check unsigned int */
134 || !TEST_ptr(p
= OSSL_PARAM_locate(params
, "i"))
135 || !TEST_true(OSSL_PARAM_get_uint(p
, &i
))
136 || !TEST_str_eq(p
->key
, "i")
137 || !TEST_uint_eq(p
->data_type
, OSSL_PARAM_UNSIGNED_INTEGER
)
138 || !TEST_size_t_eq(p
->data_size
, sizeof(int))
139 || !TEST_uint_eq(i
, 6)
140 /* Check unsigned int32 */
141 || !TEST_ptr(p
= OSSL_PARAM_locate(params
, "i32"))
142 || !TEST_true(OSSL_PARAM_get_uint32(p
, &i32
))
143 || !TEST_str_eq(p
->key
, "i32")
144 || !TEST_uint_eq(p
->data_type
, OSSL_PARAM_UNSIGNED_INTEGER
)
145 || !TEST_size_t_eq(p
->data_size
, sizeof(int32_t))
146 || !TEST_uint_eq((unsigned int)i32
, 1532)
147 /* Check unsigned int64 */
148 || !TEST_ptr(p
= OSSL_PARAM_locate(params
, "i64"))
149 || !TEST_str_eq(p
->key
, "i64")
150 || !TEST_uint_eq(p
->data_type
, OSSL_PARAM_UNSIGNED_INTEGER
)
151 || !TEST_size_t_eq(p
->data_size
, sizeof(int64_t))
152 || !TEST_true(OSSL_PARAM_get_uint64(p
, &i64
))
153 || !TEST_ulong_eq((unsigned long)i64
, 9999999)
154 /* Check unsigned long int */
155 || !TEST_ptr(p
= OSSL_PARAM_locate(params
, "l"))
156 || !TEST_str_eq(p
->key
, "l")
157 || !TEST_uint_eq(p
->data_type
, OSSL_PARAM_UNSIGNED_INTEGER
)
158 || !TEST_size_t_eq(p
->data_size
, sizeof(unsigned long int))
159 || !TEST_true(OSSL_PARAM_get_ulong(p
, &l
))
160 || !TEST_ulong_eq(l
, 42)
162 || !TEST_ptr(p
= OSSL_PARAM_locate(params
, "st"))
163 || !TEST_str_eq(p
->key
, "st")
164 || !TEST_uint_eq(p
->data_type
, OSSL_PARAM_UNSIGNED_INTEGER
)
165 || !TEST_size_t_eq(p
->data_size
, sizeof(size_t))
166 || !TEST_true(OSSL_PARAM_get_size_t(p
, &st
))
167 || !TEST_size_t_eq(st
, 65537)
168 /* Check octet string */
169 || !TEST_ptr(p
= OSSL_PARAM_locate(params
, "oct_s"))
170 || !TEST_str_eq(p
->key
, "oct_s")
171 || !TEST_uint_eq(p
->data_type
, OSSL_PARAM_OCTET_STRING
)
172 || !TEST_mem_eq(p
->data
, p
->data_size
, data1
, sizeof(data1
))
173 /* Check octet pointer */
174 || !TEST_ptr(p
= OSSL_PARAM_locate(params
, "oct_p"))
175 || !TEST_str_eq(p
->key
, "oct_p")
176 || !TEST_uint_eq(p
->data_type
, OSSL_PARAM_OCTET_PTR
)
177 || !TEST_mem_eq(*(void **)p
->data
, p
->data_size
, data2
, sizeof(data2
))
179 || !TEST_ptr(p
= OSSL_PARAM_locate(params
, "bignumber"))
180 || !TEST_str_eq(p
->key
, "bignumber")
181 || !TEST_uint_eq(p
->data_type
, OSSL_PARAM_UNSIGNED_INTEGER
)
182 || !TEST_true(OSSL_PARAM_get_BN(p
, &bn_res
))
183 || !TEST_int_eq(BN_cmp(bn_res
, bn
), 0))
187 ossl_param_bld_free(params
);
193 static int template_static_params_test(int n
)
195 unsigned char data
[1000], secure
[500];
197 OSSL_PARAM params
[20], *p
;
198 BIGNUM
*bn
= NULL
, *bn_r
= NULL
;
203 ossl_param_bld_init(&bld
);
204 if (!TEST_true(ossl_param_bld_push_uint(&bld
, "i", 6))
205 || !TEST_ptr(bn
= (n
& 1) == 0 ? BN_new() : BN_secure_new())
206 || !TEST_true(BN_set_word(bn
, 1337))
207 || !TEST_true(ossl_param_bld_push_BN(&bld
, "bn", bn
))
208 || !TEST_true(ossl_param_bld_push_utf8_string(&bld
, "utf8_s", "bar",
210 || !TEST_ptr(ossl_param_bld_to_param_ex(&bld
, params
,
213 secure
, sizeof(secure
)))
214 /* Check unsigned int */
215 || !TEST_ptr(p
= OSSL_PARAM_locate(params
, "i"))
216 || !TEST_true(OSSL_PARAM_get_uint(p
, &i
))
217 || !TEST_str_eq(p
->key
, "i")
218 || !TEST_uint_eq(p
->data_type
, OSSL_PARAM_UNSIGNED_INTEGER
)
219 || !TEST_size_t_eq(p
->data_size
, sizeof(int))
220 || !TEST_uint_eq(i
, 6)
222 || !TEST_ptr(p
= OSSL_PARAM_locate(params
, "bn"))
223 || !TEST_true(OSSL_PARAM_get_BN(p
, &bn_r
))
224 || !TEST_str_eq(p
->key
, "bn")
225 || !TEST_uint_eq(p
->data_type
, OSSL_PARAM_UNSIGNED_INTEGER
)
226 || !TEST_size_t_le(p
->data_size
, sizeof(BN_ULONG
))
227 || !TEST_uint_eq((unsigned int)BN_get_word(bn_r
), 1337)
228 /* Check UTF8 string */
229 || !TEST_ptr(p
= OSSL_PARAM_locate(params
, "utf8_s"))
230 || !TEST_str_eq(p
->data
, "bar")
231 || !TEST_true(OSSL_PARAM_get_utf8_string(p
, &utf
, 0))
232 || !TEST_str_eq(utf
, "bar"))
242 static int template_static_fail_test(int n
)
244 unsigned char data
[10000], secure
[500];
250 ossl_param_bld_init(&bld
);
251 if (!TEST_true(ossl_param_bld_push_uint(&bld
, "i", 6))
252 || !TEST_ptr(bn
= (n
& 1) == 0 ? BN_new() : BN_secure_new())
253 || !TEST_true(BN_hex2bn(&bn
, "ABCDEF78901234567890ABCDEF0987987654321"))
254 || !TEST_true(ossl_param_bld_push_BN(&bld
, "bn", bn
))
255 || !TEST_true(ossl_param_bld_push_utf8_string(&bld
, "utf8_s", "abc",
258 || !TEST_ptr_null(ossl_param_bld_to_param_ex(&bld
, NULL
, 0, data
,
259 sizeof(data
), secure
,
261 /* Short OSSL_PARAMS */
262 || !TEST_ptr_null(ossl_param_bld_to_param_ex(&bld
, prms
, 2,
264 secure
, sizeof(secure
)))
266 || !TEST_ptr_null(ossl_param_bld_to_param_ex(&bld
, prms
,
270 /* Not enough normal data */
271 || !TEST_ptr_null(ossl_param_bld_to_param_ex(&bld
, prms
,
278 if (!TEST_ptr_null(ossl_param_bld_to_param_ex(&bld
, prms
,
282 /* Not enough secure data */
283 || !TEST_ptr_null(ossl_param_bld_to_param_ex(&bld
, prms
,
295 int setup_tests(void)
297 ADD_TEST(template_public_test
);
298 ADD_TEST(template_private_test
);
299 ADD_ALL_TESTS(template_static_params_test
, 2);
300 ADD_ALL_TESTS(template_static_fail_test
, 2);