2 * Copyright 2019 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
))
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 const struct {
127 const char *q_global
;
131 { "", "colour=blue", "colour=blue" },
132 { "colour=blue", "", "colour=blue" },
133 { "colour=red", "colour=blue", "colour=blue" },
134 { "clouds=pink, urn=red", "urn=blue, colour=green",
135 "urn=blue, colour=green, clouds=pink" },
136 { "pot=gold", "urn=blue", "pot=gold, urn=blue" },
137 { "night", "day", "day=yes, night=yes" },
138 { "day", "night", "day=yes, night=yes" },
141 * The following four leave 'day' unspecified in the query, and will match
144 { "day=yes", "-day", "day=no" },
145 { "day=yes", "-day", "day=yes" },
146 { "day=yes", "-day", "day=arglebargle" },
147 { "day=yes", "-day", "pot=sesquioxidizing" },
148 { "day, night", "-night, day", "day=yes, night=no" },
149 { "-day", "day=yes", "day=yes" },
152 static int test_property_merge(int n
)
154 OSSL_METHOD_STORE
*store
;
155 OSSL_PROPERTY_LIST
*q_global
= NULL
, *q_local
= NULL
;
156 OSSL_PROPERTY_LIST
*q_combined
= NULL
, *prop
= NULL
;
159 if (TEST_ptr(store
= ossl_method_store_new(NULL
))
160 && add_property_names("colour", "urn", "clouds", "pot", "day", "night",
162 && TEST_ptr(prop
= ossl_parse_property(NULL
, merge_tests
[n
].prop
))
163 && TEST_ptr(q_global
= ossl_parse_query(NULL
, merge_tests
[n
].q_global
))
164 && TEST_ptr(q_local
= ossl_parse_query(NULL
, merge_tests
[n
].q_local
))
165 && TEST_ptr(q_combined
= ossl_property_merge(q_local
, q_global
))
166 && TEST_int_ge(ossl_property_match_count(q_combined
, prop
), 0))
168 ossl_property_free(q_global
);
169 ossl_property_free(q_local
);
170 ossl_property_free(q_combined
);
171 ossl_property_free(prop
);
172 ossl_method_store_free(store
);
176 static int test_property_defn_cache(void)
178 OSSL_METHOD_STORE
*store
;
179 OSSL_PROPERTY_LIST
*red
, *blue
;
182 if (TEST_ptr(store
= ossl_method_store_new(NULL
))
183 && add_property_names("red", "blue", NULL
)
184 && TEST_ptr(red
= ossl_parse_property(NULL
, "red"))
185 && TEST_ptr(blue
= ossl_parse_property(NULL
, "blue"))
186 && TEST_ptr_ne(red
, blue
)
187 && TEST_true(ossl_prop_defn_set(NULL
, "red", red
))
188 && TEST_true(ossl_prop_defn_set(NULL
, "blue", blue
))
189 && TEST_ptr_eq(ossl_prop_defn_get(NULL
, "red"), red
)
190 && TEST_ptr_eq(ossl_prop_defn_get(NULL
, "blue"), blue
))
192 ossl_method_store_free(store
);
196 static const struct {
200 } definition_tests
[] = {
201 { "alpha", "alpha=yes", 1 },
202 { "alpha=no", "alpha", -1 },
203 { "alpha=1", "alpha=1", 1 },
204 { "alpha=2", "alpha=1",-1 },
205 { "alpha", "omega", -1 },
206 { "alpha", "?omega", 0 },
207 { "alpha", "?omega=1", 0 },
208 { "alpha", "?omega=no", 1 },
209 { "alpha", "?omega=yes", 0 },
210 { "alpha, omega", "?omega=yes", 1 },
211 { "alpha, omega", "?omega=no", 0 }
214 static int test_definition_compares(int n
)
216 OSSL_METHOD_STORE
*store
;
217 OSSL_PROPERTY_LIST
*d
= NULL
, *q
= NULL
;
220 r
= TEST_ptr(store
= ossl_method_store_new(NULL
))
221 && add_property_names("alpha", "omega", NULL
)
222 && TEST_ptr(d
= ossl_parse_property(NULL
, definition_tests
[n
].defn
))
223 && TEST_ptr(q
= ossl_parse_query(NULL
, definition_tests
[n
].query
))
224 && TEST_int_eq(ossl_property_match_count(q
, d
), definition_tests
[n
].e
);
226 ossl_property_free(d
);
227 ossl_property_free(q
);
228 ossl_method_store_free(store
);
232 static int test_register_deregister(void)
234 static const struct {
239 { 6, "position=1", "a" },
240 { 6, "position=2", "b" },
241 { 6, "position=3", "c" },
242 { 6, "position=4", "d" },
246 OSSL_METHOD_STORE
*store
;
248 if (!TEST_ptr(store
= ossl_method_store_new(NULL
))
249 || !add_property_names("position", NULL
))
252 for (i
= 0; i
< OSSL_NELEM(impls
); i
++)
253 if (!TEST_true(ossl_method_store_add(store
, NULL
, impls
[i
].nid
,
254 impls
[i
].prop
, impls
[i
].impl
,
255 &up_ref
, &down_ref
))) {
256 TEST_note("iteration %zd", i
+ 1);
260 /* Deregister in a different order to registration */
261 for (i
= 0; i
< OSSL_NELEM(impls
); i
++) {
262 const size_t j
= (1 + i
* 3) % OSSL_NELEM(impls
);
263 int nid
= impls
[j
].nid
;
264 void *impl
= impls
[j
].impl
;
266 if (!TEST_true(ossl_method_store_remove(store
, nid
, impl
))
267 || !TEST_false(ossl_method_store_remove(store
, nid
, impl
))) {
268 TEST_note("iteration %zd, position %zd", i
+ 1, j
+ 1);
273 if (TEST_false(ossl_method_store_remove(store
, impls
[0].nid
, impls
[0].impl
)))
276 ossl_method_store_free(store
);
280 static int test_property(void)
282 static const struct {
287 { 1, "fast=no, colour=green", "a" },
288 { 1, "fast, colour=blue", "b" },
290 { 9, "sky=blue, furry", "c" },
292 { 6, "sky.colour=blue, sky=green, old.data", "e" },
300 { 1, "fast=yes", "b" },
301 { 1, "fast=no, colour=green", "a" },
302 { 1, "colour=blue, fast", "b" },
303 { 1, "colour=blue", "b" },
305 { 6, "sky.colour=blue", "e" },
306 { 6, "old.data", "e" },
307 { 9, "furry=yes, sky=blue", "c" },
311 OSSL_METHOD_STORE
*store
;
316 if (!TEST_ptr(store
= ossl_method_store_new(NULL
))
317 || !add_property_names("fast", "colour", "sky", "furry", NULL
))
320 for (i
= 0; i
< OSSL_NELEM(impls
); i
++)
321 if (!TEST_true(ossl_method_store_add(store
, NULL
, impls
[i
].nid
,
322 impls
[i
].prop
, impls
[i
].impl
,
323 &up_ref
, &down_ref
))) {
324 TEST_note("iteration %zd", i
+ 1);
327 for (i
= 0; i
< OSSL_NELEM(queries
); i
++) {
328 OSSL_PROPERTY_LIST
*pq
= NULL
;
330 if (!TEST_true(ossl_method_store_fetch(store
, queries
[i
].nid
,
331 queries
[i
].prop
, &result
))
332 || !TEST_str_eq((char *)result
, queries
[i
].expected
)) {
333 TEST_note("iteration %zd", i
+ 1);
334 ossl_property_free(pq
);
337 ossl_property_free(pq
);
341 ossl_method_store_free(store
);
345 static int test_query_cache_stochastic(void)
347 const int max
= 10000, tail
= 10;
348 OSSL_METHOD_STORE
*store
;
355 if (!TEST_ptr(store
= ossl_method_store_new(NULL
))
356 || !add_property_names("n", NULL
))
359 for (i
= 1; i
<= max
; i
++) {
361 BIO_snprintf(buf
, sizeof(buf
), "n=%d\n", i
);
362 if (!TEST_true(ossl_method_store_add(store
, NULL
, i
, buf
, "abc",
364 || !TEST_true(ossl_method_store_cache_set(store
, i
, buf
, v
+ i
,
366 || !TEST_true(ossl_method_store_cache_set(store
, i
, "n=1234",
369 TEST_note("iteration %d", i
);
373 for (i
= 1; i
<= max
; i
++) {
374 BIO_snprintf(buf
, sizeof(buf
), "n=%d\n", i
);
375 if (!ossl_method_store_cache_get(store
, i
, buf
, &result
)
379 /* There is a tiny probability that this will fail when it shouldn't */
380 res
= TEST_int_gt(errors
, tail
) && TEST_int_lt(errors
, max
- tail
);
383 ossl_method_store_free(store
);
387 static int test_fips_mode(void)
390 OPENSSL_CTX
*ctx
= NULL
;
392 if (!TEST_ptr(ctx
= OPENSSL_CTX_new()))
395 ret
= TEST_true(EVP_set_default_properties(ctx
, "default=yes,fips=yes"))
396 && TEST_true(EVP_default_properties_is_fips_enabled(ctx
))
397 && TEST_true(EVP_set_default_properties(ctx
, "fips=no,default=yes"))
398 && TEST_false(EVP_default_properties_is_fips_enabled(ctx
))
399 && TEST_true(EVP_set_default_properties(ctx
, "fips=no"))
400 && TEST_false(EVP_default_properties_is_fips_enabled(ctx
))
401 && TEST_true(EVP_set_default_properties(ctx
, "fips!=no"))
402 && TEST_true(EVP_default_properties_is_fips_enabled(ctx
))
403 && TEST_true(EVP_set_default_properties(ctx
, "fips=no"))
404 && TEST_false(EVP_default_properties_is_fips_enabled(ctx
))
405 && TEST_true(EVP_set_default_properties(ctx
, "fips=no,default=yes"))
406 && TEST_true(EVP_default_properties_enable_fips(ctx
, 1))
407 && TEST_true(EVP_default_properties_is_fips_enabled(ctx
))
408 && TEST_true(EVP_default_properties_enable_fips(ctx
, 0))
409 && TEST_false(EVP_default_properties_is_fips_enabled(ctx
));
411 OPENSSL_CTX_free(ctx
);
416 int setup_tests(void)
418 ADD_TEST(test_property_string
);
419 ADD_ALL_TESTS(test_property_parse
, OSSL_NELEM(parser_tests
));
420 ADD_ALL_TESTS(test_property_merge
, OSSL_NELEM(merge_tests
));
421 ADD_TEST(test_property_defn_cache
);
422 ADD_ALL_TESTS(test_definition_compares
, OSSL_NELEM(definition_tests
));
423 ADD_TEST(test_register_deregister
);
424 ADD_TEST(test_property
);
425 ADD_TEST(test_query_cache_stochastic
);
426 ADD_TEST(test_fips_mode
);