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