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