]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/params_conversion_test.c
2 * Copyright 2019 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_WINDOWS
19 # define strcasecmp _stricmp
29 int valid_i32
, valid_i64
, valid_u32
, valid_u64
, valid_d
;
34 static int param_conversion_load_stanza(PARAM_CONVERSION
*pc
, const STANZA
*s
)
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
),
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
;
56 memset(pc
, 0, sizeof(*pc
));
58 for (i
= 0; i
< s
->numpairs
; i
++, pp
++) {
60 if (strcasecmp(pp
->key
, "type") == 0) {
62 TEST_info("Line %d: multiple type lines", s
->curr
);
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
);
70 } else if (strcasecmp(pp
->key
, "int32") == 0) {
72 TEST_info("Line %d: multiple int32 lines", s
->curr
);
75 if (strcasecmp(pp
->value
, "invalid") != 0) {
77 pc
->i32
= (int32_t)strtoimax(pp
->value
, &p
, 10);
79 } else if (strcasecmp(pp
->key
, "int64") == 0) {
81 TEST_info("Line %d: multiple int64 lines", s
->curr
);
84 if (strcasecmp(pp
->value
, "invalid") != 0) {
86 pc
->i64
= (int64_t)strtoimax(pp
->value
, &p
, 10);
88 } else if (strcasecmp(pp
->key
, "uint32") == 0) {
90 TEST_info("Line %d: multiple uint32 lines", s
->curr
);
93 if (strcasecmp(pp
->value
, "invalid") != 0) {
95 pc
->u32
= (uint32_t)strtoumax(pp
->value
, &p
, 10);
97 } else if (strcasecmp(pp
->key
, "uint64") == 0) {
99 TEST_info("Line %d: multiple uint64 lines", s
->curr
);
102 if (strcasecmp(pp
->value
, "invalid") != 0) {
104 pc
->u64
= (uint64_t)strtoumax(pp
->value
, &p
, 10);
106 } else if (strcasecmp(pp
->key
, "double") == 0) {
108 TEST_info("Line %d: multiple double lines", s
->curr
);
111 if (strcasecmp(pp
->value
, "invalid") != 0) {
113 pc
->d
= strtod(pp
->value
, &p
);
116 TEST_info("Line %d: unknown keyword %s", s
->curr
, pp
->key
);
120 TEST_info("Line %d: extra characters at end '%s' for %s",
121 s
->curr
, p
, pp
->key
);
126 if (!TEST_ptr(type
)) {
127 TEST_info("Line %d: type not found", s
->curr
);
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
);
136 datum_i32
= ref_i32
= pc
->i32
;
137 pc
->datum
= &datum_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
);
145 datum_i64
= ref_i64
= pc
->i64
;
146 pc
->datum
= &datum_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
);
154 datum_u32
= ref_u32
= pc
->u32
;
155 pc
->datum
= &datum_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
);
163 datum_u64
= ref_u64
= pc
->u64
;
164 pc
->datum
= &datum_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
);
172 datum_d
= ref_d
= pc
->d
;
173 pc
->datum
= &datum_d
;
175 pc
->size
= sizeof(ref_d
);
177 TEST_error("type unknown at line %d", s
->curr
);
183 static int param_conversion_test(const PARAM_CONVERSION
*pc
, int line
)
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
);
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
);
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",
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
);
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
);
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",
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
);
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
);
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",
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
);
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
);
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",
272 if (!TEST_false(OSSL_PARAM_get_double(pc
->param
, &d
))) {
273 TEST_note("unexpected valid conversion to double on line %d", line
);
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
);
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",
294 static int run_param_file_tests(int i
)
298 const char *testfile
= test_get_argument(i
);
301 if (!TEST_ptr(s
= OPENSSL_zalloc(sizeof(*s
))))
303 if (!test_start_file(s
, testfile
)) {
308 while (!BIO_eof(s
->fp
)) {
309 if (!test_readstanza(s
)) {
313 if (s
->numpairs
!= 0)
314 if (!param_conversion_load_stanza(&pc
, s
)
315 || !param_conversion_test(&pc
, s
->curr
))
325 #endif /* OPENSSL_NO_INTTYPES_H */
327 OPT_TEST_DECLARE_USAGE("file...\n")
329 int setup_tests(void)
333 if (!test_skip_common_options()) {
334 TEST_error("Error parsing test options\n");
338 n
= test_get_argument_count();
342 #if !defined(OPENSSL_NO_INTTYPES_H)
343 ADD_ALL_TESTS(run_param_file_tests
, n
);
344 #endif /* OPENSSL_NO_INTTYPES_H */