]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/property_test.c
Copyright year updates
[thirdparty/openssl.git] / test / property_test.c
CommitLineData
1bdbdaff 1/*
da1c088f 2 * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
1bdbdaff
P
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 <stdarg.h>
e0624f0d 12#include <openssl/evp.h>
1bdbdaff
P
13#include "testutil.h"
14#include "internal/nelem.h"
15#include "internal/property.h"
706457b7 16#include "../crypto/property/property_local.h"
1bdbdaff 17
dc010ca6
RL
18/*
19 * We make our OSSL_PROVIDER for testing purposes. All we really need is
20 * a pointer. We know that as long as we don't try to use the method
21 * cache flush functions, the provider pointer is merely a pointer being
22 * passed around, and used as a tag of sorts.
23 */
24struct ossl_provider_st {
25 int x;
26};
27
1bdbdaff
P
28static int add_property_names(const char *n, ...)
29{
30 va_list args;
31 int res = 1;
32
33 va_start(args, n);
34 do {
1aedc35f 35 if (!TEST_int_ne(ossl_property_name(NULL, n, 1), 0))
1bdbdaff
P
36 res = 0;
37 } while ((n = va_arg(args, const char *)) != NULL);
38 va_end(args);
39 return res;
40}
41
bdbf2df2
P
42static int up_ref(void *p)
43{
44 return 1;
45}
46
47static void down_ref(void *p)
48{
49}
50
1bdbdaff
P
51static int test_property_string(void)
52{
9f6841e9
P
53 OSSL_LIB_CTX *ctx;
54 OSSL_METHOD_STORE *store = NULL;
1bdbdaff
P
55 int res = 0;
56 OSSL_PROPERTY_IDX i, j;
57
9f6841e9
P
58 /*-
59 * Use our own library context because we depend on ordering from a
60 * pristine state.
61 */
62 if (TEST_ptr(ctx = OSSL_LIB_CTX_new())
63 && TEST_ptr(store = ossl_method_store_new(ctx))
64 && TEST_int_eq(ossl_property_name(ctx, "fnord", 0), 0)
65 && TEST_int_ne(ossl_property_name(ctx, "fnord", 1), 0)
66 && TEST_int_ne(ossl_property_name(ctx, "name", 1), 0)
67 /* Pre loaded names */
68 && TEST_str_eq(ossl_property_name_str(ctx, 1), "provider")
69 && TEST_str_eq(ossl_property_name_str(ctx, 2), "version")
70 && TEST_str_eq(ossl_property_name_str(ctx, 3), "fips")
71 && TEST_str_eq(ossl_property_name_str(ctx, 4), "output")
72 && TEST_str_eq(ossl_property_name_str(ctx, 5), "input")
73 && TEST_str_eq(ossl_property_name_str(ctx, 6), "structure")
74 /* The names we added */
75 && TEST_str_eq(ossl_property_name_str(ctx, 7), "fnord")
76 && TEST_str_eq(ossl_property_name_str(ctx, 8), "name")
77 /* Out of range */
78 && TEST_ptr_null(ossl_property_name_str(ctx, 0))
79 && TEST_ptr_null(ossl_property_name_str(ctx, 9))
1bdbdaff 80 /* Property value checks */
9f6841e9
P
81 && TEST_int_eq(ossl_property_value(ctx, "fnord", 0), 0)
82 && TEST_int_ne(i = ossl_property_value(ctx, "no", 0), 0)
83 && TEST_int_ne(j = ossl_property_value(ctx, "yes", 0), 0)
1bdbdaff 84 && TEST_int_ne(i, j)
9f6841e9
P
85 && TEST_int_eq(ossl_property_value(ctx, "yes", 1), j)
86 && TEST_int_eq(ossl_property_value(ctx, "no", 1), i)
87 && TEST_int_ne(i = ossl_property_value(ctx, "illuminati", 1), 0)
88 && TEST_int_eq(j = ossl_property_value(ctx, "fnord", 1), i + 1)
89 && TEST_int_eq(ossl_property_value(ctx, "fnord", 1), j)
90 /* Pre loaded values */
91 && TEST_str_eq(ossl_property_value_str(ctx, 1), "yes")
92 && TEST_str_eq(ossl_property_value_str(ctx, 2), "no")
93 /* The value we added */
94 && TEST_str_eq(ossl_property_value_str(ctx, 3), "illuminati")
95 && TEST_str_eq(ossl_property_value_str(ctx, 4), "fnord")
96 /* Out of range */
97 && TEST_ptr_null(ossl_property_value_str(ctx, 0))
98 && TEST_ptr_null(ossl_property_value_str(ctx, 5))
1bdbdaff 99 /* Check name and values are distinct */
9f6841e9
P
100 && TEST_int_eq(ossl_property_value(ctx, "cold", 0), 0)
101 && TEST_int_ne(ossl_property_name(ctx, "fnord", 0),
102 ossl_property_value(ctx, "fnord", 0)))
1bdbdaff
P
103 res = 1;
104 ossl_method_store_free(store);
9f6841e9 105 OSSL_LIB_CTX_free(ctx);
1bdbdaff
P
106 return res;
107}
108
109static const struct {
110 const char *defn;
111 const char *query;
112 int e;
113} parser_tests[] = {
da89ac0b 114 { "", "sky=blue", -1 },
1bdbdaff 115 { "", "sky!=blue", 1 },
da89ac0b 116 { "groan", "", 0 },
1bdbdaff
P
117 { "cold=yes", "cold=yes", 1 },
118 { "cold=yes", "cold", 1 },
119 { "cold=yes", "cold!=no", 1 },
120 { "groan", "groan=yes", 1 },
da89ac0b
P
121 { "groan", "groan=no", -1 },
122 { "groan", "groan!=yes", -1 },
123 { "cold=no", "cold", -1 },
124 { "cold=no", "?cold", 0 },
1bdbdaff 125 { "cold=no", "cold=no", 1 },
da89ac0b 126 { "groan", "cold", -1 },
1bdbdaff
P
127 { "groan", "cold=no", 1 },
128 { "groan", "cold!=yes", 1 },
da89ac0b
P
129 { "groan=blue", "groan=yellow", -1 },
130 { "groan=blue", "?groan=yellow", 0 },
1bdbdaff 131 { "groan=blue", "groan!=yellow", 1 },
da89ac0b 132 { "groan=blue", "?groan!=yellow", 1 },
1bdbdaff 133 { "today=monday, tomorrow=3", "today!=2", 1 },
da89ac0b 134 { "today=monday, tomorrow=3", "today!='monday'", -1 },
1bdbdaff
P
135 { "today=monday, tomorrow=3", "tomorrow=3", 1 },
136 { "n=0x3", "n=3", 1 },
da89ac0b 137 { "n=0x3", "n=-3", -1 },
1bdbdaff
P
138 { "n=0x33", "n=51", 1 },
139 { "n=033", "n=27", 1 },
140 { "n=0", "n=00", 1 },
141 { "n=0x0", "n=0", 1 },
da89ac0b
P
142 { "n=0, sky=blue", "?n=0, sky=blue", 2 },
143 { "n=1, sky=blue", "?n=0, sky=blue", 1 },
1bdbdaff
P
144};
145
146static int test_property_parse(int n)
147{
148 OSSL_METHOD_STORE *store;
149 OSSL_PROPERTY_LIST *p = NULL, *q = NULL;
150 int r = 0;
151
1aedc35f 152 if (TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff
P
153 && add_property_names("sky", "groan", "cold", "today", "tomorrow", "n",
154 NULL)
1aedc35f 155 && TEST_ptr(p = ossl_parse_property(NULL, parser_tests[n].defn))
1e08f3ba 156 && TEST_ptr(q = ossl_parse_query(NULL, parser_tests[n].query, 0))
da89ac0b 157 && TEST_int_eq(ossl_property_match_count(q, p), parser_tests[n].e))
1bdbdaff
P
158 r = 1;
159 ossl_property_free(p);
160 ossl_property_free(q);
161 ossl_method_store_free(store);
162 return r;
163}
164
1e08f3ba
P
165static int test_property_query_value_create(void)
166{
167 OSSL_METHOD_STORE *store;
168 OSSL_PROPERTY_LIST *p = NULL, *q = NULL, *o = NULL;
169 int r = 0;
170
a289d3a4 171 /* The property value used here must not be used in other test cases */
1e08f3ba 172 if (TEST_ptr(store = ossl_method_store_new(NULL))
a289d3a4
TM
173 && add_property_names("wood", NULL)
174 && TEST_ptr(p = ossl_parse_query(NULL, "wood=oak", 0)) /* undefined */
175 && TEST_ptr(q = ossl_parse_query(NULL, "wood=oak", 1)) /* creates */
176 && TEST_ptr(o = ossl_parse_query(NULL, "wood=oak", 0)) /* defined */
1e08f3ba
P
177 && TEST_int_eq(ossl_property_match_count(q, p), -1)
178 && TEST_int_eq(ossl_property_match_count(q, o), 1))
179 r = 1;
180 ossl_property_free(o);
181 ossl_property_free(p);
182 ossl_property_free(q);
183 ossl_method_store_free(store);
184 return r;
185}
186
747d1423
P
187static const struct {
188 int query;
189 const char *ps;
190} parse_error_tests[] = {
191 { 0, "n=1, n=1" }, /* duplicate name */
192 { 0, "n=1, a=hi, n=1" }, /* duplicate name */
193 { 1, "n=1, a=bye, ?n=0" }, /* duplicate name */
194 { 0, "a=abc,#@!, n=1" }, /* non-ASCII character located */
195 { 1, "a='Hello" }, /* Unterminated string */
196 { 0, "a=\"World" }, /* Unterminated string */
543ac2f0 197 { 0, "a=_abd_" }, /* Unquoted string not starting with alphabetic */
747d1423
P
198 { 1, "a=2, n=012345678" }, /* Bad octal digit */
199 { 0, "n=0x28FG, a=3" }, /* Bad hex digit */
200 { 0, "n=145d, a=2" }, /* Bad decimal digit */
201 { 1, "@='hello'" }, /* Invalid name */
202 { 1, "n0123456789012345678901234567890123456789"
203 "0123456789012345678901234567890123456789"
204 "0123456789012345678901234567890123456789"
205 "0123456789012345678901234567890123456789=yes" }, /* Name too long */
206 { 0, ".n=3" }, /* Invalid name */
207 { 1, "fnord.fnord.=3" } /* Invalid name */
208};
209
210static int test_property_parse_error(int n)
211{
212 OSSL_METHOD_STORE *store;
213 OSSL_PROPERTY_LIST *p = NULL;
214 int r = 0;
215 const char *ps;
216
217 if (!TEST_ptr(store = ossl_method_store_new(NULL))
218 || !add_property_names("a", "n", NULL))
219 goto err;
220 ps = parse_error_tests[n].ps;
221 if (parse_error_tests[n].query) {
222 if (!TEST_ptr_null(p = ossl_parse_query(NULL, ps, 1)))
223 goto err;
224 } else if (!TEST_ptr_null(p = ossl_parse_property(NULL, ps))) {
225 goto err;
226 }
227 r = 1;
228 err:
229 ossl_property_free(p);
230 ossl_method_store_free(store);
231 return r;
232}
233
1bdbdaff
P
234static const struct {
235 const char *q_global;
236 const char *q_local;
237 const char *prop;
238} merge_tests[] = {
239 { "", "colour=blue", "colour=blue" },
240 { "colour=blue", "", "colour=blue" },
241 { "colour=red", "colour=blue", "colour=blue" },
242 { "clouds=pink, urn=red", "urn=blue, colour=green",
243 "urn=blue, colour=green, clouds=pink" },
244 { "pot=gold", "urn=blue", "pot=gold, urn=blue" },
245 { "night", "day", "day=yes, night=yes" },
246 { "day", "night", "day=yes, night=yes" },
247 { "", "", "" },
248 /*
249 * The following four leave 'day' unspecified in the query, and will match
250 * any definition
251 */
252 { "day=yes", "-day", "day=no" },
253 { "day=yes", "-day", "day=yes" },
254 { "day=yes", "-day", "day=arglebargle" },
255 { "day=yes", "-day", "pot=sesquioxidizing" },
256 { "day, night", "-night, day", "day=yes, night=no" },
257 { "-day", "day=yes", "day=yes" },
258};
259
260static int test_property_merge(int n)
261{
262 OSSL_METHOD_STORE *store;
263 OSSL_PROPERTY_LIST *q_global = NULL, *q_local = NULL;
264 OSSL_PROPERTY_LIST *q_combined = NULL, *prop = NULL;
265 int r = 0;
266
1aedc35f 267 if (TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff
P
268 && add_property_names("colour", "urn", "clouds", "pot", "day", "night",
269 NULL)
1aedc35f 270 && TEST_ptr(prop = ossl_parse_property(NULL, merge_tests[n].prop))
1e08f3ba
P
271 && TEST_ptr(q_global = ossl_parse_query(NULL, merge_tests[n].q_global,
272 0))
273 && TEST_ptr(q_local = ossl_parse_query(NULL, merge_tests[n].q_local, 0))
1bdbdaff 274 && TEST_ptr(q_combined = ossl_property_merge(q_local, q_global))
da89ac0b 275 && TEST_int_ge(ossl_property_match_count(q_combined, prop), 0))
1bdbdaff
P
276 r = 1;
277 ossl_property_free(q_global);
278 ossl_property_free(q_local);
279 ossl_property_free(q_combined);
280 ossl_property_free(prop);
281 ossl_method_store_free(store);
282 return r;
283}
284
285static int test_property_defn_cache(void)
286{
287 OSSL_METHOD_STORE *store;
92a25e24
TM
288 OSSL_PROPERTY_LIST *red = NULL, *blue = NULL, *blue2 = NULL;
289 int r;
1bdbdaff 290
92a25e24 291 r = TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff 292 && add_property_names("red", "blue", NULL)
1aedc35f
MC
293 && TEST_ptr(red = ossl_parse_property(NULL, "red"))
294 && TEST_ptr(blue = ossl_parse_property(NULL, "blue"))
1bdbdaff 295 && TEST_ptr_ne(red, blue)
92a25e24
TM
296 && TEST_true(ossl_prop_defn_set(NULL, "red", &red));
297
298 if (!r) {
299 ossl_property_free(red);
300 red = NULL;
301 ossl_property_free(blue);
302 blue = NULL;
303 }
304
305 r = r && TEST_true(ossl_prop_defn_set(NULL, "blue", &blue));
306 if (!r) {
307 ossl_property_free(blue);
308 blue = NULL;
309 }
310
311 r = r && TEST_ptr_eq(ossl_prop_defn_get(NULL, "red"), red)
312 && TEST_ptr_eq(ossl_prop_defn_get(NULL, "blue"), blue)
313 && TEST_ptr(blue2 = ossl_parse_property(NULL, "blue"))
314 && TEST_ptr_ne(blue2, blue)
315 && TEST_true(ossl_prop_defn_set(NULL, "blue", &blue2));
316 if (!r) {
317 ossl_property_free(blue2);
318 blue2 = NULL;
319 }
320
321 r = r && TEST_ptr_eq(blue2, blue)
322 && TEST_ptr_eq(ossl_prop_defn_get(NULL, "blue"), blue);
323
1bdbdaff
P
324 ossl_method_store_free(store);
325 return r;
326}
327
328static const struct {
329 const char *defn;
330 const char *query;
331 int e;
332} definition_tests[] = {
333 { "alpha", "alpha=yes", 1 },
da89ac0b 334 { "alpha=no", "alpha", -1 },
1bdbdaff 335 { "alpha=1", "alpha=1", 1 },
da89ac0b
P
336 { "alpha=2", "alpha=1",-1 },
337 { "alpha", "omega", -1 },
338 { "alpha", "?omega", 0 },
339 { "alpha", "?omega=1", 0 },
340 { "alpha", "?omega=no", 1 },
341 { "alpha", "?omega=yes", 0 },
342 { "alpha, omega", "?omega=yes", 1 },
343 { "alpha, omega", "?omega=no", 0 }
1bdbdaff
P
344};
345
346static int test_definition_compares(int n)
347{
348 OSSL_METHOD_STORE *store;
349 OSSL_PROPERTY_LIST *d = NULL, *q = NULL;
350 int r;
351
1aedc35f 352 r = TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff 353 && add_property_names("alpha", "omega", NULL)
1aedc35f 354 && TEST_ptr(d = ossl_parse_property(NULL, definition_tests[n].defn))
1e08f3ba 355 && TEST_ptr(q = ossl_parse_query(NULL, definition_tests[n].query, 0))
da89ac0b 356 && TEST_int_eq(ossl_property_match_count(q, d), definition_tests[n].e);
1bdbdaff
P
357
358 ossl_property_free(d);
359 ossl_property_free(q);
360 ossl_method_store_free(store);
361 return r;
362}
363
364static int test_register_deregister(void)
365{
366 static const struct {
367 int nid;
368 const char *prop;
369 char *impl;
370 } impls[] = {
371 { 6, "position=1", "a" },
372 { 6, "position=2", "b" },
373 { 6, "position=3", "c" },
374 { 6, "position=4", "d" },
375 };
376 size_t i;
377 int ret = 0;
378 OSSL_METHOD_STORE *store;
dc010ca6 379 OSSL_PROVIDER prov = { 1 };
1bdbdaff 380
1aedc35f 381 if (!TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff
P
382 || !add_property_names("position", NULL))
383 goto err;
384
385 for (i = 0; i < OSSL_NELEM(impls); i++)
dc010ca6 386 if (!TEST_true(ossl_method_store_add(store, &prov, impls[i].nid,
c1d56231 387 impls[i].prop, impls[i].impl,
bdbf2df2 388 &up_ref, &down_ref))) {
1bdbdaff
P
389 TEST_note("iteration %zd", i + 1);
390 goto err;
391 }
392
393 /* Deregister in a different order to registration */
394 for (i = 0; i < OSSL_NELEM(impls); i++) {
395 const size_t j = (1 + i * 3) % OSSL_NELEM(impls);
396 int nid = impls[j].nid;
397 void *impl = impls[j].impl;
398
399 if (!TEST_true(ossl_method_store_remove(store, nid, impl))
400 || !TEST_false(ossl_method_store_remove(store, nid, impl))) {
401 TEST_note("iteration %zd, position %zd", i + 1, j + 1);
402 goto err;
403 }
404 }
405
406 if (TEST_false(ossl_method_store_remove(store, impls[0].nid, impls[0].impl)))
407 ret = 1;
408err:
409 ossl_method_store_free(store);
410 return ret;
411}
412
413static int test_property(void)
414{
dc010ca6
RL
415 static OSSL_PROVIDER fake_provider1 = { 1 };
416 static OSSL_PROVIDER fake_provider2 = { 2 };
417 static const OSSL_PROVIDER *fake_prov1 = &fake_provider1;
418 static const OSSL_PROVIDER *fake_prov2 = &fake_provider2;
1bdbdaff 419 static const struct {
dc010ca6 420 const OSSL_PROVIDER **prov;
1bdbdaff
P
421 int nid;
422 const char *prop;
423 char *impl;
424 } impls[] = {
dc010ca6
RL
425 { &fake_prov1, 1, "fast=no, colour=green", "a" },
426 { &fake_prov1, 1, "fast, colour=blue", "b" },
427 { &fake_prov1, 1, "", "-" },
428 { &fake_prov2, 9, "sky=blue, furry", "c" },
429 { &fake_prov2, 3, NULL, "d" },
430 { &fake_prov2, 6, "sky.colour=blue, sky=green, old.data", "e" },
1bdbdaff
P
431 };
432 static struct {
dc010ca6 433 const OSSL_PROVIDER **prov;
1bdbdaff
P
434 int nid;
435 const char *prop;
436 char *expected;
437 } queries[] = {
dc010ca6
RL
438 { &fake_prov1, 1, "fast", "b" },
439 { &fake_prov1, 1, "fast=yes", "b" },
440 { &fake_prov1, 1, "fast=no, colour=green", "a" },
441 { &fake_prov1, 1, "colour=blue, fast", "b" },
442 { &fake_prov1, 1, "colour=blue", "b" },
443 { &fake_prov2, 9, "furry", "c" },
444 { &fake_prov2, 6, "sky.colour=blue", "e" },
445 { &fake_prov2, 6, "old.data", "e" },
446 { &fake_prov2, 9, "furry=yes, sky=blue", "c" },
447 { &fake_prov1, 1, "", "a" },
448 { &fake_prov2, 3, "", "d" },
1bdbdaff
P
449 };
450 OSSL_METHOD_STORE *store;
451 size_t i;
452 int ret = 0;
453 void *result;
454
1aedc35f 455 if (!TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff
P
456 || !add_property_names("fast", "colour", "sky", "furry", NULL))
457 goto err;
458
459 for (i = 0; i < OSSL_NELEM(impls); i++)
dc010ca6
RL
460 if (!TEST_true(ossl_method_store_add(store, *impls[i].prov,
461 impls[i].nid, impls[i].prop,
462 impls[i].impl,
bdbf2df2 463 &up_ref, &down_ref))) {
1bdbdaff
P
464 TEST_note("iteration %zd", i + 1);
465 goto err;
466 }
dc010ca6
RL
467 /*
468 * The first check of queries is with NULL given as provider. All
469 * queries are expected to succeed.
470 */
1bdbdaff 471 for (i = 0; i < OSSL_NELEM(queries); i++) {
dc010ca6 472 const OSSL_PROVIDER *nullprov = NULL;
1bdbdaff
P
473 OSSL_PROPERTY_LIST *pq = NULL;
474
dc010ca6
RL
475 if (!TEST_true(ossl_method_store_fetch(store,
476 queries[i].nid, queries[i].prop,
477 &nullprov, &result))
1bdbdaff
P
478 || !TEST_str_eq((char *)result, queries[i].expected)) {
479 TEST_note("iteration %zd", i + 1);
480 ossl_property_free(pq);
481 goto err;
482 }
483 ossl_property_free(pq);
484 }
dc010ca6
RL
485 /*
486 * The second check of queries is with &address1 given as provider.
487 */
488 for (i = 0; i < OSSL_NELEM(queries); i++) {
489 OSSL_PROPERTY_LIST *pq = NULL;
490
491 result = NULL;
492 if (queries[i].prov == &fake_prov1) {
493 if (!TEST_true(ossl_method_store_fetch(store,
494 queries[i].nid,
495 queries[i].prop,
496 &fake_prov1, &result))
497 || !TEST_ptr_eq(fake_prov1, &fake_provider1)
498 || !TEST_str_eq((char *)result, queries[i].expected)) {
499 TEST_note("iteration %zd", i + 1);
500 ossl_property_free(pq);
501 goto err;
502 }
503 } else {
504 if (!TEST_false(ossl_method_store_fetch(store,
505 queries[i].nid,
506 queries[i].prop,
507 &fake_prov1, &result))
508 || !TEST_ptr_eq(fake_prov1, &fake_provider1)
509 || !TEST_ptr_null(result)) {
510 TEST_note("iteration %zd", i + 1);
511 ossl_property_free(pq);
512 goto err;
513 }
514 }
515 ossl_property_free(pq);
516 }
517 /*
518 * The third check of queries is with &address2 given as provider.
519 */
520 for (i = 0; i < OSSL_NELEM(queries); i++) {
521 OSSL_PROPERTY_LIST *pq = NULL;
522
523 result = NULL;
524 if (queries[i].prov == &fake_prov2) {
525 if (!TEST_true(ossl_method_store_fetch(store,
526 queries[i].nid,
527 queries[i].prop,
528 &fake_prov2, &result))
529 || !TEST_ptr_eq(fake_prov2, &fake_provider2)
530 || !TEST_str_eq((char *)result, queries[i].expected)) {
531 TEST_note("iteration %zd", i + 1);
532 ossl_property_free(pq);
533 goto err;
534 }
535 } else {
536 if (!TEST_false(ossl_method_store_fetch(store,
537 queries[i].nid,
538 queries[i].prop,
539 &fake_prov2, &result))
540 || !TEST_ptr_eq(fake_prov2, &fake_provider2)
541 || !TEST_ptr_null(result)) {
542 TEST_note("iteration %zd", i + 1);
543 ossl_property_free(pq);
544 goto err;
545 }
546 }
547 ossl_property_free(pq);
548 }
1bdbdaff
P
549 ret = 1;
550err:
551 ossl_method_store_free(store);
552 return ret;
553}
554
555static int test_query_cache_stochastic(void)
556{
557 const int max = 10000, tail = 10;
558 OSSL_METHOD_STORE *store;
559 int i, res = 0;
560 char buf[50];
561 void *result;
562 int errors = 0;
563 int v[10001];
dc010ca6 564 OSSL_PROVIDER prov = { 1 };
1bdbdaff 565
1aedc35f 566 if (!TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff
P
567 || !add_property_names("n", NULL))
568 goto err;
569
570 for (i = 1; i <= max; i++) {
571 v[i] = 2 * i;
572 BIO_snprintf(buf, sizeof(buf), "n=%d\n", i);
dc010ca6 573 if (!TEST_true(ossl_method_store_add(store, &prov, i, buf, "abc",
bdbf2df2 574 &up_ref, &down_ref))
dc010ca6
RL
575 || !TEST_true(ossl_method_store_cache_set(store, &prov, i,
576 buf, v + i,
bdbf2df2 577 &up_ref, &down_ref))
dc010ca6
RL
578 || !TEST_true(ossl_method_store_cache_set(store, &prov, i,
579 "n=1234", "miss",
580 &up_ref, &down_ref))) {
1bdbdaff
P
581 TEST_note("iteration %d", i);
582 goto err;
583 }
584 }
585 for (i = 1; i <= max; i++) {
586 BIO_snprintf(buf, sizeof(buf), "n=%d\n", i);
dc010ca6 587 if (!ossl_method_store_cache_get(store, NULL, i, buf, &result)
1bdbdaff
P
588 || result != v + i)
589 errors++;
590 }
591 /* There is a tiny probability that this will fail when it shouldn't */
592 res = TEST_int_gt(errors, tail) && TEST_int_lt(errors, max - tail);
593
594err:
595 ossl_method_store_free(store);
596 return res;
597}
598
e0624f0d
SL
599static int test_fips_mode(void)
600{
601 int ret = 0;
b4250010 602 OSSL_LIB_CTX *ctx = NULL;
e0624f0d 603
b4250010 604 if (!TEST_ptr(ctx = OSSL_LIB_CTX_new()))
e0624f0d
SL
605 goto err;
606
607 ret = TEST_true(EVP_set_default_properties(ctx, "default=yes,fips=yes"))
608 && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
609 && TEST_true(EVP_set_default_properties(ctx, "fips=no,default=yes"))
610 && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
611 && TEST_true(EVP_set_default_properties(ctx, "fips=no"))
612 && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
613 && TEST_true(EVP_set_default_properties(ctx, "fips!=no"))
614 && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
615 && TEST_true(EVP_set_default_properties(ctx, "fips=no"))
616 && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
617 && TEST_true(EVP_set_default_properties(ctx, "fips=no,default=yes"))
618 && TEST_true(EVP_default_properties_enable_fips(ctx, 1))
619 && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
620 && TEST_true(EVP_default_properties_enable_fips(ctx, 0))
621 && TEST_false(EVP_default_properties_is_fips_enabled(ctx));
622err:
b4250010 623 OSSL_LIB_CTX_free(ctx);
e0624f0d
SL
624 return ret;
625}
626
862497a9
P
627static struct {
628 const char *in;
629 const char *out;
630} to_string_tests[] = {
631 { "fips=yes", "fips=yes" },
632 { "fips!=yes", "fips!=yes" },
633 { "fips = yes", "fips=yes" },
634 { "fips", "fips=yes" },
635 { "fips=no", "fips=no" },
636 { "-fips", "-fips" },
637 { "?fips=yes", "?fips=yes" },
638 { "fips=yes,provider=fips", "fips=yes,provider=fips" },
639 { "fips = yes , provider = fips", "fips=yes,provider=fips" },
640 { "fips=yes,provider!=fips", "fips=yes,provider!=fips" },
641 { "fips=yes,?provider=fips", "fips=yes,?provider=fips" },
642 { "fips=yes,-provider", "fips=yes,-provider" },
643 /* foo is an unknown internal name */
644 { "foo=yes,fips=yes", "fips=yes"},
645 { "", "" },
646 { "fips=3", "fips=3" },
647 { "fips=-3", "fips=-3" },
648 { NULL, "" }
649};
650
651static int test_property_list_to_string(int i)
ad8570a8
MC
652{
653 OSSL_PROPERTY_LIST *pl = NULL;
654 int ret = 0;
862497a9 655 size_t bufsize;
ad8570a8
MC
656 char *buf = NULL;
657
862497a9
P
658 if (to_string_tests[i].in != NULL
659 && !TEST_ptr(pl = ossl_parse_query(NULL, to_string_tests[i].in, 1)))
660 goto err;
661 bufsize = ossl_property_list_to_string(NULL, pl, NULL, 0);
662 if (!TEST_size_t_gt(bufsize, 0))
663 goto err;
664 buf = OPENSSL_malloc(bufsize);
665 if (!TEST_ptr(buf)
666 || !TEST_size_t_eq(ossl_property_list_to_string(NULL, pl, buf,
667 bufsize),
668 bufsize)
669 || !TEST_str_eq(to_string_tests[i].out, buf)
670 || !TEST_size_t_eq(bufsize, strlen(to_string_tests[i].out) + 1))
671 goto err;
ad8570a8
MC
672
673 ret = 1;
674 err:
675 OPENSSL_free(buf);
676 ossl_property_free(pl);
677 return ret;
678}
e0624f0d 679
1bdbdaff
P
680int setup_tests(void)
681{
682 ADD_TEST(test_property_string);
1e08f3ba 683 ADD_TEST(test_property_query_value_create);
1bdbdaff 684 ADD_ALL_TESTS(test_property_parse, OSSL_NELEM(parser_tests));
747d1423 685 ADD_ALL_TESTS(test_property_parse_error, OSSL_NELEM(parse_error_tests));
1bdbdaff
P
686 ADD_ALL_TESTS(test_property_merge, OSSL_NELEM(merge_tests));
687 ADD_TEST(test_property_defn_cache);
688 ADD_ALL_TESTS(test_definition_compares, OSSL_NELEM(definition_tests));
689 ADD_TEST(test_register_deregister);
690 ADD_TEST(test_property);
691 ADD_TEST(test_query_cache_stochastic);
e0624f0d 692 ADD_TEST(test_fips_mode);
862497a9 693 ADD_ALL_TESTS(test_property_list_to_string, OSSL_NELEM(to_string_tests));
1bdbdaff
P
694 return 1;
695}