Add a test for converting a property list to a string
[openssl.git] / test / property_test.c
1 /*
2  * Copyright 2019-2021 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 <openssl/evp.h>
13 #include "testutil.h"
14 #include "internal/nelem.h"
15 #include "internal/property.h"
16 #include "../crypto/property/property_local.h"
17
18 static int add_property_names(const char *n, ...)
19 {
20     va_list args;
21     int res = 1;
22
23     va_start(args, n);
24     do {
25         if (!TEST_int_ne(ossl_property_name(NULL, n, 1), 0))
26             res = 0;
27     } while ((n = va_arg(args, const char *)) != NULL);
28     va_end(args);
29     return res;
30 }
31
32 static int up_ref(void *p)
33 {
34     return 1;
35 }
36
37 static void down_ref(void *p)
38 {
39 }
40
41 static int test_property_string(void)
42 {
43     OSSL_METHOD_STORE *store;
44     int res = 0;
45     OSSL_PROPERTY_IDX i, j;
46
47     if (TEST_ptr(store = ossl_method_store_new(NULL))
48         && TEST_int_eq(ossl_property_name(NULL, "fnord", 0), 0)
49         && TEST_int_ne(ossl_property_name(NULL, "fnord", 1), 0)
50         && TEST_int_ne(ossl_property_name(NULL, "name", 1), 0)
51         /* Property value checks */
52         && TEST_int_eq(ossl_property_value(NULL, "fnord", 0), 0)
53         && TEST_int_ne(i = ossl_property_value(NULL, "no", 0), 0)
54         && TEST_int_ne(j = ossl_property_value(NULL, "yes", 0), 0)
55         && TEST_int_ne(i, j)
56         && TEST_int_eq(ossl_property_value(NULL, "yes", 1), j)
57         && TEST_int_eq(ossl_property_value(NULL, "no", 1), i)
58         && TEST_int_ne(i = ossl_property_value(NULL, "illuminati", 1), 0)
59         && TEST_int_eq(j = ossl_property_value(NULL, "fnord", 1), i + 1)
60         && TEST_int_eq(ossl_property_value(NULL, "fnord", 1), j)
61         /* Check name and values are distinct */
62         && TEST_int_eq(ossl_property_value(NULL, "cold", 0), 0)
63         && TEST_int_ne(ossl_property_name(NULL, "fnord", 0),
64                        ossl_property_value(NULL, "fnord", 0)))
65         res = 1;
66     ossl_method_store_free(store);
67     return res;
68 }
69
70 static const struct {
71     const char *defn;
72     const char *query;
73     int e;
74 } parser_tests[] = {
75     { "", "sky=blue", -1 },
76     { "", "sky!=blue", 1 },
77     { "groan", "", 0 },
78     { "cold=yes", "cold=yes", 1 },
79     { "cold=yes", "cold", 1 },
80     { "cold=yes", "cold!=no", 1 },
81     { "groan", "groan=yes", 1 },
82     { "groan", "groan=no", -1 },
83     { "groan", "groan!=yes", -1 },
84     { "cold=no", "cold", -1 },
85     { "cold=no", "?cold", 0 },
86     { "cold=no", "cold=no", 1 },
87     { "groan", "cold", -1 },
88     { "groan", "cold=no", 1 },
89     { "groan", "cold!=yes", 1 },
90     { "groan=blue", "groan=yellow", -1 },
91     { "groan=blue", "?groan=yellow", 0 },
92     { "groan=blue", "groan!=yellow", 1 },
93     { "groan=blue", "?groan!=yellow", 1 },
94     { "today=monday, tomorrow=3", "today!=2", 1 },
95     { "today=monday, tomorrow=3", "today!='monday'", -1 },
96     { "today=monday, tomorrow=3", "tomorrow=3", 1 },
97     { "n=0x3", "n=3", 1 },
98     { "n=0x3", "n=-3", -1 },
99     { "n=0x33", "n=51", 1 },
100     { "n=033", "n=27", 1 },
101     { "n=0", "n=00", 1 },
102     { "n=0x0", "n=0", 1 },
103     { "n=0, sky=blue", "?n=0, sky=blue", 2 },
104     { "n=1, sky=blue", "?n=0, sky=blue", 1 },
105 };
106
107 static int test_property_parse(int n)
108 {
109     OSSL_METHOD_STORE *store;
110     OSSL_PROPERTY_LIST *p = NULL, *q = NULL;
111     int r = 0;
112
113     if (TEST_ptr(store = ossl_method_store_new(NULL))
114         && add_property_names("sky", "groan", "cold", "today", "tomorrow", "n",
115                               NULL)
116         && TEST_ptr(p = ossl_parse_property(NULL, parser_tests[n].defn))
117         && TEST_ptr(q = ossl_parse_query(NULL, parser_tests[n].query, 0))
118         && TEST_int_eq(ossl_property_match_count(q, p), parser_tests[n].e))
119         r = 1;
120     ossl_property_free(p);
121     ossl_property_free(q);
122     ossl_method_store_free(store);
123     return r;
124 }
125
126 static int test_property_query_value_create(void)
127 {
128     OSSL_METHOD_STORE *store;
129     OSSL_PROPERTY_LIST *p = NULL, *q = NULL, *o = NULL;
130     int r = 0;
131
132     /* The property value used here must not be used in other test cases */
133     if (TEST_ptr(store = ossl_method_store_new(NULL))
134         && add_property_names("wood", NULL)
135         && TEST_ptr(p = ossl_parse_query(NULL, "wood=oak", 0)) /* undefined */
136         && TEST_ptr(q = ossl_parse_query(NULL, "wood=oak", 1)) /* creates */
137         && TEST_ptr(o = ossl_parse_query(NULL, "wood=oak", 0)) /* defined */
138         && TEST_int_eq(ossl_property_match_count(q, p), -1)
139         && TEST_int_eq(ossl_property_match_count(q, o), 1))
140         r = 1;
141     ossl_property_free(o);
142     ossl_property_free(p);
143     ossl_property_free(q);
144     ossl_method_store_free(store);
145     return r;
146 }
147
148 static const struct {
149     const char *q_global;
150     const char *q_local;
151     const char *prop;
152 } merge_tests[] = {
153     { "", "colour=blue", "colour=blue" },
154     { "colour=blue", "", "colour=blue" },
155     { "colour=red", "colour=blue", "colour=blue" },
156     { "clouds=pink, urn=red", "urn=blue, colour=green",
157         "urn=blue, colour=green, clouds=pink" },
158     { "pot=gold", "urn=blue", "pot=gold, urn=blue" },
159     { "night", "day", "day=yes, night=yes" },
160     { "day", "night", "day=yes, night=yes" },
161     { "", "", "" },
162     /*
163      * The following four leave 'day' unspecified in the query, and will match
164      * any definition
165      */
166     { "day=yes", "-day", "day=no" },
167     { "day=yes", "-day", "day=yes" },
168     { "day=yes", "-day", "day=arglebargle" },
169     { "day=yes", "-day", "pot=sesquioxidizing" },
170     { "day, night", "-night, day", "day=yes, night=no" },
171     { "-day", "day=yes", "day=yes" },
172 };
173
174 static int test_property_merge(int n)
175 {
176     OSSL_METHOD_STORE *store;
177     OSSL_PROPERTY_LIST *q_global = NULL, *q_local = NULL;
178     OSSL_PROPERTY_LIST *q_combined = NULL, *prop = NULL;
179     int r = 0;
180
181     if (TEST_ptr(store = ossl_method_store_new(NULL))
182         && add_property_names("colour", "urn", "clouds", "pot", "day", "night",
183                               NULL)
184         && TEST_ptr(prop = ossl_parse_property(NULL, merge_tests[n].prop))
185         && TEST_ptr(q_global = ossl_parse_query(NULL, merge_tests[n].q_global,
186                                                 0))
187         && TEST_ptr(q_local = ossl_parse_query(NULL, merge_tests[n].q_local, 0))
188         && TEST_ptr(q_combined = ossl_property_merge(q_local, q_global))
189         && TEST_int_ge(ossl_property_match_count(q_combined, prop), 0))
190         r = 1;
191     ossl_property_free(q_global);
192     ossl_property_free(q_local);
193     ossl_property_free(q_combined);
194     ossl_property_free(prop);
195     ossl_method_store_free(store);
196     return r;
197 }
198
199 static int test_property_defn_cache(void)
200 {
201     OSSL_METHOD_STORE *store;
202     OSSL_PROPERTY_LIST *red, *blue;
203     int r = 0;
204
205     if (TEST_ptr(store = ossl_method_store_new(NULL))
206         && add_property_names("red", "blue", NULL)
207         && TEST_ptr(red = ossl_parse_property(NULL, "red"))
208         && TEST_ptr(blue = ossl_parse_property(NULL, "blue"))
209         && TEST_ptr_ne(red, blue)
210         && TEST_true(ossl_prop_defn_set(NULL, "red", red))
211         && TEST_true(ossl_prop_defn_set(NULL, "blue", blue))
212         && TEST_ptr_eq(ossl_prop_defn_get(NULL, "red"), red)
213         && TEST_ptr_eq(ossl_prop_defn_get(NULL, "blue"), blue))
214         r = 1;
215     ossl_method_store_free(store);
216     return r;
217 }
218
219 static const struct {
220     const char *defn;
221     const char *query;
222     int e;
223 } definition_tests[] = {
224     { "alpha", "alpha=yes", 1 },
225     { "alpha=no", "alpha", -1 },
226     { "alpha=1", "alpha=1", 1 },
227     { "alpha=2", "alpha=1",-1 },
228     { "alpha", "omega", -1 },
229     { "alpha", "?omega", 0 },
230     { "alpha", "?omega=1", 0 },
231     { "alpha", "?omega=no", 1 },
232     { "alpha", "?omega=yes", 0 },
233     { "alpha, omega", "?omega=yes", 1 },
234     { "alpha, omega", "?omega=no", 0 }
235 };
236
237 static int test_definition_compares(int n)
238 {
239     OSSL_METHOD_STORE *store;
240     OSSL_PROPERTY_LIST *d = NULL, *q = NULL;
241     int r;
242
243     r = TEST_ptr(store = ossl_method_store_new(NULL))
244         && add_property_names("alpha", "omega", NULL)
245         && TEST_ptr(d = ossl_parse_property(NULL, definition_tests[n].defn))
246         && TEST_ptr(q = ossl_parse_query(NULL, definition_tests[n].query, 0))
247         && TEST_int_eq(ossl_property_match_count(q, d), definition_tests[n].e);
248
249     ossl_property_free(d);
250     ossl_property_free(q);
251     ossl_method_store_free(store);
252     return r;
253 }
254
255 static int test_register_deregister(void)
256 {
257     static const struct {
258         int nid;
259         const char *prop;
260         char *impl;
261     } impls[] = {
262         { 6, "position=1", "a" },
263         { 6, "position=2", "b" },
264         { 6, "position=3", "c" },
265         { 6, "position=4", "d" },
266     };
267     size_t i;
268     int ret = 0;
269     OSSL_METHOD_STORE *store;
270
271     if (!TEST_ptr(store = ossl_method_store_new(NULL))
272         || !add_property_names("position", NULL))
273         goto err;
274
275     for (i = 0; i < OSSL_NELEM(impls); i++)
276         if (!TEST_true(ossl_method_store_add(store, NULL, impls[i].nid,
277                                              impls[i].prop, impls[i].impl,
278                                              &up_ref, &down_ref))) {
279             TEST_note("iteration %zd", i + 1);
280             goto err;
281         }
282
283     /* Deregister in a different order to registration */
284     for (i = 0; i < OSSL_NELEM(impls); i++) {
285         const size_t j = (1 + i * 3) % OSSL_NELEM(impls);
286         int nid = impls[j].nid;
287         void *impl = impls[j].impl;
288
289         if (!TEST_true(ossl_method_store_remove(store, nid, impl))
290             || !TEST_false(ossl_method_store_remove(store, nid, impl))) {
291             TEST_note("iteration %zd, position %zd", i + 1, j + 1);
292             goto err;
293         }
294     }
295
296     if (TEST_false(ossl_method_store_remove(store, impls[0].nid, impls[0].impl)))
297         ret = 1;
298 err:
299     ossl_method_store_free(store);
300     return ret;
301 }
302
303 static int test_property(void)
304 {
305     static const struct {
306         int nid;
307         const char *prop;
308         char *impl;
309     } impls[] = {
310         { 1, "fast=no, colour=green", "a" },
311         { 1, "fast, colour=blue", "b" },
312         { 1, "", "-" },
313         { 9, "sky=blue, furry", "c" },
314         { 3, NULL, "d" },
315         { 6, "sky.colour=blue, sky=green, old.data", "e" },
316     };
317     static struct {
318         int nid;
319         const char *prop;
320         char *expected;
321     } queries[] = {
322         { 1, "fast", "b" },
323         { 1, "fast=yes", "b" },
324         { 1, "fast=no, colour=green", "a" },
325         { 1, "colour=blue, fast", "b" },
326         { 1, "colour=blue", "b" },
327         { 9, "furry", "c" },
328         { 6, "sky.colour=blue", "e" },
329         { 6, "old.data", "e" },
330         { 9, "furry=yes, sky=blue", "c" },
331         { 1, "", "a" },
332         { 3, "", "d" },
333     };
334     OSSL_METHOD_STORE *store;
335     size_t i;
336     int ret = 0;
337     void *result;
338
339     if (!TEST_ptr(store = ossl_method_store_new(NULL))
340         || !add_property_names("fast", "colour", "sky", "furry", NULL))
341         goto err;
342
343     for (i = 0; i < OSSL_NELEM(impls); i++)
344         if (!TEST_true(ossl_method_store_add(store, NULL, impls[i].nid,
345                                              impls[i].prop, impls[i].impl,
346                                              &up_ref, &down_ref))) {
347             TEST_note("iteration %zd", i + 1);
348             goto err;
349         }
350     for (i = 0; i < OSSL_NELEM(queries); i++) {
351         OSSL_PROPERTY_LIST *pq = NULL;
352
353         if (!TEST_true(ossl_method_store_fetch(store, queries[i].nid,
354                                                queries[i].prop, &result))
355             || !TEST_str_eq((char *)result, queries[i].expected)) {
356             TEST_note("iteration %zd", i + 1);
357             ossl_property_free(pq);
358             goto err;
359         }
360         ossl_property_free(pq);
361     }
362     ret = 1;
363 err:
364     ossl_method_store_free(store);
365     return ret;
366 }
367
368 static int test_query_cache_stochastic(void)
369 {
370     const int max = 10000, tail = 10;
371     OSSL_METHOD_STORE *store;
372     int i, res = 0;
373     char buf[50];
374     void *result;
375     int errors = 0;
376     int v[10001];
377
378     if (!TEST_ptr(store = ossl_method_store_new(NULL))
379         || !add_property_names("n", NULL))
380         goto err;
381
382     for (i = 1; i <= max; i++) {
383         v[i] = 2 * i;
384         BIO_snprintf(buf, sizeof(buf), "n=%d\n", i);
385         if (!TEST_true(ossl_method_store_add(store, NULL, i, buf, "abc",
386                                              &up_ref, &down_ref))
387                 || !TEST_true(ossl_method_store_cache_set(store, i, buf, v + i,
388                                                           &up_ref, &down_ref))
389                 || !TEST_true(ossl_method_store_cache_set(store, i, "n=1234",
390                                                           "miss", &up_ref,
391                                                           &down_ref))) {
392             TEST_note("iteration %d", i);
393             goto err;
394         }
395     }
396     for (i = 1; i <= max; i++) {
397         BIO_snprintf(buf, sizeof(buf), "n=%d\n", i);
398         if (!ossl_method_store_cache_get(store, i, buf, &result)
399             || result != v + i)
400             errors++;
401     }
402     /* There is a tiny probability that this will fail when it shouldn't */
403     res = TEST_int_gt(errors, tail) && TEST_int_lt(errors, max - tail);
404
405 err:
406     ossl_method_store_free(store);
407     return res;
408 }
409
410 static int test_fips_mode(void)
411 {
412     int ret = 0;
413     OSSL_LIB_CTX *ctx = NULL;
414
415     if (!TEST_ptr(ctx = OSSL_LIB_CTX_new()))
416         goto err;
417
418     ret = TEST_true(EVP_set_default_properties(ctx, "default=yes,fips=yes"))
419           && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
420           && TEST_true(EVP_set_default_properties(ctx, "fips=no,default=yes"))
421           && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
422           && TEST_true(EVP_set_default_properties(ctx, "fips=no"))
423           && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
424           && TEST_true(EVP_set_default_properties(ctx, "fips!=no"))
425           && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
426           && TEST_true(EVP_set_default_properties(ctx, "fips=no"))
427           && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
428           && TEST_true(EVP_set_default_properties(ctx, "fips=no,default=yes"))
429           && TEST_true(EVP_default_properties_enable_fips(ctx, 1))
430           && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
431           && TEST_true(EVP_default_properties_enable_fips(ctx, 0))
432           && TEST_false(EVP_default_properties_is_fips_enabled(ctx));
433 err:
434     OSSL_LIB_CTX_free(ctx);
435     return ret;
436 }
437
438 static int test_property_list_to_string(void)
439 {
440     OSSL_PROPERTY_LIST *pl = NULL;
441     int ret = 0;
442     struct props_list_str {
443         const char *in;
444         const char *out;
445     } props[] = {
446         { "fips=yes", "fips=yes" },
447         { "fips!=yes", "fips!=yes" },
448         { "fips = yes", "fips=yes" },
449         { "fips", "fips=yes" },
450         { "fips=no", "fips=no" },
451         { "-fips", "-fips" },
452         { "?fips=yes", "?fips=yes" },
453         { "fips=yes,provider=fips", "fips=yes,provider=fips" },
454         { "fips = yes , provider = fips", "fips=yes,provider=fips" },
455         { "fips=yes,provider!=fips", "fips=yes,provider!=fips" },
456         { "fips=yes,?provider=fips", "fips=yes,?provider=fips" },
457         { "fips=yes,-provider", "fips=yes,-provider" },
458           /* foo is an unknown internal name */
459         { "foo=yes,fips=yes", "fips=yes"},
460         { "", "" },
461         { NULL, "" }
462     };
463     size_t i, bufsize;
464     char *buf = NULL;
465
466     for (i = 0; i < OSSL_NELEM(props); i++) {
467         if (props[i].in != NULL
468                 && !TEST_ptr(pl = ossl_parse_query(NULL, props[i].in, 1)))
469             goto err;
470         bufsize = ossl_property_list_to_string(NULL, pl, NULL, 0);
471         if (!TEST_size_t_gt(bufsize, 0))
472             goto err;
473         buf = OPENSSL_malloc(bufsize);
474         if (!TEST_ptr(buf)
475                 || !TEST_size_t_eq(ossl_property_list_to_string(NULL, pl, buf,
476                                                                 bufsize),
477                                    bufsize)
478                 || !TEST_str_eq(props[i].out, buf)
479                 || !TEST_size_t_eq(bufsize, strlen(props[i].out) + 1))
480             goto err;
481         OPENSSL_free(buf);
482         buf = NULL;
483         ossl_property_free(pl);
484         pl = NULL;
485     }
486
487     ret = 1;
488  err:
489     OPENSSL_free(buf);
490     ossl_property_free(pl);
491     return ret;
492 }
493
494 int setup_tests(void)
495 {
496     ADD_TEST(test_property_string);
497     ADD_TEST(test_property_query_value_create);
498     ADD_ALL_TESTS(test_property_parse, OSSL_NELEM(parser_tests));
499     ADD_ALL_TESTS(test_property_merge, OSSL_NELEM(merge_tests));
500     ADD_TEST(test_property_defn_cache);
501     ADD_ALL_TESTS(test_definition_compares, OSSL_NELEM(definition_tests));
502     ADD_TEST(test_register_deregister);
503     ADD_TEST(test_property);
504     ADD_TEST(test_query_cache_stochastic);
505     ADD_TEST(test_fips_mode);
506     ADD_TEST(test_property_list_to_string);
507     return 1;
508 }