]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/property_test.c
core_get_libctx: use assert() instead of ossl_assert()
[thirdparty/openssl.git] / test / property_test.c
CommitLineData
1bdbdaff 1/*
454afd98 2 * Copyright 2019-2020 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
P
17
18static int add_property_names(const char *n, ...)
19{
20 va_list args;
21 int res = 1;
22
23 va_start(args, n);
24 do {
1aedc35f 25 if (!TEST_int_ne(ossl_property_name(NULL, n, 1), 0))
1bdbdaff
P
26 res = 0;
27 } while ((n = va_arg(args, const char *)) != NULL);
28 va_end(args);
29 return res;
30}
31
bdbf2df2
P
32static int up_ref(void *p)
33{
34 return 1;
35}
36
37static void down_ref(void *p)
38{
39}
40
1bdbdaff
P
41static int test_property_string(void)
42{
43 OSSL_METHOD_STORE *store;
44 int res = 0;
45 OSSL_PROPERTY_IDX i, j;
46
1aedc35f
MC
47 if (TEST_ptr(store = ossl_method_store_new(NULL))
48 && TEST_int_eq(ossl_property_name(NULL, "fnord", 0), 0)
49 && TEST_int_ne(ossl_property_name(NULL, "fnord", 1), 0)
50 && TEST_int_ne(ossl_property_name(NULL, "name", 1), 0)
1bdbdaff 51 /* Property value checks */
1aedc35f
MC
52 && TEST_int_eq(ossl_property_value(NULL, "fnord", 0), 0)
53 && TEST_int_ne(i = ossl_property_value(NULL, "no", 0), 0)
54 && TEST_int_ne(j = ossl_property_value(NULL, "yes", 0), 0)
1bdbdaff 55 && TEST_int_ne(i, j)
1aedc35f
MC
56 && TEST_int_eq(ossl_property_value(NULL, "yes", 1), j)
57 && TEST_int_eq(ossl_property_value(NULL, "no", 1), i)
58 && TEST_int_ne(i = ossl_property_value(NULL, "illuminati", 1), 0)
59 && TEST_int_eq(j = ossl_property_value(NULL, "fnord", 1), i + 1)
60 && TEST_int_eq(ossl_property_value(NULL, "fnord", 1), j)
1bdbdaff 61 /* Check name and values are distinct */
1aedc35f
MC
62 && TEST_int_eq(ossl_property_value(NULL, "cold", 0), 0)
63 && TEST_int_ne(ossl_property_name(NULL, "fnord", 0),
64 ossl_property_value(NULL, "fnord", 0)))
1bdbdaff
P
65 res = 1;
66 ossl_method_store_free(store);
67 return res;
68}
69
70static const struct {
71 const char *defn;
72 const char *query;
73 int e;
74} parser_tests[] = {
da89ac0b 75 { "", "sky=blue", -1 },
1bdbdaff 76 { "", "sky!=blue", 1 },
da89ac0b 77 { "groan", "", 0 },
1bdbdaff
P
78 { "cold=yes", "cold=yes", 1 },
79 { "cold=yes", "cold", 1 },
80 { "cold=yes", "cold!=no", 1 },
81 { "groan", "groan=yes", 1 },
da89ac0b
P
82 { "groan", "groan=no", -1 },
83 { "groan", "groan!=yes", -1 },
84 { "cold=no", "cold", -1 },
85 { "cold=no", "?cold", 0 },
1bdbdaff 86 { "cold=no", "cold=no", 1 },
da89ac0b 87 { "groan", "cold", -1 },
1bdbdaff
P
88 { "groan", "cold=no", 1 },
89 { "groan", "cold!=yes", 1 },
da89ac0b
P
90 { "groan=blue", "groan=yellow", -1 },
91 { "groan=blue", "?groan=yellow", 0 },
1bdbdaff 92 { "groan=blue", "groan!=yellow", 1 },
da89ac0b 93 { "groan=blue", "?groan!=yellow", 1 },
1bdbdaff 94 { "today=monday, tomorrow=3", "today!=2", 1 },
da89ac0b 95 { "today=monday, tomorrow=3", "today!='monday'", -1 },
1bdbdaff
P
96 { "today=monday, tomorrow=3", "tomorrow=3", 1 },
97 { "n=0x3", "n=3", 1 },
da89ac0b 98 { "n=0x3", "n=-3", -1 },
1bdbdaff
P
99 { "n=0x33", "n=51", 1 },
100 { "n=033", "n=27", 1 },
101 { "n=0", "n=00", 1 },
102 { "n=0x0", "n=0", 1 },
da89ac0b
P
103 { "n=0, sky=blue", "?n=0, sky=blue", 2 },
104 { "n=1, sky=blue", "?n=0, sky=blue", 1 },
1bdbdaff
P
105};
106
107static int test_property_parse(int n)
108{
109 OSSL_METHOD_STORE *store;
110 OSSL_PROPERTY_LIST *p = NULL, *q = NULL;
111 int r = 0;
112
1aedc35f 113 if (TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff
P
114 && add_property_names("sky", "groan", "cold", "today", "tomorrow", "n",
115 NULL)
1aedc35f 116 && TEST_ptr(p = ossl_parse_property(NULL, parser_tests[n].defn))
1e08f3ba 117 && TEST_ptr(q = ossl_parse_query(NULL, parser_tests[n].query, 0))
da89ac0b 118 && TEST_int_eq(ossl_property_match_count(q, p), parser_tests[n].e))
1bdbdaff
P
119 r = 1;
120 ossl_property_free(p);
121 ossl_property_free(q);
122 ossl_method_store_free(store);
123 return r;
124}
125
1e08f3ba
P
126static int test_property_query_value_create(void)
127{
128 OSSL_METHOD_STORE *store;
129 OSSL_PROPERTY_LIST *p = NULL, *q = NULL, *o = NULL;
130 int r = 0;
131
132 if (TEST_ptr(store = ossl_method_store_new(NULL))
133 && add_property_names("sky", NULL)
134 && TEST_ptr(p = ossl_parse_query(NULL, "sky=green", 0)) /* undefined */
135 && TEST_ptr(q = ossl_parse_query(NULL, "sky=green", 1)) /* creates */
136 && TEST_ptr(o = ossl_parse_query(NULL, "sky=green", 0)) /* defined */
137 && TEST_int_eq(ossl_property_match_count(q, p), -1)
138 && TEST_int_eq(ossl_property_match_count(q, o), 1))
139 r = 1;
140 ossl_property_free(o);
141 ossl_property_free(p);
142 ossl_property_free(q);
143 ossl_method_store_free(store);
144 return r;
145}
146
1bdbdaff
P
147static const struct {
148 const char *q_global;
149 const char *q_local;
150 const char *prop;
151} merge_tests[] = {
152 { "", "colour=blue", "colour=blue" },
153 { "colour=blue", "", "colour=blue" },
154 { "colour=red", "colour=blue", "colour=blue" },
155 { "clouds=pink, urn=red", "urn=blue, colour=green",
156 "urn=blue, colour=green, clouds=pink" },
157 { "pot=gold", "urn=blue", "pot=gold, urn=blue" },
158 { "night", "day", "day=yes, night=yes" },
159 { "day", "night", "day=yes, night=yes" },
160 { "", "", "" },
161 /*
162 * The following four leave 'day' unspecified in the query, and will match
163 * any definition
164 */
165 { "day=yes", "-day", "day=no" },
166 { "day=yes", "-day", "day=yes" },
167 { "day=yes", "-day", "day=arglebargle" },
168 { "day=yes", "-day", "pot=sesquioxidizing" },
169 { "day, night", "-night, day", "day=yes, night=no" },
170 { "-day", "day=yes", "day=yes" },
171};
172
173static int test_property_merge(int n)
174{
175 OSSL_METHOD_STORE *store;
176 OSSL_PROPERTY_LIST *q_global = NULL, *q_local = NULL;
177 OSSL_PROPERTY_LIST *q_combined = NULL, *prop = NULL;
178 int r = 0;
179
1aedc35f 180 if (TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff
P
181 && add_property_names("colour", "urn", "clouds", "pot", "day", "night",
182 NULL)
1aedc35f 183 && TEST_ptr(prop = ossl_parse_property(NULL, merge_tests[n].prop))
1e08f3ba
P
184 && TEST_ptr(q_global = ossl_parse_query(NULL, merge_tests[n].q_global,
185 0))
186 && TEST_ptr(q_local = ossl_parse_query(NULL, merge_tests[n].q_local, 0))
1bdbdaff 187 && TEST_ptr(q_combined = ossl_property_merge(q_local, q_global))
da89ac0b 188 && TEST_int_ge(ossl_property_match_count(q_combined, prop), 0))
1bdbdaff
P
189 r = 1;
190 ossl_property_free(q_global);
191 ossl_property_free(q_local);
192 ossl_property_free(q_combined);
193 ossl_property_free(prop);
194 ossl_method_store_free(store);
195 return r;
196}
197
198static int test_property_defn_cache(void)
199{
200 OSSL_METHOD_STORE *store;
201 OSSL_PROPERTY_LIST *red, *blue;
202 int r = 0;
203
1aedc35f 204 if (TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff 205 && add_property_names("red", "blue", NULL)
1aedc35f
MC
206 && TEST_ptr(red = ossl_parse_property(NULL, "red"))
207 && TEST_ptr(blue = ossl_parse_property(NULL, "blue"))
1bdbdaff 208 && TEST_ptr_ne(red, blue)
1aedc35f
MC
209 && TEST_true(ossl_prop_defn_set(NULL, "red", red))
210 && TEST_true(ossl_prop_defn_set(NULL, "blue", blue))
211 && TEST_ptr_eq(ossl_prop_defn_get(NULL, "red"), red)
212 && TEST_ptr_eq(ossl_prop_defn_get(NULL, "blue"), blue))
1bdbdaff
P
213 r = 1;
214 ossl_method_store_free(store);
215 return r;
216}
217
218static const struct {
219 const char *defn;
220 const char *query;
221 int e;
222} definition_tests[] = {
223 { "alpha", "alpha=yes", 1 },
da89ac0b 224 { "alpha=no", "alpha", -1 },
1bdbdaff 225 { "alpha=1", "alpha=1", 1 },
da89ac0b
P
226 { "alpha=2", "alpha=1",-1 },
227 { "alpha", "omega", -1 },
228 { "alpha", "?omega", 0 },
229 { "alpha", "?omega=1", 0 },
230 { "alpha", "?omega=no", 1 },
231 { "alpha", "?omega=yes", 0 },
232 { "alpha, omega", "?omega=yes", 1 },
233 { "alpha, omega", "?omega=no", 0 }
1bdbdaff
P
234};
235
236static int test_definition_compares(int n)
237{
238 OSSL_METHOD_STORE *store;
239 OSSL_PROPERTY_LIST *d = NULL, *q = NULL;
240 int r;
241
1aedc35f 242 r = TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff 243 && add_property_names("alpha", "omega", NULL)
1aedc35f 244 && TEST_ptr(d = ossl_parse_property(NULL, definition_tests[n].defn))
1e08f3ba 245 && TEST_ptr(q = ossl_parse_query(NULL, definition_tests[n].query, 0))
da89ac0b 246 && TEST_int_eq(ossl_property_match_count(q, d), definition_tests[n].e);
1bdbdaff
P
247
248 ossl_property_free(d);
249 ossl_property_free(q);
250 ossl_method_store_free(store);
251 return r;
252}
253
254static int test_register_deregister(void)
255{
256 static const struct {
257 int nid;
258 const char *prop;
259 char *impl;
260 } impls[] = {
261 { 6, "position=1", "a" },
262 { 6, "position=2", "b" },
263 { 6, "position=3", "c" },
264 { 6, "position=4", "d" },
265 };
266 size_t i;
267 int ret = 0;
268 OSSL_METHOD_STORE *store;
269
1aedc35f 270 if (!TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff
P
271 || !add_property_names("position", NULL))
272 goto err;
273
274 for (i = 0; i < OSSL_NELEM(impls); i++)
c1d56231
RL
275 if (!TEST_true(ossl_method_store_add(store, NULL, impls[i].nid,
276 impls[i].prop, impls[i].impl,
bdbf2df2 277 &up_ref, &down_ref))) {
1bdbdaff
P
278 TEST_note("iteration %zd", i + 1);
279 goto err;
280 }
281
282 /* Deregister in a different order to registration */
283 for (i = 0; i < OSSL_NELEM(impls); i++) {
284 const size_t j = (1 + i * 3) % OSSL_NELEM(impls);
285 int nid = impls[j].nid;
286 void *impl = impls[j].impl;
287
288 if (!TEST_true(ossl_method_store_remove(store, nid, impl))
289 || !TEST_false(ossl_method_store_remove(store, nid, impl))) {
290 TEST_note("iteration %zd, position %zd", i + 1, j + 1);
291 goto err;
292 }
293 }
294
295 if (TEST_false(ossl_method_store_remove(store, impls[0].nid, impls[0].impl)))
296 ret = 1;
297err:
298 ossl_method_store_free(store);
299 return ret;
300}
301
302static int test_property(void)
303{
304 static const struct {
305 int nid;
306 const char *prop;
307 char *impl;
308 } impls[] = {
309 { 1, "fast=no, colour=green", "a" },
310 { 1, "fast, colour=blue", "b" },
311 { 1, "", "-" },
312 { 9, "sky=blue, furry", "c" },
313 { 3, NULL, "d" },
314 { 6, "sky.colour=blue, sky=green, old.data", "e" },
315 };
316 static struct {
317 int nid;
318 const char *prop;
319 char *expected;
320 } queries[] = {
321 { 1, "fast", "b" },
322 { 1, "fast=yes", "b" },
323 { 1, "fast=no, colour=green", "a" },
324 { 1, "colour=blue, fast", "b" },
325 { 1, "colour=blue", "b" },
326 { 9, "furry", "c" },
327 { 6, "sky.colour=blue", "e" },
328 { 6, "old.data", "e" },
329 { 9, "furry=yes, sky=blue", "c" },
330 { 1, "", "a" },
331 { 3, "", "d" },
332 };
333 OSSL_METHOD_STORE *store;
334 size_t i;
335 int ret = 0;
336 void *result;
337
1aedc35f 338 if (!TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff
P
339 || !add_property_names("fast", "colour", "sky", "furry", NULL))
340 goto err;
341
342 for (i = 0; i < OSSL_NELEM(impls); i++)
c1d56231
RL
343 if (!TEST_true(ossl_method_store_add(store, NULL, impls[i].nid,
344 impls[i].prop, impls[i].impl,
bdbf2df2 345 &up_ref, &down_ref))) {
1bdbdaff
P
346 TEST_note("iteration %zd", i + 1);
347 goto err;
348 }
349 for (i = 0; i < OSSL_NELEM(queries); i++) {
350 OSSL_PROPERTY_LIST *pq = NULL;
351
ef9f6066
P
352 if (!TEST_true(ossl_method_store_fetch(store, queries[i].nid,
353 queries[i].prop, &result))
1bdbdaff
P
354 || !TEST_str_eq((char *)result, queries[i].expected)) {
355 TEST_note("iteration %zd", i + 1);
356 ossl_property_free(pq);
357 goto err;
358 }
359 ossl_property_free(pq);
360 }
361 ret = 1;
362err:
363 ossl_method_store_free(store);
364 return ret;
365}
366
367static int test_query_cache_stochastic(void)
368{
369 const int max = 10000, tail = 10;
370 OSSL_METHOD_STORE *store;
371 int i, res = 0;
372 char buf[50];
373 void *result;
374 int errors = 0;
375 int v[10001];
376
1aedc35f 377 if (!TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff
P
378 || !add_property_names("n", NULL))
379 goto err;
380
381 for (i = 1; i <= max; i++) {
382 v[i] = 2 * i;
383 BIO_snprintf(buf, sizeof(buf), "n=%d\n", i);
c1d56231 384 if (!TEST_true(ossl_method_store_add(store, NULL, i, buf, "abc",
bdbf2df2
P
385 &up_ref, &down_ref))
386 || !TEST_true(ossl_method_store_cache_set(store, i, buf, v + i,
387 &up_ref, &down_ref))
1bdbdaff 388 || !TEST_true(ossl_method_store_cache_set(store, i, "n=1234",
bdbf2df2
P
389 "miss", &up_ref,
390 &down_ref))) {
1bdbdaff
P
391 TEST_note("iteration %d", i);
392 goto err;
393 }
394 }
395 for (i = 1; i <= max; i++) {
396 BIO_snprintf(buf, sizeof(buf), "n=%d\n", i);
397 if (!ossl_method_store_cache_get(store, i, buf, &result)
398 || result != v + i)
399 errors++;
400 }
401 /* There is a tiny probability that this will fail when it shouldn't */
402 res = TEST_int_gt(errors, tail) && TEST_int_lt(errors, max - tail);
403
404err:
405 ossl_method_store_free(store);
406 return res;
407}
408
e0624f0d
SL
409static int test_fips_mode(void)
410{
411 int ret = 0;
b4250010 412 OSSL_LIB_CTX *ctx = NULL;
e0624f0d 413
b4250010 414 if (!TEST_ptr(ctx = OSSL_LIB_CTX_new()))
e0624f0d
SL
415 goto err;
416
417 ret = TEST_true(EVP_set_default_properties(ctx, "default=yes,fips=yes"))
418 && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
419 && TEST_true(EVP_set_default_properties(ctx, "fips=no,default=yes"))
420 && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
421 && TEST_true(EVP_set_default_properties(ctx, "fips=no"))
422 && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
423 && TEST_true(EVP_set_default_properties(ctx, "fips!=no"))
424 && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
425 && TEST_true(EVP_set_default_properties(ctx, "fips=no"))
426 && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
427 && TEST_true(EVP_set_default_properties(ctx, "fips=no,default=yes"))
428 && TEST_true(EVP_default_properties_enable_fips(ctx, 1))
429 && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
430 && TEST_true(EVP_default_properties_enable_fips(ctx, 0))
431 && TEST_false(EVP_default_properties_is_fips_enabled(ctx));
432err:
b4250010 433 OSSL_LIB_CTX_free(ctx);
e0624f0d
SL
434 return ret;
435}
436
437
1bdbdaff
P
438int setup_tests(void)
439{
440 ADD_TEST(test_property_string);
1e08f3ba 441 ADD_TEST(test_property_query_value_create);
1bdbdaff
P
442 ADD_ALL_TESTS(test_property_parse, OSSL_NELEM(parser_tests));
443 ADD_ALL_TESTS(test_property_merge, OSSL_NELEM(merge_tests));
444 ADD_TEST(test_property_defn_cache);
445 ADD_ALL_TESTS(test_definition_compares, OSSL_NELEM(definition_tests));
446 ADD_TEST(test_register_deregister);
447 ADD_TEST(test_property);
448 ADD_TEST(test_query_cache_stochastic);
e0624f0d 449 ADD_TEST(test_fips_mode);
1bdbdaff
P
450 return 1;
451}