]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/params_conversion_test.c
2 * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
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
12 #include <openssl/params.h>
15 /* On machines that dont support <inttypes.h> just disable the tests */
16 #if !defined(OPENSSL_NO_INTTYPES_H)
18 # ifdef OPENSSL_SYS_VMS
19 # define strtoumax strtoull
20 # define strtoimax strtoll
30 int valid_i32
, valid_i64
, valid_u32
, valid_u64
, valid_d
;
35 static int param_conversion_load_stanza(PARAM_CONVERSION
*pc
, const STANZA
*s
)
38 static int32_t datum_i32
, ref_i32
;
39 static int64_t datum_i64
, ref_i64
;
40 static uint32_t datum_u32
, ref_u32
;
41 static uint64_t datum_u64
, ref_u64
;
42 static double datum_d
, ref_d
;
43 static OSSL_PARAM params
[] = {
44 OSSL_PARAM_int32("int32", &datum_i32
),
45 OSSL_PARAM_int64("int64", &datum_i64
),
46 OSSL_PARAM_uint32("uint32", &datum_u32
),
47 OSSL_PARAM_uint64("uint64", &datum_u64
),
48 OSSL_PARAM_double("double", &datum_d
),
51 int def_i32
= 0, def_i64
= 0, def_u32
= 0, def_u64
= 0, def_d
= 0;
52 const PAIR
*pp
= s
->pairs
;
53 const char *type
= NULL
;
57 memset(pc
, 0, sizeof(*pc
));
59 for (i
= 0; i
< s
->numpairs
; i
++, pp
++) {
61 if (OPENSSL_strcasecmp(pp
->key
, "type") == 0) {
63 TEST_info("Line %d: multiple type lines", s
->curr
);
66 pc
->param
= OSSL_PARAM_locate(params
, type
= pp
->value
);
67 if (pc
->param
== NULL
) {
68 TEST_info("Line %d: unknown type line", s
->curr
);
71 } else if (OPENSSL_strcasecmp(pp
->key
, "int32") == 0) {
73 TEST_info("Line %d: multiple int32 lines", s
->curr
);
76 if (OPENSSL_strcasecmp(pp
->value
, "invalid") != 0) {
78 pc
->i32
= (int32_t)strtoimax(pp
->value
, &p
, 10);
80 } else if (OPENSSL_strcasecmp(pp
->key
, "int64") == 0) {
82 TEST_info("Line %d: multiple int64 lines", s
->curr
);
85 if (OPENSSL_strcasecmp(pp
->value
, "invalid") != 0) {
87 pc
->i64
= (int64_t)strtoimax(pp
->value
, &p
, 10);
89 } else if (OPENSSL_strcasecmp(pp
->key
, "uint32") == 0) {
91 TEST_info("Line %d: multiple uint32 lines", s
->curr
);
94 if (OPENSSL_strcasecmp(pp
->value
, "invalid") != 0) {
96 pc
->u32
= (uint32_t)strtoumax(pp
->value
, &p
, 10);
98 } else if (OPENSSL_strcasecmp(pp
->key
, "uint64") == 0) {
100 TEST_info("Line %d: multiple uint64 lines", s
->curr
);
103 if (OPENSSL_strcasecmp(pp
->value
, "invalid") != 0) {
105 pc
->u64
= (uint64_t)strtoumax(pp
->value
, &p
, 10);
107 } else if (OPENSSL_strcasecmp(pp
->key
, "double") == 0) {
109 TEST_info("Line %d: multiple double lines", s
->curr
);
112 if (OPENSSL_strcasecmp(pp
->value
, "invalid") != 0) {
114 pc
->d
= strtod(pp
->value
, &p
);
117 TEST_info("Line %d: unknown keyword %s", s
->curr
, pp
->key
);
121 TEST_info("Line %d: extra characters at end '%s' for %s",
122 s
->curr
, p
, pp
->key
);
127 if (!TEST_ptr(type
)) {
128 TEST_info("Line %d: type not found", s
->curr
);
132 if (OPENSSL_strcasecmp(type
, "int32") == 0) {
133 if (!TEST_true(def_i32
) || !TEST_true(pc
->valid_i32
)) {
134 TEST_note("errant int32 on line %d", s
->curr
);
137 datum_i32
= ref_i32
= pc
->i32
;
138 pc
->datum
= &datum_i32
;
140 pc
->size
= sizeof(ref_i32
);
141 } else if (OPENSSL_strcasecmp(type
, "int64") == 0) {
142 if (!TEST_true(def_i64
) || !TEST_true(pc
->valid_i64
)) {
143 TEST_note("errant int64 on line %d", s
->curr
);
146 datum_i64
= ref_i64
= pc
->i64
;
147 pc
->datum
= &datum_i64
;
149 pc
->size
= sizeof(ref_i64
);
150 } else if (OPENSSL_strcasecmp(type
, "uint32") == 0) {
151 if (!TEST_true(def_u32
) || !TEST_true(pc
->valid_u32
)) {
152 TEST_note("errant uint32 on line %d", s
->curr
);
155 datum_u32
= ref_u32
= pc
->u32
;
156 pc
->datum
= &datum_u32
;
158 pc
->size
= sizeof(ref_u32
);
159 } else if (OPENSSL_strcasecmp(type
, "uint64") == 0) {
160 if (!TEST_true(def_u64
) || !TEST_true(pc
->valid_u64
)) {
161 TEST_note("errant uint64 on line %d", s
->curr
);
164 datum_u64
= ref_u64
= pc
->u64
;
165 pc
->datum
= &datum_u64
;
167 pc
->size
= sizeof(ref_u64
);
168 } else if (OPENSSL_strcasecmp(type
, "double") == 0) {
169 if (!TEST_true(def_d
) || !TEST_true(pc
->valid_d
)) {
170 TEST_note("errant double on line %d", s
->curr
);
173 datum_d
= ref_d
= pc
->d
;
174 pc
->datum
= &datum_d
;
176 pc
->size
= sizeof(ref_d
);
178 TEST_error("type unknown at line %d", s
->curr
);
184 static int param_conversion_test(const PARAM_CONVERSION
*pc
, int line
)
192 if (!pc
->valid_i32
) {
193 if (!TEST_false(OSSL_PARAM_get_int32(pc
->param
, &i32
))
194 || !TEST_ulong_ne(ERR_get_error(), 0)) {
195 TEST_note("unexpected valid conversion to int32 on line %d", line
);
199 if (!TEST_true(OSSL_PARAM_get_int32(pc
->param
, &i32
))
200 || !TEST_true(i32
== pc
->i32
)) {
201 TEST_note("unexpected conversion to int32 on line %d", line
);
204 memset(pc
->datum
, 44, pc
->size
);
205 if (!TEST_true(OSSL_PARAM_set_int32(pc
->param
, i32
))
206 || !TEST_mem_eq(pc
->datum
, pc
->size
, pc
->ref
, pc
->size
)) {
207 TEST_note("unexpected valid conversion from int32 on line %d",
213 if (!pc
->valid_i64
) {
214 if (!TEST_false(OSSL_PARAM_get_int64(pc
->param
, &i64
))
215 || !TEST_ulong_ne(ERR_get_error(), 0)) {
216 TEST_note("unexpected valid conversion to int64 on line %d", line
);
220 if (!TEST_true(OSSL_PARAM_get_int64(pc
->param
, &i64
))
221 || !TEST_true(i64
== pc
->i64
)) {
222 TEST_note("unexpected conversion to int64 on line %d", line
);
225 memset(pc
->datum
, 44, pc
->size
);
226 if (!TEST_true(OSSL_PARAM_set_int64(pc
->param
, i64
))
227 || !TEST_mem_eq(pc
->datum
, pc
->size
, pc
->ref
, pc
->size
)) {
228 TEST_note("unexpected valid conversion from int64 on line %d",
234 if (!pc
->valid_u32
) {
235 if (!TEST_false(OSSL_PARAM_get_uint32(pc
->param
, &u32
))
236 || !TEST_ulong_ne(ERR_get_error(), 0)) {
237 TEST_note("unexpected valid conversion to uint32 on line %d", line
);
241 if (!TEST_true(OSSL_PARAM_get_uint32(pc
->param
, &u32
))
242 || !TEST_true(u32
== pc
->u32
)) {
243 TEST_note("unexpected conversion to uint32 on line %d", line
);
246 memset(pc
->datum
, 44, pc
->size
);
247 if (!TEST_true(OSSL_PARAM_set_uint32(pc
->param
, u32
))
248 || !TEST_mem_eq(pc
->datum
, pc
->size
, pc
->ref
, pc
->size
)) {
249 TEST_note("unexpected valid conversion from uint32 on line %d",
255 if (!pc
->valid_u64
) {
256 if (!TEST_false(OSSL_PARAM_get_uint64(pc
->param
, &u64
))
257 || !TEST_ulong_ne(ERR_get_error(), 0)) {
258 TEST_note("unexpected valid conversion to uint64 on line %d", line
);
262 if (!TEST_true(OSSL_PARAM_get_uint64(pc
->param
, &u64
))
263 || !TEST_true(u64
== pc
->u64
)) {
264 TEST_note("unexpected conversion to uint64 on line %d", line
);
267 memset(pc
->datum
, 44, pc
->size
);
268 if (!TEST_true(OSSL_PARAM_set_uint64(pc
->param
, u64
))
269 || !TEST_mem_eq(pc
->datum
, pc
->size
, pc
->ref
, pc
->size
)) {
270 TEST_note("unexpected valid conversion from uint64 on line %d",
277 if (!TEST_false(OSSL_PARAM_get_double(pc
->param
, &d
))
278 || !TEST_ulong_ne(ERR_get_error(), 0)) {
279 TEST_note("unexpected valid conversion to double on line %d", line
);
283 if (!TEST_true(OSSL_PARAM_get_double(pc
->param
, &d
))) {
284 TEST_note("unable to convert to double on line %d", line
);
288 * Check for not a number (NaN) without using the libm functions.
289 * When d is a NaN, the standard requires d == d to be false.
290 * It's less clear if d != d should be true even though it generally is.
291 * Hence we use the equality test and a not.
295 * We've encountered a NaN so check it's really meant to be a NaN.
296 * We ignore the case where the two values are both different NaN,
297 * that's not resolvable without knowing the underlying format
298 * or using libm functions.
300 if (!TEST_false(pc
->d
== pc
->d
)) {
301 TEST_note("unexpected NaN on line %d", line
);
304 } else if (!TEST_true(d
== pc
->d
)) {
305 TEST_note("unexpected conversion to double on line %d", line
);
308 memset(pc
->datum
, 44, pc
->size
);
309 if (!TEST_true(OSSL_PARAM_set_double(pc
->param
, d
))
310 || !TEST_mem_eq(pc
->datum
, pc
->size
, pc
->ref
, pc
->size
)) {
311 TEST_note("unexpected valid conversion from double on line %d",
320 static int run_param_file_tests(int i
)
324 const char *testfile
= test_get_argument(i
);
327 if (!TEST_ptr(s
= OPENSSL_zalloc(sizeof(*s
))))
329 if (!test_start_file(s
, testfile
)) {
334 while (!BIO_eof(s
->fp
)) {
335 if (!test_readstanza(s
)) {
339 if (s
->numpairs
!= 0)
340 if (!param_conversion_load_stanza(&pc
, s
)
341 || !param_conversion_test(&pc
, s
->curr
))
351 #endif /* OPENSSL_NO_INTTYPES_H */
353 OPT_TEST_DECLARE_USAGE("file...\n")
355 int setup_tests(void)
359 if (!test_skip_common_options()) {
360 TEST_error("Error parsing test options\n");
364 n
= test_get_argument_count();
368 #if !defined(OPENSSL_NO_INTTYPES_H)
369 ADD_ALL_TESTS(run_param_file_tests
, n
);
370 #endif /* OPENSSL_NO_INTTYPES_H */