2 * Copyright 2019-2020 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
12 #include <openssl/evp.h>
14 #include "internal/nelem.h"
15 #include "internal/property.h"
16 #include "../crypto/property/property_local.h"
18 static int add_property_names(const char *n
, ...)
25 if (!TEST_int_ne(ossl_property_name(NULL
, n
, 1), 0))
27 } while ((n
= va_arg(args
, const char *)) != NULL
);
32 static int up_ref(void *p
)
37 static void down_ref(void *p
)
41 static int test_property_string(void)
43 OSSL_METHOD_STORE
*store
;
45 OSSL_PROPERTY_IDX i
, j
;
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)
51 /* Property value checks */
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)
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
)
61 /* Check name and values are distinct */
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)))
66 ossl_method_store_free(store
);
75 { "", "sky=blue", -1 },
76 { "", "sky!=blue", 1 },
78 { "cold=yes", "cold=yes", 1 },
79 { "cold=yes", "cold", 1 },
80 { "cold=yes", "cold!=no", 1 },
81 { "groan", "groan=yes", 1 },
82 { "groan", "groan=no", -1 },
83 { "groan", "groan!=yes", -1 },
84 { "cold=no", "cold", -1 },
85 { "cold=no", "?cold", 0 },
86 { "cold=no", "cold=no", 1 },
87 { "groan", "cold", -1 },
88 { "groan", "cold=no", 1 },
89 { "groan", "cold!=yes", 1 },
90 { "groan=blue", "groan=yellow", -1 },
91 { "groan=blue", "?groan=yellow", 0 },
92 { "groan=blue", "groan!=yellow", 1 },
93 { "groan=blue", "?groan!=yellow", 1 },
94 { "today=monday, tomorrow=3", "today!=2", 1 },
95 { "today=monday, tomorrow=3", "today!='monday'", -1 },
96 { "today=monday, tomorrow=3", "tomorrow=3", 1 },
97 { "n=0x3", "n=3", 1 },
98 { "n=0x3", "n=-3", -1 },
99 { "n=0x33", "n=51", 1 },
100 { "n=033", "n=27", 1 },
101 { "n=0", "n=00", 1 },
102 { "n=0x0", "n=0", 1 },
103 { "n=0, sky=blue", "?n=0, sky=blue", 2 },
104 { "n=1, sky=blue", "?n=0, sky=blue", 1 },
107 static int test_property_parse(int n
)
109 OSSL_METHOD_STORE
*store
;
110 OSSL_PROPERTY_LIST
*p
= NULL
, *q
= NULL
;
113 if (TEST_ptr(store
= ossl_method_store_new(NULL
))
114 && add_property_names("sky", "groan", "cold", "today", "tomorrow", "n",
116 && TEST_ptr(p
= ossl_parse_property(NULL
, parser_tests
[n
].defn
))
117 && TEST_ptr(q
= ossl_parse_query(NULL
, parser_tests
[n
].query
, 0))
118 && TEST_int_eq(ossl_property_match_count(q
, p
), parser_tests
[n
].e
))
120 ossl_property_free(p
);
121 ossl_property_free(q
);
122 ossl_method_store_free(store
);
126 static int test_property_query_value_create(void)
128 OSSL_METHOD_STORE
*store
;
129 OSSL_PROPERTY_LIST
*p
= NULL
, *q
= NULL
, *o
= NULL
;
132 /* The property value used here must not be used in other test cases */
133 if (TEST_ptr(store
= ossl_method_store_new(NULL
))
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 */
138 && TEST_int_eq(ossl_property_match_count(q
, p
), -1)
139 && TEST_int_eq(ossl_property_match_count(q
, o
), 1))
141 ossl_property_free(o
);
142 ossl_property_free(p
);
143 ossl_property_free(q
);
144 ossl_method_store_free(store
);
148 static const struct {
149 const char *q_global
;
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" },
163 * The following four leave 'day' unspecified in the query, and will match
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" },
174 static int test_property_merge(int n
)
176 OSSL_METHOD_STORE
*store
;
177 OSSL_PROPERTY_LIST
*q_global
= NULL
, *q_local
= NULL
;
178 OSSL_PROPERTY_LIST
*q_combined
= NULL
, *prop
= NULL
;
181 if (TEST_ptr(store
= ossl_method_store_new(NULL
))
182 && add_property_names("colour", "urn", "clouds", "pot", "day", "night",
184 && TEST_ptr(prop
= ossl_parse_property(NULL
, merge_tests
[n
].prop
))
185 && TEST_ptr(q_global
= ossl_parse_query(NULL
, merge_tests
[n
].q_global
,
187 && TEST_ptr(q_local
= ossl_parse_query(NULL
, merge_tests
[n
].q_local
, 0))
188 && TEST_ptr(q_combined
= ossl_property_merge(q_local
, q_global
))
189 && TEST_int_ge(ossl_property_match_count(q_combined
, prop
), 0))
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
);
199 static int test_property_defn_cache(void)
201 OSSL_METHOD_STORE
*store
;
202 OSSL_PROPERTY_LIST
*red
, *blue
;
205 if (TEST_ptr(store
= ossl_method_store_new(NULL
))
206 && add_property_names("red", "blue", NULL
)
207 && TEST_ptr(red
= ossl_parse_property(NULL
, "red"))
208 && TEST_ptr(blue
= ossl_parse_property(NULL
, "blue"))
209 && TEST_ptr_ne(red
, blue
)
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
))
215 ossl_method_store_free(store
);
219 static const struct {
223 } definition_tests
[] = {
224 { "alpha", "alpha=yes", 1 },
225 { "alpha=no", "alpha", -1 },
226 { "alpha=1", "alpha=1", 1 },
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 }
237 static int test_definition_compares(int n
)
239 OSSL_METHOD_STORE
*store
;
240 OSSL_PROPERTY_LIST
*d
= NULL
, *q
= NULL
;
243 r
= TEST_ptr(store
= ossl_method_store_new(NULL
))
244 && add_property_names("alpha", "omega", NULL
)
245 && TEST_ptr(d
= ossl_parse_property(NULL
, definition_tests
[n
].defn
))
246 && TEST_ptr(q
= ossl_parse_query(NULL
, definition_tests
[n
].query
, 0))
247 && TEST_int_eq(ossl_property_match_count(q
, d
), definition_tests
[n
].e
);
249 ossl_property_free(d
);
250 ossl_property_free(q
);
251 ossl_method_store_free(store
);
255 static int test_register_deregister(void)
257 static const struct {
262 { 6, "position=1", "a" },
263 { 6, "position=2", "b" },
264 { 6, "position=3", "c" },
265 { 6, "position=4", "d" },
269 OSSL_METHOD_STORE
*store
;
271 if (!TEST_ptr(store
= ossl_method_store_new(NULL
))
272 || !add_property_names("position", NULL
))
275 for (i
= 0; i
< OSSL_NELEM(impls
); i
++)
276 if (!TEST_true(ossl_method_store_add(store
, NULL
, impls
[i
].nid
,
277 impls
[i
].prop
, impls
[i
].impl
,
278 &up_ref
, &down_ref
))) {
279 TEST_note("iteration %zd", i
+ 1);
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
;
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);
296 if (TEST_false(ossl_method_store_remove(store
, impls
[0].nid
, impls
[0].impl
)))
299 ossl_method_store_free(store
);
303 static int test_property(void)
305 static const struct {
310 { 1, "fast=no, colour=green", "a" },
311 { 1, "fast, colour=blue", "b" },
313 { 9, "sky=blue, furry", "c" },
315 { 6, "sky.colour=blue, sky=green, old.data", "e" },
323 { 1, "fast=yes", "b" },
324 { 1, "fast=no, colour=green", "a" },
325 { 1, "colour=blue, fast", "b" },
326 { 1, "colour=blue", "b" },
328 { 6, "sky.colour=blue", "e" },
329 { 6, "old.data", "e" },
330 { 9, "furry=yes, sky=blue", "c" },
334 OSSL_METHOD_STORE
*store
;
339 if (!TEST_ptr(store
= ossl_method_store_new(NULL
))
340 || !add_property_names("fast", "colour", "sky", "furry", NULL
))
343 for (i
= 0; i
< OSSL_NELEM(impls
); i
++)
344 if (!TEST_true(ossl_method_store_add(store
, NULL
, impls
[i
].nid
,
345 impls
[i
].prop
, impls
[i
].impl
,
346 &up_ref
, &down_ref
))) {
347 TEST_note("iteration %zd", i
+ 1);
350 for (i
= 0; i
< OSSL_NELEM(queries
); i
++) {
351 OSSL_PROPERTY_LIST
*pq
= NULL
;
353 if (!TEST_true(ossl_method_store_fetch(store
, queries
[i
].nid
,
354 queries
[i
].prop
, &result
))
355 || !TEST_str_eq((char *)result
, queries
[i
].expected
)) {
356 TEST_note("iteration %zd", i
+ 1);
357 ossl_property_free(pq
);
360 ossl_property_free(pq
);
364 ossl_method_store_free(store
);
368 static int test_query_cache_stochastic(void)
370 const int max
= 10000, tail
= 10;
371 OSSL_METHOD_STORE
*store
;
378 if (!TEST_ptr(store
= ossl_method_store_new(NULL
))
379 || !add_property_names("n", NULL
))
382 for (i
= 1; i
<= max
; i
++) {
384 BIO_snprintf(buf
, sizeof(buf
), "n=%d\n", i
);
385 if (!TEST_true(ossl_method_store_add(store
, NULL
, i
, buf
, "abc",
387 || !TEST_true(ossl_method_store_cache_set(store
, i
, buf
, v
+ i
,
389 || !TEST_true(ossl_method_store_cache_set(store
, i
, "n=1234",
392 TEST_note("iteration %d", i
);
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
)
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
);
406 ossl_method_store_free(store
);
410 static int test_fips_mode(void)
413 OSSL_LIB_CTX
*ctx
= NULL
;
415 if (!TEST_ptr(ctx
= OSSL_LIB_CTX_new()))
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
));
434 OSSL_LIB_CTX_free(ctx
);
439 int setup_tests(void)
441 ADD_TEST(test_property_string
);
442 ADD_TEST(test_property_query_value_create
);
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
);
450 ADD_TEST(test_fips_mode
);