]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/property_test.c
Modify ossl_method_store_add() to handle reference counting
[thirdparty/openssl.git] / test / property_test.c
CommitLineData
1bdbdaff
P
1/*
2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
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>
12#include "testutil.h"
13#include "internal/nelem.h"
14#include "internal/property.h"
15#include "../crypto/property/property_lcl.h"
16
17static int add_property_names(const char *n, ...)
18{
19 va_list args;
20 int res = 1;
21
22 va_start(args, n);
23 do {
1aedc35f 24 if (!TEST_int_ne(ossl_property_name(NULL, n, 1), 0))
1bdbdaff
P
25 res = 0;
26 } while ((n = va_arg(args, const char *)) != NULL);
27 va_end(args);
28 return res;
29}
30
31static int test_property_string(void)
32{
33 OSSL_METHOD_STORE *store;
34 int res = 0;
35 OSSL_PROPERTY_IDX i, j;
36
1aedc35f
MC
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)
1bdbdaff 41 /* Property value checks */
1aedc35f
MC
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)
1bdbdaff 45 && TEST_int_ne(i, j)
1aedc35f
MC
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)
1bdbdaff 51 /* Check name and values are distinct */
1aedc35f
MC
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)))
1bdbdaff
P
55 res = 1;
56 ossl_method_store_free(store);
57 return res;
58}
59
60static const struct {
61 const char *defn;
62 const char *query;
63 int e;
64} parser_tests[] = {
da89ac0b 65 { "", "sky=blue", -1 },
1bdbdaff 66 { "", "sky!=blue", 1 },
da89ac0b 67 { "groan", "", 0 },
1bdbdaff
P
68 { "cold=yes", "cold=yes", 1 },
69 { "cold=yes", "cold", 1 },
70 { "cold=yes", "cold!=no", 1 },
71 { "groan", "groan=yes", 1 },
da89ac0b
P
72 { "groan", "groan=no", -1 },
73 { "groan", "groan!=yes", -1 },
74 { "cold=no", "cold", -1 },
75 { "cold=no", "?cold", 0 },
1bdbdaff 76 { "cold=no", "cold=no", 1 },
da89ac0b 77 { "groan", "cold", -1 },
1bdbdaff
P
78 { "groan", "cold=no", 1 },
79 { "groan", "cold!=yes", 1 },
da89ac0b
P
80 { "groan=blue", "groan=yellow", -1 },
81 { "groan=blue", "?groan=yellow", 0 },
1bdbdaff 82 { "groan=blue", "groan!=yellow", 1 },
da89ac0b 83 { "groan=blue", "?groan!=yellow", 1 },
1bdbdaff 84 { "today=monday, tomorrow=3", "today!=2", 1 },
da89ac0b 85 { "today=monday, tomorrow=3", "today!='monday'", -1 },
1bdbdaff
P
86 { "today=monday, tomorrow=3", "tomorrow=3", 1 },
87 { "n=0x3", "n=3", 1 },
da89ac0b 88 { "n=0x3", "n=-3", -1 },
1bdbdaff
P
89 { "n=0x33", "n=51", 1 },
90 { "n=033", "n=27", 1 },
91 { "n=0", "n=00", 1 },
92 { "n=0x0", "n=0", 1 },
da89ac0b
P
93 { "n=0, sky=blue", "?n=0, sky=blue", 2 },
94 { "n=1, sky=blue", "?n=0, sky=blue", 1 },
1bdbdaff
P
95};
96
97static int test_property_parse(int n)
98{
99 OSSL_METHOD_STORE *store;
100 OSSL_PROPERTY_LIST *p = NULL, *q = NULL;
101 int r = 0;
102
1aedc35f 103 if (TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff
P
104 && add_property_names("sky", "groan", "cold", "today", "tomorrow", "n",
105 NULL)
1aedc35f
MC
106 && TEST_ptr(p = ossl_parse_property(NULL, parser_tests[n].defn))
107 && TEST_ptr(q = ossl_parse_query(NULL, parser_tests[n].query))
da89ac0b 108 && TEST_int_eq(ossl_property_match_count(q, p), parser_tests[n].e))
1bdbdaff
P
109 r = 1;
110 ossl_property_free(p);
111 ossl_property_free(q);
112 ossl_method_store_free(store);
113 return r;
114}
115
116static const struct {
117 const char *q_global;
118 const char *q_local;
119 const char *prop;
120} merge_tests[] = {
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" },
129 { "", "", "" },
130 /*
131 * The following four leave 'day' unspecified in the query, and will match
132 * any definition
133 */
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" },
140};
141
142static int test_property_merge(int n)
143{
144 OSSL_METHOD_STORE *store;
145 OSSL_PROPERTY_LIST *q_global = NULL, *q_local = NULL;
146 OSSL_PROPERTY_LIST *q_combined = NULL, *prop = NULL;
147 int r = 0;
148
1aedc35f 149 if (TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff
P
150 && add_property_names("colour", "urn", "clouds", "pot", "day", "night",
151 NULL)
1aedc35f
MC
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))
1bdbdaff 155 && TEST_ptr(q_combined = ossl_property_merge(q_local, q_global))
da89ac0b 156 && TEST_int_ge(ossl_property_match_count(q_combined, prop), 0))
1bdbdaff
P
157 r = 1;
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);
163 return r;
164}
165
166static int test_property_defn_cache(void)
167{
168 OSSL_METHOD_STORE *store;
169 OSSL_PROPERTY_LIST *red, *blue;
170 int r = 0;
171
1aedc35f 172 if (TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff 173 && add_property_names("red", "blue", NULL)
1aedc35f
MC
174 && TEST_ptr(red = ossl_parse_property(NULL, "red"))
175 && TEST_ptr(blue = ossl_parse_property(NULL, "blue"))
1bdbdaff 176 && TEST_ptr_ne(red, blue)
1aedc35f
MC
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))
1bdbdaff
P
181 r = 1;
182 ossl_method_store_free(store);
183 return r;
184}
185
186static const struct {
187 const char *defn;
188 const char *query;
189 int e;
190} definition_tests[] = {
191 { "alpha", "alpha=yes", 1 },
da89ac0b 192 { "alpha=no", "alpha", -1 },
1bdbdaff 193 { "alpha=1", "alpha=1", 1 },
da89ac0b
P
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 }
1bdbdaff
P
202};
203
204static int test_definition_compares(int n)
205{
206 OSSL_METHOD_STORE *store;
207 OSSL_PROPERTY_LIST *d = NULL, *q = NULL;
208 int r;
209
1aedc35f 210 r = TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff 211 && add_property_names("alpha", "omega", NULL)
1aedc35f
MC
212 && TEST_ptr(d = ossl_parse_property(NULL, definition_tests[n].defn))
213 && TEST_ptr(q = ossl_parse_query(NULL, definition_tests[n].query))
da89ac0b 214 && TEST_int_eq(ossl_property_match_count(q, d), definition_tests[n].e);
1bdbdaff
P
215
216 ossl_property_free(d);
217 ossl_property_free(q);
218 ossl_method_store_free(store);
219 return r;
220}
221
222static int test_register_deregister(void)
223{
224 static const struct {
225 int nid;
226 const char *prop;
227 char *impl;
228 } impls[] = {
229 { 6, "position=1", "a" },
230 { 6, "position=2", "b" },
231 { 6, "position=3", "c" },
232 { 6, "position=4", "d" },
233 };
234 size_t i;
235 int ret = 0;
236 OSSL_METHOD_STORE *store;
237
1aedc35f 238 if (!TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff
P
239 || !add_property_names("position", NULL))
240 goto err;
241
242 for (i = 0; i < OSSL_NELEM(impls); i++)
243 if (!TEST_true(ossl_method_store_add(store, impls[i].nid, impls[i].prop,
b1d40ddf 244 impls[i].impl, NULL, NULL))) {
1bdbdaff
P
245 TEST_note("iteration %zd", i + 1);
246 goto err;
247 }
248
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;
254
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);
258 goto err;
259 }
260 }
261
262 if (TEST_false(ossl_method_store_remove(store, impls[0].nid, impls[0].impl)))
263 ret = 1;
264err:
265 ossl_method_store_free(store);
266 return ret;
267}
268
269static int test_property(void)
270{
271 static const struct {
272 int nid;
273 const char *prop;
274 char *impl;
275 } impls[] = {
276 { 1, "fast=no, colour=green", "a" },
277 { 1, "fast, colour=blue", "b" },
278 { 1, "", "-" },
279 { 9, "sky=blue, furry", "c" },
280 { 3, NULL, "d" },
281 { 6, "sky.colour=blue, sky=green, old.data", "e" },
282 };
283 static struct {
284 int nid;
285 const char *prop;
286 char *expected;
287 } queries[] = {
288 { 1, "fast", "b" },
289 { 1, "fast=yes", "b" },
290 { 1, "fast=no, colour=green", "a" },
291 { 1, "colour=blue, fast", "b" },
292 { 1, "colour=blue", "b" },
293 { 9, "furry", "c" },
294 { 6, "sky.colour=blue", "e" },
295 { 6, "old.data", "e" },
296 { 9, "furry=yes, sky=blue", "c" },
297 { 1, "", "a" },
298 { 3, "", "d" },
299 };
300 OSSL_METHOD_STORE *store;
301 size_t i;
302 int ret = 0;
303 void *result;
304
1aedc35f 305 if (!TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff
P
306 || !add_property_names("fast", "colour", "sky", "furry", NULL))
307 goto err;
308
309 for (i = 0; i < OSSL_NELEM(impls); i++)
310 if (!TEST_true(ossl_method_store_add(store, impls[i].nid, impls[i].prop,
b1d40ddf 311 impls[i].impl, NULL, NULL))) {
1bdbdaff
P
312 TEST_note("iteration %zd", i + 1);
313 goto err;
314 }
315 for (i = 0; i < OSSL_NELEM(queries); i++) {
316 OSSL_PROPERTY_LIST *pq = NULL;
317
ef9f6066
P
318 if (!TEST_true(ossl_method_store_fetch(store, queries[i].nid,
319 queries[i].prop, &result))
1bdbdaff
P
320 || !TEST_str_eq((char *)result, queries[i].expected)) {
321 TEST_note("iteration %zd", i + 1);
322 ossl_property_free(pq);
323 goto err;
324 }
325 ossl_property_free(pq);
326 }
327 ret = 1;
328err:
329 ossl_method_store_free(store);
330 return ret;
331}
332
333static int test_query_cache_stochastic(void)
334{
335 const int max = 10000, tail = 10;
336 OSSL_METHOD_STORE *store;
337 int i, res = 0;
338 char buf[50];
339 void *result;
340 int errors = 0;
341 int v[10001];
342
1aedc35f 343 if (!TEST_ptr(store = ossl_method_store_new(NULL))
1bdbdaff
P
344 || !add_property_names("n", NULL))
345 goto err;
346
347 for (i = 1; i <= max; i++) {
348 v[i] = 2 * i;
349 BIO_snprintf(buf, sizeof(buf), "n=%d\n", i);
b1d40ddf 350 if (!TEST_true(ossl_method_store_add(store, i, buf, "abc", NULL, NULL))
1bdbdaff
P
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",
353 "miss"))) {
354 TEST_note("iteration %d", i);
355 goto err;
356 }
357 }
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)
361 || result != v + i)
362 errors++;
363 }
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);
366
367err:
368 ossl_method_store_free(store);
369 return res;
370}
371
372int setup_tests(void)
373{
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);
382 return 1;
383}