d2cf78dc972e2d56316a4815347677869d0430b2
[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     char *utf = NULL;
28     const char *cutf;
29     int res = 0;
30
31     if (!TEST_ptr(bld)
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",
41                                                       sizeof("foo")))
42         || !TEST_true(OSSL_PARAM_BLD_push_utf8_ptr(bld, "utf8_p", "bar-boom",
43                                                    0))
44         || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
45         /* Check int */
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)
52         /* Check int32 */
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)
59         /* Check int64 */
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)
66         /* Check long */
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)
73         /* Check double */
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")
89         /* Check BN */
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))
95         goto err;
96     res = 1;
97 err:
98     OSSL_PARAM_BLD_free_params(params);
99     OSSL_PARAM_BLD_free(bld);
100     OPENSSL_free(utf);
101     BN_free(bn);
102     BN_free(bn_res);
103     return res;
104 }
105
106 static int template_private_test(void)
107 {
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;
112     unsigned int i;
113     unsigned long int l;
114     uint32_t i32;
115     uint64_t i64;
116     size_t st;
117     BIGNUM *bn = NULL, *bn_res = NULL;
118     int res = 0;
119
120     if (!TEST_ptr(bld)
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,
130                                                        sizeof(data1)))
131         || !TEST_true(OSSL_PARAM_BLD_push_octet_ptr(bld, "oct_p", data2,
132                                                     sizeof(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)
162         /* Check size_t */
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))
179         /* Check BN */
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))
185         goto err;
186     res = 1;
187 err:
188     OSSL_PARAM_BLD_free_params(params);
189     OSSL_PARAM_BLD_free(bld);
190     BN_free(bn);
191     BN_free(bn_res);
192     return res;
193 }
194
195 static int builder_limit_test(void)
196 {
197     const int n = 100;
198     char names[100][3];
199     OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
200     OSSL_PARAM *params = NULL;
201     int i, res = 0;
202
203     if (!TEST_ptr(bld))
204         goto err;
205     
206     for (i = 0; i < n; i++) {
207         names[i][0] = 'A' + (i / 26) - 1;
208         names[i][1] = 'a' + (i % 26) - 1;
209         names[i][2] = '\0';
210         if (!TEST_true(OSSL_PARAM_BLD_push_int(bld, names[i], 3 * i + 1)))
211             goto err;
212     }
213     if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
214         goto err;
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))
218         goto err;
219
220     /* Verify that the build, cleared the builder structure */
221     OSSL_PARAM_BLD_free_params(params);
222     params = NULL;
223
224     if (!TEST_true(OSSL_PARAM_BLD_push_int(bld, "g", 2))
225         || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
226         goto err;
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))
230         goto err;
231     res = 1;
232 err:
233     OSSL_PARAM_BLD_free_params(params);
234     OSSL_PARAM_BLD_free(bld);
235     return res;
236 }
237
238 int setup_tests(void)
239 {
240     ADD_TEST(template_public_test);
241     ADD_TEST(template_private_test);
242     ADD_TEST(builder_limit_test);
243     return 1;
244 }