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
13 #include <openssl/params.h>
16 #ifdef OPENSSL_SYS_WINDOWS
17 # define strcasecmp _stricmp
21 const OSSL_PARAM *param;
27 int valid_i32, valid_i64, valid_u32, valid_u64, valid_d;
32 static int param_conversion_load_stanza(PARAM_CONVERSION *pc, const STANZA *s)
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),
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;
54 memset(pc, 0, sizeof(*pc));
56 for (i = 0; i < s->numpairs; i++, pp++) {
58 if (strcasecmp(pp->key, "type") == 0) {
60 TEST_info("Line %d: multiple type lines", s->curr);
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);
68 } else if (strcasecmp(pp->key, "int32") == 0) {
70 TEST_info("Line %d: multiple int32 lines", s->curr);
73 if (strcasecmp(pp->value, "invalid") != 0) {
75 pc->i32 = (int32_t)strtoimax(pp->value, &p, 10);
77 } else if (strcasecmp(pp->key, "int64") == 0) {
79 TEST_info("Line %d: multiple int64 lines", s->curr);
82 if (strcasecmp(pp->value, "invalid") != 0) {
84 pc->i64 = (int64_t)strtoimax(pp->value, &p, 10);
86 } else if (strcasecmp(pp->key, "uint32") == 0) {
88 TEST_info("Line %d: multiple uint32 lines", s->curr);
91 if (strcasecmp(pp->value, "invalid") != 0) {
93 pc->u32 = (uint32_t)strtoumax(pp->value, &p, 10);
95 } else if (strcasecmp(pp->key, "uint64") == 0) {
97 TEST_info("Line %d: multiple uint64 lines", s->curr);
100 if (strcasecmp(pp->value, "invalid") != 0) {
102 pc->u64 = (uint64_t)strtoumax(pp->value, &p, 10);
104 } else if (strcasecmp(pp->key, "double") == 0) {
106 TEST_info("Line %d: multiple double lines", s->curr);
109 if (strcasecmp(pp->value, "invalid") != 0) {
111 pc->d = strtod(pp->value, &p);
114 TEST_info("Line %d: unknown keyword %s", s->curr, pp->key);
118 TEST_info("Line %d: extra characters at end '%s' for %s",
119 s->curr, p, pp->key);
124 if (!TEST_ptr(type)) {
125 TEST_info("Line %d: type not found", s->curr);
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);
134 datum_i32 = ref_i32 = pc->i32;
135 pc->datum = &datum_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);
143 datum_i64 = ref_i64 = pc->i64;
144 pc->datum = &datum_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);
152 datum_u32 = ref_u32 = pc->u32;
153 pc->datum = &datum_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);
161 datum_u64 = ref_u64 = pc->u64;
162 pc->datum = &datum_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);
170 datum_d = ref_d = pc->d;
171 pc->datum = &datum_d;
173 pc->size = sizeof(ref_d);
175 TEST_error("type unknown at line %d", s->curr);
181 static int param_conversion_test(const PARAM_CONVERSION *pc, int line)
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);
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);
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",
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);
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);
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",
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);
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);
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",
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);
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);
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",
270 if (!TEST_false(OSSL_PARAM_get_double(pc->param, &d))) {
271 TEST_note("unexpected valid conversion to double on line %d", line);
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);
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",
292 static int run_param_file_tests(int i)
296 const char *testfile = test_get_argument(i);
299 if (!TEST_ptr(s = OPENSSL_zalloc(sizeof(*s))))
301 if (!test_start_file(s, testfile)) {
306 while (!BIO_eof(s->fp)) {
307 if (!test_readstanza(s)) {
311 if (s->numpairs != 0)
312 if (!param_conversion_load_stanza(&pc, s)
313 || !param_conversion_test(&pc, s->curr))
323 OPT_TEST_DECLARE_USAGE("file...\n")
325 int setup_tests(void)
327 size_t n = test_get_argument_count();
332 ADD_ALL_TESTS(run_param_file_tests, n);