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_lcl.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 test_property_string(void)
33 OSSL_METHOD_STORE
*store
;
35 OSSL_PROPERTY_IDX i
, j
;
37 if (TEST_ptr(store
= ossl_method_store_new(NULL
))
38 && TEST_int_eq(ossl_property_name(NULL
, "fnord", 0), 0)
39 && TEST_int_ne(ossl_property_name(NULL
, "fnord", 1), 0)
40 && TEST_int_ne(ossl_property_name(NULL
, "name", 1), 0)
41 /* Property value checks */
42 && TEST_int_eq(ossl_property_value(NULL
, "fnord", 0), 0)
43 && TEST_int_ne(i
= ossl_property_value(NULL
, "no", 0), 0)
44 && TEST_int_ne(j
= ossl_property_value(NULL
, "yes", 0), 0)
46 && TEST_int_eq(ossl_property_value(NULL
, "yes", 1), j
)
47 && TEST_int_eq(ossl_property_value(NULL
, "no", 1), i
)
48 && TEST_int_ne(i
= ossl_property_value(NULL
, "illuminati", 1), 0)
49 && TEST_int_eq(j
= ossl_property_value(NULL
, "fnord", 1), i
+ 1)
50 && TEST_int_eq(ossl_property_value(NULL
, "fnord", 1), j
)
51 /* Check name and values are distinct */
52 && TEST_int_eq(ossl_property_value(NULL
, "cold", 0), 0)
53 && TEST_int_ne(ossl_property_name(NULL
, "fnord", 0),
54 ossl_property_value(NULL
, "fnord", 0)))
56 ossl_method_store_free(store
);
65 { "", "sky=blue", -1 },
66 { "", "sky!=blue", 1 },
68 { "cold=yes", "cold=yes", 1 },
69 { "cold=yes", "cold", 1 },
70 { "cold=yes", "cold!=no", 1 },
71 { "groan", "groan=yes", 1 },
72 { "groan", "groan=no", -1 },
73 { "groan", "groan!=yes", -1 },
74 { "cold=no", "cold", -1 },
75 { "cold=no", "?cold", 0 },
76 { "cold=no", "cold=no", 1 },
77 { "groan", "cold", -1 },
78 { "groan", "cold=no", 1 },
79 { "groan", "cold!=yes", 1 },
80 { "groan=blue", "groan=yellow", -1 },
81 { "groan=blue", "?groan=yellow", 0 },
82 { "groan=blue", "groan!=yellow", 1 },
83 { "groan=blue", "?groan!=yellow", 1 },
84 { "today=monday, tomorrow=3", "today!=2", 1 },
85 { "today=monday, tomorrow=3", "today!='monday'", -1 },
86 { "today=monday, tomorrow=3", "tomorrow=3", 1 },
87 { "n=0x3", "n=3", 1 },
88 { "n=0x3", "n=-3", -1 },
89 { "n=0x33", "n=51", 1 },
90 { "n=033", "n=27", 1 },
92 { "n=0x0", "n=0", 1 },
93 { "n=0, sky=blue", "?n=0, sky=blue", 2 },
94 { "n=1, sky=blue", "?n=0, sky=blue", 1 },
97 static int test_property_parse(int n
)
99 OSSL_METHOD_STORE
*store
;
100 OSSL_PROPERTY_LIST
*p
= NULL
, *q
= NULL
;
103 if (TEST_ptr(store
= ossl_method_store_new(NULL
))
104 && add_property_names("sky", "groan", "cold", "today", "tomorrow", "n",
106 && TEST_ptr(p
= ossl_parse_property(NULL
, parser_tests
[n
].defn
))
107 && TEST_ptr(q
= ossl_parse_query(NULL
, parser_tests
[n
].query
))
108 && TEST_int_eq(ossl_property_match_count(q
, p
), parser_tests
[n
].e
))
110 ossl_property_free(p
);
111 ossl_property_free(q
);
112 ossl_method_store_free(store
);
116 static const struct {
117 const char *q_global
;
121 { "", "colour=blue", "colour=blue" },
122 { "colour=blue", "", "colour=blue" },
123 { "colour=red", "colour=blue", "colour=blue" },
124 { "clouds=pink, urn=red", "urn=blue, colour=green",
125 "urn=blue, colour=green, clouds=pink" },
126 { "pot=gold", "urn=blue", "pot=gold, urn=blue" },
127 { "night", "day", "day=yes, night=yes" },
128 { "day", "night", "day=yes, night=yes" },
131 * The following four leave 'day' unspecified in the query, and will match
134 { "day=yes", "-day", "day=no" },
135 { "day=yes", "-day", "day=yes" },
136 { "day=yes", "-day", "day=arglebargle" },
137 { "day=yes", "-day", "pot=sesquioxidizing" },
138 { "day, night", "-night, day", "day=yes, night=no" },
139 { "-day", "day=yes", "day=yes" },
142 static int test_property_merge(int n
)
144 OSSL_METHOD_STORE
*store
;
145 OSSL_PROPERTY_LIST
*q_global
= NULL
, *q_local
= NULL
;
146 OSSL_PROPERTY_LIST
*q_combined
= NULL
, *prop
= NULL
;
149 if (TEST_ptr(store
= ossl_method_store_new(NULL
))
150 && add_property_names("colour", "urn", "clouds", "pot", "day", "night",
152 && TEST_ptr(prop
= ossl_parse_property(NULL
, merge_tests
[n
].prop
))
153 && TEST_ptr(q_global
= ossl_parse_query(NULL
, merge_tests
[n
].q_global
))
154 && TEST_ptr(q_local
= ossl_parse_query(NULL
, merge_tests
[n
].q_local
))
155 && TEST_ptr(q_combined
= ossl_property_merge(q_local
, q_global
))
156 && TEST_int_ge(ossl_property_match_count(q_combined
, prop
), 0))
158 ossl_property_free(q_global
);
159 ossl_property_free(q_local
);
160 ossl_property_free(q_combined
);
161 ossl_property_free(prop
);
162 ossl_method_store_free(store
);
166 static int test_property_defn_cache(void)
168 OSSL_METHOD_STORE
*store
;
169 OSSL_PROPERTY_LIST
*red
, *blue
;
172 if (TEST_ptr(store
= ossl_method_store_new(NULL
))
173 && add_property_names("red", "blue", NULL
)
174 && TEST_ptr(red
= ossl_parse_property(NULL
, "red"))
175 && TEST_ptr(blue
= ossl_parse_property(NULL
, "blue"))
176 && TEST_ptr_ne(red
, blue
)
177 && TEST_true(ossl_prop_defn_set(NULL
, "red", red
))
178 && TEST_true(ossl_prop_defn_set(NULL
, "blue", blue
))
179 && TEST_ptr_eq(ossl_prop_defn_get(NULL
, "red"), red
)
180 && TEST_ptr_eq(ossl_prop_defn_get(NULL
, "blue"), blue
))
182 ossl_method_store_free(store
);
186 static const struct {
190 } definition_tests
[] = {
191 { "alpha", "alpha=yes", 1 },
192 { "alpha=no", "alpha", -1 },
193 { "alpha=1", "alpha=1", 1 },
194 { "alpha=2", "alpha=1",-1 },
195 { "alpha", "omega", -1 },
196 { "alpha", "?omega", 0 },
197 { "alpha", "?omega=1", 0 },
198 { "alpha", "?omega=no", 1 },
199 { "alpha", "?omega=yes", 0 },
200 { "alpha, omega", "?omega=yes", 1 },
201 { "alpha, omega", "?omega=no", 0 }
204 static int test_definition_compares(int n
)
206 OSSL_METHOD_STORE
*store
;
207 OSSL_PROPERTY_LIST
*d
= NULL
, *q
= NULL
;
210 r
= TEST_ptr(store
= ossl_method_store_new(NULL
))
211 && add_property_names("alpha", "omega", NULL
)
212 && TEST_ptr(d
= ossl_parse_property(NULL
, definition_tests
[n
].defn
))
213 && TEST_ptr(q
= ossl_parse_query(NULL
, definition_tests
[n
].query
))
214 && TEST_int_eq(ossl_property_match_count(q
, d
), definition_tests
[n
].e
);
216 ossl_property_free(d
);
217 ossl_property_free(q
);
218 ossl_method_store_free(store
);
222 static int test_register_deregister(void)
224 static const struct {
229 { 6, "position=1", "a" },
230 { 6, "position=2", "b" },
231 { 6, "position=3", "c" },
232 { 6, "position=4", "d" },
236 OSSL_METHOD_STORE
*store
;
238 if (!TEST_ptr(store
= ossl_method_store_new(NULL
))
239 || !add_property_names("position", NULL
))
242 for (i
= 0; i
< OSSL_NELEM(impls
); i
++)
243 if (!TEST_true(ossl_method_store_add(store
, impls
[i
].nid
, impls
[i
].prop
,
244 impls
[i
].impl
, NULL
))) {
245 TEST_note("iteration %zd", i
+ 1);
249 /* Deregister in a different order to registration */
250 for (i
= 0; i
< OSSL_NELEM(impls
); i
++) {
251 const size_t j
= (1 + i
* 3) % OSSL_NELEM(impls
);
252 int nid
= impls
[j
].nid
;
253 void *impl
= impls
[j
].impl
;
255 if (!TEST_true(ossl_method_store_remove(store
, nid
, impl
))
256 || !TEST_false(ossl_method_store_remove(store
, nid
, impl
))) {
257 TEST_note("iteration %zd, position %zd", i
+ 1, j
+ 1);
262 if (TEST_false(ossl_method_store_remove(store
, impls
[0].nid
, impls
[0].impl
)))
265 ossl_method_store_free(store
);
269 static int test_property(void)
271 static const struct {
276 { 1, "fast=no, colour=green", "a" },
277 { 1, "fast, colour=blue", "b" },
279 { 9, "sky=blue, furry", "c" },
281 { 6, "sky.colour=blue, sky=green, old.data", "e" },
289 { 1, "fast=yes", "b" },
290 { 1, "fast=no, colour=green", "a" },
291 { 1, "colour=blue, fast", "b" },
292 { 1, "colour=blue", "b" },
294 { 6, "sky.colour=blue", "e" },
295 { 6, "old.data", "e" },
296 { 9, "furry=yes, sky=blue", "c" },
300 OSSL_METHOD_STORE
*store
;
305 if (!TEST_ptr(store
= ossl_method_store_new(NULL
))
306 || !add_property_names("fast", "colour", "sky", "furry", NULL
))
309 for (i
= 0; i
< OSSL_NELEM(impls
); i
++)
310 if (!TEST_true(ossl_method_store_add(store
, impls
[i
].nid
, impls
[i
].prop
,
311 impls
[i
].impl
, NULL
))) {
312 TEST_note("iteration %zd", i
+ 1);
315 for (i
= 0; i
< OSSL_NELEM(queries
); i
++) {
316 OSSL_PROPERTY_LIST
*pq
= NULL
;
318 if (!TEST_true(ossl_method_store_fetch(store
, queries
[i
].nid
,
319 queries
[i
].prop
, &result
))
320 || !TEST_str_eq((char *)result
, queries
[i
].expected
)) {
321 TEST_note("iteration %zd", i
+ 1);
322 ossl_property_free(pq
);
325 ossl_property_free(pq
);
329 ossl_method_store_free(store
);
333 static int test_query_cache_stochastic(void)
335 const int max
= 10000, tail
= 10;
336 OSSL_METHOD_STORE
*store
;
343 if (!TEST_ptr(store
= ossl_method_store_new(NULL
))
344 || !add_property_names("n", NULL
))
347 for (i
= 1; i
<= max
; i
++) {
349 BIO_snprintf(buf
, sizeof(buf
), "n=%d\n", i
);
350 if (!TEST_true(ossl_method_store_add(store
, i
, buf
, "abc", NULL
))
351 || !TEST_true(ossl_method_store_cache_set(store
, i
, buf
, v
+ i
))
352 || !TEST_true(ossl_method_store_cache_set(store
, i
, "n=1234",
354 TEST_note("iteration %d", i
);
358 for (i
= 1; i
<= max
; i
++) {
359 BIO_snprintf(buf
, sizeof(buf
), "n=%d\n", i
);
360 if (!ossl_method_store_cache_get(store
, i
, buf
, &result
)
364 /* There is a tiny probability that this will fail when it shouldn't */
365 res
= TEST_int_gt(errors
, tail
) && TEST_int_lt(errors
, max
- tail
);
368 ossl_method_store_free(store
);
372 int setup_tests(void)
374 ADD_TEST(test_property_string
);
375 ADD_ALL_TESTS(test_property_parse
, OSSL_NELEM(parser_tests
));
376 ADD_ALL_TESTS(test_property_merge
, OSSL_NELEM(merge_tests
));
377 ADD_TEST(test_property_defn_cache
);
378 ADD_ALL_TESTS(test_definition_compares
, OSSL_NELEM(definition_tests
));
379 ADD_TEST(test_register_deregister
);
380 ADD_TEST(test_property
);
381 ADD_TEST(test_query_cache_stochastic
);