EVP_Digest*: enable SHA3 pre-hashing for DSA
[openssl.git] / test / params_conversion_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 <inttypes.h>
13 #include <openssl/params.h>
14 #include "testutil.h"
15
16 #ifdef OPENSSL_SYS_WINDOWS
17 # define strcasecmp _stricmp
18 #endif
19
20 typedef struct {
21     const OSSL_PARAM *param;
22     int32_t i32;
23     int64_t i64;
24     uint32_t u32;
25     uint64_t u64;
26     double d;
27     int valid_i32, valid_i64, valid_u32, valid_u64, valid_d;
28     void *ref, *datum;
29     size_t size;
30 } PARAM_CONVERSION;
31
32 static int param_conversion_load_stanza(PARAM_CONVERSION *pc, const STANZA *s)
33 {
34
35     static int32_t datum_i32, ref_i32;
36     static int64_t datum_i64, ref_i64;
37     static uint32_t datum_u32, ref_u32;
38     static uint64_t datum_u64, ref_u64;
39     static double datum_d, ref_d;
40     static const OSSL_PARAM params[] = {
41         OSSL_PARAM_int32("int32",   &datum_i32),
42         OSSL_PARAM_int64("int64",   &datum_i64),
43         OSSL_PARAM_uint32("uint32", &datum_u32),
44         OSSL_PARAM_uint64("uint64", &datum_u64),
45         OSSL_PARAM_double("double", &datum_d),
46         OSSL_PARAM_END
47     };
48     int def_i32 = 0, def_i64 = 0, def_u32 = 0, def_u64 = 0, def_d = 0;
49     const PAIR *pp = s->pairs;
50     const char *type = NULL;
51     char *p;
52     int i;
53
54     memset(pc, 0, sizeof(*pc));
55
56     for (i = 0; i < s->numpairs; i++, pp++) {
57         p = "";
58         if (strcasecmp(pp->key, "type") == 0) {
59             if (type != NULL) {
60                 TEST_info("Line %d: multiple type lines", s->curr);
61                 return 0;
62             }
63             pc->param = OSSL_PARAM_locate(params, type = pp->value);
64             if (pc->param == NULL) {
65                 TEST_info("Line %d: unknown type line", s->curr);
66                 return 0;
67             }
68         } else if (strcasecmp(pp->key, "int32") == 0) {
69             if (def_i32++) {
70                 TEST_info("Line %d: multiple int32 lines", s->curr);
71                 return 0;
72             }
73             if (strcasecmp(pp->value, "invalid") != 0) {
74                 pc->valid_i32 = 1;
75                 pc->i32 = (int32_t)strtoimax(pp->value, &p, 10);
76             }
77         } else if (strcasecmp(pp->key, "int64") == 0) {
78             if (def_i64++) {
79                 TEST_info("Line %d: multiple int64 lines", s->curr);
80                 return 0;
81             }
82             if (strcasecmp(pp->value, "invalid") != 0) {
83                 pc->valid_i64 = 1;
84                 pc->i64 = (int64_t)strtoimax(pp->value, &p, 10);
85             }
86         } else if (strcasecmp(pp->key, "uint32") == 0) {
87             if (def_u32++) {
88                 TEST_info("Line %d: multiple uint32 lines", s->curr);
89                 return 0;
90             }
91             if (strcasecmp(pp->value, "invalid") != 0) {
92                 pc->valid_u32 = 1;
93                 pc->u32 = (uint32_t)strtoumax(pp->value, &p, 10);
94             }
95         } else if (strcasecmp(pp->key, "uint64") == 0) {
96             if (def_u64++) {
97                 TEST_info("Line %d: multiple uint64 lines", s->curr);
98                 return 0;
99             }
100             if (strcasecmp(pp->value, "invalid") != 0) {
101                 pc->valid_u64 = 1;
102                 pc->u64 = (uint64_t)strtoumax(pp->value, &p, 10);
103             }
104         } else if (strcasecmp(pp->key, "double") == 0) {
105             if (def_d++) {
106                 TEST_info("Line %d: multiple double lines", s->curr);
107                 return 0;
108             }
109             if (strcasecmp(pp->value, "invalid") != 0) {
110                 pc->valid_d = 1;
111                 pc->d = strtod(pp->value, &p);
112             }
113         } else {
114             TEST_info("Line %d: unknown keyword %s", s->curr, pp->key);
115             return 0;
116         }
117         if (*p != '\0') {
118             TEST_info("Line %d: extra characters at end '%s' for %s",
119                       s->curr, p, pp->key);
120             return 0;
121         }
122     }
123
124     if (!TEST_ptr(type)) {
125         TEST_info("Line %d: type not found", s->curr);
126         return 0;
127     }
128
129     if (strcasecmp(type, "int32") == 0) {
130         if (!TEST_true(def_i32) || !TEST_true(pc->valid_i32)) {
131             TEST_note("errant int32 on line %d", s->curr);
132             return 0;
133         }
134         datum_i32 = ref_i32 = pc->i32;
135         pc->datum = &datum_i32;
136         pc->ref = &ref_i32;
137         pc->size = sizeof(ref_i32);
138     } else if (strcasecmp(type, "int64") == 0) {
139         if (!TEST_true(def_i64) || !TEST_true(pc->valid_i64)) {
140             TEST_note("errant int64 on line %d", s->curr);
141             return 0;
142         }
143         datum_i64 = ref_i64 = pc->i64;
144         pc->datum = &datum_i64;
145         pc->ref = &ref_i64;
146         pc->size = sizeof(ref_i64);
147     } else if (strcasecmp(type, "uint32") == 0) {
148         if (!TEST_true(def_u32) || !TEST_true(pc->valid_u32)) {
149             TEST_note("errant uint32 on line %d", s->curr);
150             return 0;
151         }
152         datum_u32 = ref_u32 = pc->u32;
153         pc->datum = &datum_u32;
154         pc->ref = &ref_u32;
155         pc->size = sizeof(ref_u32);
156     } else if (strcasecmp(type, "uint64") == 0) {
157         if (!TEST_true(def_u64) || !TEST_true(pc->valid_u64)) {
158             TEST_note("errant uint64 on line %d", s->curr);
159             return 0;
160         }
161         datum_u64 = ref_u64 = pc->u64;
162         pc->datum = &datum_u64;
163         pc->ref = &ref_u64;
164         pc->size = sizeof(ref_u64);
165     } else if (strcasecmp(type, "double") == 0) {
166         if (!TEST_true(def_d) || !TEST_true(pc->valid_d)) {
167             TEST_note("errant double on line %d", s->curr);
168             return 0;
169         }
170         datum_d = ref_d = pc->d;
171         pc->datum = &datum_d;
172         pc->ref = &ref_d;
173         pc->size = sizeof(ref_d);
174     } else {
175         TEST_error("type unknown at line %d", s->curr);
176         return 0;
177     }
178     return 1;
179 }
180
181 static int param_conversion_test(const PARAM_CONVERSION *pc, int line)
182 {
183     int32_t i32;
184     int64_t i64;
185     uint32_t u32;
186     uint64_t u64;
187     double d;
188
189     if (!pc->valid_i32) {
190         if (!TEST_false(OSSL_PARAM_get_int32(pc->param, &i32))) {
191             TEST_note("unexpected valid conversion to int32 on line %d", line);
192             return 0;
193         }
194     } else {
195         if (!TEST_true(OSSL_PARAM_get_int32(pc->param, &i32))
196             || !TEST_true(i32 == pc->i32)) {
197             TEST_note("unexpected conversion to int32 on line %d", line);
198             return 0;
199         }
200         memset(pc->datum, 44, pc->size);
201         if (!TEST_true(OSSL_PARAM_set_int32(pc->param, i32))
202             || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
203             TEST_note("unexpected valid conversion from int32 on line %d",
204                       line);
205             return 0;
206         }
207     }
208
209     if (!pc->valid_i64) {
210         if (!TEST_false(OSSL_PARAM_get_int64(pc->param, &i64))) {
211             TEST_note("unexpected valid conversion to int64 on line %d", line);
212             return 0;
213         }
214     } else {
215         if (!TEST_true(OSSL_PARAM_get_int64(pc->param, &i64))
216             || !TEST_true(i64 == pc->i64)) {
217             TEST_note("unexpected conversion to int64 on line %d", line);
218             return 0;
219         }
220         memset(pc->datum, 44, pc->size);
221         if (!TEST_true(OSSL_PARAM_set_int64(pc->param, i64))
222             || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
223             TEST_note("unexpected valid conversion from int64 on line %d",
224                       line);
225             return 0;
226         }
227     }
228
229     if (!pc->valid_u32) {
230         if (!TEST_false(OSSL_PARAM_get_uint32(pc->param, &u32))) {
231             TEST_note("unexpected valid conversion to uint32 on line %d", line);
232             return 0;
233         }
234     } else {
235         if (!TEST_true(OSSL_PARAM_get_uint32(pc->param, &u32))
236             || !TEST_true(u32 == pc->u32)) {
237             TEST_note("unexpected conversion to uint32 on line %d", line);
238             return 0;
239         }
240         memset(pc->datum, 44, pc->size);
241         if (!TEST_true(OSSL_PARAM_set_uint32(pc->param, u32))
242             || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
243             TEST_note("unexpected valid conversion from uint32 on line %d",
244                       line);
245             return 0;
246         }
247     }
248
249     if (!pc->valid_u64) {
250         if (!TEST_false(OSSL_PARAM_get_uint64(pc->param, &u64))) {
251             TEST_note("unexpected valid conversion to uint64 on line %d", line);
252             return 0;
253         }
254     } else {
255         if (!TEST_true(OSSL_PARAM_get_uint64(pc->param, &u64))
256             || !TEST_true(u64 == pc->u64)) {
257             TEST_note("unexpected conversion to uint64 on line %d", line);
258             return 0;
259         }
260         memset(pc->datum, 44, pc->size);
261         if (!TEST_true(OSSL_PARAM_set_uint64(pc->param, u64))
262             || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
263             TEST_note("unexpected valid conversion from uint64 on line %d",
264                       line);
265             return 0;
266         }
267     }
268
269     if (!pc->valid_d) {
270         if (!TEST_false(OSSL_PARAM_get_double(pc->param, &d))) {
271             TEST_note("unexpected valid conversion to double on line %d", line);
272             return 0;
273         }
274     } else {
275         if (!TEST_true(OSSL_PARAM_get_double(pc->param, &d))
276             || !TEST_true(d == pc->d)) {
277             TEST_note("unexpected conversion to double on line %d", line);
278             return 0;
279         }
280         memset(pc->datum, 44, pc->size);
281         if (!TEST_true(OSSL_PARAM_set_double(pc->param, d))
282             || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
283             TEST_note("unexpected valid conversion from double on line %d",
284                       line);
285             return 0;
286         }
287     }
288
289     return 1;
290 }
291
292 static int run_param_file_tests(int i)
293 {
294     STANZA *s;
295     PARAM_CONVERSION pc;
296     const char *testfile = test_get_argument(i);
297     int res = 1;
298
299     if (!TEST_ptr(s = OPENSSL_zalloc(sizeof(*s))))
300         return 0;
301     if (!test_start_file(s, testfile)) {
302         OPENSSL_free(s);
303         return 0;
304     }
305
306     while (!BIO_eof(s->fp)) {
307         if (!test_readstanza(s)) {
308             res = 0;
309             goto end;
310         }
311         if (s->numpairs != 0)
312             if (!param_conversion_load_stanza(&pc, s)
313                 || !param_conversion_test(&pc, s->curr))
314                 res = 0;
315         test_clearstanza(s);
316     }
317 end:
318     test_end_file(s);
319     OPENSSL_free(s);
320     return res;
321 }
322
323 OPT_TEST_DECLARE_USAGE("file...\n")
324
325 int setup_tests(void)
326 {
327     size_t n = test_get_argument_count();
328
329     if (n == 0)
330         return 0;
331
332     ADD_ALL_TESTS(run_param_file_tests, n);
333     return 1;
334 }