Write a test provider to test the TLS-GROUPS capability
[openssl.git] / test / property_test.c
1 /*
2  * Copyright 2019-2020 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))
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 const struct {
127     const char *q_global;
128     const char *q_local;
129     const char *prop;
130 } merge_tests[] = {
131     { "", "colour=blue", "colour=blue" },
132     { "colour=blue", "", "colour=blue" },
133     { "colour=red", "colour=blue", "colour=blue" },
134     { "clouds=pink, urn=red", "urn=blue, colour=green",
135         "urn=blue, colour=green, clouds=pink" },
136     { "pot=gold", "urn=blue", "pot=gold, urn=blue" },
137     { "night", "day", "day=yes, night=yes" },
138     { "day", "night", "day=yes, night=yes" },
139     { "", "", "" },
140     /*
141      * The following four leave 'day' unspecified in the query, and will match
142      * any definition
143      */
144     { "day=yes", "-day", "day=no" },
145     { "day=yes", "-day", "day=yes" },
146     { "day=yes", "-day", "day=arglebargle" },
147     { "day=yes", "-day", "pot=sesquioxidizing" },
148     { "day, night", "-night, day", "day=yes, night=no" },
149     { "-day", "day=yes", "day=yes" },
150 };
151
152 static int test_property_merge(int n)
153 {
154     OSSL_METHOD_STORE *store;
155     OSSL_PROPERTY_LIST *q_global = NULL, *q_local = NULL;
156     OSSL_PROPERTY_LIST *q_combined = NULL, *prop = NULL;
157     int r = 0;
158
159     if (TEST_ptr(store = ossl_method_store_new(NULL))
160         && add_property_names("colour", "urn", "clouds", "pot", "day", "night",
161                               NULL)
162         && TEST_ptr(prop = ossl_parse_property(NULL, merge_tests[n].prop))
163         && TEST_ptr(q_global = ossl_parse_query(NULL, merge_tests[n].q_global))
164         && TEST_ptr(q_local = ossl_parse_query(NULL, merge_tests[n].q_local))
165         && TEST_ptr(q_combined = ossl_property_merge(q_local, q_global))
166         && TEST_int_ge(ossl_property_match_count(q_combined, prop), 0))
167         r = 1;
168     ossl_property_free(q_global);
169     ossl_property_free(q_local);
170     ossl_property_free(q_combined);
171     ossl_property_free(prop);
172     ossl_method_store_free(store);
173     return r;
174 }
175
176 static int test_property_defn_cache(void)
177 {
178     OSSL_METHOD_STORE *store;
179     OSSL_PROPERTY_LIST *red, *blue;
180     int r = 0;
181
182     if (TEST_ptr(store = ossl_method_store_new(NULL))
183         && add_property_names("red", "blue", NULL)
184         && TEST_ptr(red = ossl_parse_property(NULL, "red"))
185         && TEST_ptr(blue = ossl_parse_property(NULL, "blue"))
186         && TEST_ptr_ne(red, blue)
187         && TEST_true(ossl_prop_defn_set(NULL, "red", red))
188         && TEST_true(ossl_prop_defn_set(NULL, "blue", blue))
189         && TEST_ptr_eq(ossl_prop_defn_get(NULL, "red"), red)
190         && TEST_ptr_eq(ossl_prop_defn_get(NULL, "blue"), blue))
191         r = 1;
192     ossl_method_store_free(store);
193     return r;
194 }
195
196 static const struct {
197     const char *defn;
198     const char *query;
199     int e;
200 } definition_tests[] = {
201     { "alpha", "alpha=yes", 1 },
202     { "alpha=no", "alpha", -1 },
203     { "alpha=1", "alpha=1", 1 },
204     { "alpha=2", "alpha=1",-1 },
205     { "alpha", "omega", -1 },
206     { "alpha", "?omega", 0 },
207     { "alpha", "?omega=1", 0 },
208     { "alpha", "?omega=no", 1 },
209     { "alpha", "?omega=yes", 0 },
210     { "alpha, omega", "?omega=yes", 1 },
211     { "alpha, omega", "?omega=no", 0 }
212 };
213
214 static int test_definition_compares(int n)
215 {
216     OSSL_METHOD_STORE *store;
217     OSSL_PROPERTY_LIST *d = NULL, *q = NULL;
218     int r;
219
220     r = TEST_ptr(store = ossl_method_store_new(NULL))
221         && add_property_names("alpha", "omega", NULL)
222         && TEST_ptr(d = ossl_parse_property(NULL, definition_tests[n].defn))
223         && TEST_ptr(q = ossl_parse_query(NULL, definition_tests[n].query))
224         && TEST_int_eq(ossl_property_match_count(q, d), definition_tests[n].e);
225
226     ossl_property_free(d);
227     ossl_property_free(q);
228     ossl_method_store_free(store);
229     return r;
230 }
231
232 static int test_register_deregister(void)
233 {
234     static const struct {
235         int nid;
236         const char *prop;
237         char *impl;
238     } impls[] = {
239         { 6, "position=1", "a" },
240         { 6, "position=2", "b" },
241         { 6, "position=3", "c" },
242         { 6, "position=4", "d" },
243     };
244     size_t i;
245     int ret = 0;
246     OSSL_METHOD_STORE *store;
247
248     if (!TEST_ptr(store = ossl_method_store_new(NULL))
249         || !add_property_names("position", NULL))
250         goto err;
251
252     for (i = 0; i < OSSL_NELEM(impls); i++)
253         if (!TEST_true(ossl_method_store_add(store, NULL, impls[i].nid,
254                                              impls[i].prop, impls[i].impl,
255                                              &up_ref, &down_ref))) {
256             TEST_note("iteration %zd", i + 1);
257             goto err;
258         }
259
260     /* Deregister in a different order to registration */
261     for (i = 0; i < OSSL_NELEM(impls); i++) {
262         const size_t j = (1 + i * 3) % OSSL_NELEM(impls);
263         int nid = impls[j].nid;
264         void *impl = impls[j].impl;
265
266         if (!TEST_true(ossl_method_store_remove(store, nid, impl))
267             || !TEST_false(ossl_method_store_remove(store, nid, impl))) {
268             TEST_note("iteration %zd, position %zd", i + 1, j + 1);
269             goto err;
270         }
271     }
272
273     if (TEST_false(ossl_method_store_remove(store, impls[0].nid, impls[0].impl)))
274         ret = 1;
275 err:
276     ossl_method_store_free(store);
277     return ret;
278 }
279
280 static int test_property(void)
281 {
282     static const struct {
283         int nid;
284         const char *prop;
285         char *impl;
286     } impls[] = {
287         { 1, "fast=no, colour=green", "a" },
288         { 1, "fast, colour=blue", "b" },
289         { 1, "", "-" },
290         { 9, "sky=blue, furry", "c" },
291         { 3, NULL, "d" },
292         { 6, "sky.colour=blue, sky=green, old.data", "e" },
293     };
294     static struct {
295         int nid;
296         const char *prop;
297         char *expected;
298     } queries[] = {
299         { 1, "fast", "b" },
300         { 1, "fast=yes", "b" },
301         { 1, "fast=no, colour=green", "a" },
302         { 1, "colour=blue, fast", "b" },
303         { 1, "colour=blue", "b" },
304         { 9, "furry", "c" },
305         { 6, "sky.colour=blue", "e" },
306         { 6, "old.data", "e" },
307         { 9, "furry=yes, sky=blue", "c" },
308         { 1, "", "a" },
309         { 3, "", "d" },
310     };
311     OSSL_METHOD_STORE *store;
312     size_t i;
313     int ret = 0;
314     void *result;
315
316     if (!TEST_ptr(store = ossl_method_store_new(NULL))
317         || !add_property_names("fast", "colour", "sky", "furry", NULL))
318         goto err;
319
320     for (i = 0; i < OSSL_NELEM(impls); i++)
321         if (!TEST_true(ossl_method_store_add(store, NULL, impls[i].nid,
322                                              impls[i].prop, impls[i].impl,
323                                              &up_ref, &down_ref))) {
324             TEST_note("iteration %zd", i + 1);
325             goto err;
326         }
327     for (i = 0; i < OSSL_NELEM(queries); i++) {
328         OSSL_PROPERTY_LIST *pq = NULL;
329
330         if (!TEST_true(ossl_method_store_fetch(store, queries[i].nid,
331                                                queries[i].prop, &result))
332             || !TEST_str_eq((char *)result, queries[i].expected)) {
333             TEST_note("iteration %zd", i + 1);
334             ossl_property_free(pq);
335             goto err;
336         }
337         ossl_property_free(pq);
338     }
339     ret = 1;
340 err:
341     ossl_method_store_free(store);
342     return ret;
343 }
344
345 static int test_query_cache_stochastic(void)
346 {
347     const int max = 10000, tail = 10;
348     OSSL_METHOD_STORE *store;
349     int i, res = 0;
350     char buf[50];
351     void *result;
352     int errors = 0;
353     int v[10001];
354
355     if (!TEST_ptr(store = ossl_method_store_new(NULL))
356         || !add_property_names("n", NULL))
357         goto err;
358
359     for (i = 1; i <= max; i++) {
360         v[i] = 2 * i;
361         BIO_snprintf(buf, sizeof(buf), "n=%d\n", i);
362         if (!TEST_true(ossl_method_store_add(store, NULL, i, buf, "abc",
363                                              &up_ref, &down_ref))
364                 || !TEST_true(ossl_method_store_cache_set(store, i, buf, v + i,
365                                                           &up_ref, &down_ref))
366                 || !TEST_true(ossl_method_store_cache_set(store, i, "n=1234",
367                                                           "miss", &up_ref,
368                                                           &down_ref))) {
369             TEST_note("iteration %d", i);
370             goto err;
371         }
372     }
373     for (i = 1; i <= max; i++) {
374         BIO_snprintf(buf, sizeof(buf), "n=%d\n", i);
375         if (!ossl_method_store_cache_get(store, i, buf, &result)
376             || result != v + i)
377             errors++;
378     }
379     /* There is a tiny probability that this will fail when it shouldn't */
380     res = TEST_int_gt(errors, tail) && TEST_int_lt(errors, max - tail);
381
382 err:
383     ossl_method_store_free(store);
384     return res;
385 }
386
387 static int test_fips_mode(void)
388 {
389     int ret = 0;
390     OPENSSL_CTX *ctx = NULL;
391
392     if (!TEST_ptr(ctx = OPENSSL_CTX_new()))
393         goto err;
394
395     ret = TEST_true(EVP_set_default_properties(ctx, "default=yes,fips=yes"))
396           && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
397           && TEST_true(EVP_set_default_properties(ctx, "fips=no,default=yes"))
398           && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
399           && TEST_true(EVP_set_default_properties(ctx, "fips=no"))
400           && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
401           && TEST_true(EVP_set_default_properties(ctx, "fips!=no"))
402           && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
403           && TEST_true(EVP_set_default_properties(ctx, "fips=no"))
404           && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
405           && TEST_true(EVP_set_default_properties(ctx, "fips=no,default=yes"))
406           && TEST_true(EVP_default_properties_enable_fips(ctx, 1))
407           && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
408           && TEST_true(EVP_default_properties_enable_fips(ctx, 0))
409           && TEST_false(EVP_default_properties_is_fips_enabled(ctx));
410 err:
411     OPENSSL_CTX_free(ctx);
412     return ret;
413 }
414
415
416 int setup_tests(void)
417 {
418     ADD_TEST(test_property_string);
419     ADD_ALL_TESTS(test_property_parse, OSSL_NELEM(parser_tests));
420     ADD_ALL_TESTS(test_property_merge, OSSL_NELEM(merge_tests));
421     ADD_TEST(test_property_defn_cache);
422     ADD_ALL_TESTS(test_definition_compares, OSSL_NELEM(definition_tests));
423     ADD_TEST(test_register_deregister);
424     ADD_TEST(test_property);
425     ADD_TEST(test_query_cache_stochastic);
426     ADD_TEST(test_fips_mode);
427     return 1;
428 }