chunk 7 of CMP contribution to OpenSSL
[openssl.git] / test / param_build_test.c
1 /*
2  * Copyright 2019 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 "internal/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;
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     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",
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);
99     OPENSSL_free(utf);
100     BN_free(bn);
101     BN_free(bn_res);
102     return res;
103 }
104
105 static int template_private_test(void)
106 {
107     static int data1[] = { 2, 3, 5, 7, 11, 15, 17 };
108     static unsigned char data2[] = { 2, 4, 6, 8, 10 };
109     OSSL_PARAM_BLD bld;
110     OSSL_PARAM *params = NULL, *p;
111     unsigned int i;
112     unsigned long int l;
113     uint32_t i32;
114     uint64_t i64;
115     size_t st;
116     BIGNUM *bn = NULL, *bn_res = NULL;
117     int res = 0;
118
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,
129                                                        sizeof(data1)))
130         || !TEST_true(ossl_param_bld_push_octet_ptr(&bld, "oct_p", data2,
131                                                     sizeof(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)
161         /* Check size_t */
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))
178         /* Check BN */
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))
184         goto err;
185     res = 1;
186 err:
187     ossl_param_bld_free(params);
188     BN_free(bn);
189     BN_free(bn_res);
190     return res;
191 }
192
193 int setup_tests(void)
194 {
195     ADD_TEST(template_public_test);
196     ADD_TEST(template_private_test);
197     return 1;
198 }