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
13 #include "internal/nelem.h"
14 #include "internal/property.h"
15 #include "../crypto/property/property_local.h"
17 static int add_property_names(const char *n
, ...)
24 if (!TEST_int_ne(ossl_property_name(NULL
, n
, 1), 0))
26 } while ((n
= va_arg(args
, const char *)) != NULL
);
31 static int up_ref(void *p
)
36 static void down_ref(void *p
)
40 static int test_property_string(void)
42 OSSL_METHOD_STORE
*store
;
44 OSSL_PROPERTY_IDX i
, j
;
46 if (TEST_ptr(store
= ossl_method_store_new(NULL
))
47 && TEST_int_eq(ossl_property_name(NULL
, "fnord", 0), 0)
48 && TEST_int_ne(ossl_property_name(NULL
, "fnord", 1), 0)
49 && TEST_int_ne(ossl_property_name(NULL
, "name", 1), 0)
50 /* Property value checks */
51 && TEST_int_eq(ossl_property_value(NULL
, "fnord", 0), 0)
52 && TEST_int_ne(i
= ossl_property_value(NULL
, "no", 0), 0)
53 && TEST_int_ne(j
= ossl_property_value(NULL
, "yes", 0), 0)
55 && TEST_int_eq(ossl_property_value(NULL
, "yes", 1), j
)
56 && TEST_int_eq(ossl_property_value(NULL
, "no", 1), i
)
57 && TEST_int_ne(i
= ossl_property_value(NULL
, "illuminati", 1), 0)
58 && TEST_int_eq(j
= ossl_property_value(NULL
, "fnord", 1), i
+ 1)
59 && TEST_int_eq(ossl_property_value(NULL
, "fnord", 1), j
)
60 /* Check name and values are distinct */
61 && TEST_int_eq(ossl_property_value(NULL
, "cold", 0), 0)
62 && TEST_int_ne(ossl_property_name(NULL
, "fnord", 0),
63 ossl_property_value(NULL
, "fnord", 0)))
65 ossl_method_store_free(store
);
74 { "", "sky=blue", -1 },
75 { "", "sky!=blue", 1 },
77 { "cold=yes", "cold=yes", 1 },
78 { "cold=yes", "cold", 1 },
79 { "cold=yes", "cold!=no", 1 },
80 { "groan", "groan=yes", 1 },
81 { "groan", "groan=no", -1 },
82 { "groan", "groan!=yes", -1 },
83 { "cold=no", "cold", -1 },
84 { "cold=no", "?cold", 0 },
85 { "cold=no", "cold=no", 1 },
86 { "groan", "cold", -1 },
87 { "groan", "cold=no", 1 },
88 { "groan", "cold!=yes", 1 },
89 { "groan=blue", "groan=yellow", -1 },
90 { "groan=blue", "?groan=yellow", 0 },
91 { "groan=blue", "groan!=yellow", 1 },
92 { "groan=blue", "?groan!=yellow", 1 },
93 { "today=monday, tomorrow=3", "today!=2", 1 },
94 { "today=monday, tomorrow=3", "today!='monday'", -1 },
95 { "today=monday, tomorrow=3", "tomorrow=3", 1 },
96 { "n=0x3", "n=3", 1 },
97 { "n=0x3", "n=-3", -1 },
98 { "n=0x33", "n=51", 1 },
99 { "n=033", "n=27", 1 },
100 { "n=0", "n=00", 1 },
101 { "n=0x0", "n=0", 1 },
102 { "n=0, sky=blue", "?n=0, sky=blue", 2 },
103 { "n=1, sky=blue", "?n=0, sky=blue", 1 },
106 static int test_property_parse(int n
)
108 OSSL_METHOD_STORE
*store
;
109 OSSL_PROPERTY_LIST
*p
= NULL
, *q
= NULL
;
112 if (TEST_ptr(store
= ossl_method_store_new(NULL
))
113 && add_property_names("sky", "groan", "cold", "today", "tomorrow", "n",
115 && TEST_ptr(p
= ossl_parse_property(NULL
, parser_tests
[n
].defn
))
116 && TEST_ptr(q
= ossl_parse_query(NULL
, parser_tests
[n
].query
))
117 && TEST_int_eq(ossl_property_match_count(q
, p
), parser_tests
[n
].e
))
119 ossl_property_free(p
);
120 ossl_property_free(q
);
121 ossl_method_store_free(store
);
125 static const struct {
126 const char *q_global
;
130 { "", "colour=blue", "colour=blue" },
131 { "colour=blue", "", "colour=blue" },
132 { "colour=red", "colour=blue", "colour=blue" },
133 { "clouds=pink, urn=red", "urn=blue, colour=green",
134 "urn=blue, colour=green, clouds=pink" },
135 { "pot=gold", "urn=blue", "pot=gold, urn=blue" },
136 { "night", "day", "day=yes, night=yes" },
137 { "day", "night", "day=yes, night=yes" },
140 * The following four leave 'day' unspecified in the query, and will match
143 { "day=yes", "-day", "day=no" },
144 { "day=yes", "-day", "day=yes" },
145 { "day=yes", "-day", "day=arglebargle" },
146 { "day=yes", "-day", "pot=sesquioxidizing" },
147 { "day, night", "-night, day", "day=yes, night=no" },
148 { "-day", "day=yes", "day=yes" },
151 static int test_property_merge(int n
)
153 OSSL_METHOD_STORE
*store
;
154 OSSL_PROPERTY_LIST
*q_global
= NULL
, *q_local
= NULL
;
155 OSSL_PROPERTY_LIST
*q_combined
= NULL
, *prop
= NULL
;
158 if (TEST_ptr(store
= ossl_method_store_new(NULL
))
159 && add_property_names("colour", "urn", "clouds", "pot", "day", "night",
161 && TEST_ptr(prop
= ossl_parse_property(NULL
, merge_tests
[n
].prop
))
162 && TEST_ptr(q_global
= ossl_parse_query(NULL
, merge_tests
[n
].q_global
))
163 && TEST_ptr(q_local
= ossl_parse_query(NULL
, merge_tests
[n
].q_local
))
164 && TEST_ptr(q_combined
= ossl_property_merge(q_local
, q_global
))
165 && TEST_int_ge(ossl_property_match_count(q_combined
, prop
), 0))
167 ossl_property_free(q_global
);
168 ossl_property_free(q_local
);
169 ossl_property_free(q_combined
);
170 ossl_property_free(prop
);
171 ossl_method_store_free(store
);
175 static int test_property_defn_cache(void)
177 OSSL_METHOD_STORE
*store
;
178 OSSL_PROPERTY_LIST
*red
, *blue
;
181 if (TEST_ptr(store
= ossl_method_store_new(NULL
))
182 && add_property_names("red", "blue", NULL
)
183 && TEST_ptr(red
= ossl_parse_property(NULL
, "red"))
184 && TEST_ptr(blue
= ossl_parse_property(NULL
, "blue"))
185 && TEST_ptr_ne(red
, blue
)
186 && TEST_true(ossl_prop_defn_set(NULL
, "red", red
))
187 && TEST_true(ossl_prop_defn_set(NULL
, "blue", blue
))
188 && TEST_ptr_eq(ossl_prop_defn_get(NULL
, "red"), red
)
189 && TEST_ptr_eq(ossl_prop_defn_get(NULL
, "blue"), blue
))
191 ossl_method_store_free(store
);
195 static const struct {
199 } definition_tests
[] = {
200 { "alpha", "alpha=yes", 1 },
201 { "alpha=no", "alpha", -1 },
202 { "alpha=1", "alpha=1", 1 },
203 { "alpha=2", "alpha=1",-1 },
204 { "alpha", "omega", -1 },
205 { "alpha", "?omega", 0 },
206 { "alpha", "?omega=1", 0 },
207 { "alpha", "?omega=no", 1 },
208 { "alpha", "?omega=yes", 0 },
209 { "alpha, omega", "?omega=yes", 1 },
210 { "alpha, omega", "?omega=no", 0 }
213 static int test_definition_compares(int n
)
215 OSSL_METHOD_STORE
*store
;
216 OSSL_PROPERTY_LIST
*d
= NULL
, *q
= NULL
;
219 r
= TEST_ptr(store
= ossl_method_store_new(NULL
))
220 && add_property_names("alpha", "omega", NULL
)
221 && TEST_ptr(d
= ossl_parse_property(NULL
, definition_tests
[n
].defn
))
222 && TEST_ptr(q
= ossl_parse_query(NULL
, definition_tests
[n
].query
))
223 && TEST_int_eq(ossl_property_match_count(q
, d
), definition_tests
[n
].e
);
225 ossl_property_free(d
);
226 ossl_property_free(q
);
227 ossl_method_store_free(store
);
231 static int test_register_deregister(void)
233 static const struct {
238 { 6, "position=1", "a" },
239 { 6, "position=2", "b" },
240 { 6, "position=3", "c" },
241 { 6, "position=4", "d" },
245 OSSL_METHOD_STORE
*store
;
247 if (!TEST_ptr(store
= ossl_method_store_new(NULL
))
248 || !add_property_names("position", NULL
))
251 for (i
= 0; i
< OSSL_NELEM(impls
); i
++)
252 if (!TEST_true(ossl_method_store_add(store
, NULL
, impls
[i
].nid
,
253 impls
[i
].prop
, impls
[i
].impl
,
254 &up_ref
, &down_ref
))) {
255 TEST_note("iteration %zd", i
+ 1);
259 /* Deregister in a different order to registration */
260 for (i
= 0; i
< OSSL_NELEM(impls
); i
++) {
261 const size_t j
= (1 + i
* 3) % OSSL_NELEM(impls
);
262 int nid
= impls
[j
].nid
;
263 void *impl
= impls
[j
].impl
;
265 if (!TEST_true(ossl_method_store_remove(store
, nid
, impl
))
266 || !TEST_false(ossl_method_store_remove(store
, nid
, impl
))) {
267 TEST_note("iteration %zd, position %zd", i
+ 1, j
+ 1);
272 if (TEST_false(ossl_method_store_remove(store
, impls
[0].nid
, impls
[0].impl
)))
275 ossl_method_store_free(store
);
279 static int test_property(void)
281 static const struct {
286 { 1, "fast=no, colour=green", "a" },
287 { 1, "fast, colour=blue", "b" },
289 { 9, "sky=blue, furry", "c" },
291 { 6, "sky.colour=blue, sky=green, old.data", "e" },
299 { 1, "fast=yes", "b" },
300 { 1, "fast=no, colour=green", "a" },
301 { 1, "colour=blue, fast", "b" },
302 { 1, "colour=blue", "b" },
304 { 6, "sky.colour=blue", "e" },
305 { 6, "old.data", "e" },
306 { 9, "furry=yes, sky=blue", "c" },
310 OSSL_METHOD_STORE
*store
;
315 if (!TEST_ptr(store
= ossl_method_store_new(NULL
))
316 || !add_property_names("fast", "colour", "sky", "furry", NULL
))
319 for (i
= 0; i
< OSSL_NELEM(impls
); i
++)
320 if (!TEST_true(ossl_method_store_add(store
, NULL
, impls
[i
].nid
,
321 impls
[i
].prop
, impls
[i
].impl
,
322 &up_ref
, &down_ref
))) {
323 TEST_note("iteration %zd", i
+ 1);
326 for (i
= 0; i
< OSSL_NELEM(queries
); i
++) {
327 OSSL_PROPERTY_LIST
*pq
= NULL
;
329 if (!TEST_true(ossl_method_store_fetch(store
, queries
[i
].nid
,
330 queries
[i
].prop
, &result
))
331 || !TEST_str_eq((char *)result
, queries
[i
].expected
)) {
332 TEST_note("iteration %zd", i
+ 1);
333 ossl_property_free(pq
);
336 ossl_property_free(pq
);
340 ossl_method_store_free(store
);
344 static int test_query_cache_stochastic(void)
346 const int max
= 10000, tail
= 10;
347 OSSL_METHOD_STORE
*store
;
354 if (!TEST_ptr(store
= ossl_method_store_new(NULL
))
355 || !add_property_names("n", NULL
))
358 for (i
= 1; i
<= max
; i
++) {
360 BIO_snprintf(buf
, sizeof(buf
), "n=%d\n", i
);
361 if (!TEST_true(ossl_method_store_add(store
, NULL
, i
, buf
, "abc",
363 || !TEST_true(ossl_method_store_cache_set(store
, i
, buf
, v
+ i
,
365 || !TEST_true(ossl_method_store_cache_set(store
, i
, "n=1234",
368 TEST_note("iteration %d", i
);
372 for (i
= 1; i
<= max
; i
++) {
373 BIO_snprintf(buf
, sizeof(buf
), "n=%d\n", i
);
374 if (!ossl_method_store_cache_get(store
, i
, buf
, &result
)
378 /* There is a tiny probability that this will fail when it shouldn't */
379 res
= TEST_int_gt(errors
, tail
) && TEST_int_lt(errors
, max
- tail
);
382 ossl_method_store_free(store
);
386 int setup_tests(void)
388 ADD_TEST(test_property_string
);
389 ADD_ALL_TESTS(test_property_parse
, OSSL_NELEM(parser_tests
));
390 ADD_ALL_TESTS(test_property_merge
, OSSL_NELEM(merge_tests
));
391 ADD_TEST(test_property_defn_cache
);
392 ADD_ALL_TESTS(test_definition_compares
, OSSL_NELEM(definition_tests
));
393 ADD_TEST(test_register_deregister
);
394 ADD_TEST(test_property
);
395 ADD_TEST(test_query_cache_stochastic
);