xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/krb5/test_config.c (revision d3273b5b76f5afaafe308cead5511dbb8df8c5e9)
1*d3273b5bSchristos /*	$NetBSD: test_config.c,v 1.2 2017/01/28 21:31:49 christos Exp $	*/
2ca1c9b0cSelric 
3ca1c9b0cSelric /*
4ca1c9b0cSelric  * Copyright (c) 2003 Kungliga Tekniska Högskolan
5ca1c9b0cSelric  * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric  * All rights reserved.
7ca1c9b0cSelric  *
8ca1c9b0cSelric  * Redistribution and use in source and binary forms, with or without
9ca1c9b0cSelric  * modification, are permitted provided that the following conditions
10ca1c9b0cSelric  * are met:
11ca1c9b0cSelric  *
12ca1c9b0cSelric  * 1. Redistributions of source code must retain the above copyright
13ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer.
14ca1c9b0cSelric  *
15ca1c9b0cSelric  * 2. Redistributions in binary form must reproduce the above copyright
16ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer in the
17ca1c9b0cSelric  *    documentation and/or other materials provided with the distribution.
18ca1c9b0cSelric  *
19ca1c9b0cSelric  * 3. Neither the name of KTH nor the names of its contributors may be
20ca1c9b0cSelric  *    used to endorse or promote products derived from this software without
21ca1c9b0cSelric  *    specific prior written permission.
22ca1c9b0cSelric  *
23ca1c9b0cSelric  * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24ca1c9b0cSelric  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca1c9b0cSelric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26ca1c9b0cSelric  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27ca1c9b0cSelric  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28ca1c9b0cSelric  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29ca1c9b0cSelric  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30ca1c9b0cSelric  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31ca1c9b0cSelric  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32ca1c9b0cSelric  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33ca1c9b0cSelric  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34ca1c9b0cSelric  */
35ca1c9b0cSelric 
36ca1c9b0cSelric #include "krb5_locl.h"
37ca1c9b0cSelric #include <err.h>
38ca1c9b0cSelric 
39ca1c9b0cSelric static int
check_config_file(krb5_context context,char * filelist,char ** res,int def)40ca1c9b0cSelric check_config_file(krb5_context context, char *filelist, char **res, int def)
41ca1c9b0cSelric {
42ca1c9b0cSelric     krb5_error_code ret;
43ca1c9b0cSelric     char **pp;
44ca1c9b0cSelric     int i;
45ca1c9b0cSelric 
46ca1c9b0cSelric     pp = NULL;
47ca1c9b0cSelric 
48ca1c9b0cSelric     if (def)
49ca1c9b0cSelric 	ret = krb5_prepend_config_files_default(filelist, &pp);
50ca1c9b0cSelric     else
51ca1c9b0cSelric 	ret = krb5_prepend_config_files(filelist, NULL, &pp);
52ca1c9b0cSelric 
53ca1c9b0cSelric     if (ret)
54ca1c9b0cSelric 	krb5_err(context, 1, ret, "prepend_config_files");
55ca1c9b0cSelric 
56ca1c9b0cSelric     for (i = 0; res[i] && pp[i]; i++)
57ca1c9b0cSelric 	if (strcmp(pp[i], res[i]) != 0)
58ca1c9b0cSelric 	    krb5_errx(context, 1, "'%s' != '%s'", pp[i], res[i]);
59ca1c9b0cSelric 
60ca1c9b0cSelric     if (res[i] != NULL)
61ca1c9b0cSelric 	krb5_errx(context, 1, "pp ended before res list");
62ca1c9b0cSelric 
63ca1c9b0cSelric     if (def) {
64ca1c9b0cSelric 	char **deflist;
65ca1c9b0cSelric 	int j;
66ca1c9b0cSelric 
67ca1c9b0cSelric 	ret = krb5_get_default_config_files(&deflist);
68ca1c9b0cSelric 	if (ret)
69ca1c9b0cSelric 	    krb5_err(context, 1, ret, "get_default_config_files");
70ca1c9b0cSelric 
71ca1c9b0cSelric 	for (j = 0 ; pp[i] && deflist[j]; i++, j++)
72ca1c9b0cSelric 	    if (strcmp(pp[i], deflist[j]) != 0)
73ca1c9b0cSelric 		krb5_errx(context, 1, "'%s' != '%s'", pp[i], deflist[j]);
74ca1c9b0cSelric 
75ca1c9b0cSelric 	if (deflist[j] != NULL)
76ca1c9b0cSelric 	    krb5_errx(context, 1, "pp ended before def list");
77ca1c9b0cSelric 	krb5_free_config_files(deflist);
78ca1c9b0cSelric     }
79ca1c9b0cSelric 
80ca1c9b0cSelric     if (pp[i] != NULL)
81ca1c9b0cSelric 	krb5_errx(context, 1, "pp ended after res (and def) list");
82ca1c9b0cSelric 
83ca1c9b0cSelric     krb5_free_config_files(pp);
84ca1c9b0cSelric 
85ca1c9b0cSelric     return 0;
86ca1c9b0cSelric }
87ca1c9b0cSelric 
88ca1c9b0cSelric char *list0[] =  { "/tmp/foo", NULL };
89ca1c9b0cSelric char *list1[] =  { "/tmp/foo", "/tmp/foo/bar", NULL };
90ca1c9b0cSelric char *list2[] =  { "", NULL };
91ca1c9b0cSelric 
92ca1c9b0cSelric struct {
93ca1c9b0cSelric     char *fl;
94ca1c9b0cSelric     char **res;
95ca1c9b0cSelric } test[] = {
96ca1c9b0cSelric     { "/tmp/foo", NULL },
97ca1c9b0cSelric     { "/tmp/foo" PATH_SEP "/tmp/foo/bar", NULL },
98ca1c9b0cSelric     { "", NULL }
99ca1c9b0cSelric };
100ca1c9b0cSelric 
101ca1c9b0cSelric static void
check_config_files(void)102ca1c9b0cSelric check_config_files(void)
103ca1c9b0cSelric {
104ca1c9b0cSelric     krb5_context context;
105ca1c9b0cSelric     krb5_error_code ret;
106ca1c9b0cSelric     int i;
107ca1c9b0cSelric 
108ca1c9b0cSelric     ret = krb5_init_context(&context);
109ca1c9b0cSelric     if (ret)
110ca1c9b0cSelric 	errx(1, "krb5_init_context %d", ret);
111ca1c9b0cSelric 
112ca1c9b0cSelric     test[0].res = list0;
113ca1c9b0cSelric     test[1].res = list1;
114ca1c9b0cSelric     test[2].res = list2;
115ca1c9b0cSelric 
116ca1c9b0cSelric     for (i = 0; i < sizeof(test)/sizeof(*test); i++) {
117ca1c9b0cSelric 	check_config_file(context, test[i].fl, test[i].res, 0);
118ca1c9b0cSelric 	check_config_file(context, test[i].fl, test[i].res, 1);
119ca1c9b0cSelric     }
120ca1c9b0cSelric 
121ca1c9b0cSelric     krb5_free_context(context);
122ca1c9b0cSelric }
123ca1c9b0cSelric 
124ca1c9b0cSelric const char *config_string_result0[] = {
125ca1c9b0cSelric     "A", "B", "C", "D", NULL
126ca1c9b0cSelric };
127ca1c9b0cSelric 
128ca1c9b0cSelric const char *config_string_result1[] = {
129ca1c9b0cSelric     "A", "B", "C D", NULL
130ca1c9b0cSelric };
131ca1c9b0cSelric 
132ca1c9b0cSelric const char *config_string_result2[] = {
133ca1c9b0cSelric     "A", "B", "", NULL
134ca1c9b0cSelric };
135ca1c9b0cSelric 
136ca1c9b0cSelric const char *config_string_result3[] = {
137ca1c9b0cSelric     "A B;C: D", NULL
138ca1c9b0cSelric };
139ca1c9b0cSelric 
140ca1c9b0cSelric const char *config_string_result4[] = {
141ca1c9b0cSelric     "\"\"", "", "\"\"", NULL
142ca1c9b0cSelric };
143ca1c9b0cSelric 
144ca1c9b0cSelric const char *config_string_result5[] = {
145ca1c9b0cSelric     "A\"BQd", NULL
146ca1c9b0cSelric };
147ca1c9b0cSelric 
148ca1c9b0cSelric const char *config_string_result6[] = {
149ca1c9b0cSelric     "efgh\"", "ABC", NULL
150ca1c9b0cSelric };
151ca1c9b0cSelric 
152ca1c9b0cSelric const char *config_string_result7[] = {
153ca1c9b0cSelric     "SnapeKills\\", "Dumbledore", NULL
154ca1c9b0cSelric };
155ca1c9b0cSelric 
156ca1c9b0cSelric const char *config_string_result8[] = {
157ca1c9b0cSelric     "\"TownOf Sandwich: Massachusetts\"Oldest", "Town", "In", "Cape Cod", NULL
158ca1c9b0cSelric };
159ca1c9b0cSelric 
160ca1c9b0cSelric const char *config_string_result9[] = {
161ca1c9b0cSelric     "\"Begins and\"ends", "In", "One", "String", NULL
162ca1c9b0cSelric };
163ca1c9b0cSelric 
164ca1c9b0cSelric const char *config_string_result10[] = {
165ca1c9b0cSelric     "Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:",
166ca1c9b0cSelric     "1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.",
167ca1c9b0cSelric     "2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.",
168ca1c9b0cSelric     "3. Neither the name of the Institute nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.",
169ca1c9b0cSelric     "THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.",
170ca1c9b0cSelric     "Why do we test with such long strings? Because some people have config files",
171ca1c9b0cSelric     "That", "look", "Like this.", NULL
172ca1c9b0cSelric };
173ca1c9b0cSelric 
174ca1c9b0cSelric const struct {
175ca1c9b0cSelric     const char * name;
176ca1c9b0cSelric     const char ** expected;
177ca1c9b0cSelric } config_strings_tests[] = {
178ca1c9b0cSelric     { "foo", config_string_result0 },
179ca1c9b0cSelric     { "bar", config_string_result1 },
180ca1c9b0cSelric     { "baz", config_string_result2 },
181ca1c9b0cSelric     { "quux", config_string_result3 },
182ca1c9b0cSelric     { "questionable", config_string_result4 },
183ca1c9b0cSelric     { "mismatch1", config_string_result5 },
184ca1c9b0cSelric     { "mismatch2", config_string_result6 },
185ca1c9b0cSelric     { "internal1", config_string_result7 },
186ca1c9b0cSelric     { "internal2", config_string_result8 },
187ca1c9b0cSelric     { "internal3", config_string_result9 },
188ca1c9b0cSelric     { "longer_strings", config_string_result10 }
189ca1c9b0cSelric };
190ca1c9b0cSelric 
191ca1c9b0cSelric static void
check_escaped_strings(void)192ca1c9b0cSelric check_escaped_strings(void)
193ca1c9b0cSelric {
194ca1c9b0cSelric     krb5_context context;
195ca1c9b0cSelric     krb5_config_section *c = NULL;
196ca1c9b0cSelric     krb5_error_code ret;
197ca1c9b0cSelric     int i;
198ca1c9b0cSelric 
199ca1c9b0cSelric     ret = krb5_init_context(&context);
200ca1c9b0cSelric     if (ret)
201ca1c9b0cSelric         errx(1, "krb5_init_context %d", ret);
202ca1c9b0cSelric 
203ca1c9b0cSelric     ret = krb5_config_parse_file(context, "test_config_strings.out", &c);
204ca1c9b0cSelric     if (ret)
205ca1c9b0cSelric         krb5_errx(context, 1, "krb5_config_parse_file()");
206ca1c9b0cSelric 
207ca1c9b0cSelric     for (i=0; i < sizeof(config_strings_tests)/sizeof(config_strings_tests[0]); i++) {
208ca1c9b0cSelric         char **ps;
209ca1c9b0cSelric         const char **s;
210ca1c9b0cSelric         const char **e;
211ca1c9b0cSelric 
212ca1c9b0cSelric         ps = krb5_config_get_strings(context, c, "escapes", config_strings_tests[i].name,
213ca1c9b0cSelric                                      NULL);
214ca1c9b0cSelric         if (ps == NULL)
215ca1c9b0cSelric             errx(1, "Failed to read string value %s", config_strings_tests[i].name);
216ca1c9b0cSelric 
217ca1c9b0cSelric         e = config_strings_tests[i].expected;
218ca1c9b0cSelric 
219ca1c9b0cSelric         for (s = (const char **)ps; *s && *e; s++, e++) {
220ca1c9b0cSelric             if (strcmp(*s, *e))
221ca1c9b0cSelric                 errx(1,
222ca1c9b0cSelric                      "Unexpected configuration string at value [%s].\n"
223ca1c9b0cSelric                      "Actual=[%s]\n"
224ca1c9b0cSelric                      "Expected=[%s]\n",
225ca1c9b0cSelric                      config_strings_tests[i].name, *s, *e);
226ca1c9b0cSelric         }
227ca1c9b0cSelric 
228ca1c9b0cSelric         if (*s || *e)
229ca1c9b0cSelric             errx(1, "Configuation string list for value [%s] has incorrect length.",
230ca1c9b0cSelric 		 config_strings_tests[i].name);
231ca1c9b0cSelric 
232ca1c9b0cSelric         krb5_config_free_strings(ps);
233ca1c9b0cSelric     }
234ca1c9b0cSelric 
235ca1c9b0cSelric     ret = krb5_config_file_free(context, c);
236ca1c9b0cSelric     if (ret)
237ca1c9b0cSelric         krb5_errx(context, 1, "krb5_config_file_free()");
238ca1c9b0cSelric 
239ca1c9b0cSelric     krb5_free_context(context);
240ca1c9b0cSelric }
241ca1c9b0cSelric 
242ca1c9b0cSelric int
main(int argc,char ** argv)243ca1c9b0cSelric main(int argc, char **argv)
244ca1c9b0cSelric {
245ca1c9b0cSelric     check_config_files();
246ca1c9b0cSelric     check_escaped_strings();
247ca1c9b0cSelric     return 0;
248ca1c9b0cSelric }
249