]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/property_test.c
Update copyright year
[thirdparty/openssl.git] / test / property_test.c
CommitLineData
1bdbdaff 1/*
3c2bdd7d 2 * Copyright 2019-2021 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
a289d3a4 132 /* The property value used here must not be used in other test cases */
1e08f3ba 133 if (TEST_ptr(store = ossl_method_store_new(NULL))
a289d3a4
TM
134 && add_property_names("wood", NULL)
135 && TEST_ptr(p = ossl_parse_query(NULL, "wood=oak", 0)) /* undefined */
136 && TEST_ptr(q = ossl_parse_query(NULL, "wood=oak", 1)) /* creates */
137 && TEST_ptr(o = ossl_parse_query(NULL, "wood=oak", 0)) /* defined */
1e08f3ba
P
138 && TEST_int_eq(ossl_property_match_count(q, p), -1)
139 && TEST_int_eq(ossl_property_match_count(q, o), 1))
140 r = 1;
141 ossl_property_free(o);
142 ossl_property_free(p);
143 ossl_property_free(q);
144 ossl_method_store_free(store);
145 return r;
146}
147
1bdbdaff
P
148static const struct {
149 const char *q_global;
150 const char *q_local;
151 const char *prop;
152} merge_tests[] = {
153 { "", "colour=blue", "colour=blue" },
154 { "colour=blue", "", "colour=blue" },
155 { "colour=red", "colour=blue", "colour=blue" },
156 { "clouds=pink, urn=red", "urn=blue, colour=green",
157 "urn=blue, colour=green, clouds=pink" },
158 { "pot=gold", "urn=blue", "pot=gold, urn=blue" },
159 { "night", "day", "day=yes, night=yes" },
160 { "day", "night", "day=yes, night=yes" },
161 { "", "", "" },
162 /*
163 * The following four leave 'day' unspecified in the query, and will match
164 * any definition
165 */
166 { "day=yes", "-day", "day=no" },
167 { "day=yes", "-day", "day=yes" },
168 { "day=yes", "-day", "day=arglebargle" },
169 { "day=yes", "-day", "pot=sesquioxidizing" },
170 { "day, night", "-night, day", "day=yes, night=no" },
171 { "-day", "day=yes", "day=yes" },
172};
173
174static int test_property_merge(int n)
175{
176 OSSL_METHOD_STORE *store;
177 OSSL_PROPERTY_LIST *q_global = NULL, *q_local = NULL;
178 OSSL_PROPERTY_LIST *q_combined = NULL, *prop = NULL;
179 int r = 0;
180
1aedc35f 181 if (TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff
P
182 && add_property_names("colour", "urn", "clouds", "pot", "day", "night",
183 NULL)
1aedc35f 184 && TEST_ptr(prop = ossl_parse_property(NULL, merge_tests[n].prop))
1e08f3ba
P
185 && TEST_ptr(q_global = ossl_parse_query(NULL, merge_tests[n].q_global,
186 0))
187 && TEST_ptr(q_local = ossl_parse_query(NULL, merge_tests[n].q_local, 0))
1bdbdaff 188 && TEST_ptr(q_combined = ossl_property_merge(q_local, q_global))
da89ac0b 189 && TEST_int_ge(ossl_property_match_count(q_combined, prop), 0))
1bdbdaff
P
190 r = 1;
191 ossl_property_free(q_global);
192 ossl_property_free(q_local);
193 ossl_property_free(q_combined);
194 ossl_property_free(prop);
195 ossl_method_store_free(store);
196 return r;
197}
198
199static int test_property_defn_cache(void)
200{
201 OSSL_METHOD_STORE *store;
202 OSSL_PROPERTY_LIST *red, *blue;
203 int r = 0;
204
1aedc35f 205 if (TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff 206 && add_property_names("red", "blue", NULL)
1aedc35f
MC
207 && TEST_ptr(red = ossl_parse_property(NULL, "red"))
208 && TEST_ptr(blue = ossl_parse_property(NULL, "blue"))
1bdbdaff 209 && TEST_ptr_ne(red, blue)
1aedc35f
MC
210 && TEST_true(ossl_prop_defn_set(NULL, "red", red))
211 && TEST_true(ossl_prop_defn_set(NULL, "blue", blue))
212 && TEST_ptr_eq(ossl_prop_defn_get(NULL, "red"), red)
213 && TEST_ptr_eq(ossl_prop_defn_get(NULL, "blue"), blue))
1bdbdaff
P
214 r = 1;
215 ossl_method_store_free(store);
216 return r;
217}
218
219static const struct {
220 const char *defn;
221 const char *query;
222 int e;
223} definition_tests[] = {
224 { "alpha", "alpha=yes", 1 },
da89ac0b 225 { "alpha=no", "alpha", -1 },
1bdbdaff 226 { "alpha=1", "alpha=1", 1 },
da89ac0b
P
227 { "alpha=2", "alpha=1",-1 },
228 { "alpha", "omega", -1 },
229 { "alpha", "?omega", 0 },
230 { "alpha", "?omega=1", 0 },
231 { "alpha", "?omega=no", 1 },
232 { "alpha", "?omega=yes", 0 },
233 { "alpha, omega", "?omega=yes", 1 },
234 { "alpha, omega", "?omega=no", 0 }
1bdbdaff
P
235};
236
237static int test_definition_compares(int n)
238{
239 OSSL_METHOD_STORE *store;
240 OSSL_PROPERTY_LIST *d = NULL, *q = NULL;
241 int r;
242
1aedc35f 243 r = TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff 244 && add_property_names("alpha", "omega", NULL)
1aedc35f 245 && TEST_ptr(d = ossl_parse_property(NULL, definition_tests[n].defn))
1e08f3ba 246 && TEST_ptr(q = ossl_parse_query(NULL, definition_tests[n].query, 0))
da89ac0b 247 && TEST_int_eq(ossl_property_match_count(q, d), definition_tests[n].e);
1bdbdaff
P
248
249 ossl_property_free(d);
250 ossl_property_free(q);
251 ossl_method_store_free(store);
252 return r;
253}
254
255static int test_register_deregister(void)
256{
257 static const struct {
258 int nid;
259 const char *prop;
260 char *impl;
261 } impls[] = {
262 { 6, "position=1", "a" },
263 { 6, "position=2", "b" },
264 { 6, "position=3", "c" },
265 { 6, "position=4", "d" },
266 };
267 size_t i;
268 int ret = 0;
269 OSSL_METHOD_STORE *store;
270
1aedc35f 271 if (!TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff
P
272 || !add_property_names("position", NULL))
273 goto err;
274
275 for (i = 0; i < OSSL_NELEM(impls); i++)
c1d56231
RL
276 if (!TEST_true(ossl_method_store_add(store, NULL, impls[i].nid,
277 impls[i].prop, impls[i].impl,
bdbf2df2 278 &up_ref, &down_ref))) {
1bdbdaff
P
279 TEST_note("iteration %zd", i + 1);
280 goto err;
281 }
282
283 /* Deregister in a different order to registration */
284 for (i = 0; i < OSSL_NELEM(impls); i++) {
285 const size_t j = (1 + i * 3) % OSSL_NELEM(impls);
286 int nid = impls[j].nid;
287 void *impl = impls[j].impl;
288
289 if (!TEST_true(ossl_method_store_remove(store, nid, impl))
290 || !TEST_false(ossl_method_store_remove(store, nid, impl))) {
291 TEST_note("iteration %zd, position %zd", i + 1, j + 1);
292 goto err;
293 }
294 }
295
296 if (TEST_false(ossl_method_store_remove(store, impls[0].nid, impls[0].impl)))
297 ret = 1;
298err:
299 ossl_method_store_free(store);
300 return ret;
301}
302
303static int test_property(void)
304{
305 static const struct {
306 int nid;
307 const char *prop;
308 char *impl;
309 } impls[] = {
310 { 1, "fast=no, colour=green", "a" },
311 { 1, "fast, colour=blue", "b" },
312 { 1, "", "-" },
313 { 9, "sky=blue, furry", "c" },
314 { 3, NULL, "d" },
315 { 6, "sky.colour=blue, sky=green, old.data", "e" },
316 };
317 static struct {
318 int nid;
319 const char *prop;
320 char *expected;
321 } queries[] = {
322 { 1, "fast", "b" },
323 { 1, "fast=yes", "b" },
324 { 1, "fast=no, colour=green", "a" },
325 { 1, "colour=blue, fast", "b" },
326 { 1, "colour=blue", "b" },
327 { 9, "furry", "c" },
328 { 6, "sky.colour=blue", "e" },
329 { 6, "old.data", "e" },
330 { 9, "furry=yes, sky=blue", "c" },
331 { 1, "", "a" },
332 { 3, "", "d" },
333 };
334 OSSL_METHOD_STORE *store;
335 size_t i;
336 int ret = 0;
337 void *result;
338
1aedc35f 339 if (!TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff
P
340 || !add_property_names("fast", "colour", "sky", "furry", NULL))
341 goto err;
342
343 for (i = 0; i < OSSL_NELEM(impls); i++)
c1d56231
RL
344 if (!TEST_true(ossl_method_store_add(store, NULL, impls[i].nid,
345 impls[i].prop, impls[i].impl,
bdbf2df2 346 &up_ref, &down_ref))) {
1bdbdaff
P
347 TEST_note("iteration %zd", i + 1);
348 goto err;
349 }
350 for (i = 0; i < OSSL_NELEM(queries); i++) {
351 OSSL_PROPERTY_LIST *pq = NULL;
352
ef9f6066
P
353 if (!TEST_true(ossl_method_store_fetch(store, queries[i].nid,
354 queries[i].prop, &result))
1bdbdaff
P
355 || !TEST_str_eq((char *)result, queries[i].expected)) {
356 TEST_note("iteration %zd", i + 1);
357 ossl_property_free(pq);
358 goto err;
359 }
360 ossl_property_free(pq);
361 }
362 ret = 1;
363err:
364 ossl_method_store_free(store);
365 return ret;
366}
367
368static int test_query_cache_stochastic(void)
369{
370 const int max = 10000, tail = 10;
371 OSSL_METHOD_STORE *store;
372 int i, res = 0;
373 char buf[50];
374 void *result;
375 int errors = 0;
376 int v[10001];
377
1aedc35f 378 if (!TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff
P
379 || !add_property_names("n", NULL))
380 goto err;
381
382 for (i = 1; i <= max; i++) {
383 v[i] = 2 * i;
384 BIO_snprintf(buf, sizeof(buf), "n=%d\n", i);
c1d56231 385 if (!TEST_true(ossl_method_store_add(store, NULL, i, buf, "abc",
bdbf2df2
P
386 &up_ref, &down_ref))
387 || !TEST_true(ossl_method_store_cache_set(store, i, buf, v + i,
388 &up_ref, &down_ref))
1bdbdaff 389 || !TEST_true(ossl_method_store_cache_set(store, i, "n=1234",
bdbf2df2
P
390 "miss", &up_ref,
391 &down_ref))) {
1bdbdaff
P
392 TEST_note("iteration %d", i);
393 goto err;
394 }
395 }
396 for (i = 1; i <= max; i++) {
397 BIO_snprintf(buf, sizeof(buf), "n=%d\n", i);
398 if (!ossl_method_store_cache_get(store, i, buf, &result)
399 || result != v + i)
400 errors++;
401 }
402 /* There is a tiny probability that this will fail when it shouldn't */
403 res = TEST_int_gt(errors, tail) && TEST_int_lt(errors, max - tail);
404
405err:
406 ossl_method_store_free(store);
407 return res;
408}
409
e0624f0d
SL
410static int test_fips_mode(void)
411{
412 int ret = 0;
b4250010 413 OSSL_LIB_CTX *ctx = NULL;
e0624f0d 414
b4250010 415 if (!TEST_ptr(ctx = OSSL_LIB_CTX_new()))
e0624f0d
SL
416 goto err;
417
418 ret = TEST_true(EVP_set_default_properties(ctx, "default=yes,fips=yes"))
419 && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
420 && TEST_true(EVP_set_default_properties(ctx, "fips=no,default=yes"))
421 && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
422 && TEST_true(EVP_set_default_properties(ctx, "fips=no"))
423 && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
424 && TEST_true(EVP_set_default_properties(ctx, "fips!=no"))
425 && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
426 && TEST_true(EVP_set_default_properties(ctx, "fips=no"))
427 && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
428 && TEST_true(EVP_set_default_properties(ctx, "fips=no,default=yes"))
429 && TEST_true(EVP_default_properties_enable_fips(ctx, 1))
430 && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
431 && TEST_true(EVP_default_properties_enable_fips(ctx, 0))
432 && TEST_false(EVP_default_properties_is_fips_enabled(ctx));
433err:
b4250010 434 OSSL_LIB_CTX_free(ctx);
e0624f0d
SL
435 return ret;
436}
437
438
1bdbdaff
P
439int setup_tests(void)
440{
441 ADD_TEST(test_property_string);
1e08f3ba 442 ADD_TEST(test_property_query_value_create);
1bdbdaff
P
443 ADD_ALL_TESTS(test_property_parse, OSSL_NELEM(parser_tests));
444 ADD_ALL_TESTS(test_property_merge, OSSL_NELEM(merge_tests));
445 ADD_TEST(test_property_defn_cache);
446 ADD_ALL_TESTS(test_definition_compares, OSSL_NELEM(definition_tests));
447 ADD_TEST(test_register_deregister);
448 ADD_TEST(test_property);
449 ADD_TEST(test_query_cache_stochastic);
e0624f0d 450 ADD_TEST(test_fips_mode);
1bdbdaff
P
451 return 1;
452}