Reorganize local header files
[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 test_property_string(void)
32 {
33     OSSL_METHOD_STORE *store;
34     int res = 0;
35     OSSL_PROPERTY_IDX i, j;
36
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)
45         && TEST_int_ne(i, j)
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)))
55         res = 1;
56     ossl_method_store_free(store);
57     return res;
58 }
59
60 static const struct {
61     const char *defn;
62     const char *query;
63     int e;
64 } parser_tests[] = {
65     { "", "sky=blue", -1 },
66     { "", "sky!=blue", 1 },
67     { "groan", "", 0 },
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 },
91     { "n=0", "n=00", 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 },
95 };
96
97 static 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
103     if (TEST_ptr(store = ossl_method_store_new(NULL))
104         && add_property_names("sky", "groan", "cold", "today", "tomorrow", "n",
105                               NULL)
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))
109         r = 1;
110     ossl_property_free(p);
111     ossl_property_free(q);
112     ossl_method_store_free(store);
113     return r;
114 }
115
116 static 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
142 static 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
149     if (TEST_ptr(store = ossl_method_store_new(NULL))
150         && add_property_names("colour", "urn", "clouds", "pot", "day", "night",
151                               NULL)
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))
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
166 static int test_property_defn_cache(void)
167 {
168     OSSL_METHOD_STORE *store;
169     OSSL_PROPERTY_LIST *red, *blue;
170     int r = 0;
171
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))
181         r = 1;
182     ossl_method_store_free(store);
183     return r;
184 }
185
186 static const struct {
187     const char *defn;
188     const char *query;
189     int e;
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 }
202 };
203
204 static int test_definition_compares(int n)
205 {
206     OSSL_METHOD_STORE *store;
207     OSSL_PROPERTY_LIST *d = NULL, *q = NULL;
208     int r;
209
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);
215
216     ossl_property_free(d);
217     ossl_property_free(q);
218     ossl_method_store_free(store);
219     return r;
220 }
221
222 static 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
238     if (!TEST_ptr(store = ossl_method_store_new(NULL))
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, NULL, impls[i].nid,
244                                              impls[i].prop, impls[i].impl,
245                                              NULL, NULL))) {
246             TEST_note("iteration %zd", i + 1);
247             goto err;
248         }
249
250     /* Deregister in a different order to registration */
251     for (i = 0; i < OSSL_NELEM(impls); i++) {
252         const size_t j = (1 + i * 3) % OSSL_NELEM(impls);
253         int nid = impls[j].nid;
254         void *impl = impls[j].impl;
255
256         if (!TEST_true(ossl_method_store_remove(store, nid, impl))
257             || !TEST_false(ossl_method_store_remove(store, nid, impl))) {
258             TEST_note("iteration %zd, position %zd", i + 1, j + 1);
259             goto err;
260         }
261     }
262
263     if (TEST_false(ossl_method_store_remove(store, impls[0].nid, impls[0].impl)))
264         ret = 1;
265 err:
266     ossl_method_store_free(store);
267     return ret;
268 }
269
270 static int test_property(void)
271 {
272     static const struct {
273         int nid;
274         const char *prop;
275         char *impl;
276     } impls[] = {
277         { 1, "fast=no, colour=green", "a" },
278         { 1, "fast, colour=blue", "b" },
279         { 1, "", "-" },
280         { 9, "sky=blue, furry", "c" },
281         { 3, NULL, "d" },
282         { 6, "sky.colour=blue, sky=green, old.data", "e" },
283     };
284     static struct {
285         int nid;
286         const char *prop;
287         char *expected;
288     } queries[] = {
289         { 1, "fast", "b" },
290         { 1, "fast=yes", "b" },
291         { 1, "fast=no, colour=green", "a" },
292         { 1, "colour=blue, fast", "b" },
293         { 1, "colour=blue", "b" },
294         { 9, "furry", "c" },
295         { 6, "sky.colour=blue", "e" },
296         { 6, "old.data", "e" },
297         { 9, "furry=yes, sky=blue", "c" },
298         { 1, "", "a" },
299         { 3, "", "d" },
300     };
301     OSSL_METHOD_STORE *store;
302     size_t i;
303     int ret = 0;
304     void *result;
305
306     if (!TEST_ptr(store = ossl_method_store_new(NULL))
307         || !add_property_names("fast", "colour", "sky", "furry", NULL))
308         goto err;
309
310     for (i = 0; i < OSSL_NELEM(impls); i++)
311         if (!TEST_true(ossl_method_store_add(store, NULL, impls[i].nid,
312                                              impls[i].prop, impls[i].impl,
313                                              NULL, NULL))) {
314             TEST_note("iteration %zd", i + 1);
315             goto err;
316         }
317     for (i = 0; i < OSSL_NELEM(queries); i++) {
318         OSSL_PROPERTY_LIST *pq = NULL;
319
320         if (!TEST_true(ossl_method_store_fetch(store, queries[i].nid,
321                                                queries[i].prop, &result))
322             || !TEST_str_eq((char *)result, queries[i].expected)) {
323             TEST_note("iteration %zd", i + 1);
324             ossl_property_free(pq);
325             goto err;
326         }
327         ossl_property_free(pq);
328     }
329     ret = 1;
330 err:
331     ossl_method_store_free(store);
332     return ret;
333 }
334
335 static int test_query_cache_stochastic(void)
336 {
337     const int max = 10000, tail = 10;
338     OSSL_METHOD_STORE *store;
339     int i, res = 0;
340     char buf[50];
341     void *result;
342     int errors = 0;
343     int v[10001];
344
345     if (!TEST_ptr(store = ossl_method_store_new(NULL))
346         || !add_property_names("n", NULL))
347         goto err;
348
349     for (i = 1; i <= max; i++) {
350         v[i] = 2 * i;
351         BIO_snprintf(buf, sizeof(buf), "n=%d\n", i);
352         if (!TEST_true(ossl_method_store_add(store, NULL, i, buf, "abc",
353                                              NULL, NULL))
354                 || !TEST_true(ossl_method_store_cache_set(store, i, buf, v + i))
355                 || !TEST_true(ossl_method_store_cache_set(store, i, "n=1234",
356                                                           "miss"))) {
357             TEST_note("iteration %d", i);
358             goto err;
359         }
360     }
361     for (i = 1; i <= max; i++) {
362         BIO_snprintf(buf, sizeof(buf), "n=%d\n", i);
363         if (!ossl_method_store_cache_get(store, i, buf, &result)
364             || result != v + i)
365             errors++;
366     }
367     /* There is a tiny probability that this will fail when it shouldn't */
368     res = TEST_int_gt(errors, tail) && TEST_int_lt(errors, max - tail);
369
370 err:
371     ossl_method_store_free(store);
372     return res;
373 }
374
375 int setup_tests(void)
376 {
377     ADD_TEST(test_property_string);
378     ADD_ALL_TESTS(test_property_parse, OSSL_NELEM(parser_tests));
379     ADD_ALL_TESTS(test_property_merge, OSSL_NELEM(merge_tests));
380     ADD_TEST(test_property_defn_cache);
381     ADD_ALL_TESTS(test_definition_compares, OSSL_NELEM(definition_tests));
382     ADD_TEST(test_register_deregister);
383     ADD_TEST(test_property);
384     ADD_TEST(test_query_cache_stochastic);
385     return 1;
386 }