ca407b2ba43a6d5786a92ad650e2c5d3d636659f
[openssl.git] / test / property_test.c
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_local.h"
16
17 static int add_property_names(const char *n, ...)
18 {
19     va_list args;
20     int res = 1;
21
22     va_start(args, n);
23     do {
24         if (!TEST_int_ne(ossl_property_name(NULL, n, 1), 0))
25             res = 0;
26     } while ((n = va_arg(args, const char *)) != NULL);
27     va_end(args);
28     return res;
29 }
30
31 static int up_ref(void *p)
32 {
33     return 1;
34 }
35
36 static void down_ref(void *p)
37 {
38 }
39
40 static int test_property_string(void)
41 {
42     OSSL_METHOD_STORE *store;
43     int res = 0;
44     OSSL_PROPERTY_IDX i, j;
45
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)
54         && TEST_int_ne(i, j)
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)))
64         res = 1;
65     ossl_method_store_free(store);
66     return res;
67 }
68
69 static const struct {
70     const char *defn;
71     const char *query;
72     int e;
73 } parser_tests[] = {
74     { "", "sky=blue", -1 },
75     { "", "sky!=blue", 1 },
76     { "groan", "", 0 },
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 },
104 };
105
106 static int test_property_parse(int n)
107 {
108     OSSL_METHOD_STORE *store;
109     OSSL_PROPERTY_LIST *p = NULL, *q = NULL;
110     int r = 0;
111
112     if (TEST_ptr(store = ossl_method_store_new(NULL))
113         && add_property_names("sky", "groan", "cold", "today", "tomorrow", "n",
114                               NULL)
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))
118         r = 1;
119     ossl_property_free(p);
120     ossl_property_free(q);
121     ossl_method_store_free(store);
122     return r;
123 }
124
125 static const struct {
126     const char *q_global;
127     const char *q_local;
128     const char *prop;
129 } merge_tests[] = {
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" },
138     { "", "", "" },
139     /*
140      * The following four leave 'day' unspecified in the query, and will match
141      * any definition
142      */
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" },
149 };
150
151 static int test_property_merge(int n)
152 {
153     OSSL_METHOD_STORE *store;
154     OSSL_PROPERTY_LIST *q_global = NULL, *q_local = NULL;
155     OSSL_PROPERTY_LIST *q_combined = NULL, *prop = NULL;
156     int r = 0;
157
158     if (TEST_ptr(store = ossl_method_store_new(NULL))
159         && add_property_names("colour", "urn", "clouds", "pot", "day", "night",
160                               NULL)
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))
166         r = 1;
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);
172     return r;
173 }
174
175 static int test_property_defn_cache(void)
176 {
177     OSSL_METHOD_STORE *store;
178     OSSL_PROPERTY_LIST *red, *blue;
179     int r = 0;
180
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))
190         r = 1;
191     ossl_method_store_free(store);
192     return r;
193 }
194
195 static const struct {
196     const char *defn;
197     const char *query;
198     int e;
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 }
211 };
212
213 static int test_definition_compares(int n)
214 {
215     OSSL_METHOD_STORE *store;
216     OSSL_PROPERTY_LIST *d = NULL, *q = NULL;
217     int r;
218
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);
224
225     ossl_property_free(d);
226     ossl_property_free(q);
227     ossl_method_store_free(store);
228     return r;
229 }
230
231 static int test_register_deregister(void)
232 {
233     static const struct {
234         int nid;
235         const char *prop;
236         char *impl;
237     } impls[] = {
238         { 6, "position=1", "a" },
239         { 6, "position=2", "b" },
240         { 6, "position=3", "c" },
241         { 6, "position=4", "d" },
242     };
243     size_t i;
244     int ret = 0;
245     OSSL_METHOD_STORE *store;
246
247     if (!TEST_ptr(store = ossl_method_store_new(NULL))
248         || !add_property_names("position", NULL))
249         goto err;
250
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);
256             goto err;
257         }
258
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;
264
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);
268             goto err;
269         }
270     }
271
272     if (TEST_false(ossl_method_store_remove(store, impls[0].nid, impls[0].impl)))
273         ret = 1;
274 err:
275     ossl_method_store_free(store);
276     return ret;
277 }
278
279 static int test_property(void)
280 {
281     static const struct {
282         int nid;
283         const char *prop;
284         char *impl;
285     } impls[] = {
286         { 1, "fast=no, colour=green", "a" },
287         { 1, "fast, colour=blue", "b" },
288         { 1, "", "-" },
289         { 9, "sky=blue, furry", "c" },
290         { 3, NULL, "d" },
291         { 6, "sky.colour=blue, sky=green, old.data", "e" },
292     };
293     static struct {
294         int nid;
295         const char *prop;
296         char *expected;
297     } queries[] = {
298         { 1, "fast", "b" },
299         { 1, "fast=yes", "b" },
300         { 1, "fast=no, colour=green", "a" },
301         { 1, "colour=blue, fast", "b" },
302         { 1, "colour=blue", "b" },
303         { 9, "furry", "c" },
304         { 6, "sky.colour=blue", "e" },
305         { 6, "old.data", "e" },
306         { 9, "furry=yes, sky=blue", "c" },
307         { 1, "", "a" },
308         { 3, "", "d" },
309     };
310     OSSL_METHOD_STORE *store;
311     size_t i;
312     int ret = 0;
313     void *result;
314
315     if (!TEST_ptr(store = ossl_method_store_new(NULL))
316         || !add_property_names("fast", "colour", "sky", "furry", NULL))
317         goto err;
318
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);
324             goto err;
325         }
326     for (i = 0; i < OSSL_NELEM(queries); i++) {
327         OSSL_PROPERTY_LIST *pq = NULL;
328
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);
334             goto err;
335         }
336         ossl_property_free(pq);
337     }
338     ret = 1;
339 err:
340     ossl_method_store_free(store);
341     return ret;
342 }
343
344 static int test_query_cache_stochastic(void)
345 {
346     const int max = 10000, tail = 10;
347     OSSL_METHOD_STORE *store;
348     int i, res = 0;
349     char buf[50];
350     void *result;
351     int errors = 0;
352     int v[10001];
353
354     if (!TEST_ptr(store = ossl_method_store_new(NULL))
355         || !add_property_names("n", NULL))
356         goto err;
357
358     for (i = 1; i <= max; i++) {
359         v[i] = 2 * i;
360         BIO_snprintf(buf, sizeof(buf), "n=%d\n", i);
361         if (!TEST_true(ossl_method_store_add(store, NULL, i, buf, "abc",
362                                              &up_ref, &down_ref))
363                 || !TEST_true(ossl_method_store_cache_set(store, i, buf, v + i,
364                                                           &up_ref, &down_ref))
365                 || !TEST_true(ossl_method_store_cache_set(store, i, "n=1234",
366                                                           "miss", &up_ref,
367                                                           &down_ref))) {
368             TEST_note("iteration %d", i);
369             goto err;
370         }
371     }
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)
375             || result != v + i)
376             errors++;
377     }
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);
380
381 err:
382     ossl_method_store_free(store);
383     return res;
384 }
385
386 int setup_tests(void)
387 {
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);
396     return 1;
397 }