VirtualBox

source: vbox/trunk/src/libs/openssl-3.0.3/test/params_conversion_test.c@ 95218

最後變更 在這個檔案從95218是 94320,由 vboxsync 提交於 3 年 前

libs/openssl-3.0.1: Export to OSE and fix copyright headers in Makefiles, bugref:10128

檔案大小: 11.9 KB
 
1/*
2 * Copyright 2019-2021 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# ifdef OPENSSL_SYS_VMS
23# define strtoumax strtoull
24# define strtoimax strtoll
25# endif
26
27typedef struct {
28 OSSL_PARAM *param;
29 int32_t i32;
30 int64_t i64;
31 uint32_t u32;
32 uint64_t u64;
33 double d;
34 int valid_i32, valid_i64, valid_u32, valid_u64, valid_d;
35 void *ref, *datum;
36 size_t size;
37} PARAM_CONVERSION;
38
39static int param_conversion_load_stanza(PARAM_CONVERSION *pc, const STANZA *s)
40{
41
42 static int32_t datum_i32, ref_i32;
43 static int64_t datum_i64, ref_i64;
44 static uint32_t datum_u32, ref_u32;
45 static uint64_t datum_u64, ref_u64;
46 static double datum_d, ref_d;
47 static OSSL_PARAM params[] = {
48 OSSL_PARAM_int32("int32", &datum_i32),
49 OSSL_PARAM_int64("int64", &datum_i64),
50 OSSL_PARAM_uint32("uint32", &datum_u32),
51 OSSL_PARAM_uint64("uint64", &datum_u64),
52 OSSL_PARAM_double("double", &datum_d),
53 OSSL_PARAM_END
54 };
55 int def_i32 = 0, def_i64 = 0, def_u32 = 0, def_u64 = 0, def_d = 0;
56 const PAIR *pp = s->pairs;
57 const char *type = NULL;
58 char *p;
59 int i;
60
61 memset(pc, 0, sizeof(*pc));
62
63 for (i = 0; i < s->numpairs; i++, pp++) {
64 p = "";
65 if (strcasecmp(pp->key, "type") == 0) {
66 if (type != NULL) {
67 TEST_info("Line %d: multiple type lines", s->curr);
68 return 0;
69 }
70 pc->param = OSSL_PARAM_locate(params, type = pp->value);
71 if (pc->param == NULL) {
72 TEST_info("Line %d: unknown type line", s->curr);
73 return 0;
74 }
75 } else if (strcasecmp(pp->key, "int32") == 0) {
76 if (def_i32++) {
77 TEST_info("Line %d: multiple int32 lines", s->curr);
78 return 0;
79 }
80 if (strcasecmp(pp->value, "invalid") != 0) {
81 pc->valid_i32 = 1;
82 pc->i32 = (int32_t)strtoimax(pp->value, &p, 10);
83 }
84 } else if (strcasecmp(pp->key, "int64") == 0) {
85 if (def_i64++) {
86 TEST_info("Line %d: multiple int64 lines", s->curr);
87 return 0;
88 }
89 if (strcasecmp(pp->value, "invalid") != 0) {
90 pc->valid_i64 = 1;
91 pc->i64 = (int64_t)strtoimax(pp->value, &p, 10);
92 }
93 } else if (strcasecmp(pp->key, "uint32") == 0) {
94 if (def_u32++) {
95 TEST_info("Line %d: multiple uint32 lines", s->curr);
96 return 0;
97 }
98 if (strcasecmp(pp->value, "invalid") != 0) {
99 pc->valid_u32 = 1;
100 pc->u32 = (uint32_t)strtoumax(pp->value, &p, 10);
101 }
102 } else if (strcasecmp(pp->key, "uint64") == 0) {
103 if (def_u64++) {
104 TEST_info("Line %d: multiple uint64 lines", s->curr);
105 return 0;
106 }
107 if (strcasecmp(pp->value, "invalid") != 0) {
108 pc->valid_u64 = 1;
109 pc->u64 = (uint64_t)strtoumax(pp->value, &p, 10);
110 }
111 } else if (strcasecmp(pp->key, "double") == 0) {
112 if (def_d++) {
113 TEST_info("Line %d: multiple double lines", s->curr);
114 return 0;
115 }
116 if (strcasecmp(pp->value, "invalid") != 0) {
117 pc->valid_d = 1;
118 pc->d = strtod(pp->value, &p);
119 }
120 } else {
121 TEST_info("Line %d: unknown keyword %s", s->curr, pp->key);
122 return 0;
123 }
124 if (*p != '\0') {
125 TEST_info("Line %d: extra characters at end '%s' for %s",
126 s->curr, p, pp->key);
127 return 0;
128 }
129 }
130
131 if (!TEST_ptr(type)) {
132 TEST_info("Line %d: type not found", s->curr);
133 return 0;
134 }
135
136 if (strcasecmp(type, "int32") == 0) {
137 if (!TEST_true(def_i32) || !TEST_true(pc->valid_i32)) {
138 TEST_note("errant int32 on line %d", s->curr);
139 return 0;
140 }
141 datum_i32 = ref_i32 = pc->i32;
142 pc->datum = &datum_i32;
143 pc->ref = &ref_i32;
144 pc->size = sizeof(ref_i32);
145 } else if (strcasecmp(type, "int64") == 0) {
146 if (!TEST_true(def_i64) || !TEST_true(pc->valid_i64)) {
147 TEST_note("errant int64 on line %d", s->curr);
148 return 0;
149 }
150 datum_i64 = ref_i64 = pc->i64;
151 pc->datum = &datum_i64;
152 pc->ref = &ref_i64;
153 pc->size = sizeof(ref_i64);
154 } else if (strcasecmp(type, "uint32") == 0) {
155 if (!TEST_true(def_u32) || !TEST_true(pc->valid_u32)) {
156 TEST_note("errant uint32 on line %d", s->curr);
157 return 0;
158 }
159 datum_u32 = ref_u32 = pc->u32;
160 pc->datum = &datum_u32;
161 pc->ref = &ref_u32;
162 pc->size = sizeof(ref_u32);
163 } else if (strcasecmp(type, "uint64") == 0) {
164 if (!TEST_true(def_u64) || !TEST_true(pc->valid_u64)) {
165 TEST_note("errant uint64 on line %d", s->curr);
166 return 0;
167 }
168 datum_u64 = ref_u64 = pc->u64;
169 pc->datum = &datum_u64;
170 pc->ref = &ref_u64;
171 pc->size = sizeof(ref_u64);
172 } else if (strcasecmp(type, "double") == 0) {
173 if (!TEST_true(def_d) || !TEST_true(pc->valid_d)) {
174 TEST_note("errant double on line %d", s->curr);
175 return 0;
176 }
177 datum_d = ref_d = pc->d;
178 pc->datum = &datum_d;
179 pc->ref = &ref_d;
180 pc->size = sizeof(ref_d);
181 } else {
182 TEST_error("type unknown at line %d", s->curr);
183 return 0;
184 }
185 return 1;
186}
187
188static int param_conversion_test(const PARAM_CONVERSION *pc, int line)
189{
190 int32_t i32;
191 int64_t i64;
192 uint32_t u32;
193 uint64_t u64;
194 double d;
195
196 if (!pc->valid_i32) {
197 if (!TEST_false(OSSL_PARAM_get_int32(pc->param, &i32))) {
198 TEST_note("unexpected valid conversion to int32 on line %d", line);
199 return 0;
200 }
201 } else {
202 if (!TEST_true(OSSL_PARAM_get_int32(pc->param, &i32))
203 || !TEST_true(i32 == pc->i32)) {
204 TEST_note("unexpected conversion to int32 on line %d", line);
205 return 0;
206 }
207 memset(pc->datum, 44, pc->size);
208 if (!TEST_true(OSSL_PARAM_set_int32(pc->param, i32))
209 || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
210 TEST_note("unexpected valid conversion from int32 on line %d",
211 line);
212 return 0;
213 }
214 }
215
216 if (!pc->valid_i64) {
217 if (!TEST_false(OSSL_PARAM_get_int64(pc->param, &i64))) {
218 TEST_note("unexpected valid conversion to int64 on line %d", line);
219 return 0;
220 }
221 } else {
222 if (!TEST_true(OSSL_PARAM_get_int64(pc->param, &i64))
223 || !TEST_true(i64 == pc->i64)) {
224 TEST_note("unexpected conversion to int64 on line %d", line);
225 return 0;
226 }
227 memset(pc->datum, 44, pc->size);
228 if (!TEST_true(OSSL_PARAM_set_int64(pc->param, i64))
229 || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
230 TEST_note("unexpected valid conversion from int64 on line %d",
231 line);
232 return 0;
233 }
234 }
235
236 if (!pc->valid_u32) {
237 if (!TEST_false(OSSL_PARAM_get_uint32(pc->param, &u32))) {
238 TEST_note("unexpected valid conversion to uint32 on line %d", line);
239 return 0;
240 }
241 } else {
242 if (!TEST_true(OSSL_PARAM_get_uint32(pc->param, &u32))
243 || !TEST_true(u32 == pc->u32)) {
244 TEST_note("unexpected conversion to uint32 on line %d", line);
245 return 0;
246 }
247 memset(pc->datum, 44, pc->size);
248 if (!TEST_true(OSSL_PARAM_set_uint32(pc->param, u32))
249 || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
250 TEST_note("unexpected valid conversion from uint32 on line %d",
251 line);
252 return 0;
253 }
254 }
255
256 if (!pc->valid_u64) {
257 if (!TEST_false(OSSL_PARAM_get_uint64(pc->param, &u64))) {
258 TEST_note("unexpected valid conversion to uint64 on line %d", line);
259 return 0;
260 }
261 } else {
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);
265 return 0;
266 }
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",
271 line);
272 return 0;
273 }
274 }
275
276 if (!pc->valid_d) {
277 if (!TEST_false(OSSL_PARAM_get_double(pc->param, &d))) {
278 TEST_note("unexpected valid conversion to double on line %d", line);
279 return 0;
280 }
281 } else {
282 if (!TEST_true(OSSL_PARAM_get_double(pc->param, &d))) {
283 TEST_note("unable to convert to double on line %d", line);
284 return 0;
285 }
286 /*
287 * Check for not a number (NaN) without using the libm functions.
288 * When d is a NaN, the standard requires d == d to be false.
289 * It's less clear if d != d should be true even though it generally is.
290 * Hence we use the equality test and a not.
291 */
292 if (!(d == d)) {
293 /*
294 * We've encountered a NaN so check it's really meant to be a NaN.
295 * We ignore the case where the two values are both different NaN,
296 * that's not resolvable without knowing the underlying format
297 * or using libm functions.
298 */
299 if (!TEST_false(pc->d == pc->d)) {
300 TEST_note("unexpected NaN on line %d", line);
301 return 0;
302 }
303 } else if (!TEST_true(d == pc->d)) {
304 TEST_note("unexpected conversion to double on line %d", line);
305 return 0;
306 }
307 memset(pc->datum, 44, pc->size);
308 if (!TEST_true(OSSL_PARAM_set_double(pc->param, d))
309 || !TEST_mem_eq(pc->datum, pc->size, pc->ref, pc->size)) {
310 TEST_note("unexpected valid conversion from double on line %d",
311 line);
312 return 0;
313 }
314 }
315
316 return 1;
317}
318
319static int run_param_file_tests(int i)
320{
321 STANZA *s;
322 PARAM_CONVERSION pc;
323 const char *testfile = test_get_argument(i);
324 int res = 1;
325
326 if (!TEST_ptr(s = OPENSSL_zalloc(sizeof(*s))))
327 return 0;
328 if (!test_start_file(s, testfile)) {
329 OPENSSL_free(s);
330 return 0;
331 }
332
333 while (!BIO_eof(s->fp)) {
334 if (!test_readstanza(s)) {
335 res = 0;
336 goto end;
337 }
338 if (s->numpairs != 0)
339 if (!param_conversion_load_stanza(&pc, s)
340 || !param_conversion_test(&pc, s->curr))
341 res = 0;
342 test_clearstanza(s);
343 }
344end:
345 test_end_file(s);
346 OPENSSL_free(s);
347 return res;
348}
349
350#endif /* OPENSSL_NO_INTTYPES_H */
351
352OPT_TEST_DECLARE_USAGE("file...\n")
353
354int setup_tests(void)
355{
356 size_t n;
357
358 if (!test_skip_common_options()) {
359 TEST_error("Error parsing test options\n");
360 return 0;
361 }
362
363 n = test_get_argument_count();
364 if (n == 0)
365 return 0;
366
367#if !defined(OPENSSL_NO_INTTYPES_H)
368 ADD_ALL_TESTS(run_param_file_tests, n);
369#endif /* OPENSSL_NO_INTTYPES_H */
370
371 return 1;
372}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette