xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/hx509/test_expr.c (revision d3273b5b76f5afaafe308cead5511dbb8df8c5e9)
1*d3273b5bSchristos /*	$NetBSD: test_expr.c,v 1.2 2017/01/28 21:31:48 christos Exp $	*/
2ca1c9b0cSelric 
3ca1c9b0cSelric 
4ca1c9b0cSelric #include "hx_locl.h"
5ca1c9b0cSelric #include <err.h>
6ca1c9b0cSelric 
7ca1c9b0cSelric struct foo {
8ca1c9b0cSelric     int val;
9ca1c9b0cSelric     char *str;
10ca1c9b0cSelric } foo[] = {
11ca1c9b0cSelric     { 0, "FALSE" },
12ca1c9b0cSelric     { 1, "TRUE" },
13ca1c9b0cSelric     { 0, "!TRUE" },
14ca1c9b0cSelric     { 0, "! TRUE" },
15ca1c9b0cSelric     { 0, "!\tTRUE" },
16ca1c9b0cSelric     { 0, "( FALSE AND FALSE )" },
17ca1c9b0cSelric     { 0, "( TRUE AND FALSE )" },
18ca1c9b0cSelric     { 1, "( TRUE AND TRUE )" },
19ca1c9b0cSelric     { 1, "( TRUE OR TRUE )" },
20ca1c9b0cSelric     { 1, "( TRUE OR FALSE )" },
21ca1c9b0cSelric     { 0, "( FALSE OR FALSE )" },
22ca1c9b0cSelric     { 1, "! ( FALSE OR FALSE )" },
23ca1c9b0cSelric 
24ca1c9b0cSelric     { 1, "\"foo\" TAILMATCH \"foo\"" },
25ca1c9b0cSelric     { 1, "\"foobar\" TAILMATCH \"bar\"" },
26ca1c9b0cSelric     { 0, "\"foobar\" TAILMATCH \"foo\"" },
27ca1c9b0cSelric 
28ca1c9b0cSelric     { 1, "\"foo\" == \"foo\"" },
29ca1c9b0cSelric     { 0, "\"foo\" == \"bar\"" },
30ca1c9b0cSelric     { 0, "\"foo\" != \"foo\"" },
31ca1c9b0cSelric     { 1, "\"foo\" != \"bar\"" },
32ca1c9b0cSelric     { 1, "%{variable} == \"foo\"" },
33ca1c9b0cSelric     { 0, "%{variable} == \"bar\"" },
34ca1c9b0cSelric     { 1, "%{context.variable} == \"foo\"" },
35ca1c9b0cSelric     { 0, "%{context.variable} == \"bar\"" },
36ca1c9b0cSelric     { 1, "\"foo\" IN ( \"bar\", \"foo\")" },
37ca1c9b0cSelric     { 0, "\"foo\" IN ( \"bar\", \"baz\")" },
38ca1c9b0cSelric     { 0, "\"bar\" IN %{context}" },
39ca1c9b0cSelric     { 1, "\"foo\" IN %{context}" },
40ca1c9b0cSelric     { 1, "\"variable\" IN %{context}" },
41ca1c9b0cSelric 
42ca1c9b0cSelric     { 1, "\"foo\" IN %{context} AND %{context.variable} == \"foo\"" }
43ca1c9b0cSelric };
44ca1c9b0cSelric 
45ca1c9b0cSelric int
main(int argc,char ** argv)46ca1c9b0cSelric main(int argc, char **argv)
47ca1c9b0cSelric {
48ca1c9b0cSelric     struct hx_expr *expr;
49ca1c9b0cSelric     hx509_context context;
50ca1c9b0cSelric     hx509_env env = NULL, env2 = NULL;
51ca1c9b0cSelric     int val, i, ret;
52ca1c9b0cSelric 
53ca1c9b0cSelric #if 0
54ca1c9b0cSelric     extern int yydebug;
55ca1c9b0cSelric     yydebug = 1;
56ca1c9b0cSelric #endif
57ca1c9b0cSelric 
58ca1c9b0cSelric     ret = hx509_context_init(&context);
59ca1c9b0cSelric     if (ret)
60ca1c9b0cSelric 	errx(1, "hx509_context_init failed with %d", ret);
61ca1c9b0cSelric 
62ca1c9b0cSelric     hx509_env_add(context, &env, "variable", "foo");
63ca1c9b0cSelric     hx509_env_add(context, &env2, "variable", "foo");
64ca1c9b0cSelric     hx509_env_add_binding(context, &env, "context", env2);
65ca1c9b0cSelric 
66ca1c9b0cSelric     for (i = 0; i < sizeof(foo)/sizeof(foo[0]); i++) {
67ca1c9b0cSelric 
68ca1c9b0cSelric 	expr = _hx509_expr_parse(foo[i].str);
69ca1c9b0cSelric 	if (expr == NULL)
70ca1c9b0cSelric 	    errx(1, "_hx509_expr_parse failed for %d: %s", i, foo[i].str);
71ca1c9b0cSelric 
72ca1c9b0cSelric 	val = _hx509_expr_eval(context, env, expr);
73ca1c9b0cSelric 	if (foo[i].val) {
74ca1c9b0cSelric 	    if (val == 0)
75ca1c9b0cSelric 		errx(1, "_hx509_expr_eval not true when it should: %d: %s",
76ca1c9b0cSelric 		     i, foo[i].str);
77ca1c9b0cSelric 	} else {
78ca1c9b0cSelric 	    if (val)
79ca1c9b0cSelric 		errx(1, "_hx509_expr_eval true when it should not: %d: %s",
80ca1c9b0cSelric 		     i, foo[i].str);
81ca1c9b0cSelric 	}
82ca1c9b0cSelric 
83ca1c9b0cSelric 	_hx509_expr_free(expr);
84ca1c9b0cSelric     }
85ca1c9b0cSelric 
86ca1c9b0cSelric     hx509_env_free(&env);
87ca1c9b0cSelric 
88ca1c9b0cSelric     return 0;
89ca1c9b0cSelric }
90